From 5c335ae9ad88e46c2135a557390f6e808c9a6088 Mon Sep 17 00:00:00 2001 From: Sebastian Neubauer Date: Mon, 19 Aug 2024 18:33:39 +0200 Subject: [PATCH] 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 --- .changes/avoid-rebuilds.md | 7 +++++++ core/tauri-acl-schema/build.rs | 3 +-- core/tauri-build/src/acl.rs | 10 +++------- core/tauri-build/src/mobile.rs | 27 ++++++++++++++++----------- core/tauri-utils/src/acl/build.rs | 11 +++-------- core/tauri/build.rs | 6 ++++-- 6 files changed, 34 insertions(+), 30 deletions(-) create mode 100644 .changes/avoid-rebuilds.md diff --git a/.changes/avoid-rebuilds.md b/.changes/avoid-rebuilds.md new file mode 100644 index 000000000..5963d5453 --- /dev/null +++ b/.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. diff --git a/core/tauri-acl-schema/build.rs b/core/tauri-acl-schema/build.rs index 8a2f3d1bb..1c7302576 100644 --- a/core/tauri-acl-schema/build.rs +++ b/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, }; diff --git a/core/tauri-build/src/acl.rs b/core/tauri-build/src/acl.rs index 2f916f1ae..bbae74dd0 100644 --- a/core/tauri-build/src/acl.rs +++ b/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, diff --git a/core/tauri-build/src/mobile.rs b/core/tauri-build/src/mobile.rs index 3c8e7c1cb..836b0d935 100644 --- a/core/tauri-build/src/mobile.rs +++ b/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()); diff --git a/core/tauri-utils/src/acl/build.rs b/core/tauri-utils/src/acl/build.rs index 7cadf044d..8c9956752 100644 --- a/core/tauri-utils/src/acl/build.rs +++ b/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:?}")); - } -} diff --git a/core/tauri/build.rs b/core/tauri/build.rs index b1935105d..84dd0291d 100644 --- a/core/tauri/build.rs +++ b/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()); } }