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::path::PathBuf;
use std::process::{Command, ExitStatus, Stdio};
#[cfg(not(windows))]
const CARGO_EXPAND_BIN: &str = "cargo-expand";
const CARGO_EXPAND: &str = "cargo-expand";
#[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 is_cargo_expand_present() -> bool {
if let Ok(var) = env::var("PATH") {
for path in var.split(":").map(PathBuf::from) {
let cargo_expand_path = path.join(CARGO_EXPAND_BIN);
if cargo_expand_path.exists() {
return true;
}
}
}
false
}
pub fn main() {
if is_cargo_expand_present() {
fn main() {
if Command::new(CARGO_EXPAND)
.arg("--version")
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
.as_ref()
.map(ExitStatus::success)
.unwrap_or(false)
{
println!("cargo:rustc-cfg=cargo_expand");
}
}