Skip to content

Commit

Permalink
use write_if_changed utils
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Aug 19, 2024
1 parent e871e55 commit 1d4593d
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 42 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.
7 changes: 3 additions & 4 deletions core/tauri-acl-schema/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -27,7 +26,7 @@ pub fn main() -> Result<(), Box<dyn Error>> {
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(())
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 @@ -23,6 +23,7 @@ use tauri_utils::{
manifest::Manifest,
APP_ACL_KEY,
},
io::write_if_changed,
platform::Target,
};

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, &default_permission_toml_path)
.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
20 changes: 5 additions & 15 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, io::write_if_changed};

use crate::is_dev;

Expand Down Expand Up @@ -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!(
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, io::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(&toml, &out_path)
.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:?}"));
}
}
11 changes: 10 additions & 1 deletion core/tauri-utils/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down
10 changes: 3 additions & 7 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::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},
Expand Down Expand Up @@ -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());
}
}
Expand Down

0 comments on commit 1d4593d

Please sign in to comment.