Skip to content

Commit 5f00080

Browse files
committed
Allow to set env vars and pipe stdin via not_bash
1 parent 36775ef commit 5f00080

File tree

2 files changed

+79
-92
lines changed

2 files changed

+79
-92
lines changed

xtask/src/lib.rs

Lines changed: 30 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,18 @@ mod ast_src;
1212

1313
use std::{
1414
env,
15-
io::Write,
1615
path::{Path, PathBuf},
17-
process::{Command, Stdio},
1816
};
17+
1918
use walkdir::{DirEntry, WalkDir};
2019

2120
use crate::{
2221
codegen::Mode,
23-
not_bash::{date_iso, fs2, pushd, rm_rf, run},
22+
not_bash::{date_iso, fs2, pushd, pushenv, rm_rf, run},
2423
};
2524

2625
pub use anyhow::{bail, Context as _, Result};
2726

28-
const RUSTFMT_TOOLCHAIN: &str = "stable";
29-
3027
pub fn project_root() -> PathBuf {
3128
Path::new(
3229
&env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| env!("CARGO_MANIFEST_DIR").to_owned()),
@@ -54,77 +51,44 @@ pub fn rust_files(path: &Path) -> impl Iterator<Item = PathBuf> {
5451

5552
pub fn run_rustfmt(mode: Mode) -> Result<()> {
5653
let _dir = pushd(project_root());
54+
let _e = pushenv("RUSTUP_TOOLCHAIN", "stable");
5755
ensure_rustfmt()?;
58-
59-
if Command::new("cargo")
60-
.env("RUSTUP_TOOLCHAIN", RUSTFMT_TOOLCHAIN)
61-
.args(&["fmt", "--"])
62-
.args(if mode == Mode::Verify { &["--check"][..] } else { &[] })
63-
.stderr(Stdio::inherit())
64-
.status()?
65-
.success()
66-
{
67-
Ok(())
68-
} else {
69-
bail!("Rustfmt failed");
70-
}
56+
match mode {
57+
Mode::Overwrite => run!("cargo fmt"),
58+
Mode::Verify => run!("cargo fmt -- --check"),
59+
}?;
60+
Ok(())
7161
}
7262

7363
fn reformat(text: impl std::fmt::Display) -> Result<String> {
64+
let _e = pushenv("RUSTUP_TOOLCHAIN", "stable");
7465
ensure_rustfmt()?;
75-
let mut rustfmt = Command::new("rustfmt")
76-
.env("RUSTUP_TOOLCHAIN", RUSTFMT_TOOLCHAIN)
77-
.args(&["--config-path"])
78-
.arg(project_root().join("rustfmt.toml"))
79-
.args(&["--config", "fn_single_line=true"])
80-
.stdin(Stdio::piped())
81-
.stdout(Stdio::piped())
82-
.spawn()?;
83-
write!(rustfmt.stdin.take().unwrap(), "{}", text)?;
84-
let output = rustfmt.wait_with_output()?;
85-
let stdout = String::from_utf8(output.stdout)?;
66+
let stdout = run!(
67+
"rustfmt --config-path {} --config fn_single_line=true", project_root().join("rustfmt.toml").display();
68+
<text.to_string().as_bytes()
69+
)?;
8670
let preamble = "Generated file, do not edit by hand, see `xtask/src/codegen`";
87-
Ok(format!("//! {}\n\n{}", preamble, stdout))
71+
Ok(format!("//! {}\n\n{}\n", preamble, stdout))
8872
}
8973

9074
fn ensure_rustfmt() -> Result<()> {
91-
match Command::new("rustfmt")
92-
.args(&["--version"])
93-
.env("RUSTUP_TOOLCHAIN", RUSTFMT_TOOLCHAIN)
94-
.stdout(Stdio::piped())
95-
.stderr(Stdio::null())
96-
.spawn()
97-
.and_then(|child| child.wait_with_output())
98-
{
99-
Ok(output)
100-
if output.status.success()
101-
&& std::str::from_utf8(&output.stdout)?.contains(RUSTFMT_TOOLCHAIN) =>
102-
{
103-
Ok(())
104-
}
105-
_ => {
106-
bail!(
107-
"Failed to run rustfmt from toolchain '{0}'. \
108-
Please run `rustup component add rustfmt --toolchain {0}` to install it.",
109-
RUSTFMT_TOOLCHAIN,
110-
);
111-
}
75+
let out = run!("rustfmt --version")?;
76+
if !out.contains("stable") {
77+
bail!(
78+
"Failed to run rustfmt from toolchain 'stable'. \
79+
Please run `rustup component add rustfmt --toolchain stable` to install it.",
80+
)
11281
}
82+
Ok(())
11383
}
11484

11585
pub fn run_clippy() -> Result<()> {
116-
match Command::new("cargo")
117-
.args(&["clippy", "--version"])
118-
.stderr(Stdio::null())
119-
.stdout(Stdio::null())
120-
.status()
121-
{
122-
Ok(status) if status.success() => (),
123-
_ => bail!(
86+
if run!("cargo clippy --version").is_err() {
87+
bail!(
12488
"Failed run cargo clippy. \
12589
Please run `rustup component add clippy` to install it.",
126-
),
127-
};
90+
)
91+
}
12892

12993
let allowed_lints = [
13094
"clippy::collapsible_if",
@@ -138,33 +102,18 @@ pub fn run_clippy() -> Result<()> {
138102

139103
pub fn run_fuzzer() -> Result<()> {
140104
let _d = pushd("./crates/ra_syntax");
105+
let _e = pushenv("RUSTUP_TOOLCHAIN", "nightly");
141106
if run!("cargo fuzz --help").is_err() {
142107
run!("cargo install cargo-fuzz")?;
143108
};
144109

145110
// Expecting nightly rustc
146-
match Command::new("rustc")
147-
.args(&["--version"])
148-
.env("RUSTUP_TOOLCHAIN", "nightly")
149-
.stdout(Stdio::piped())
150-
.stderr(Stdio::null())
151-
.spawn()
152-
.and_then(|child| child.wait_with_output())
153-
{
154-
Ok(output)
155-
if output.status.success()
156-
&& std::str::from_utf8(&output.stdout)?.contains("nightly") => {}
157-
_ => bail!("fuzz tests require nightly rustc"),
111+
let out = run!("rustc --version")?;
112+
if !out.contains("nightly") {
113+
bail!("fuzz tests require nightly rustc")
158114
}
159115

160-
let status = Command::new("cargo")
161-
.env("RUSTUP_TOOLCHAIN", "nightly")
162-
.args(&["fuzz", "run", "parser"])
163-
.stderr(Stdio::inherit())
164-
.status()?;
165-
if !status.success() {
166-
bail!("{}", status);
167-
}
116+
run!("cargo fuzz run parser")?;
168117
Ok(())
169118
}
170119

xtask/src/not_bash.rs

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
use std::{
44
cell::RefCell,
55
env,
6+
ffi::OsString,
7+
io::Write,
68
path::{Path, PathBuf},
79
process::{Command, Stdio},
810
};
@@ -57,7 +59,10 @@ macro_rules! _run {
5759
run!($($expr),*; echo = true)
5860
};
5961
($($expr:expr),* ; echo = $echo:expr) => {
60-
$crate::not_bash::run_process(format!($($expr),*), $echo)
62+
$crate::not_bash::run_process(format!($($expr),*), $echo, None)
63+
};
64+
($($expr:expr),* ; <$stdin:expr) => {
65+
$crate::not_bash::run_process(format!($($expr),*), false, Some($stdin))
6166
};
6267
}
6368
pub(crate) use _run as run;
@@ -77,6 +82,21 @@ impl Drop for Pushd {
7782
}
7883
}
7984

85+
pub struct Pushenv {
86+
_p: (),
87+
}
88+
89+
pub fn pushenv(var: &str, value: &str) -> Pushenv {
90+
Env::with(|env| env.pushenv(var.into(), value.into()));
91+
Pushenv { _p: () }
92+
}
93+
94+
impl Drop for Pushenv {
95+
fn drop(&mut self) {
96+
Env::with(|env| env.popenv())
97+
}
98+
}
99+
80100
pub fn rm_rf(path: impl AsRef<Path>) -> Result<()> {
81101
let path = path.as_ref();
82102
if !path.exists() {
@@ -90,15 +110,15 @@ pub fn rm_rf(path: impl AsRef<Path>) -> Result<()> {
90110
}
91111

92112
#[doc(hidden)]
93-
pub fn run_process(cmd: String, echo: bool) -> Result<String> {
94-
run_process_inner(&cmd, echo).with_context(|| format!("process `{}` failed", cmd))
113+
pub fn run_process(cmd: String, echo: bool, stdin: Option<&[u8]>) -> Result<String> {
114+
run_process_inner(&cmd, echo, stdin).with_context(|| format!("process `{}` failed", cmd))
95115
}
96116

97117
pub fn date_iso() -> Result<String> {
98118
run!("date --iso --utc")
99119
}
100120

101-
fn run_process_inner(cmd: &str, echo: bool) -> Result<String> {
121+
fn run_process_inner(cmd: &str, echo: bool, stdin: Option<&[u8]>) -> Result<String> {
102122
let mut args = shelx(cmd);
103123
let binary = args.remove(0);
104124
let current_dir = Env::with(|it| it.cwd().to_path_buf());
@@ -107,12 +127,17 @@ fn run_process_inner(cmd: &str, echo: bool) -> Result<String> {
107127
println!("> {}", cmd)
108128
}
109129

110-
let output = Command::new(binary)
111-
.args(args)
112-
.current_dir(current_dir)
113-
.stdin(Stdio::null())
114-
.stderr(Stdio::inherit())
115-
.output()?;
130+
let mut command = Command::new(binary);
131+
command.args(args).current_dir(current_dir).stderr(Stdio::inherit());
132+
let output = match stdin {
133+
None => command.stdin(Stdio::null()).output(),
134+
Some(stdin) => {
135+
command.stdin(Stdio::piped()).stdout(Stdio::piped());
136+
let mut process = command.spawn()?;
137+
process.stdin.take().unwrap().write_all(stdin)?;
138+
process.wait_with_output()
139+
}
140+
}?;
116141
let stdout = String::from_utf8(output.stdout)?;
117142

118143
if echo {
@@ -133,13 +158,15 @@ fn shelx(cmd: &str) -> Vec<String> {
133158

134159
struct Env {
135160
pushd_stack: Vec<PathBuf>,
161+
pushenv_stack: Vec<(OsString, Option<OsString>)>,
136162
}
137163

138164
impl Env {
139165
fn with<F: FnOnce(&mut Env) -> T, T>(f: F) -> T {
140166
thread_local! {
141167
static ENV: RefCell<Env> = RefCell::new(Env {
142-
pushd_stack: vec![env::current_dir().unwrap()]
168+
pushd_stack: vec![env::current_dir().unwrap()],
169+
pushenv_stack: vec![],
143170
});
144171
}
145172
ENV.with(|it| f(&mut *it.borrow_mut()))
@@ -154,6 +181,17 @@ impl Env {
154181
self.pushd_stack.pop().unwrap();
155182
env::set_current_dir(self.cwd()).unwrap();
156183
}
184+
fn pushenv(&mut self, var: OsString, value: OsString) {
185+
self.pushenv_stack.push((var.clone(), env::var_os(&var)));
186+
env::set_var(var, value)
187+
}
188+
fn popenv(&mut self) {
189+
let (var, value) = self.pushenv_stack.pop().unwrap();
190+
match value {
191+
None => env::remove_var(var),
192+
Some(value) => env::set_var(var, value),
193+
}
194+
}
157195
fn cwd(&self) -> &Path {
158196
self.pushd_stack.last().unwrap()
159197
}

0 commit comments

Comments
 (0)