Skip to content

Commit

Permalink
fix(android): allow and escape Kotlin keyword as package identifier (#…
Browse files Browse the repository at this point in the history
…328)

* fix(android): allows and escape kotlin only keyword as identifier

* fix: add android_identifier_escape_kotlin_keyword method

* chore: add changelog

* mod must be public to be used in tauri

* Fix build errors for iOS (#272)

* refactor!: use config.domain as package name and bundle id (#330)

* fix(android): use tauri.config.json > identifier as package name

* chore: add changelog

* rename `domain` to `identifier`

* rename domain mod

* lint

---------

Co-authored-by: Lucas Nogueira <[email protected]>

* fix(deps): update rust crate textwrap to v0.16.1 (#329)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>

* fix(deps): update rust crate os_pipe to v1.1.5 (#325)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>

* fix(deps): update rust crate libc to v0.2.155 (#324)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>

* fix(deps): update rust crate ignore to v0.4.22 (#323)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>

* fix(deps): update rust crate deunicode to v1.6.0 (#321)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>

* fix(deps): update rust crate core-foundation to v0.9.4 (#320)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>

* chore(deps): update rust crate serde to v1.0.202 (#311)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>

* chore(deps): update rust crate embed-resource to v2.4.2 (#317)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>

* chore(deps): update rust crate serde_json to v1.0.117 (#318)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>

* chore(deps): update rust crate thiserror to v1.0.61 (#319)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>

* fix(deps): update rust crate duct to v0.13.7 (#322)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>

* fix(deps): update rust crate toml to v0.8.13 (#332)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>

* fix(deps): update rust crate ureq to v2.9.7 (#333)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>

* publish new versions (#301)

Co-authored-by: lucasfernog <[email protected]>

* fix: add `true` `false` `null` to java keywords

* fix: wry template package name should reverse

---------

Co-authored-by: Lucas Nogueira <[email protected]>
Co-authored-by: Hampus Lidin <[email protected]>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: lucasfernog <[email protected]>
  • Loading branch information
6 people authored May 27, 2024
1 parent 0f72cad commit 7d260ba
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changes/android-kotlin-keyword-as-ident.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"cargo-mobile2": "patch"
---

On Android, allows using Kotlin keywords as identifiers and escape them in templates.
15 changes: 3 additions & 12 deletions src/config/app/identifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use std::error::Error;
use std::fmt;

static RESERVED_PACKAGE_NAMES: [&str; 2] = ["kotlin", "java"];
static RESERVED_KEYWORDS: [&str; 63] = [
// https://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html
static RESERVED_JAVA_KEYWORDS: [&str; 53] = [
"abstract",
"as",
"assert",
"boolean",
"break",
Expand All @@ -27,21 +27,17 @@ static RESERVED_KEYWORDS: [&str; 63] = [
"finally",
"float",
"for",
"fun",
"goto",
"if",
"implements",
"import",
"instanceof",
"in",
"int",
"interface",
"is",
"long",
"native",
"new",
"null",
"object",
"package",
"private",
"protected",
Expand All @@ -59,13 +55,8 @@ static RESERVED_KEYWORDS: [&str; 63] = [
"transient",
"true",
"try",
"typealias",
"typeof",
"val",
"var",
"void",
"volatile",
"when",
"while",
];

Expand Down Expand Up @@ -129,7 +120,7 @@ pub fn check_identifier_syntax(identifier_name: &str) -> Result<(), IdentifierEr
if label.is_empty() {
return Err(IdentifierError::EmptyLabel);
}
if RESERVED_KEYWORDS.contains(&label) {
if RESERVED_JAVA_KEYWORDS.contains(&label) {
return Err(IdentifierError::ReservedKeyword {
keyword: label.to_owned(),
});
Expand Down
14 changes: 14 additions & 0 deletions src/config/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,20 @@ impl App {
.join(".")
}

pub fn android_identifier_escape_kotlin_keyword(&self) -> String {
self.reverse_identifier()
.split('.')
.map(|s| {
if crate::reserved_names::KOTLIN_ONLY_KEYWORDS.contains(&s) {
format!("`{}`", s)
} else {
s.to_string()
}
})
.collect::<Vec<_>>()
.join(".")
}

pub fn manifest_path(&self) -> PathBuf {
self.root_dir().join("Cargo.toml")
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub mod init;
pub mod opts;
pub mod os;
mod project;
mod reserved_names;
pub mod reserved_names;
pub mod target;
mod templating;
pub mod update;
Expand Down
13 changes: 13 additions & 0 deletions src/reserved_names/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ pub static WINDOWS: &[&str] = &[

pub static ARTIFACTS: &[&str] = &["deps", "examples", "build", "incremental"];

pub static KOTLIN_ONLY_KEYWORDS: &[&str] = &[
"as",
"fun",
"in",
"is",
"object",
"typealias",
"typeof",
"val",
"var",
"when",
];

pub fn in_keywords(s: impl AsRef<str>) -> bool {
KEYWORDS.contains(&s.as_ref())
}
Expand Down
24 changes: 24 additions & 0 deletions src/templating/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{
Bicycle, EscapeFn, HelperDef, JsonMap,
},
config::{app, Config},
reserved_names::KOTLIN_ONLY_KEYWORDS,
util::{self, Git},
};
use std::collections::HashMap;
Expand Down Expand Up @@ -122,6 +123,28 @@ fn reverse_domain_snake_case(
.map_err(Into::into)
}

fn escape_kotlin_keyword(
helper: &Helper,
_: &Handlebars,
_: &Context,
_: &mut RenderContext,
out: &mut dyn Output,
) -> HelperResult {
let escaped_result = get_str(helper)
.split('.')
.map(|s| {
if KOTLIN_ONLY_KEYWORDS.contains(&s) {
format!("`{}`", s)
} else {
s.to_string()
}
})
.collect::<Vec<_>>()
.join(".");

out.write(&escaped_result).map_err(Into::into)
}

fn app_root(ctx: &Context) -> Result<&str, RenderErrorReason> {
let app_root = ctx
.data()
Expand Down Expand Up @@ -219,6 +242,7 @@ pub fn init(config: Option<&Config>) -> Bicycle {
"reverse-domain-snake-case",
Box::new(reverse_domain_snake_case),
);
helpers.insert("escape-kotlin-keyword", Box::new(escape_kotlin_keyword));
helpers.insert("dot-to-slash", Box::new(dot_to_slash));
if config.is_some() {
// don't mix these up or very bad things will happen to all of us
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package {{reverse-domain app.identifier}}
package {{escape-kotlin-keyword (reverse-domain app.identifier)}}

class MainActivity : WryActivity()

0 comments on commit 7d260ba

Please sign in to comment.