Skip to content

Commit

Permalink
fix(cli): when we generate a file that exists and has the same conten…
Browse files Browse the repository at this point in the history
…t, skip writing
  • Loading branch information
W95Psp committed Aug 13, 2024
1 parent 35faf49 commit 610d514
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
22 changes: 18 additions & 4 deletions cli/subcommands/src/cargo_hax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,18 @@ impl HaxMessage {
let message = Level::Error.title(&message);
eprintln!("{}", renderer.render(message))
}
Self::WroteFile { path } => {
let title = format!("hax: wrote file {}", path.display());
Self::ProducedFile { mut path, wrote } => {
// Make path relative if possible
if let Ok(current_dir) = std::env::current_dir() {
if let Ok(relative) = path.strip_prefix(current_dir) {
path = PathBuf::from(".").join(relative).to_path_buf();
}
}
let title = if wrote {
format!("hax: wrote file {}", path.display())
} else {
format!("hax: unchanged file {}", path.display())
};
eprintln!("{}", renderer.render(Level::Info.title(&title)))
}
Self::HaxEngineFailure { exit_code } => {
Expand Down Expand Up @@ -279,8 +289,12 @@ fn run_engine(
} else {
let path = out_dir.join(&file.path);
std::fs::create_dir_all(&path.parent().unwrap()).unwrap();
std::fs::write(&path, file.contents).unwrap();
HaxMessage::WroteFile { path }.report(message_format, None)
let mut wrote = false;
if fs::read_to_string(&path).as_ref().ok() != Some(&file.contents) {
std::fs::write(&path, file.contents).unwrap();
wrote = true;
}
HaxMessage::ProducedFile { path, wrote }.report(message_format, None)
}
}
FromEngine::DebugString(debug) => {
Expand Down
3 changes: 2 additions & 1 deletion hax-types/src/diagnostics/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ pub enum HaxMessage {
EngineNotFound {
is_opam_setup_correctly: bool,
} = 0,
WroteFile {
ProducedFile {
path: PathBuf,
wrote: bool,
} = 1,
HaxEngineFailure {
exit_code: i32,
Expand Down

0 comments on commit 610d514

Please sign in to comment.