Skip to content

Commit

Permalink
fix(android): avoid rebuilds if nothing changed (#10648)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
Flakebi and lucasfernog committed Aug 19, 2024
1 parent 8ae52a6 commit 5c335ae
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 30 deletions.
7 changes: 7 additions & 0 deletions .changes/avoid-rebuilds.md
Original file line number Diff line number Diff line change
@@ -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.
3 changes: 1 addition & 2 deletions core/tauri-acl-schema/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down
10 changes: 3 additions & 7 deletions core/tauri-build/src/acl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use tauri_utils::{
APP_ACL_KEY,
},
platform::Target,
write_if_changed,
};

const CAPABILITIES_SCHEMA_FILE_NAME: &str = "schema.json";
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand Down
27 changes: 16 additions & 11 deletions core/tauri-build/src/mobile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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());
Expand Down
11 changes: 3 additions & 8 deletions core/tauri-utils/src/acl/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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:?}"));
}
}
6 changes: 4 additions & 2 deletions core/tauri/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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());
}
}
Expand Down

0 comments on commit 5c335ae

Please sign in to comment.