diff options
Diffstat (limited to 'xtask/src/lib.rs')
-rw-r--r-- | xtask/src/lib.rs | 332 |
1 files changed, 332 insertions, 0 deletions
diff --git a/xtask/src/lib.rs b/xtask/src/lib.rs new file mode 100644 index 000000000..a8685f567 --- /dev/null +++ b/xtask/src/lib.rs | |||
@@ -0,0 +1,332 @@ | |||
1 | //! FIXME: write short doc here | ||
2 | |||
3 | mod boilerplate_gen; | ||
4 | |||
5 | use std::{ | ||
6 | collections::HashMap, | ||
7 | error::Error, | ||
8 | fs, | ||
9 | io::{Error as IoError, ErrorKind}, | ||
10 | path::{Path, PathBuf}, | ||
11 | process::{Command, Output, Stdio}, | ||
12 | }; | ||
13 | |||
14 | use itertools::Itertools; | ||
15 | |||
16 | pub use self::boilerplate_gen::generate_boilerplate; | ||
17 | |||
18 | pub type Result<T> = std::result::Result<T, Box<dyn Error>>; | ||
19 | |||
20 | pub const GRAMMAR: &str = "crates/ra_syntax/src/grammar.ron"; | ||
21 | const GRAMMAR_DIR: &str = "crates/ra_parser/src/grammar"; | ||
22 | const OK_INLINE_TESTS_DIR: &str = "crates/ra_syntax/test_data/parser/inline/ok"; | ||
23 | const ERR_INLINE_TESTS_DIR: &str = "crates/ra_syntax/test_data/parser/inline/err"; | ||
24 | |||
25 | pub const SYNTAX_KINDS: &str = "crates/ra_parser/src/syntax_kind/generated.rs"; | ||
26 | pub const AST: &str = "crates/ra_syntax/src/ast/generated.rs"; | ||
27 | const TOOLCHAIN: &str = "stable"; | ||
28 | |||
29 | #[derive(Debug, PartialEq, Eq, Clone, Copy)] | ||
30 | pub enum Mode { | ||
31 | Overwrite, | ||
32 | Verify, | ||
33 | } | ||
34 | pub use Mode::*; | ||
35 | |||
36 | #[derive(Debug)] | ||
37 | pub struct Test { | ||
38 | pub name: String, | ||
39 | pub text: String, | ||
40 | pub ok: bool, | ||
41 | } | ||
42 | |||
43 | pub fn collect_tests(s: &str) -> Vec<(usize, Test)> { | ||
44 | let mut res = vec![]; | ||
45 | let prefix = "// "; | ||
46 | let comment_blocks = s | ||
47 | .lines() | ||
48 | .map(str::trim_start) | ||
49 | .enumerate() | ||
50 | .group_by(|(_idx, line)| line.starts_with(prefix)); | ||
51 | |||
52 | 'outer: for (is_comment, block) in comment_blocks.into_iter() { | ||
53 | if !is_comment { | ||
54 | continue; | ||
55 | } | ||
56 | let mut block = block.map(|(idx, line)| (idx, &line[prefix.len()..])); | ||
57 | |||
58 | let mut ok = true; | ||
59 | let (start_line, name) = loop { | ||
60 | match block.next() { | ||
61 | Some((idx, line)) if line.starts_with("test ") => { | ||
62 | break (idx, line["test ".len()..].to_string()); | ||
63 | } | ||
64 | Some((idx, line)) if line.starts_with("test_err ") => { | ||
65 | ok = false; | ||
66 | break (idx, line["test_err ".len()..].to_string()); | ||
67 | } | ||
68 | Some(_) => (), | ||
69 | None => continue 'outer, | ||
70 | } | ||
71 | }; | ||
72 | let text: String = | ||
73 | itertools::join(block.map(|(_, line)| line).chain(::std::iter::once("")), "\n"); | ||
74 | assert!(!text.trim().is_empty() && text.ends_with('\n')); | ||
75 | res.push((start_line, Test { name, text, ok })) | ||
76 | } | ||
77 | res | ||
78 | } | ||
79 | |||
80 | pub fn project_root() -> PathBuf { | ||
81 | Path::new(&env!("CARGO_MANIFEST_DIR")).ancestors().nth(1).unwrap().to_path_buf() | ||
82 | } | ||
83 | |||
84 | pub struct Cmd<'a> { | ||
85 | pub unix: &'a str, | ||
86 | pub windows: &'a str, | ||
87 | pub work_dir: &'a str, | ||
88 | } | ||
89 | |||
90 | impl Cmd<'_> { | ||
91 | pub fn run(self) -> Result<()> { | ||
92 | if cfg!(windows) { | ||
93 | run(self.windows, self.work_dir) | ||
94 | } else { | ||
95 | run(self.unix, self.work_dir) | ||
96 | } | ||
97 | } | ||
98 | pub fn run_with_output(self) -> Result<Output> { | ||
99 | if cfg!(windows) { | ||
100 | run_with_output(self.windows, self.work_dir) | ||
101 | } else { | ||
102 | run_with_output(self.unix, self.work_dir) | ||
103 | } | ||
104 | } | ||
105 | } | ||
106 | |||
107 | pub fn run(cmdline: &str, dir: &str) -> Result<()> { | ||
108 | do_run(cmdline, dir, |c| { | ||
109 | c.stdout(Stdio::inherit()); | ||
110 | }) | ||
111 | .map(|_| ()) | ||
112 | } | ||
113 | |||
114 | pub fn run_with_output(cmdline: &str, dir: &str) -> Result<Output> { | ||
115 | do_run(cmdline, dir, |_| {}) | ||
116 | } | ||
117 | |||
118 | pub fn run_rustfmt(mode: Mode) -> Result<()> { | ||
119 | match Command::new("rustup") | ||
120 | .args(&["run", TOOLCHAIN, "--", "cargo", "fmt", "--version"]) | ||
121 | .stderr(Stdio::null()) | ||
122 | .stdout(Stdio::null()) | ||
123 | .status() | ||
124 | { | ||
125 | Ok(status) if status.success() => (), | ||
126 | _ => install_rustfmt()?, | ||
127 | }; | ||
128 | |||
129 | if mode == Verify { | ||
130 | run(&format!("rustup run {} -- cargo fmt -- --check", TOOLCHAIN), ".")?; | ||
131 | } else { | ||
132 | run(&format!("rustup run {} -- cargo fmt", TOOLCHAIN), ".")?; | ||
133 | } | ||
134 | Ok(()) | ||
135 | } | ||
136 | |||
137 | pub fn install_rustfmt() -> Result<()> { | ||
138 | run(&format!("rustup install {}", TOOLCHAIN), ".")?; | ||
139 | run(&format!("rustup component add rustfmt --toolchain {}", TOOLCHAIN), ".") | ||
140 | } | ||
141 | |||
142 | pub fn install_format_hook() -> Result<()> { | ||
143 | let result_path = Path::new(if cfg!(windows) { | ||
144 | "./.git/hooks/pre-commit.exe" | ||
145 | } else { | ||
146 | "./.git/hooks/pre-commit" | ||
147 | }); | ||
148 | if !result_path.exists() { | ||
149 | run("cargo build --package xtask --bin pre-commit", ".")?; | ||
150 | if cfg!(windows) { | ||
151 | fs::copy("./target/debug/pre-commit.exe", result_path)?; | ||
152 | } else { | ||
153 | fs::copy("./target/debug/pre-commit", result_path)?; | ||
154 | } | ||
155 | } else { | ||
156 | Err(IoError::new(ErrorKind::AlreadyExists, "Git hook already created"))?; | ||
157 | } | ||
158 | Ok(()) | ||
159 | } | ||
160 | |||
161 | pub fn run_clippy() -> Result<()> { | ||
162 | match Command::new("rustup") | ||
163 | .args(&["run", TOOLCHAIN, "--", "cargo", "clippy", "--version"]) | ||
164 | .stderr(Stdio::null()) | ||
165 | .stdout(Stdio::null()) | ||
166 | .status() | ||
167 | { | ||
168 | Ok(status) if status.success() => (), | ||
169 | _ => install_clippy()?, | ||
170 | }; | ||
171 | |||
172 | let allowed_lints = [ | ||
173 | "clippy::collapsible_if", | ||
174 | "clippy::map_clone", // FIXME: remove when Iterator::copied stabilizes (1.36.0) | ||
175 | "clippy::needless_pass_by_value", | ||
176 | "clippy::nonminimal_bool", | ||
177 | "clippy::redundant_pattern_matching", | ||
178 | ]; | ||
179 | run( | ||
180 | &format!( | ||
181 | "rustup run {} -- cargo clippy --all-features --all-targets -- -A {}", | ||
182 | TOOLCHAIN, | ||
183 | allowed_lints.join(" -A ") | ||
184 | ), | ||
185 | ".", | ||
186 | )?; | ||
187 | Ok(()) | ||
188 | } | ||
189 | |||
190 | pub fn install_clippy() -> Result<()> { | ||
191 | run(&format!("rustup install {}", TOOLCHAIN), ".")?; | ||
192 | run(&format!("rustup component add clippy --toolchain {}", TOOLCHAIN), ".") | ||
193 | } | ||
194 | |||
195 | pub fn run_fuzzer() -> Result<()> { | ||
196 | match Command::new("cargo") | ||
197 | .args(&["fuzz", "--help"]) | ||
198 | .stderr(Stdio::null()) | ||
199 | .stdout(Stdio::null()) | ||
200 | .status() | ||
201 | { | ||
202 | Ok(status) if status.success() => (), | ||
203 | _ => run("cargo install cargo-fuzz", ".")?, | ||
204 | }; | ||
205 | |||
206 | run("rustup run nightly -- cargo fuzz run parser", "./crates/ra_syntax") | ||
207 | } | ||
208 | |||
209 | pub fn gen_tests(mode: Mode) -> Result<()> { | ||
210 | let tests = tests_from_dir(&project_root().join(Path::new(GRAMMAR_DIR)))?; | ||
211 | fn install_tests(tests: &HashMap<String, Test>, into: &str, mode: Mode) -> Result<()> { | ||
212 | let tests_dir = project_root().join(into); | ||
213 | if !tests_dir.is_dir() { | ||
214 | fs::create_dir_all(&tests_dir)?; | ||
215 | } | ||
216 | // ok is never actually read, but it needs to be specified to create a Test in existing_tests | ||
217 | let existing = existing_tests(&tests_dir, true)?; | ||
218 | for t in existing.keys().filter(|&t| !tests.contains_key(t)) { | ||
219 | panic!("Test is deleted: {}", t); | ||
220 | } | ||
221 | |||
222 | let mut new_idx = existing.len() + 1; | ||
223 | for (name, test) in tests { | ||
224 | let path = match existing.get(name) { | ||
225 | Some((path, _test)) => path.clone(), | ||
226 | None => { | ||
227 | let file_name = format!("{:04}_{}.rs", new_idx, name); | ||
228 | new_idx += 1; | ||
229 | tests_dir.join(file_name) | ||
230 | } | ||
231 | }; | ||
232 | update(&path, &test.text, mode)?; | ||
233 | } | ||
234 | Ok(()) | ||
235 | } | ||
236 | install_tests(&tests.ok, OK_INLINE_TESTS_DIR, mode)?; | ||
237 | install_tests(&tests.err, ERR_INLINE_TESTS_DIR, mode) | ||
238 | } | ||
239 | |||
240 | fn do_run<F>(cmdline: &str, dir: &str, mut f: F) -> Result<Output> | ||
241 | where | ||
242 | F: FnMut(&mut Command), | ||
243 | { | ||
244 | eprintln!("\nwill run: {}", cmdline); | ||
245 | let proj_dir = project_root().join(dir); | ||
246 | let mut args = cmdline.split_whitespace(); | ||
247 | let exec = args.next().unwrap(); | ||
248 | let mut cmd = Command::new(exec); | ||
249 | f(cmd.args(args).current_dir(proj_dir).stderr(Stdio::inherit())); | ||
250 | let output = cmd.output()?; | ||
251 | if !output.status.success() { | ||
252 | Err(format!("`{}` exited with {}", cmdline, output.status))?; | ||
253 | } | ||
254 | Ok(output) | ||
255 | } | ||
256 | |||
257 | #[derive(Default, Debug)] | ||
258 | struct Tests { | ||
259 | pub ok: HashMap<String, Test>, | ||
260 | pub err: HashMap<String, Test>, | ||
261 | } | ||
262 | |||
263 | fn tests_from_dir(dir: &Path) -> Result<Tests> { | ||
264 | let mut res = Tests::default(); | ||
265 | for entry in ::walkdir::WalkDir::new(dir) { | ||
266 | let entry = entry.unwrap(); | ||
267 | if !entry.file_type().is_file() { | ||
268 | continue; | ||
269 | } | ||
270 | if entry.path().extension().unwrap_or_default() != "rs" { | ||
271 | continue; | ||
272 | } | ||
273 | process_file(&mut res, entry.path())?; | ||
274 | } | ||
275 | let grammar_rs = dir.parent().unwrap().join("grammar.rs"); | ||
276 | process_file(&mut res, &grammar_rs)?; | ||
277 | return Ok(res); | ||
278 | fn process_file(res: &mut Tests, path: &Path) -> Result<()> { | ||
279 | let text = fs::read_to_string(path)?; | ||
280 | |||
281 | for (_, test) in collect_tests(&text) { | ||
282 | if test.ok { | ||
283 | if let Some(old_test) = res.ok.insert(test.name.clone(), test) { | ||
284 | Err(format!("Duplicate test: {}", old_test.name))? | ||
285 | } | ||
286 | } else { | ||
287 | if let Some(old_test) = res.err.insert(test.name.clone(), test) { | ||
288 | Err(format!("Duplicate test: {}", old_test.name))? | ||
289 | } | ||
290 | } | ||
291 | } | ||
292 | Ok(()) | ||
293 | } | ||
294 | } | ||
295 | |||
296 | fn existing_tests(dir: &Path, ok: bool) -> Result<HashMap<String, (PathBuf, Test)>> { | ||
297 | let mut res = HashMap::new(); | ||
298 | for file in fs::read_dir(dir)? { | ||
299 | let file = file?; | ||
300 | let path = file.path(); | ||
301 | if path.extension().unwrap_or_default() != "rs" { | ||
302 | continue; | ||
303 | } | ||
304 | let name = { | ||
305 | let file_name = path.file_name().unwrap().to_str().unwrap(); | ||
306 | file_name[5..file_name.len() - 3].to_string() | ||
307 | }; | ||
308 | let text = fs::read_to_string(&path)?; | ||
309 | let test = Test { name: name.clone(), text, ok }; | ||
310 | if let Some(old) = res.insert(name, (path, test)) { | ||
311 | println!("Duplicate test: {:?}", old); | ||
312 | } | ||
313 | } | ||
314 | Ok(res) | ||
315 | } | ||
316 | |||
317 | /// A helper to update file on disk if it has changed. | ||
318 | /// With verify = false, | ||
319 | pub fn update(path: &Path, contents: &str, mode: Mode) -> Result<()> { | ||
320 | match fs::read_to_string(path) { | ||
321 | Ok(ref old_contents) if old_contents == contents => { | ||
322 | return Ok(()); | ||
323 | } | ||
324 | _ => (), | ||
325 | } | ||
326 | if mode == Verify { | ||
327 | Err(format!("`{}` is not up-to-date", path.display()))?; | ||
328 | } | ||
329 | eprintln!("updating {}", path.display()); | ||
330 | fs::write(path, contents)?; | ||
331 | Ok(()) | ||
332 | } | ||