aboutsummaryrefslogtreecommitdiff
path: root/xtask/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'xtask/src/lib.rs')
-rw-r--r--xtask/src/lib.rs125
1 files changed, 41 insertions, 84 deletions
diff --git a/xtask/src/lib.rs b/xtask/src/lib.rs
index 51a868dee..e46c21db7 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
3mod cmd;
4pub mod install;
5pub mod pre_commit;
6
3pub mod codegen; 7pub mod codegen;
4mod ast_src; 8mod ast_src;
5 9
6use anyhow::Context; 10use anyhow::Context;
7pub use anyhow::Result;
8use std::{ 11use std::{
9 env, fs, 12 env, fs,
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
15use crate::codegen::Mode; 17use crate::{cmd::run, codegen::Mode};
18
19pub use anyhow::Result;
16 20
17const TOOLCHAIN: &str = "stable"; 21const 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
29pub struct Cmd<'a> {
30 pub unix: &'a str,
31 pub windows: &'a str,
32 pub work_dir: &'a str,
33}
34
35impl 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
52pub fn run(cmdline: &str, dir: &str) -> Result<()> {
53 do_run(cmdline, dir, |c| {
54 c.stdout(Stdio::inherit());
55 })
56 .map(|_| ())
57}
58
59pub fn run_with_output(cmdline: &str, dir: &str) -> Result<Output> {
60 do_run(cmdline, dir, |_| {})
61}
62
63pub fn run_rustfmt(mode: Mode) -> Result<()> { 33pub 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
82pub fn install_rustfmt() -> Result<()> { 52fn 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
87pub 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
99pub fn run_clippy() -> Result<()> { 57pub 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
128pub fn install_clippy() -> Result<()> { 86fn 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}
@@ -144,41 +102,40 @@ pub fn run_fuzzer() -> Result<()> {
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 104
147pub fn reformat_staged_files() -> Result<()> { 105/// Cleans the `./target` dir after the build such that only
148 run_rustfmt(Mode::Overwrite)?; 106/// dependencies are cached on CI.
149 let root = project_root(); 107pub fn run_pre_cache() -> Result<()> {
150 let output = Command::new("git") 108 let slow_tests_cookie = Path::new("./target/.slow_tests_cookie");
151 .arg("diff") 109 if !slow_tests_cookie.exists() {
152 .arg("--diff-filter=MAR") 110 panic!("slow tests were skipped on CI!")
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 } 111 }
163 for line in String::from_utf8(output.stdout)?.lines() { 112 rm_rf(slow_tests_cookie)?;
164 run(&format!("git update-index --add {}", root.join(line).to_string_lossy()), ".")?; 113
114 for entry in Path::new("./target/debug").read_dir()? {
115 let entry = entry?;
116 if entry.file_type().map(|it| it.is_file()).ok() == Some(true) {
117 // Can't delete yourself on windows :-(
118 if !entry.path().ends_with("xtask.exe") {
119 rm_rf(&entry.path())?
120 }
121 }
122 }
123
124 fs::remove_file("./target/.rustc_info.json")?;
125 let to_delete = ["ra_", "heavy_test"];
126 for &dir in ["./target/debug/deps", "target/debug/.fingerprint"].iter() {
127 for entry in Path::new(dir).read_dir()? {
128 let entry = entry?;
129 if to_delete.iter().any(|&it| entry.path().display().to_string().contains(it)) {
130 rm_rf(&entry.path())?
131 }
132 }
165 } 133 }
134
166 Ok(()) 135 Ok(())
167} 136}
168 137
169fn do_run<F>(cmdline: &str, dir: &str, mut f: F) -> Result<Output> 138fn rm_rf(path: &Path) -> Result<()> {
170where 139 if path.is_file() { fs::remove_file(path) } else { fs::remove_dir_all(path) }
171 F: FnMut(&mut Command), 140 .with_context(|| format!("failed to remove {:?}", path))
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} 141}