Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(android): allow and escape Kotlin keywords as package identifier, closes #9743 #9799

Merged
merged 8 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
Loading