Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

convert only if the matcha file has been modified #27

Merged
merged 2 commits into from
Dec 4, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading