From fa3f28ae64c94c74f498601d624876443ff59279 Mon Sep 17 00:00:00 2001 From: Julien Cretin Date: Tue, 21 Nov 2023 16:42:12 +0100 Subject: [PATCH] Add fs::exists helper to xtask --- crates/xtask/src/fs.rs | 6 +++++- crates/xtask/src/main.rs | 17 +++++++---------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/crates/xtask/src/fs.rs b/crates/xtask/src/fs.rs index f23fe63a1..e3caf598d 100644 --- a/crates/xtask/src/fs.rs +++ b/crates/xtask/src/fs.rs @@ -43,6 +43,10 @@ fn create_parent(path: impl AsRef) -> Result<()> { Ok(()) } +pub fn exists(path: impl AsRef) -> bool { + path.as_ref().exists() +} + pub fn metadata(path: impl AsRef) -> Result { let name = path.as_ref().display(); std::fs::metadata(path.as_ref()).with_context(|| format!("reading {name} metadata")) @@ -59,7 +63,7 @@ pub fn remove_file(path: impl AsRef) -> Result<()> { } pub fn touch(path: impl AsRef) -> Result<()> { - if path.as_ref().exists() { + if exists(path.as_ref()) { return Ok(()); } write(path, "") diff --git a/crates/xtask/src/main.rs b/crates/xtask/src/main.rs index 26579c27b..762636359 100644 --- a/crates/xtask/src/main.rs +++ b/crates/xtask/src/main.rs @@ -18,7 +18,6 @@ use std::collections::BinaryHeap; use std::fmt::Display; use std::num::ParseIntError; use std::os::unix::prelude::CommandExt; -use std::path::Path; use std::process::{Command, Output}; use std::str::FromStr; @@ -285,7 +284,7 @@ impl AppletOptions { } else { format!("examples/{}/{}", self.lang, self.name) }; - ensure!(Path::new(&dir).exists(), "{dir} does not exist"); + ensure!(fs::exists(&dir), "{dir} does not exist"); let native = match (main.native, &main.native_target, command) { (_, Some(target), command) => { if let Some(AppletCommand::Runner(x)) = command { @@ -509,8 +508,8 @@ impl RunnerOptions { cargo.current_dir(format!("crates/runner-{}", self.name)); fs::touch("target/wasefire/applet.wasm")?; if run && self.name == "host" { - let path = Path::new("target/wasefire/storage.bin"); - if self.erase_flash && path.exists() { + let path = "target/wasefire/storage.bin"; + if self.erase_flash && fs::exists(path) { fs::remove_file(path)?; } replace_command(cargo); @@ -672,15 +671,13 @@ fn wrap_command() -> Result { /// /// Returns whether the copy took place. fn copy_if_changed(src: &str, dst: &str) -> Result { - let dst_file = format!("{dst}.hash"); + let dst_hash = format!("{dst}.hash"); let src_hash = Sha256::digest(fs::read(src)?); - let dst_path = Path::new(dst); - let changed = - !dst_path.exists() || !Path::new(&dst_file).exists() || fs::read(&dst_file)? != *src_hash; + let changed = !fs::exists(dst) || !fs::exists(&dst_hash) || fs::read(&dst_hash)? != *src_hash; if changed { println!("cp {src} {dst}"); fs::copy(src, dst)?; - fs::write(&dst_file, src_hash)?; + fs::write(&dst_hash, src_hash)?; } Ok(changed) } @@ -688,7 +685,7 @@ fn copy_if_changed(src: &str, dst: &str) -> Result { fn ensure_assemblyscript() -> Result<()> { const ASC_VERSION: &str = "0.27.17"; // scripts/upgrade.sh relies on this name const PATH: &str = "examples/assemblyscript/node_modules/assemblyscript/package.json"; - if Path::new(PATH).exists() { + if fs::exists(PATH) { let mut sed = Command::new("sed"); sed.args(["-n", r#"s/^ "version": "\(.*\)",$/\1/p"#, PATH]); if read_output_line(&mut sed)? == ASC_VERSION {