diff options
Diffstat (limited to 'xtask/src/lib.rs')
-rw-r--r-- | xtask/src/lib.rs | 103 |
1 files changed, 11 insertions, 92 deletions
diff --git a/xtask/src/lib.rs b/xtask/src/lib.rs index 51a868dee..b76278635 100644 --- a/xtask/src/lib.rs +++ b/xtask/src/lib.rs | |||
@@ -1,18 +1,22 @@ | |||
1 | //! FIXME: write short doc here | 1 | //! FIXME: write short doc here |
2 | 2 | ||
3 | mod cmd; | ||
4 | pub mod install; | ||
5 | pub mod pre_commit; | ||
6 | |||
3 | pub mod codegen; | 7 | pub mod codegen; |
4 | mod ast_src; | 8 | mod ast_src; |
5 | 9 | ||
6 | use anyhow::Context; | 10 | use anyhow::Context; |
7 | pub use anyhow::Result; | ||
8 | use std::{ | 11 | use std::{ |
9 | env, fs, | 12 | env, |
10 | io::{Error as IoError, ErrorKind}, | ||
11 | path::{Path, PathBuf}, | 13 | path::{Path, PathBuf}, |
12 | process::{Command, Output, Stdio}, | 14 | process::{Command, Stdio}, |
13 | }; | 15 | }; |
14 | 16 | ||
15 | use crate::codegen::Mode; | 17 | use crate::{cmd::run, codegen::Mode}; |
18 | |||
19 | pub use anyhow::Result; | ||
16 | 20 | ||
17 | const TOOLCHAIN: &str = "stable"; | 21 | const TOOLCHAIN: &str = "stable"; |
18 | 22 | ||
@@ -26,40 +30,6 @@ pub fn project_root() -> PathBuf { | |||
26 | .to_path_buf() | 30 | .to_path_buf() |
27 | } | 31 | } |
28 | 32 | ||
29 | pub struct Cmd<'a> { | ||
30 | pub unix: &'a str, | ||
31 | pub windows: &'a str, | ||
32 | pub work_dir: &'a str, | ||
33 | } | ||
34 | |||
35 | impl Cmd<'_> { | ||
36 | pub fn run(self) -> Result<()> { | ||
37 | if cfg!(windows) { | ||
38 | run(self.windows, self.work_dir) | ||
39 | } else { | ||
40 | run(self.unix, self.work_dir) | ||
41 | } | ||
42 | } | ||
43 | pub fn run_with_output(self) -> Result<Output> { | ||
44 | if cfg!(windows) { | ||
45 | run_with_output(self.windows, self.work_dir) | ||
46 | } else { | ||
47 | run_with_output(self.unix, self.work_dir) | ||
48 | } | ||
49 | } | ||
50 | } | ||
51 | |||
52 | pub fn run(cmdline: &str, dir: &str) -> Result<()> { | ||
53 | do_run(cmdline, dir, |c| { | ||
54 | c.stdout(Stdio::inherit()); | ||
55 | }) | ||
56 | .map(|_| ()) | ||
57 | } | ||
58 | |||
59 | pub fn run_with_output(cmdline: &str, dir: &str) -> Result<Output> { | ||
60 | do_run(cmdline, dir, |_| {}) | ||
61 | } | ||
62 | |||
63 | pub fn run_rustfmt(mode: Mode) -> Result<()> { | 33 | pub fn run_rustfmt(mode: Mode) -> Result<()> { |
64 | match Command::new("rustup") | 34 | match Command::new("rustup") |
65 | .args(&["run", TOOLCHAIN, "--", "cargo", "fmt", "--version"]) | 35 | .args(&["run", TOOLCHAIN, "--", "cargo", "fmt", "--version"]) |
@@ -79,23 +49,11 @@ pub fn run_rustfmt(mode: Mode) -> Result<()> { | |||
79 | Ok(()) | 49 | Ok(()) |
80 | } | 50 | } |
81 | 51 | ||
82 | pub fn install_rustfmt() -> Result<()> { | 52 | fn install_rustfmt() -> Result<()> { |
83 | run(&format!("rustup toolchain install {}", TOOLCHAIN), ".")?; | 53 | run(&format!("rustup toolchain install {}", TOOLCHAIN), ".")?; |
84 | run(&format!("rustup component add rustfmt --toolchain {}", TOOLCHAIN), ".") | 54 | run(&format!("rustup component add rustfmt --toolchain {}", TOOLCHAIN), ".") |
85 | } | 55 | } |
86 | 56 | ||
87 | pub fn install_pre_commit_hook() -> Result<()> { | ||
88 | let result_path = | ||
89 | PathBuf::from(format!("./.git/hooks/pre-commit{}", std::env::consts::EXE_SUFFIX)); | ||
90 | if !result_path.exists() { | ||
91 | let me = std::env::current_exe()?; | ||
92 | fs::copy(me, result_path)?; | ||
93 | } else { | ||
94 | Err(IoError::new(ErrorKind::AlreadyExists, "Git hook already created"))?; | ||
95 | } | ||
96 | Ok(()) | ||
97 | } | ||
98 | |||
99 | pub fn run_clippy() -> Result<()> { | 57 | pub fn run_clippy() -> Result<()> { |
100 | match Command::new("rustup") | 58 | match Command::new("rustup") |
101 | .args(&["run", TOOLCHAIN, "--", "cargo", "clippy", "--version"]) | 59 | .args(&["run", TOOLCHAIN, "--", "cargo", "clippy", "--version"]) |
@@ -125,7 +83,7 @@ pub fn run_clippy() -> Result<()> { | |||
125 | Ok(()) | 83 | Ok(()) |
126 | } | 84 | } |
127 | 85 | ||
128 | pub fn install_clippy() -> Result<()> { | 86 | fn install_clippy() -> Result<()> { |
129 | run(&format!("rustup toolchain install {}", TOOLCHAIN), ".")?; | 87 | run(&format!("rustup toolchain install {}", TOOLCHAIN), ".")?; |
130 | run(&format!("rustup component add clippy --toolchain {}", TOOLCHAIN), ".") | 88 | run(&format!("rustup component add clippy --toolchain {}", TOOLCHAIN), ".") |
131 | } | 89 | } |
@@ -143,42 +101,3 @@ pub fn run_fuzzer() -> Result<()> { | |||
143 | 101 | ||
144 | run("rustup run nightly -- cargo fuzz run parser", "./crates/ra_syntax") | 102 | run("rustup run nightly -- cargo fuzz run parser", "./crates/ra_syntax") |
145 | } | 103 | } |
146 | |||
147 | pub fn reformat_staged_files() -> Result<()> { | ||
148 | run_rustfmt(Mode::Overwrite)?; | ||
149 | let root = project_root(); | ||
150 | let output = Command::new("git") | ||
151 | .arg("diff") | ||
152 | .arg("--diff-filter=MAR") | ||
153 | .arg("--name-only") | ||
154 | .arg("--cached") | ||
155 | .current_dir(&root) | ||
156 | .output()?; | ||
157 | if !output.status.success() { | ||
158 | anyhow::bail!( | ||
159 | "`git diff --diff-filter=MAR --name-only --cached` exited with {}", | ||
160 | output.status | ||
161 | ); | ||
162 | } | ||
163 | for line in String::from_utf8(output.stdout)?.lines() { | ||
164 | run(&format!("git update-index --add {}", root.join(line).to_string_lossy()), ".")?; | ||
165 | } | ||
166 | Ok(()) | ||
167 | } | ||
168 | |||
169 | fn do_run<F>(cmdline: &str, dir: &str, mut f: F) -> Result<Output> | ||
170 | where | ||
171 | F: FnMut(&mut Command), | ||
172 | { | ||
173 | eprintln!("\nwill run: {}", cmdline); | ||
174 | let proj_dir = project_root().join(dir); | ||
175 | let mut args = cmdline.split_whitespace(); | ||
176 | let exec = args.next().unwrap(); | ||
177 | let mut cmd = Command::new(exec); | ||
178 | f(cmd.args(args).current_dir(proj_dir).stderr(Stdio::inherit())); | ||
179 | let output = cmd.output().with_context(|| format!("running `{}`", cmdline))?; | ||
180 | if !output.status.success() { | ||
181 | anyhow::bail!("`{}` exited with {}", cmdline, output.status); | ||
182 | } | ||
183 | Ok(output) | ||
184 | } | ||