Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 91f8ced

Browse files
bkchrandresilva
andauthored
Make wasm-builder check before copy/write files if the content is the same (#6149)
* Make `wasm-builder` check before copy/write files if the content is the same * Update utils/wasm-builder/src/lib.rs Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com> Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com>
1 parent 4fd3ee1 commit 91f8ced

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

utils/wasm-builder/src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,17 @@ fn write_file_if_changed(file: PathBuf, content: String) {
185185
}
186186
}
187187

188+
/// Copy `src` to `dst` if the `dst` does not exist or is different.
189+
fn copy_file_if_changed(src: PathBuf, dst: PathBuf) {
190+
let src_file = fs::read_to_string(&src).ok();
191+
let dst_file = fs::read_to_string(&dst).ok();
192+
193+
if src_file != dst_file {
194+
fs::copy(&src, &dst)
195+
.expect(&format!("Copying `{}` to `{}` can not fail; qed", src.display(), dst.display()));
196+
}
197+
}
198+
188199
/// Get a cargo command that compiles with nightly
189200
fn get_nightly_cargo() -> CargoCommand {
190201
let env_cargo = CargoCommand::new(

utils/wasm-builder/src/wasm_project.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -302,10 +302,10 @@ fn create_wasm_workspace_project(wasm_workspace: &Path, workspace_root_path: &Pa
302302
wasm_workspace_toml.insert("patch".into(), patch.into());
303303
}
304304

305-
fs::write(
305+
write_file_if_changed(
306306
wasm_workspace.join("Cargo.toml"),
307307
toml::to_string_pretty(&wasm_workspace_toml).expect("Wasm workspace toml is valid; qed"),
308-
).expect("WASM workspace `Cargo.toml` writing can not fail; qed");
308+
);
309309
}
310310

311311
/// Get a list of enabled features for the project.
@@ -382,8 +382,7 @@ fn create_project(cargo_manifest: &Path, wasm_workspace: &Path, crate_metadata:
382382

383383
if let Some(crate_lock_file) = find_cargo_lock(cargo_manifest) {
384384
// Use the `Cargo.lock` of the main project.
385-
fs::copy(crate_lock_file, wasm_workspace.join("Cargo.lock"))
386-
.expect("Copying the `Cargo.lock` can not fail; qed");
385+
crate::copy_file_if_changed(crate_lock_file, wasm_workspace.join("Cargo.lock"));
387386
}
388387

389388
project_folder

0 commit comments

Comments
 (0)