From 71a5e2ba24017b0b9a0e84e053167265b7eddcce Mon Sep 17 00:00:00 2001 From: Jason Tsai Date: Tue, 28 May 2024 18:52:48 +0800 Subject: [PATCH] fix(android): allow and escape Kotlin keywords as package identifier, closes #9743 (#9799) * fix(android): escape kotlin only keyword in template * fix: escape keywords in wry templates aswell * chore: add changelog * chore: remove unused code * fix(android): wry template, package name should reverse * update cargo-mobile2 --------- Co-authored-by: Lucas Nogueira --- .changes/cli-allow-kotlin-keyword-as-ident.md | 5 ++++ tooling/cli/Cargo.lock | 4 ++-- tooling/cli/Cargo.toml | 2 +- tooling/cli/src/mobile/android/mod.rs | 5 +++- tooling/cli/src/mobile/init.rs | 24 +++++++++++++++++++ .../android/app/src/main/MainActivity.kt | 2 +- 6 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 .changes/cli-allow-kotlin-keyword-as-ident.md diff --git a/.changes/cli-allow-kotlin-keyword-as-ident.md b/.changes/cli-allow-kotlin-keyword-as-ident.md new file mode 100644 index 000000000000..a26d9a8810e4 --- /dev/null +++ b/.changes/cli-allow-kotlin-keyword-as-ident.md @@ -0,0 +1,5 @@ +--- +"tauri-cli": "patch:enhance" +--- + +On Android, allow using Kotlin keywords as identifiers and escape them in templates. diff --git a/tooling/cli/Cargo.lock b/tooling/cli/Cargo.lock index 3aca3322fd56..9e379bcc42bc 100644 --- a/tooling/cli/Cargo.lock +++ b/tooling/cli/Cargo.lock @@ -483,9 +483,9 @@ dependencies = [ [[package]] name = "cargo-mobile2" -version = "0.12.0" +version = "0.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "79fadc84c218c513afcace5b0115bf615fb1c64dac782c1978f1620491339d7d" +checksum = "c7ac384d832f346303c9f80328bb11e04000b66743b543578ed8926a6fbc264f" dependencies = [ "colored", "core-foundation", diff --git a/tooling/cli/Cargo.toml b/tooling/cli/Cargo.toml index 0b17590e4f0a..1243868369d9 100644 --- a/tooling/cli/Cargo.toml +++ b/tooling/cli/Cargo.toml @@ -39,7 +39,7 @@ name = "cargo-tauri" path = "src/main.rs" [dependencies] -cargo-mobile2 = { version = "0.12", default-features = false } +cargo-mobile2 = { version = "0.12.1", default-features = false } jsonrpsee = { version = "0.22", features = [ "server" ] } jsonrpsee-core = "0.22" jsonrpsee-client-transport = { version = "0.22", features = [ "ws" ] } diff --git a/tooling/cli/src/mobile/android/mod.rs b/tooling/cli/src/mobile/android/mod.rs index b0ce9c1a622d..f78f5e599563 100644 --- a/tooling/cli/src/mobile/android/mod.rs +++ b/tooling/cli/src/mobile/android/mod.rs @@ -133,7 +133,10 @@ pub fn get_config( ..Default::default() }; - set_var("WRY_ANDROID_PACKAGE", app.reverse_identifier()); + set_var( + "WRY_ANDROID_PACKAGE", + app.android_identifier_escape_kotlin_keyword(), + ); set_var("WRY_ANDROID_LIBRARY", app.lib_name()); set_var("TAURI_ANDROID_PROJECT_PATH", config.project_dir()); diff --git a/tooling/cli/src/mobile/init.rs b/tooling/cli/src/mobile/init.rs index c1db20e860fa..60ca9f834d73 100644 --- a/tooling/cli/src/mobile/init.rs +++ b/tooling/cli/src/mobile/init.rs @@ -14,6 +14,7 @@ use cargo_mobile2::{ }, config::app::App, dot_cargo, + reserved_names::KOTLIN_ONLY_KEYWORDS, target::TargetTrait as _, util::{ self, @@ -215,6 +216,7 @@ fn handlebars(app: &App) -> (Handlebars<'static>, JsonMap) { "reverse-domain-snake-case", Box::new(reverse_domain_snake_case), ); + h.register_helper("escape-kotlin-keyword", Box::new(escape_kotlin_keyword)); // don't mix these up or very bad things will happen to all of us h.register_helper("prefix-path", Box::new(prefix_path)); h.register_helper("unprefix-path", Box::new(unprefix_path)); @@ -360,6 +362,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::>() + .join("."); + + out.write(&escaped_result).map_err(Into::into) +} + fn app_root(ctx: &Context) -> Result<&str, RenderError> { let app_root = ctx .data() diff --git a/tooling/cli/templates/mobile/android/app/src/main/MainActivity.kt b/tooling/cli/templates/mobile/android/app/src/main/MainActivity.kt index 056677cd6886..a10ce27d54cd 100644 --- a/tooling/cli/templates/mobile/android/app/src/main/MainActivity.kt +++ b/tooling/cli/templates/mobile/android/app/src/main/MainActivity.kt @@ -1,3 +1,3 @@ -package {{reverse-domain app.identifier}} +package {{escape-kotlin-keyword (reverse-domain app.identifier)}} class MainActivity : TauriActivity() \ No newline at end of file