Rely on std::process::Command's path search

This commit is contained in:
David Tolnay 2019-11-24 17:23:02 -08:00
parent bcaebdb2b7
commit d17d8eb3ee
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82

View File

@ -1,28 +1,22 @@
use std::env; use std::process::{Command, ExitStatus, Stdio};
use std::path::PathBuf;
#[cfg(not(windows))] #[cfg(not(windows))]
const CARGO_EXPAND_BIN: &str = "cargo-expand"; const CARGO_EXPAND: &str = "cargo-expand";
#[cfg(windows)] #[cfg(windows)]
const CARGO_EXPAND_BIN: &str = "cargo-expand.exe"; const CARGO_EXPAND: &str = "cargo-expand.exe";
/// Scans paths in PATH env variable for a presence of `CARGO_EXPAND_BIN` file. fn main() {
fn is_cargo_expand_present() -> bool { if Command::new(CARGO_EXPAND)
if let Ok(var) = env::var("PATH") { .arg("--version")
for path in var.split(":").map(PathBuf::from) { .stdin(Stdio::null())
let cargo_expand_path = path.join(CARGO_EXPAND_BIN); .stdout(Stdio::null())
if cargo_expand_path.exists() { .stderr(Stdio::null())
return true; .status()
} .as_ref()
} .map(ExitStatus::success)
} .unwrap_or(false)
{
false
}
pub fn main() {
if is_cargo_expand_present() {
println!("cargo:rustc-cfg=cargo_expand"); println!("cargo:rustc-cfg=cargo_expand");
} }
} }