From a40871d7159983d26022cb5d85e09dcb0e74124e Mon Sep 17 00:00:00 2001 From: Julien Cretin Date: Mon, 24 Apr 2023 16:44:52 +0200 Subject: [PATCH] Check applet.wasm modification to avoid rebuild (#122) --- crates/xtask/src/main.rs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/crates/xtask/src/main.rs b/crates/xtask/src/main.rs index b5b9b6f8..e8c5b2b8 100644 --- a/crates/xtask/src/main.rs +++ b/crates/xtask/src/main.rs @@ -269,8 +269,10 @@ impl AppletOptions { cargo.env("RUSTFLAGS", rustflags.join(" ")); cargo.current_dir(dir); execute_command(&mut cargo)?; - std::fs::copy(wasm, "target/applet.wasm")?; - self.execute_wasm(main) + if copy_if_newer(&wasm, "target/applet.wasm")? { + self.execute_wasm(main)?; + } + Ok(()) } fn execute_assemblyscript(&self, main: &MainOptions) -> Result<()> { @@ -564,6 +566,17 @@ fn ensure_command(cmd: &[&str]) -> Result<()> { execute_command(&mut ensure_bloat) } +/// Copies a file if newer than the destination. +/// +/// Returns whether the copy took place. +fn copy_if_newer(src: &str, dst: &str) -> Result { + let newer = std::fs::metadata(dst)?.modified()? < std::fs::metadata(src)?.modified()?; + if newer { + std::fs::copy(src, dst)?; + } + Ok(newer) +} + fn main() -> Result<()> { env_logger::init_from_env(env_logger::Env::new().default_filter_or("warn")); Flags::parse().execute()?;