diff options
-rw-r--r-- | xtask/src/cmd.rs | 53 | ||||
-rw-r--r-- | xtask/src/lib.rs | 58 |
2 files changed, 58 insertions, 53 deletions
diff --git a/xtask/src/cmd.rs b/xtask/src/cmd.rs new file mode 100644 index 000000000..2027f4893 --- /dev/null +++ b/xtask/src/cmd.rs | |||
@@ -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 | } | ||
diff --git a/xtask/src/lib.rs b/xtask/src/lib.rs index fb853e71a..7bfc4a285 100644 --- a/xtask/src/lib.rs +++ b/xtask/src/lib.rs | |||
@@ -1,20 +1,23 @@ | |||
1 | //! FIXME: write short doc here | 1 | //! FIXME: write short doc here |
2 | 2 | ||
3 | mod cmd; | ||
3 | pub mod codegen; | 4 | pub mod codegen; |
4 | pub mod install; | 5 | pub mod install; |
5 | pub mod pre_commit; | 6 | pub mod pre_commit; |
6 | mod ast_src; | 7 | mod ast_src; |
7 | 8 | ||
8 | use anyhow::Context; | 9 | use anyhow::Context; |
9 | pub use anyhow::Result; | ||
10 | use std::{ | 10 | use std::{ |
11 | env, | 11 | env, |
12 | path::{Path, PathBuf}, | 12 | path::{Path, PathBuf}, |
13 | process::{Command, Output, Stdio}, | 13 | process::{Command, Stdio}, |
14 | }; | 14 | }; |
15 | 15 | ||
16 | use crate::codegen::Mode; | 16 | use crate::codegen::Mode; |
17 | 17 | ||
18 | pub use anyhow::Result; | ||
19 | pub use cmd::{run, run_with_output, Cmd}; | ||
20 | |||
18 | const TOOLCHAIN: &str = "stable"; | 21 | const TOOLCHAIN: &str = "stable"; |
19 | 22 | ||
20 | pub fn project_root() -> PathBuf { | 23 | pub fn project_root() -> PathBuf { |
@@ -27,40 +30,6 @@ pub fn project_root() -> PathBuf { | |||
27 | .to_path_buf() | 30 | .to_path_buf() |
28 | } | 31 | } |
29 | 32 | ||
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 | |||
64 | pub fn run_rustfmt(mode: Mode) -> Result<()> { | 33 | pub fn run_rustfmt(mode: Mode) -> Result<()> { |
65 | match Command::new("rustup") | 34 | match Command::new("rustup") |
66 | .args(&["run", TOOLCHAIN, "--", "cargo", "fmt", "--version"]) | 35 | .args(&["run", TOOLCHAIN, "--", "cargo", "fmt", "--version"]) |
@@ -132,20 +101,3 @@ pub fn run_fuzzer() -> Result<()> { | |||
132 | 101 | ||
133 | run("rustup run nightly -- cargo fuzz run parser", "./crates/ra_syntax") | 102 | run("rustup run nightly -- cargo fuzz run parser", "./crates/ra_syntax") |
134 | } | 103 | } |
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 | } | ||