|
| 1 | +use proc_macro::TokenStream; |
| 2 | +use proc_macro2::TokenStream as TokenStream2; |
| 3 | +use quote::{format_ident, quote}; |
| 4 | +use std::env::var; |
| 5 | +use syn::{parse_macro_input, spanned::Spanned, ItemFn}; |
| 6 | + |
| 7 | +fn get_env_var(name: &str, error: &mut Option<TokenStream2>, function: &ItemFn) -> TokenStream2 { |
| 8 | + match var(name) { |
| 9 | + Ok(value) => { |
| 10 | + let ident = format_ident!("{}", value); |
| 11 | + quote!(#ident) |
| 12 | + } |
| 13 | + Err(_) => { |
| 14 | + error.replace( |
| 15 | + syn::Error::new( |
| 16 | + function.span(), |
| 17 | + format!( |
| 18 | + "`{}` env var not set, do you have a build script with tauri-build?", |
| 19 | + name, |
| 20 | + ), |
| 21 | + ) |
| 22 | + .into_compile_error(), |
| 23 | + ); |
| 24 | + quote!() |
| 25 | + } |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +pub fn entry_point(_attributes: TokenStream, item: TokenStream) -> TokenStream { |
| 30 | + let function = parse_macro_input!(item as ItemFn); |
| 31 | + let function_name = function.sig.ident.clone(); |
| 32 | + |
| 33 | + let mut error = None; |
| 34 | + let domain = get_env_var("TAURI_ANDROID_DOMAIN", &mut error, &function); |
| 35 | + let app_name = get_env_var("TAURI_ANDROID_APP_NAME", &mut error, &function); |
| 36 | + |
| 37 | + if let Some(e) = error { |
| 38 | + quote!(#e).into() |
| 39 | + } else { |
| 40 | + quote!( |
| 41 | + fn stop_unwind<F: FnOnce() -> T, T>(f: F) -> T { |
| 42 | + match std::panic::catch_unwind(std::panic::AssertUnwindSafe(f)) { |
| 43 | + Ok(t) => t, |
| 44 | + Err(err) => { |
| 45 | + eprintln!("attempt to unwind out of `rust` with err: {:?}", err); |
| 46 | + std::process::abort() |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + #function |
| 52 | + |
| 53 | + fn _start_app() { |
| 54 | + #[cfg(target_os = "android")] |
| 55 | + { |
| 56 | + use ::tauri::paste; |
| 57 | + ::tauri::wry_android_binding!(#domain, #app_name, _start_app, ::tauri::wry); |
| 58 | + } |
| 59 | + stop_unwind(#function_name); |
| 60 | + } |
| 61 | + |
| 62 | + #[cfg(not(target_os = "android"))] |
| 63 | + #[no_mangle] |
| 64 | + #[inline(never)] |
| 65 | + pub extern "C" fn start_app() { |
| 66 | + _start_app() |
| 67 | + } |
| 68 | + ) |
| 69 | + .into() |
| 70 | + } |
| 71 | +} |
0 commit comments