Skip to content

Commit

Permalink
Merge pull request #27 from perrygeo/mp/conditional
Browse files Browse the repository at this point in the history
convert only if the matcha file has been modified
  • Loading branch information
michaeljones committed Dec 4, 2023
2 parents 4f4fb99 + d2611d9 commit 84c7682
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,26 @@ fn color_choice() -> ColorChoice {
}
}

fn requires_update(matcha_path: &std::path::Path) -> bool {
let matcha_result = std::fs::metadata(matcha_path).and_then(|data| data.modified());

// If we fail to get the modified time for any reason than assume we have to update
let Ok(matcha_time) = matcha_result else {
return true;
};

let gleam_path = matcha_path.with_extension("gleam");
let gleam_result = std::fs::metadata(gleam_path).and_then(|data| data.modified());

// If we fail to get the modified time for any reason than assume we have to update
let Ok(gleam_time) = gleam_result else {
return true;
};

// Should update the gleam file if the matcha file is newer than it
matcha_time > gleam_time
}

#[derive(Debug, StructOpt)]
#[structopt(name = "matcha", about = "Compiles templates into Gleam modules")]
struct Opt {
Expand Down Expand Up @@ -86,10 +106,17 @@ fn main() {
let path = entry.path();

if path.extension() == Some(std::ffi::OsStr::new("matcha")) {
if opt.verbose {
println!("Converting {}", path.display());
if requires_update(path) {
if opt.verbose {
println!("Converting {}", path.display());
}
Some(convert(NAME, path))
} else {
if opt.verbose {
println!("Skipping {}, not modified", path.display());
}
None
}
Some(convert(NAME, path))
} else {
None
}
Expand Down

0 comments on commit 84c7682

Please sign in to comment.