Jelajahi Sumber

fix(android): force build script to rerun project injection on dir clear (#6249)

Lucas Fernandes Nogueira 2 tahun lalu
induk
melakukan
7258a64730

+ 1 - 0
core/tauri-build/Cargo.toml

@@ -26,6 +26,7 @@ serde_json = "1"
 heck = "0.4"
 json-patch = "0.2"
 walkdir = "2"
+filetime = "0.2"
 
 [target."cfg(target_os = \"macos\")".dependencies]
 swift-rs = { git = "https://github.com/Brendonovich/swift-rs", rev = "eb6de914ad57501da5019154d476d45660559999", features = ["build"] }

+ 16 - 11
core/tauri-build/src/mobile.rs

@@ -48,18 +48,12 @@ impl PluginBuilder {
             &[],
           )?;
 
-          if let Ok(project_dir) = var("TAURI_ANDROID_PROJECT_PATH") {
+          if let Some(project_dir) = var_os("TAURI_ANDROID_PROJECT_PATH").map(PathBuf::from) {
             let pkg_name = var("CARGO_PKG_NAME").unwrap();
-
             println!("cargo:rerun-if-env-changed=TAURI_ANDROID_PROJECT_PATH");
+            let android_plugin_project_path = project_dir.join("tauri-plugins").join(&pkg_name);
 
-            let project_dir = PathBuf::from(project_dir);
-
-            inject_android_project(
-              source,
-              project_dir.join("tauri-plugins").join(&pkg_name),
-              &["tauri-api"],
-            )?;
+            inject_android_project(&source, android_plugin_project_path, &["tauri-api"])?;
 
             let gradle_settings_path = project_dir.join("tauri.settings.gradle");
             let gradle_settings = fs::read_to_string(&gradle_settings_path)?;
@@ -148,6 +142,15 @@ pub fn inject_android_project(
     rename(out_dir, &build_path)?;
   }
 
+  let rerun_path = target.join("build.gradle.kts");
+  let metadata = source.join("build.gradle.kts").metadata()?;
+  filetime::set_file_mtime(
+    &rerun_path,
+    filetime::FileTime::from_last_modification_time(&metadata),
+  )?;
+
+  println!("cargo:rerun-if-changed={}", rerun_path.display());
+
   Ok(())
 }
 
@@ -165,10 +168,12 @@ fn copy_folder(source: &Path, target: &Path, ignore_paths: &[&str]) -> Result<()
       continue;
     }
     let dest_path = target.join(rel_path);
+
     if entry.file_type().is_dir() {
-      fs::create_dir(dest_path)?;
+      fs::create_dir(&dest_path)?;
     } else {
-      fs::copy(entry.path(), dest_path)?;
+      fs::copy(entry.path(), &dest_path)?;
+      println!("cargo:rerun-if-changed={}", entry.path().display());
     }
   }
 

+ 8 - 7
core/tauri/build.rs

@@ -8,9 +8,12 @@ use heck::ToSnakeCase;
 
 use once_cell::sync::OnceCell;
 
-use std::env::var;
-use std::path::PathBuf;
-use std::{path::Path, sync::Mutex};
+use std::env::var_os;
+use std::{
+  env::var,
+  path::{Path, PathBuf},
+  sync::Mutex,
+};
 
 static CHECKED_FEATURES: OnceCell<Mutex<Vec<String>>> = OnceCell::new();
 
@@ -136,8 +139,7 @@ fn main() {
 
   alias_module("app", &["show", "hide"], api_all);
 
-  let checked_features_out_path =
-    Path::new(&std::env::var("OUT_DIR").unwrap()).join("checked_features");
+  let checked_features_out_path = Path::new(&var("OUT_DIR").unwrap()).join("checked_features");
   std::fs::write(
     checked_features_out_path,
     CHECKED_FEATURES.get().unwrap().lock().unwrap().join(","),
@@ -145,8 +147,7 @@ fn main() {
   .expect("failed to write checked_features file");
 
   if target_os == "android" {
-    if let Ok(project_dir) = var("TAURI_ANDROID_PROJECT_PATH") {
-      let project_dir = PathBuf::from(project_dir);
+    if let Some(project_dir) = var_os("TAURI_ANDROID_PROJECT_PATH").map(PathBuf::from) {
       tauri_build::mobile::inject_android_project(
         "./mobile/android",
         project_dir.join("tauri-api"),

+ 1 - 0
examples/api/src-tauri/Cargo.lock

@@ -3072,6 +3072,7 @@ version = "2.0.0-alpha.1"
 dependencies = [
  "anyhow",
  "cargo_toml",
+ "filetime",
  "heck 0.4.1",
  "json-patch",
  "quote",