Skip to content

Commit 64f0510

Browse files
committed
Move cmd to a separate dir
1 parent 91f9bc2 commit 64f0510

File tree

2 files changed

+58
-53
lines changed

2 files changed

+58
-53
lines changed

xtask/src/cmd.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
use std::process::{Command, Output, Stdio};
2+
3+
use anyhow::{Context, Result};
4+
5+
use crate::project_root;
6+
7+
pub struct Cmd<'a> {
8+
pub unix: &'a str,
9+
pub windows: &'a str,
10+
pub work_dir: &'a str,
11+
}
12+
13+
impl Cmd<'_> {
14+
pub fn run(self) -> Result<()> {
15+
if cfg!(windows) {
16+
run(self.windows, self.work_dir)
17+
} else {
18+
run(self.unix, self.work_dir)
19+
}
20+
}
21+
pub fn run_with_output(self) -> Result<Output> {
22+
if cfg!(windows) {
23+
run_with_output(self.windows, self.work_dir)
24+
} else {
25+
run_with_output(self.unix, self.work_dir)
26+
}
27+
}
28+
}
29+
30+
pub fn run(cmdline: &str, dir: &str) -> Result<()> {
31+
do_run(cmdline, dir, &mut |c| {
32+
c.stdout(Stdio::inherit());
33+
})
34+
.map(|_| ())
35+
}
36+
37+
pub fn run_with_output(cmdline: &str, dir: &str) -> Result<Output> {
38+
do_run(cmdline, dir, &mut |_| {})
39+
}
40+
41+
fn do_run(cmdline: &str, dir: &str, f: &mut dyn FnMut(&mut Command)) -> Result<Output> {
42+
eprintln!("\nwill run: {}", cmdline);
43+
let proj_dir = project_root().join(dir);
44+
let mut args = cmdline.split_whitespace();
45+
let exec = args.next().unwrap();
46+
let mut cmd = Command::new(exec);
47+
f(cmd.args(args).current_dir(proj_dir).stderr(Stdio::inherit()));
48+
let output = cmd.output().with_context(|| format!("running `{}`", cmdline))?;
49+
if !output.status.success() {
50+
anyhow::bail!("`{}` exited with {}", cmdline, output.status);
51+
}
52+
Ok(output)
53+
}

xtask/src/lib.rs

Lines changed: 5 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
//! FIXME: write short doc here
22
3+
mod cmd;
34
pub mod codegen;
45
pub mod install;
56
pub mod pre_commit;
67
mod ast_src;
78

89
use anyhow::Context;
9-
pub use anyhow::Result;
1010
use std::{
1111
env,
1212
path::{Path, PathBuf},
13-
process::{Command, Output, Stdio},
13+
process::{Command, Stdio},
1414
};
1515

1616
use crate::codegen::Mode;
1717

18+
pub use anyhow::Result;
19+
pub use cmd::{run, run_with_output, Cmd};
20+
1821
const TOOLCHAIN: &str = "stable";
1922

2023
pub fn project_root() -> PathBuf {
@@ -27,40 +30,6 @@ pub fn project_root() -> PathBuf {
2730
.to_path_buf()
2831
}
2932

30-
pub struct Cmd<'a> {
31-
pub unix: &'a str,
32-
pub windows: &'a str,
33-
pub work_dir: &'a str,
34-
}
35-
36-
impl Cmd<'_> {
37-
pub fn run(self) -> Result<()> {
38-
if cfg!(windows) {
39-
run(self.windows, self.work_dir)
40-
} else {
41-
run(self.unix, self.work_dir)
42-
}
43-
}
44-
pub fn run_with_output(self) -> Result<Output> {
45-
if cfg!(windows) {
46-
run_with_output(self.windows, self.work_dir)
47-
} else {
48-
run_with_output(self.unix, self.work_dir)
49-
}
50-
}
51-
}
52-
53-
pub fn run(cmdline: &str, dir: &str) -> Result<()> {
54-
do_run(cmdline, dir, |c| {
55-
c.stdout(Stdio::inherit());
56-
})
57-
.map(|_| ())
58-
}
59-
60-
pub fn run_with_output(cmdline: &str, dir: &str) -> Result<Output> {
61-
do_run(cmdline, dir, |_| {})
62-
}
63-
6433
pub fn run_rustfmt(mode: Mode) -> Result<()> {
6534
match Command::new("rustup")
6635
.args(&["run", TOOLCHAIN, "--", "cargo", "fmt", "--version"])
@@ -132,20 +101,3 @@ pub fn run_fuzzer() -> Result<()> {
132101

133102
run("rustup run nightly -- cargo fuzz run parser", "./crates/ra_syntax")
134103
}
135-
136-
fn do_run<F>(cmdline: &str, dir: &str, mut f: F) -> Result<Output>
137-
where
138-
F: FnMut(&mut Command),
139-
{
140-
eprintln!("\nwill run: {}", cmdline);
141-
let proj_dir = project_root().join(dir);
142-
let mut args = cmdline.split_whitespace();
143-
let exec = args.next().unwrap();
144-
let mut cmd = Command::new(exec);
145-
f(cmd.args(args).current_dir(proj_dir).stderr(Stdio::inherit()));
146-
let output = cmd.output().with_context(|| format!("running `{}`", cmdline))?;
147-
if !output.status.success() {
148-
anyhow::bail!("`{}` exited with {}", cmdline, output.status);
149-
}
150-
Ok(output)
151-
}

0 commit comments

Comments
 (0)