diff --git a/.changes/avoid-rebuilds.md b/.changes/avoid-rebuilds.md new file mode 100644 index 00000000000..5963d54535a --- /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 8a2f3d1bb71..7d6ad45ef17 100644 --- a/core/tauri-acl-schema/build.rs +++ b/core/tauri-acl-schema/build.rs @@ -6,9 +6,8 @@ use std::{error::Error, path::PathBuf}; use schemars::schema_for; use tauri_utils::{ - acl::capability::Capability, - acl::{Permission, Scopes}, - write_if_changed, + acl::{capability::Capability, Permission, Scopes}, + io::write_if_changed, }; macro_rules! schema { @@ -27,7 +26,7 @@ pub fn main() -> Result<(), Box> { let out = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR")?); for (filename, schema) in schemas { let schema = serde_json::to_string_pretty(&schema)?; - write_if_changed(out.join(filename), schema)?; + write_if_changed(&schema, &out.join(filename))?; } Ok(()) diff --git a/core/tauri-build/src/acl.rs b/core/tauri-build/src/acl.rs index 2f916f1ae1c..821a8093692 100644 --- a/core/tauri-build/src/acl.rs +++ b/core/tauri-build/src/acl.rs @@ -23,6 +23,7 @@ use tauri_utils::{ manifest::Manifest, APP_ACL_KEY, }, + io::write_if_changed, platform::Target, }; @@ -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, &default_permission_toml_path) + .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 4e60a9b335d..76068b81c67 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, io::write_if_changed}; use crate::is_dev; @@ -81,21 +81,11 @@ dependencies {" } // Overwrite only if changed to not trigger rebuilds - if std::fs::read_to_string(&gradle_settings_path) - .map(|o| o != gradle_settings) - .unwrap_or(true) - { - write(&gradle_settings_path, gradle_settings) - .context("failed to write tauri.settings.gradle")?; - } + write_if_changed(&gradle_settings, &gradle_settings_path) + .context("failed to write tauri.settings.gradle")?; - if std::fs::read_to_string(&app_build_gradle_path) - .map(|o| o != app_build_gradle) - .unwrap_or(true) - { - write(&app_build_gradle_path, app_build_gradle) - .context("failed to write tauri.build.gradle.kts")?; - } + write_if_changed(&app_build_gradle, &app_build_gradle_path) + .context("failed to write tauri.build.gradle.kts")?; if !app_tauri_properties.is_empty() { let app_tauri_properties_content = format!( diff --git a/core/tauri-utils/src/acl/build.rs b/core/tauri-utils/src/acl/build.rs index 7cadf044de0..abcc5107d76 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, io::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(&toml, &out_path) + .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-utils/src/io.rs b/core/tauri-utils/src/io.rs index be4fd0d0717..758d1363f86 100644 --- a/core/tauri-utils/src/io.rs +++ b/core/tauri-utils/src/io.rs @@ -4,7 +4,16 @@ //! IO helpers. -use std::io::BufRead; +use std::{fs::read_to_string, io::BufRead, path::Path}; + +/// Write to the given file if the content is different. +pub fn write_if_changed(content: &str, path: &Path) -> std::io::Result<()> { + if content != read_to_string(path).unwrap_or_default() { + std::fs::write(path, content) + } else { + Ok(()) + } +} /// Read all bytes until a newline (the `0xA` byte) or a carriage return (`\r`) is reached, and append them to the provided buffer. /// diff --git a/core/tauri/build.rs b/core/tauri/build.rs index 1f7bfeb3592..58b9363841a 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::io::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}, @@ -290,12 +290,8 @@ fn main() { let out_path = kotlin_out_dir.join(file.file_name()); // Overwrite only if changed to not trigger rebuilds - if std::fs::read_to_string(&out_path) - .map(|o| o != content) - .unwrap_or(true) - { - write(&out_path, content).expect("Failed to write kotlin file"); - } + write_if_changed(&content, &out_path).expect("Failed to write kotlin file"); + println!("cargo:rerun-if-changed={}", out_path.display()); } }