Skip to content

Commit

Permalink
Add fs::exists helper to xtask (#302)
Browse files Browse the repository at this point in the history
  • Loading branch information
ia0 authored Nov 21, 2023
1 parent b7d29e1 commit d722ceb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
6 changes: 5 additions & 1 deletion crates/xtask/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ fn create_parent(path: impl AsRef<Path>) -> Result<()> {
Ok(())
}

pub fn exists(path: impl AsRef<Path>) -> bool {
path.as_ref().exists()
}

pub fn metadata(path: impl AsRef<Path>) -> Result<Metadata> {
let name = path.as_ref().display();
std::fs::metadata(path.as_ref()).with_context(|| format!("reading {name} metadata"))
Expand All @@ -59,7 +63,7 @@ pub fn remove_file(path: impl AsRef<Path>) -> Result<()> {
}

pub fn touch(path: impl AsRef<Path>) -> Result<()> {
if path.as_ref().exists() {
if exists(path.as_ref()) {
return Ok(());
}
write(path, "")
Expand Down
17 changes: 7 additions & 10 deletions crates/xtask/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -672,23 +671,21 @@ fn wrap_command() -> Result<Command> {
///
/// Returns whether the copy took place.
fn copy_if_changed(src: &str, dst: &str) -> Result<bool> {
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)
}

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 {
Expand Down

0 comments on commit d722ceb

Please sign in to comment.