use std::path::Path;
use std::path::PathBuf;
use std::process::Command;
use path_slash::PathBufExt;
const DRY_RUN: bool = false;
pub const VERBOSE: bool = false;
pub fn command(command: &mut Command, description: &str) -> anyhow::Result<()> {
if VERBOSE {
println!("\ndescription: {}; command: {:?}", description, command);
}
if DRY_RUN {
println!("\tOnly a dry run; not executing the command.");
} else {
let status = command
.status()
.map_err(|error| anyhow::anyhow!("{} process: {}", description, error))?;
if !status.success() {
anyhow::bail!("{} failed", description);
}
}
Ok(())
}
pub fn ninja(build_dir: &Path) -> anyhow::Result<()> {
let mut ninja = Command::new("ninja");
ninja.args(["-C", build_dir.to_string_lossy().as_ref()]);
if std::env::var("DRY_RUN").is_ok() {
ninja.arg("-n");
}
command(ninja.arg("install"), "Running ninja install")?;
Ok(())
}
pub fn absolute_path<P: AsRef<Path>>(path: P) -> anyhow::Result<PathBuf> {
let mut full_path = std::env::current_dir()?;
full_path.push(path);
Ok(full_path)
}
pub fn path_windows_to_unix<P: AsRef<Path> + PathBufExt>(path: P) -> anyhow::Result<PathBuf> {
path.to_slash()
.map(|pathbuf| PathBuf::from(pathbuf.to_string()))
.ok_or_else(|| anyhow::anyhow!("Windows-to-Unix path conversion error"))
}
pub fn check_presence(name: &str) -> anyhow::Result<()> {
let status = Command::new("which")
.arg(name)
.status()
.map_err(|error| anyhow::anyhow!("`which {}` process: {}", name, error))?;
if !status.success() {
anyhow::bail!("Tool `{}` is missing. Please install", name);
}
Ok(())
}