From 83cdd8594f30bb5c70c89c9dc6441539a1b6ed06 Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Tue, 20 Aug 2024 22:37:49 -0300 Subject: [PATCH 1/4] feat(macros): allow Android domain parts to have underscores Ref https://github.com/tauri-apps/tauri/issues/9707 --- .changes/android-package-underscores.md | 5 +++++ tao-macros/src/lib.rs | 20 ++++++++++++++++---- 2 files changed, 21 insertions(+), 4 deletions(-) create mode 100644 .changes/android-package-underscores.md diff --git a/.changes/android-package-underscores.md b/.changes/android-package-underscores.md new file mode 100644 index 000000000..2f80d4c9e --- /dev/null +++ b/.changes/android-package-underscores.md @@ -0,0 +1,5 @@ +--- +"tao-macros": patch +"tao": patch +--- + diff --git a/tao-macros/src/lib.rs b/tao-macros/src/lib.rs index b212e26d2..693637209 100644 --- a/tao-macros/src/lib.rs +++ b/tao-macros/src/lib.rs @@ -101,7 +101,7 @@ impl Parse for AndroidFnInput { /// 5. List of extra types your Rust function expects. Pass empty array if the function doesn't need any arugments. /// - If your function takes an arguments as reference with a lifetime tied to the [`JNIEnv`], it should use `'local` as the lifetime name as it is the /// lifetime name generated by the macro. -/// Note that all rust functions should expect the first two parameters to be [`JNIEnv`] and [`JClass`] so make sure they are imported into scope). +/// Note that all rust functions should expect the first two parameters to be [`JNIEnv`] and [`JClass`] so make sure they are imported into scope). /// 6. (Optional) Return type of your rust function. /// - If your function returns a reference with a lifetime tied to the [`JNIEnv`], it should use `'local` as the lifetime name as it is the /// lifetime name generated by the macro. @@ -202,7 +202,7 @@ pub fn android_fn(tokens: TokenStream) -> TokenStream { } = tokens; let domain = domain.to_string(); - let package = package.to_string().replace('_', "_1").replace('-', "_1"); + let package = package.to_string().replace('_', "_1"); let class = class.to_string(); let args = args .into_iter() @@ -273,6 +273,11 @@ impl Parse for GeneratePackageNameInput { /// 1. snake_case representation of the reversed domain of the app. /// 2. snake_case representation of the package name. /// +/// Note that `_` is the separator for the domain parts. +/// For instance the `com.tauri.app` identifier should be represented as +/// `generate_package_name!(com_tauri, app)`. +/// To escape the `_` character you can use `_1` which aligns with the default NDK implementation. +/// /// ## Example /// /// ``` @@ -295,8 +300,15 @@ pub fn generate_package_name(tokens: TokenStream) -> TokenStream { let tokens = parse_macro_input!(tokens as GeneratePackageNameInput); let GeneratePackageNameInput { domain, package } = tokens; - let domain = domain.to_string().replace('_', "/"); - let package = package.to_string().replace('-', "_"); + // note that this character is invalid in an identifier so it's safe to use as replacement + const TEMP_ESCAPE_UNDERSCORE_REPLACEMENT: &str = "<"; + + let domain = domain + .to_string() + .replace("_1", TEMP_ESCAPE_UNDERSCORE_REPLACEMENT) + .replace('_', "/") + .replace(TEMP_ESCAPE_UNDERSCORE_REPLACEMENT, "_"); + let package = package.to_string().replace("_1", "_"); let path = format!("{}/{}", domain, package); let litstr = LitStr::new(&path, proc_macro2::Span::call_site()); From 0d83c2e4ab8b06f00a3b681775ee51e6d7efc1e0 Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Wed, 21 Aug 2024 07:31:58 -0300 Subject: [PATCH 2/4] fill change file --- .changes/android-package-underscores.md | 1 + 1 file changed, 1 insertion(+) diff --git a/.changes/android-package-underscores.md b/.changes/android-package-underscores.md index 2f80d4c9e..f479632bb 100644 --- a/.changes/android-package-underscores.md +++ b/.changes/android-package-underscores.md @@ -3,3 +3,4 @@ "tao": patch --- +Allow Android domain and package names to include `_1` as escaped `_` characters - required because `_` is the separator for domain parts. From d858ecfedd080329af7a257a94200e733ac90f18 Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Wed, 21 Aug 2024 07:43:29 -0300 Subject: [PATCH 3/4] no need to unescape package --- tao-macros/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tao-macros/src/lib.rs b/tao-macros/src/lib.rs index 693637209..61b46a611 100644 --- a/tao-macros/src/lib.rs +++ b/tao-macros/src/lib.rs @@ -308,7 +308,7 @@ pub fn generate_package_name(tokens: TokenStream) -> TokenStream { .replace("_1", TEMP_ESCAPE_UNDERSCORE_REPLACEMENT) .replace('_', "/") .replace(TEMP_ESCAPE_UNDERSCORE_REPLACEMENT, "_"); - let package = package.to_string().replace("_1", "_"); + let package = package.to_string(); let path = format!("{}/{}", domain, package); let litstr = LitStr::new(&path, proc_macro2::Span::call_site()); From 003f13251764317ad236ab8d6da56a146350ba8f Mon Sep 17 00:00:00 2001 From: Lucas Fernandes Nogueira Date: Wed, 21 Aug 2024 07:46:52 -0300 Subject: [PATCH 4/4] Update .changes/android-package-underscores.md --- .changes/android-package-underscores.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changes/android-package-underscores.md b/.changes/android-package-underscores.md index f479632bb..a4f9cc99b 100644 --- a/.changes/android-package-underscores.md +++ b/.changes/android-package-underscores.md @@ -3,4 +3,4 @@ "tao": patch --- -Allow Android domain and package names to include `_1` as escaped `_` characters - required because `_` is the separator for domain parts. +Allow Android domain names to include `_1` as escaped `_` characters - required because `_` is the separator for domain parts.