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

refactor: use tauri.config.json > identifier as package name and bundle id #930

Closed
wants to merge 2 commits into from
Closed
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
25 changes: 12 additions & 13 deletions src/platform_impl/android/ndk_glue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,33 +48,32 @@ pub static PACKAGE: OnceCell<&str> = OnceCell::new();
/// 5. the main entry point of your android application.
#[macro_export]
macro_rules! android_binding {
($domain:ident, $package:ident, $activity:ident, $setup:path, $main:ident) => {
::tao::android_binding!($domain, $package, $activity, $setup, $main, ::tao)
($domain:ident, $activity:ident, $setup:path, $main:ident) => {
::tao::android_binding!($domain, $activity, $setup, $main, ::tao)
};
($domain:ident, $package:ident, $activity:ident, $setup:path, $main:ident, $tao:path) => {{
($domain:ident, $activity:ident, $setup:path, $main:ident, $tao:path) => {{
use $tao::{platform::android::prelude::android_fn, platform::android::prelude::*};
fn _____tao_store_package_name__() {
PACKAGE.get_or_init(move || generate_package_name!($domain, $package));
PACKAGE.get_or_init(move || generate_package_name!($domain));
}

android_fn!(
$domain,
$package,
$activity,
create,
[JObject],
__VOID__,
[$setup, $main],
_____tao_store_package_name__,
);
android_fn!($domain, $package, $activity, start, [JObject]);
android_fn!($domain, $package, $activity, stop, [JObject]);
android_fn!($domain, $package, $activity, resume, [JObject]);
android_fn!($domain, $package, $activity, pause, [JObject]);
android_fn!($domain, $package, $activity, save, [JObject]);
android_fn!($domain, $package, $activity, destroy, [JObject]);
android_fn!($domain, $package, $activity, memory, [JObject]);
android_fn!($domain, $package, $activity, focus, [i32]);
android_fn!($domain, $activity, start, [JObject]);
android_fn!($domain, $activity, stop, [JObject]);
android_fn!($domain, $activity, resume, [JObject]);
android_fn!($domain, $activity, pause, [JObject]);
android_fn!($domain, $activity, save, [JObject]);
android_fn!($domain, $activity, destroy, [JObject]);
android_fn!($domain, $activity, memory, [JObject]);
android_fn!($domain, $activity, focus, [i32]);
}};
}

Expand Down
21 changes: 9 additions & 12 deletions tao-macros/examples/android_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,32 @@ struct JClass<'a> {
_marker: &'a PhantomData<()>,
}

android_fn![com_example, tao_app, SomeClass, add, []];
android_fn![com_example_tao_app, SomeClass, add, []];
unsafe fn add(_env: JNIEnv, _class: JClass) {}

android_fn![com_example, tao_app, SomeClass, add2, [i32, i32]];
android_fn![com_example_tao_app, SomeClass, add2, [i32, i32]];
unsafe fn add2(_env: JNIEnv, _class: JClass, _a: i32, _b: i32) {}

android_fn![com_example, tao_app, SomeClass, add3, [i32, i32], i32];
android_fn![com_example_tao_app, SomeClass, add3, [i32, i32], i32];
unsafe fn add3(_env: JNIEnv, _class: JClass, a: i32, b: i32) -> i32 {
a + b
}

android_fn![com_example, tao_app, SomeClass, add4, [], i32];
android_fn![com_example_tao_app, SomeClass, add4, [], i32];
unsafe fn add4(_env: JNIEnv, _class: JClass) -> i32 {
0
}

android_fn![com_example, tao_app, SomeClass, add5, [], __VOID__];
android_fn![com_example_tao_app, SomeClass, add5, [], __VOID__];
unsafe fn add5(_env: JNIEnv, _class: JClass) {}

android_fn![com_example, tao_app, SomeClass, add6, [i32], __VOID__];
android_fn![com_example_tao_app, SomeClass, add6, [i32], __VOID__];
unsafe fn add6(_env: JNIEnv, _class: JClass, _a: i32) {}

fn __setup__() {}
fn __store_package_name__() {}
android_fn!(
com_example,
tao_app,
com_example_tao_app,
SomeClass,
add7,
[i32, i32],
Expand All @@ -47,8 +46,7 @@ android_fn!(
unsafe fn add7(_env: JNIEnv, _class: JClass, _a: i32, _b: i32, _setup: fn(), _main: fn()) {}

android_fn!(
com_example,
tao_app,
com_example_tao_app,
SomeClass,
add8,
[i32, i32],
Expand All @@ -61,8 +59,7 @@ unsafe fn add8(_env: JNIEnv, _class: JClass, _a: i32, _b: i32) -> i32 {
}

android_fn![
com_example,
tao_app,
com_example_tao_app,
SomeClass,
add10,
[JClass<'local>, i32],
Expand Down
2 changes: 1 addition & 1 deletion tao-macros/examples/generate_package_name.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use tao_macros::generate_package_name;

pub const PACKAGE: &str = generate_package_name!(com_example, tao_app);
pub const PACKAGE: &str = generate_package_name!(com_example_tao_app);

fn main() {}

Expand Down
28 changes: 8 additions & 20 deletions tao-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use syn::{

struct AndroidFnInput {
domain: Ident,
package: Ident,
class: Ident,
function: Ident,
args: Punctuated<Type, Comma>,
Expand All @@ -37,8 +36,6 @@ impl Parse for AndroidFnInput {
fn parse(input: ParseStream) -> syn::Result<Self> {
let domain: Ident = input.parse()?;
let _: Comma = input.parse()?;
let package: Ident = input.parse()?;
let _: Comma = input.parse()?;
let class: Ident = input.parse()?;
let _: Comma = input.parse()?;
let function: Ident = input.parse()?;
Expand Down Expand Up @@ -80,7 +77,6 @@ impl Parse for AndroidFnInput {
};
Ok(Self {
domain,
package,
class,
function,
ret,
Expand Down Expand Up @@ -117,7 +113,7 @@ impl Parse for AndroidFnInput {
/// # _marker: &'a std::marker::PhantomData<()>,
/// # }
/// # type JClass<'a> = JNIEnv<'a>;
/// android_fn![com_example, tao, OperationsClass, add, [i32, i32], i32];
/// android_fn![com_example_tao, OperationsClass, add, [i32, i32], i32];
/// unsafe fn add(_env: JNIEnv, _class: JClass, a: i32, b: i32) -> i32 {
/// a + b
/// }
Expand Down Expand Up @@ -159,7 +155,7 @@ impl Parse for AndroidFnInput {
/// # }
/// # type JClass<'a> = JNIEnv<'a>;
/// # type JObject<'a> = JNIEnv<'a>;
/// android_fn![com_example, tao, OperationsClass, add, [JObject<'local>], JClass<'local>];
/// android_fn![com_example_tao, OperationsClass, add, [JObject<'local>], JClass<'local>];
/// unsafe fn add<'local>(mut _env: JNIEnv<'local>, class: JClass<'local>, obj: JObject<'local>) -> JClass<'local> {
/// class
/// }
Expand Down Expand Up @@ -192,7 +188,6 @@ pub fn android_fn(tokens: TokenStream) -> TokenStream {
let tokens = parse_macro_input!(tokens as AndroidFnInput);
let AndroidFnInput {
domain,
package,
class,
function,
ret,
Expand All @@ -202,7 +197,6 @@ pub fn android_fn(tokens: TokenStream) -> TokenStream {
} = tokens;

let domain = domain.to_string();
let package = package.to_string().replace('_', "_1").replace('-', "_1");
let class = class.to_string();
let args = args
.into_iter()
Expand All @@ -212,9 +206,8 @@ pub fn android_fn(tokens: TokenStream) -> TokenStream {
let non_jni_args = non_jni_args.into_iter().collect::<Vec<_>>();

let java_fn_name = format_ident!(
"Java_{domain}_{package}_{class}_{function}",
"Java_{domain}_{class}_{function}",
domain = domain,
package = package,
class = class,
function = function,
);
Expand Down Expand Up @@ -253,17 +246,14 @@ pub fn android_fn(tokens: TokenStream) -> TokenStream {

struct GeneratePackageNameInput {
domain: Ident,
package: Ident,
}

impl Parse for GeneratePackageNameInput {
fn parse(input: ParseStream) -> syn::Result<Self> {
let domain: Ident = input.parse()?;
let _: Comma = input.parse()?;
let package: Ident = input.parse()?;
let _: syn::Result<Comma> = input.parse();

Ok(Self { domain, package })
Ok(Self { domain })
}
}

Expand All @@ -278,7 +268,7 @@ impl Parse for GeneratePackageNameInput {
/// ```
/// # use tao_macros::generate_package_name;
///
/// const PACKAGE_NAME: &str = generate_package_name!(com_example, tao_app);
/// const PACKAGE_NAME: &str = generate_package_name!(com_example_tao_app);
/// ```
/// which can be used later on with JNI:
/// ```
Expand All @@ -287,19 +277,17 @@ impl Parse for GeneratePackageNameInput {
/// # let env = 0;
/// # let activity = 0;
///
/// const PACKAGE_NAME: &str = generate_package_name!(com_example, tao_app); // constructs `com/example/tao_app`
/// const PACKAGE_NAME: &str = generate_package_name!(com_example_tao_app); // constructs `com/example/tao_app`
/// let ipc_class = find_my_class(env, activity, format!("{}/Ipc", PACKAGE_NAME)).unwrap();
/// ```
#[proc_macro]
pub fn generate_package_name(tokens: TokenStream) -> TokenStream {
let tokens = parse_macro_input!(tokens as GeneratePackageNameInput);
let GeneratePackageNameInput { domain, package } = tokens;
let GeneratePackageNameInput { domain } = tokens;

let domain = domain.to_string().replace('_', "/");
let package = package.to_string().replace('-', "_");

let path = format!("{}/{}", domain, package);
let litstr = LitStr::new(&path, proc_macro2::Span::call_site());
let litstr = LitStr::new(&domain, proc_macro2::Span::call_site());

quote! {#litstr}.into()
}
Loading