Skip to content

Commit 5c335ae

Browse files
Flakebilucasfernog
andauthored
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 <[email protected]>
1 parent 8ae52a6 commit 5c335ae

File tree

6 files changed

+34
-30
lines changed

6 files changed

+34
-30
lines changed

.changes/avoid-rebuilds.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"tauri": patch:bug
3+
"tauri-build": patch:bug
4+
"tauri-utils": patch:bug
5+
---
6+
7+
Prevent build script from rerunning unnecessarily by only writing files when the content changes.

core/tauri-acl-schema/build.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ use std::{error::Error, path::PathBuf};
66

77
use schemars::schema_for;
88
use tauri_utils::{
9-
acl::capability::Capability,
10-
acl::{Permission, Scopes},
9+
acl::{capability::Capability, Permission, Scopes},
1110
write_if_changed,
1211
};
1312

core/tauri-build/src/acl.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use tauri_utils::{
2424
APP_ACL_KEY,
2525
},
2626
platform::Target,
27+
write_if_changed,
2728
};
2829

2930
const CAPABILITIES_SCHEMA_FILE_NAME: &str = "schema.json";
@@ -384,7 +385,8 @@ permissions = [{default_permissions}]
384385

385386
let default_permission_toml_path = plugin_out_dir.join("default.toml");
386387

387-
write_if_changed(&default_permission_toml, &default_permission_toml_path);
388+
write_if_changed(&default_permission_toml_path, default_permission_toml)
389+
.unwrap_or_else(|_| panic!("unable to autogenerate {default_permission_toml_path:?}"));
388390
}
389391

390392
tauri_utils::acl::build::define_permissions(
@@ -428,12 +430,6 @@ permissions = [{default_permissions}]
428430
Ok(acl_manifests)
429431
}
430432

431-
fn write_if_changed(content: &str, path: &Path) {
432-
if content != read_to_string(path).unwrap_or_default() {
433-
std::fs::write(path, content).unwrap_or_else(|_| panic!("unable to autogenerate {path:?}"));
434-
}
435-
}
436-
437433
pub fn app_manifest_permissions(
438434
out_dir: &Path,
439435
manifest: AppManifest,

core/tauri-build/src/mobile.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::{fs::write, path::PathBuf};
66

77
use anyhow::{Context, Result};
88
use semver::Version;
9-
use tauri_utils::config::Config;
9+
use tauri_utils::{config::Config, write_if_changed};
1010

1111
use crate::is_dev;
1212

@@ -80,20 +80,25 @@ dependencies {"
8080
}
8181
}
8282

83-
write(&gradle_settings_path, gradle_settings).context("failed to write tauri.settings.gradle")?;
83+
// Overwrite only if changed to not trigger rebuilds
84+
write_if_changed(&gradle_settings_path, gradle_settings)
85+
.context("failed to write tauri.settings.gradle")?;
8486

85-
write(&app_build_gradle_path, app_build_gradle)
87+
write_if_changed(&app_build_gradle_path, app_build_gradle)
8688
.context("failed to write tauri.build.gradle.kts")?;
8789

8890
if !app_tauri_properties.is_empty() {
89-
write(
90-
&app_tauri_properties_path,
91-
format!(
92-
"// THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.\n{}",
93-
app_tauri_properties.join("\n")
94-
),
95-
)
96-
.context("failed to write tauri.properties")?;
91+
let app_tauri_properties_content = format!(
92+
"// THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.\n{}",
93+
app_tauri_properties.join("\n")
94+
);
95+
if std::fs::read_to_string(&app_tauri_properties_path)
96+
.map(|o| o != app_tauri_properties_content)
97+
.unwrap_or(true)
98+
{
99+
write(&app_tauri_properties_path, app_tauri_properties_content)
100+
.context("failed to write tauri.properties")?;
101+
}
97102
}
98103

99104
println!("cargo:rerun-if-changed={}", gradle_settings_path.display());

core/tauri-utils/src/acl/build.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use std::{
1111
path::{Path, PathBuf},
1212
};
1313

14-
use crate::acl::Error;
14+
use crate::{acl::Error, write_if_changed};
1515
use schemars::{
1616
schema::{InstanceType, Metadata, RootSchema, Schema, SchemaObject, SubschemaValidation},
1717
schema_for,
@@ -450,7 +450,8 @@ commands.deny = ["{command}"]
450450
);
451451

452452
let out_path = path.join(format!("{command}.toml"));
453-
write_if_changed(&toml, &out_path);
453+
write_if_changed(&out_path, toml)
454+
.unwrap_or_else(|_| panic!("unable to autogenerate {out_path:?}"));
454455

455456
autogenerated
456457
.allowed
@@ -462,9 +463,3 @@ commands.deny = ["{command}"]
462463

463464
autogenerated
464465
}
465-
466-
fn write_if_changed(content: &str, path: &Path) {
467-
if content != read_to_string(path).unwrap_or_default() {
468-
std::fs::write(path, content).unwrap_or_else(|_| panic!("unable to autogenerate {path:?}"));
469-
}
470-
}

core/tauri/build.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
// SPDX-License-Identifier: MIT
44

55
use heck::AsShoutySnakeCase;
6+
use tauri_utils::write_if_changed;
67

78
use std::env::var_os;
89
use std::fs::create_dir_all;
910
use std::fs::read_dir;
1011
use std::fs::read_to_string;
11-
use std::fs::write;
1212
use std::{
1313
env::var,
1414
path::{Path, PathBuf},
@@ -289,7 +289,9 @@ fn main() {
289289
.replace("{{library}}", &library);
290290

291291
let out_path = kotlin_out_dir.join(file.file_name());
292-
write(&out_path, content).expect("Failed to write kotlin file");
292+
// Overwrite only if changed to not trigger rebuilds
293+
write_if_changed(&out_path, &content).expect("Failed to write kotlin file");
294+
293295
println!("cargo:rerun-if-changed={}", out_path.display());
294296
}
295297
}

0 commit comments

Comments
 (0)