use std::path::Path;
use std::process::Command;
pub fn verify_compiler_binary(binary_path: &Path, required_version: &str) {
let binary_name = binary_path.display();
let expected_prefix = binary_path
.file_name()
.and_then(|name| name.to_str())
.unwrap_or_else(|| panic!("{binary_name} has no UTF-8 file name."));
let install_instructions =
"Run 'scripts/install_compiler_binaries.sh' to install the correct version.";
match Command::new(binary_path).arg("--version").output() {
Ok(output) => {
let version_output = String::from_utf8_lossy(&output.stdout);
let first_line = version_output.lines().find(|line| !line.trim().is_empty());
let installed_version = first_line
.and_then(|line| line.strip_prefix(expected_prefix))
.map(|rest| rest.trim_start())
.and_then(|rest| rest.split_whitespace().next())
.unwrap_or("");
if installed_version != required_version {
panic!(
"{binary_name} version {required_version} is required, but found: \
{installed_version:?} (raw output: {version_output:?}). \
{install_instructions}"
);
}
}
Err(_) => {
panic!("{binary_name} not found. {install_instructions}");
}
}
}