Przeglądaj źródła

fix(core): rerun build script if platform config changes, closes #10963 (#11099)

Lucas Fernandes Nogueira 10 miesięcy temu
rodzic
commit
1efa5e7184

+ 7 - 0
.changes/rerun-if-platform-conf.changes.md

@@ -0,0 +1,7 @@
+---
+"tauri-utils": patch:bug
+"tauri-build": patch:bug
+"tauri-codegen": patch:bug
+---
+
+Rerun build script if the platform-specific configuration file changes.

+ 10 - 0
Cargo.lock

@@ -5867,6 +5867,16 @@ dependencies = [
  "windows-registry",
 ]
 
+[[package]]
+name = "resources"
+version = "0.1.0"
+dependencies = [
+ "serde",
+ "serde_json",
+ "tauri",
+ "tauri-build",
+]
+
 [[package]]
 name = "restart"
 version = "0.1.0"

+ 1 - 0
Cargo.toml

@@ -31,6 +31,7 @@ members = [
   # examples
   "examples/file-associations/src-tauri",
   "examples/api/src-tauri",
+  "examples/resources/src-tauri",
   "examples/api/src-tauri/tauri-plugin-sample",
 ]
 resolver = "2"

+ 6 - 4
crates/tauri-build/src/lib.rs

@@ -473,10 +473,12 @@ pub fn try_build(attributes: Attributes) -> Result<()> {
   let target_triple = env::var("TARGET").unwrap();
   let target = tauri_utils::platform::Target::from_triple(&target_triple);
 
-  let mut config = serde_json::from_value(tauri_utils::config::parse::read_from(
-    target,
-    env::current_dir().unwrap(),
-  )?)?;
+  let (config, merged_config_path) =
+    tauri_utils::config::parse::read_from(target, env::current_dir().unwrap())?;
+  if let Some(merged_config_path) = merged_config_path {
+    println!("cargo:rerun-if-changed={}", merged_config_path.display());
+  }
+  let mut config = serde_json::from_value(config)?;
   if let Ok(env) = env::var("TAURI_CONFIG") {
     let merge_config: serde_json::Value = serde_json::from_str(&env)?;
     json_patch::merge(&mut config, &merge_config);

+ 2 - 4
crates/tauri-codegen/src/lib.rs

@@ -79,10 +79,8 @@ pub fn get_config(path: &Path) -> Result<(Config, PathBuf), CodegenConfigError>
   // it is impossible for the content of two separate configs to get mixed up. The chances are
   // already unlikely unless the developer goes out of their way to run the cli on a different
   // project than the target crate.
-  let mut config = serde_json::from_value(tauri_utils::config::parse::read_from(
-    target,
-    parent.clone(),
-  )?)?;
+  let mut config =
+    serde_json::from_value(tauri_utils::config::parse::read_from(target, parent.clone())?.0)?;
 
   if let Ok(env) = std::env::var("TAURI_CONFIG") {
     let merge_config: serde_json::Value =

+ 10 - 3
crates/tauri-utils/src/config/parse.rs

@@ -174,13 +174,20 @@ pub fn is_configuration_file(target: Target, path: &Path) -> bool {
 /// - `tauri.ios.conf.json[5]` or `Tauri.ios.toml` on iOS
 ///   Merging the configurations using [JSON Merge Patch (RFC 7396)].
 ///
+/// Returns the raw configuration and the platform config path, if any.
+///
 /// [JSON Merge Patch (RFC 7396)]: https://datatracker.ietf.org/doc/html/rfc7396.
-pub fn read_from(target: Target, root_dir: PathBuf) -> Result<Value, ConfigError> {
+pub fn read_from(
+  target: Target,
+  root_dir: PathBuf,
+) -> Result<(Value, Option<PathBuf>), ConfigError> {
   let mut config: Value = parse_value(target, root_dir.join("tauri.conf.json"))?.0;
-  if let Some((platform_config, _)) = read_platform(target, root_dir)? {
+  if let Some((platform_config, path)) = read_platform(target, root_dir)? {
     merge(&mut config, &platform_config);
+    Ok((config, Some(path)))
+  } else {
+    Ok((config, None))
   }
-  Ok(config)
 }
 
 /// Reads the platform-specific configuration file from the given root directory if it exists.