From db455ab7266ee1440ee2b3bd535a548806a89513 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Tue, 15 May 2018 09:37:58 -0700 Subject: [PATCH] Attempt to find a nightly toolchain --- src/main.rs | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 99364ad..7f28cd2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,7 +14,7 @@ use isatty::{stderr_isatty, stdout_isatty}; extern crate tempfile; fn main() { - let result = cargo_expand(); + let result = cargo_expand_or_run_nightly(); process::exit(match result { Ok(code) => code, Err(err) => { @@ -24,6 +24,47 @@ fn main() { }); } +fn cargo_expand_or_run_nightly() -> io::Result { + const NO_RUN_NIGHTLY: &str = "CARGO_EXPAND_NO_RUN_NIGHTLY"; + + let maybe_nightly = !definitely_not_nightly(); + if maybe_nightly || env::var_os(NO_RUN_NIGHTLY).is_some() { + return cargo_expand(); + } + + let mut nightly = Command::new("cargo"); + nightly.arg("+nightly"); + nightly.arg("expand"); + nightly.args(env::args_os().skip(1)); + + // Hopefully prevent infinite re-run loop. + nightly.env(NO_RUN_NIGHTLY, ""); + + let status = nightly.status()?; + + Ok(match status.code() { + Some(code) => code, + None => if status.success() { 0 } else { 1 }, + }) +} + +fn definitely_not_nightly() -> bool { + let mut cmd = Command::new(cargo_binary()); + cmd.arg("--version"); + + let output = match cmd.output() { + Ok(output) => output, + Err(_) => return false, + }; + + let version = match String::from_utf8(output.stdout) { + Ok(version) => version, + Err(_) => return false, + }; + + version.starts_with("cargo 1") && !version.contains("nightly") +} + fn cargo_binary() -> OsString { env::var_os("CARGO").unwrap_or_else(|| "cargo".to_owned().into()) }