Skip to content

Commit

Permalink
fix(android): allow and escape Kotlin keywords as package identifier, c…
Browse files Browse the repository at this point in the history
…loses #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>
  • Loading branch information
pewsheen and lucasfernog committed May 28, 2024
1 parent 4754786 commit 71a5e2b
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changes/cli-allow-kotlin-keyword-as-ident.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri-cli": "patch:enhance"
---

On Android, allow using Kotlin keywords as identifiers and escape them in templates.
4 changes: 2 additions & 2 deletions tooling/cli/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tooling/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" ] }
Expand Down
5 changes: 4 additions & 1 deletion tooling/cli/src/mobile/android/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
24 changes: 24 additions & 0 deletions tooling/cli/src/mobile/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use cargo_mobile2::{
},
config::app::App,
dot_cargo,
reserved_names::KOTLIN_ONLY_KEYWORDS,
target::TargetTrait as _,
util::{
self,
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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()
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 : TauriActivity()

0 comments on commit 71a5e2b

Please sign in to comment.