From f2e49c84ccc3618a7a85b0323d2648b9137b7489 Mon Sep 17 00:00:00 2001 From: William Kral Date: Wed, 30 Mar 2022 14:26:35 -0700 Subject: [PATCH] Improve rustfmt missing error during build (#1379) --- build/main.rs | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/build/main.rs b/build/main.rs index 1129e44c..a5164deb 100644 --- a/build/main.rs +++ b/build/main.rs @@ -9,7 +9,7 @@ use std::fs; use std::fs::File; use std::io::Write; use std::path::Path; -use std::process::Command; +use std::process::{self, Command}; fn find_prolog_files(libraries: &mut File, prefix: &str, current_dir: &Path) { let entries = match current_dir.read_dir() { @@ -68,10 +68,7 @@ fn main() { .write_all(quoted_output.to_string().as_bytes()) .unwrap(); - Command::new("rustfmt") - .arg(instructions_path.as_os_str()) - .spawn().unwrap() - .wait().unwrap(); + format_generated_file(instructions_path.as_path()); let static_atoms_path = Path::new(&out_dir).join("static_atoms.rs"); let mut static_atoms_file = File::create(&static_atoms_path).unwrap(); @@ -82,10 +79,24 @@ fn main() { .write_all(quoted_output.to_string().as_bytes()) .unwrap(); - Command::new("rustfmt") - .arg(static_atoms_path.as_os_str()) - .spawn().unwrap() - .wait().unwrap(); + format_generated_file(static_atoms_path.as_path()); println!("cargo:rerun-if-changed=src/"); } + +fn format_generated_file(path: &Path) { + Command::new("rustfmt") + .arg(path.as_os_str()) + .spawn() + .unwrap_or_else(|err| { + eprintln!( + "{}: rustfmt (https://github.com/rust-lang/rustfmt) \ + is required build time dependency and must be available \ + in the environment's PATH variable.", + err + ); + process::exit(1); + }) + .wait() + .unwrap(); +} -- 2.54.0