Przeglądaj źródła

feat(cli): add mobile support to the app template (#5046)

Lucas Fernandes Nogueira 3 lat temu
rodzic
commit
80a301ea63

+ 1 - 1
tooling/cli/src/mobile/android/android_studio_script.rs

@@ -31,7 +31,7 @@ pub fn command(options: Options) -> Result<()> {
   } else {
     Profile::Debug
   };
-  let noise_level = NoiseLevel::FranklyQuitePedantic;
+  let noise_level = NoiseLevel::LoudAndProud;
 
   with_config(None, |root_conf, config, metadata| {
     ensure_init(config.project_dir(), MobileTarget::Android)

+ 1 - 1
tooling/cli/src/mobile/ios/xcode_script.rs

@@ -44,7 +44,7 @@ pub fn command(options: Options) -> Result<()> {
 
   let profile = profile_from_configuration(&options.configuration);
   let macos = macos_from_platform(&options.platform);
-  let noise_level = NoiseLevel::FranklyQuitePedantic;
+  let noise_level = NoiseLevel::LoudAndProud;
 
   with_config(None, |root_conf, config, metadata| {
     let env = env()?;

+ 12 - 1
tooling/cli/templates/app/src-tauri/Cargo.crate-manifest

@@ -5,12 +5,14 @@ description = "A Tauri App"
 authors = ["you"]
 license = ""
 repository = ""
-default-run = "app"
 edition = "2021"
 rust-version = "1.57"
 
 # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
 
+[lib]
+crate-type = ["staticlib", "cdylib", "rlib"]
+
 [build-dependencies]
 tauri-build = {{{  tauri_build_dep  }}}
 
@@ -19,6 +21,15 @@ serde_json = "1.0"
 serde = { version = "1.0", features = ["derive"] }
 tauri = {{{  tauri_dep  }}}
 
+[target.'cfg(any(target_os = "android", target_os = "ios"))'.dependencies]
+log = "0.4"
+
+[target.'cfg(target_os = "android")'.dependencies]
+android_logger = "0.9.0"
+
+[target.'cfg(target_os = "ios")'.dependencies]
+env_logger = "0.9.0"
+
 [features]
 # by default Tauri runs in production mode
 # when `tauri dev` runs it is executed with `cargo run --no-default-features` if `devPath` is an URL

+ 8 - 0
tooling/cli/templates/app/src-tauri/src/desktop.rs

@@ -0,0 +1,8 @@
+#![cfg_attr(
+  all(not(debug_assertions), target_os = "windows"),
+  windows_subsystem = "windows"
+)]
+
+fn main() {
+  app::AppBuilder::new()
+}

+ 41 - 0
tooling/cli/templates/app/src-tauri/src/lib.rs

@@ -0,0 +1,41 @@
+use tauri::App;
+
+#[cfg(mobile)]
+mod mobile;
+#[cfg(mobile)]
+pub use mobile::*;
+
+pub type SetupHook = Box<dyn FnOnce(&mut App) -> Result<(), Box<dyn std::error::Error>> + Send>;
+
+#[derive(Default)]
+pub struct AppBuilder {
+  setup: Option<SetupHook>,
+}
+
+impl AppBuilder {
+  pub fn new() -> Self {
+    Self::default()
+  }
+
+  #[must_use]
+  pub fn setup<F>(mut self, setup: F) -> Self
+  where
+    F: FnOnce(&mut App) -> Result<(), Box<dyn std::error::Error>> + Send + 'static,
+  {
+    self.setup.replace(Box::new(setup));
+    self
+  }
+
+  pub fn run(self) {
+    let setup = self.setup;
+    tauri::Builder::default()
+      .setup(move |app| {
+        if let Some(setup) = setup {
+          (setup)(app)?;
+        }
+        Ok(())
+      })
+      .run(tauri::generate_context!())
+      .expect("error while running tauri application");
+  }
+}

+ 4 - 7
tooling/cli/templates/app/src-tauri/src/main.rs

@@ -1,10 +1,7 @@
-#![cfg_attr(
-  all(not(debug_assertions), target_os = "windows"),
-  windows_subsystem = "windows"
-)]
+#[cfg(desktop)]
+mod desktop;
 
 fn main() {
-  tauri::Builder::default()
-    .run(tauri::generate_context!())
-    .expect("error while running tauri application");
+  #[cfg(desktop)]
+  desktop::main();
 }

+ 23 - 0
tooling/cli/templates/app/src-tauri/src/mobile.rs

@@ -0,0 +1,23 @@
+#[cfg(target_os = "android")]
+fn init_logging(app_name: &str) {
+  android_logger::init_once(
+    android_logger::Config::default()
+      .with_min_level(log::Level::Trace)
+      .with_tag(app_name),
+  );
+}
+
+#[cfg(not(target_os = "android"))]
+fn init_logging(_app_name: &str) {
+  env_logger::init();
+}
+
+#[tauri::mobile_entry_point]
+fn main() {
+  super::AppBuilder::new()
+    .setup(|app| {
+      init_logging(&app.package_info().name);
+      Ok(())
+    })
+    .run()
+}

+ 1 - 2
tooling/cli/templates/mobile/android/app/src/main/TauriActivity.kt

@@ -1,6 +1,5 @@
-package {{app-domain-reversed}}.{{app-name-snake-case}}
+package {{reverse-domain app.domain}}.{{snake-case app.name}}
 
-import android.os.Bundle
 import androidx.appcompat.app.AppCompatActivity
 
 abstract class TauriActivity : AppCompatActivity()