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 <lucas@tauri.studio>
This commit is contained in:
Jason Tsai 2024-05-28 18:52:48 +08:00 committed by GitHub
parent 4754786aa2
commit 71a5e2ba24
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 37 additions and 5 deletions

View File

@ -0,0 +1,5 @@
---
"tauri-cli": "patch:enhance"
---
On Android, allow using Kotlin keywords as identifiers and escape them in templates.

View File

@ -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",

View File

@ -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" ] }

View File

@ -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());

View File

@ -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::<Vec<_>>()
.join(".");
out.write(&escaped_result).map_err(Into::into)
}
fn app_root(ctx: &Context) -> Result<&str, RenderError> {
let app_root = ctx
.data()

View File

@ -1,3 +1,3 @@
package {{reverse-domain app.identifier}}
package {{escape-kotlin-keyword (reverse-domain app.identifier)}}
class MainActivity : TauriActivity()