Parcourir la source

fix(android): avoid rebuilds if nothing changed (#10648)

* fix(android): avoid rebuilds if nothing changed

Unconditionally overwriting files where the build reruns if they changed
leads to rebuilds every time.
Only overwrite a file if its content is different to not rebuild in such
a case.

* use write_if_changed utils

* use existing function

---------

Co-authored-by: Lucas Nogueira <lucas@tauri.app>
Sebastian Neubauer il y a 11 mois
Parent
commit
5c335ae9ad

+ 7 - 0
.changes/avoid-rebuilds.md

@@ -0,0 +1,7 @@
+---
+"tauri": patch:bug
+"tauri-build": patch:bug
+"tauri-utils": patch:bug
+---
+
+Prevent build script from rerunning unnecessarily by only writing files when the content changes.

+ 1 - 2
core/tauri-acl-schema/build.rs

@@ -6,8 +6,7 @@ use std::{error::Error, path::PathBuf};
 
 use schemars::schema_for;
 use tauri_utils::{
-  acl::capability::Capability,
-  acl::{Permission, Scopes},
+  acl::{capability::Capability, Permission, Scopes},
   write_if_changed,
 };
 

+ 3 - 7
core/tauri-build/src/acl.rs

@@ -24,6 +24,7 @@ use tauri_utils::{
     APP_ACL_KEY,
   },
   platform::Target,
+  write_if_changed,
 };
 
 const CAPABILITIES_SCHEMA_FILE_NAME: &str = "schema.json";
@@ -384,7 +385,8 @@ permissions = [{default_permissions}]
 
         let default_permission_toml_path = plugin_out_dir.join("default.toml");
 
-        write_if_changed(&default_permission_toml, &default_permission_toml_path);
+        write_if_changed(&default_permission_toml_path, default_permission_toml)
+          .unwrap_or_else(|_| panic!("unable to autogenerate {default_permission_toml_path:?}"));
       }
 
       tauri_utils::acl::build::define_permissions(
@@ -428,12 +430,6 @@ permissions = [{default_permissions}]
   Ok(acl_manifests)
 }
 
-fn write_if_changed(content: &str, path: &Path) {
-  if content != read_to_string(path).unwrap_or_default() {
-    std::fs::write(path, content).unwrap_or_else(|_| panic!("unable to autogenerate {path:?}"));
-  }
-}
-
 pub fn app_manifest_permissions(
   out_dir: &Path,
   manifest: AppManifest,

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

@@ -6,7 +6,7 @@ use std::{fs::write, path::PathBuf};
 
 use anyhow::{Context, Result};
 use semver::Version;
-use tauri_utils::config::Config;
+use tauri_utils::{config::Config, write_if_changed};
 
 use crate::is_dev;
 
@@ -80,20 +80,25 @@ dependencies {"
     }
   }
 
-  write(&gradle_settings_path, gradle_settings).context("failed to write tauri.settings.gradle")?;
+  // Overwrite only if changed to not trigger rebuilds
+  write_if_changed(&gradle_settings_path, gradle_settings)
+    .context("failed to write tauri.settings.gradle")?;
 
-  write(&app_build_gradle_path, app_build_gradle)
+  write_if_changed(&app_build_gradle_path, app_build_gradle)
     .context("failed to write tauri.build.gradle.kts")?;
 
   if !app_tauri_properties.is_empty() {
-    write(
-      &app_tauri_properties_path,
-      format!(
-        "// THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.\n{}",
-        app_tauri_properties.join("\n")
-      ),
-    )
-    .context("failed to write tauri.properties")?;
+    let app_tauri_properties_content = format!(
+      "// THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.\n{}",
+      app_tauri_properties.join("\n")
+    );
+    if std::fs::read_to_string(&app_tauri_properties_path)
+      .map(|o| o != app_tauri_properties_content)
+      .unwrap_or(true)
+    {
+      write(&app_tauri_properties_path, app_tauri_properties_content)
+        .context("failed to write tauri.properties")?;
+    }
   }
 
   println!("cargo:rerun-if-changed={}", gradle_settings_path.display());

+ 3 - 8
core/tauri-utils/src/acl/build.rs

@@ -11,7 +11,7 @@ use std::{
   path::{Path, PathBuf},
 };
 
-use crate::acl::Error;
+use crate::{acl::Error, write_if_changed};
 use schemars::{
   schema::{InstanceType, Metadata, RootSchema, Schema, SchemaObject, SubschemaValidation},
   schema_for,
@@ -450,7 +450,8 @@ commands.deny = ["{command}"]
     );
 
     let out_path = path.join(format!("{command}.toml"));
-    write_if_changed(&toml, &out_path);
+    write_if_changed(&out_path, toml)
+      .unwrap_or_else(|_| panic!("unable to autogenerate {out_path:?}"));
 
     autogenerated
       .allowed
@@ -462,9 +463,3 @@ commands.deny = ["{command}"]
 
   autogenerated
 }
-
-fn write_if_changed(content: &str, path: &Path) {
-  if content != read_to_string(path).unwrap_or_default() {
-    std::fs::write(path, content).unwrap_or_else(|_| panic!("unable to autogenerate {path:?}"));
-  }
-}

+ 4 - 2
core/tauri/build.rs

@@ -3,12 +3,12 @@
 // SPDX-License-Identifier: MIT
 
 use heck::AsShoutySnakeCase;
+use tauri_utils::write_if_changed;
 
 use std::env::var_os;
 use std::fs::create_dir_all;
 use std::fs::read_dir;
 use std::fs::read_to_string;
-use std::fs::write;
 use std::{
   env::var,
   path::{Path, PathBuf},
@@ -289,7 +289,9 @@ fn main() {
           .replace("{{library}}", &library);
 
         let out_path = kotlin_out_dir.join(file.file_name());
-        write(&out_path, content).expect("Failed to write kotlin file");
+        // Overwrite only if changed to not trigger rebuilds
+        write_if_changed(&out_path, &content).expect("Failed to write kotlin file");
+
         println!("cargo:rerun-if-changed={}", out_path.display());
       }
     }