aboutsummaryrefslogtreecommitdiff
path: root/xtask/src/lib.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-01-07 14:33:09 +0000
committerAleksey Kladov <[email protected]>2020-01-07 14:33:09 +0000
commit64f0510d18bc62c6bbe6f3c7d4b0d43665ccc1eb (patch)
treedc5376a0e7a656d103190666b7def3bc5ca33d34 /xtask/src/lib.rs
parent91f9bc2b866fc87dbaadb29a8c72eba10ae57c5c (diff)
Move cmd to a separate dir
Diffstat (limited to 'xtask/src/lib.rs')
-rw-r--r--xtask/src/lib.rs58
1 files changed, 5 insertions, 53 deletions
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
3mod cmd;
3pub mod codegen; 4pub mod codegen;
4pub mod install; 5pub mod install;
5pub mod pre_commit; 6pub mod pre_commit;
6mod ast_src; 7mod ast_src;
7 8
8use anyhow::Context; 9use anyhow::Context;
9pub use anyhow::Result;
10use std::{ 10use 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
16use crate::codegen::Mode; 16use crate::codegen::Mode;
17 17
18pub use anyhow::Result;
19pub use cmd::{run, run_with_output, Cmd};
20
18const TOOLCHAIN: &str = "stable"; 21const TOOLCHAIN: &str = "stable";
19 22
20pub fn project_root() -> PathBuf { 23pub 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
30pub struct Cmd<'a> {
31 pub unix: &'a str,
32 pub windows: &'a str,
33 pub work_dir: &'a str,
34}
35
36impl 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
53pub fn run(cmdline: &str, dir: &str) -> Result<()> {
54 do_run(cmdline, dir, |c| {
55 c.stdout(Stdio::inherit());
56 })
57 .map(|_| ())
58}
59
60pub fn run_with_output(cmdline: &str, dir: &str) -> Result<Output> {
61 do_run(cmdline, dir, |_| {})
62}
63
64pub fn run_rustfmt(mode: Mode) -> Result<()> { 33pub 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
136fn do_run<F>(cmdline: &str, dir: &str, mut f: F) -> Result<Output>
137where
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}