aboutsummaryrefslogtreecommitdiff
path: root/crates/tools/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/tools/src')
-rw-r--r--crates/tools/src/bin/pre-commit.rs7
-rw-r--r--crates/tools/src/lib.rs106
-rw-r--r--crates/tools/src/main.rs127
3 files changed, 111 insertions, 129 deletions
diff --git a/crates/tools/src/bin/pre-commit.rs b/crates/tools/src/bin/pre-commit.rs
index bae3b26d3..e00bd0d3d 100644
--- a/crates/tools/src/bin/pre-commit.rs
+++ b/crates/tools/src/bin/pre-commit.rs
@@ -1,10 +1,9 @@
1use std::{ 1use std::process::Command;
2 process::{Command},
3};
4 2
5use tools::{Result, run_rustfmt, run, project_root};
6use failure::bail; 3use failure::bail;
7 4
5use tools::{Result, run_rustfmt, run, project_root};
6
8fn main() -> tools::Result<()> { 7fn main() -> tools::Result<()> {
9 run_rustfmt(tools::Overwrite)?; 8 run_rustfmt(tools::Overwrite)?;
10 update_staged() 9 update_staged()
diff --git a/crates/tools/src/lib.rs b/crates/tools/src/lib.rs
index d404db214..311bcb4d8 100644
--- a/crates/tools/src/lib.rs
+++ b/crates/tools/src/lib.rs
@@ -1,7 +1,8 @@
1use std::{ 1use std::{
2 fs,
3 collections::HashMap,
2 path::{Path, PathBuf}, 4 path::{Path, PathBuf},
3 process::{Command, Stdio}, 5 process::{Command, Stdio},
4 fs::copy,
5 io::{Error, ErrorKind} 6 io::{Error, ErrorKind}
6}; 7};
7 8
@@ -13,6 +14,10 @@ pub use teraron::{Mode, Overwrite, Verify};
13pub type Result<T> = std::result::Result<T, failure::Error>; 14pub type Result<T> = std::result::Result<T, failure::Error>;
14 15
15pub const GRAMMAR: &str = "crates/ra_syntax/src/grammar.ron"; 16pub const GRAMMAR: &str = "crates/ra_syntax/src/grammar.ron";
17const GRAMMAR_DIR: &str = "crates/ra_syntax/src/grammar";
18const OK_INLINE_TESTS_DIR: &str = "crates/ra_syntax/tests/data/parser/inline/ok";
19const ERR_INLINE_TESTS_DIR: &str = "crates/ra_syntax/tests/data/parser/inline/err";
20
16pub const SYNTAX_KINDS: &str = "crates/ra_syntax/src/syntax_kinds/generated.rs.tera"; 21pub const SYNTAX_KINDS: &str = "crates/ra_syntax/src/syntax_kinds/generated.rs.tera";
17pub const AST: &str = "crates/ra_syntax/src/ast/generated.rs.tera"; 22pub const AST: &str = "crates/ra_syntax/src/ast/generated.rs.tera";
18const TOOLCHAIN: &str = "stable"; 23const TOOLCHAIN: &str = "stable";
@@ -130,9 +135,9 @@ pub fn install_format_hook() -> Result<()> {
130 if !result_path.exists() { 135 if !result_path.exists() {
131 run("cargo build --package tools --bin pre-commit", ".")?; 136 run("cargo build --package tools --bin pre-commit", ".")?;
132 if cfg!(windows) { 137 if cfg!(windows) {
133 copy("./target/debug/pre-commit.exe", result_path)?; 138 fs::copy("./target/debug/pre-commit.exe", result_path)?;
134 } else { 139 } else {
135 copy("./target/debug/pre-commit", result_path)?; 140 fs::copy("./target/debug/pre-commit", result_path)?;
136 } 141 }
137 } else { 142 } else {
138 return Err(Error::new(ErrorKind::AlreadyExists, "Git hook already created").into()); 143 return Err(Error::new(ErrorKind::AlreadyExists, "Git hook already created").into());
@@ -156,3 +161,98 @@ pub fn run_fuzzer() -> Result<()> {
156 "./crates/ra_syntax", 161 "./crates/ra_syntax",
157 ) 162 )
158} 163}
164
165pub fn gen_tests(mode: Mode) -> Result<()> {
166 let tests = tests_from_dir(&project_root().join(Path::new(GRAMMAR_DIR)))?;
167 fn install_tests(tests: &HashMap<String, Test>, into: &str, mode: Mode) -> Result<()> {
168 let tests_dir = project_root().join(into);
169 if !tests_dir.is_dir() {
170 fs::create_dir_all(&tests_dir)?;
171 }
172 // ok is never actually read, but it needs to be specified to create a Test in existing_tests
173 let existing = existing_tests(&tests_dir, true)?;
174 for t in existing.keys().filter(|&t| !tests.contains_key(t)) {
175 panic!("Test is deleted: {}", t);
176 }
177
178 let mut new_idx = existing.len() + 1;
179 for (name, test) in tests {
180 let path = match existing.get(name) {
181 Some((path, _test)) => path.clone(),
182 None => {
183 let file_name = format!("{:04}_{}.rs", new_idx, name);
184 new_idx += 1;
185 tests_dir.join(file_name)
186 }
187 };
188 teraron::update(&path, &test.text, mode)?;
189 }
190 Ok(())
191 }
192 install_tests(&tests.ok, OK_INLINE_TESTS_DIR, mode)?;
193 install_tests(&tests.err, ERR_INLINE_TESTS_DIR, mode)
194}
195
196#[derive(Default, Debug)]
197struct Tests {
198 pub ok: HashMap<String, Test>,
199 pub err: HashMap<String, Test>,
200}
201
202fn tests_from_dir(dir: &Path) -> Result<Tests> {
203 let mut res = Tests::default();
204 for entry in ::walkdir::WalkDir::new(dir) {
205 let entry = entry.unwrap();
206 if !entry.file_type().is_file() {
207 continue;
208 }
209 if entry.path().extension().unwrap_or_default() != "rs" {
210 continue;
211 }
212 process_file(&mut res, entry.path())?;
213 }
214 let grammar_rs = dir.parent().unwrap().join("grammar.rs");
215 process_file(&mut res, &grammar_rs)?;
216 return Ok(res);
217 fn process_file(res: &mut Tests, path: &Path) -> Result<()> {
218 let text = fs::read_to_string(path)?;
219
220 for (_, test) in collect_tests(&text) {
221 if test.ok {
222 if let Some(old_test) = res.ok.insert(test.name.clone(), test) {
223 bail!("Duplicate test: {}", old_test.name)
224 }
225 } else {
226 if let Some(old_test) = res.err.insert(test.name.clone(), test) {
227 bail!("Duplicate test: {}", old_test.name)
228 }
229 }
230 }
231 Ok(())
232 }
233}
234
235fn existing_tests(dir: &Path, ok: bool) -> Result<HashMap<String, (PathBuf, Test)>> {
236 let mut res = HashMap::new();
237 for file in fs::read_dir(dir)? {
238 let file = file?;
239 let path = file.path();
240 if path.extension().unwrap_or_default() != "rs" {
241 continue;
242 }
243 let name = {
244 let file_name = path.file_name().unwrap().to_str().unwrap();
245 file_name[5..file_name.len() - 3].to_string()
246 };
247 let text = fs::read_to_string(&path)?;
248 let test = Test {
249 name: name.clone(),
250 text,
251 ok,
252 };
253 if let Some(old) = res.insert(name, (path, test)) {
254 println!("Duplicate test: {:?}", old);
255 }
256 }
257 Ok(res)
258}
diff --git a/crates/tools/src/main.rs b/crates/tools/src/main.rs
index d6eabce6c..c3e293911 100644
--- a/crates/tools/src/main.rs
+++ b/crates/tools/src/main.rs
@@ -1,30 +1,13 @@
1use std::{ 1use clap::{App, SubCommand};
2 collections::HashMap,
3 fs,
4 path::{Path, PathBuf},
5};
6
7use clap::{App, Arg, SubCommand};
8use failure::bail;
9 2
10use tools::{ 3use tools::{
11 collect_tests, generate,install_format_hook, run, run_rustfmt, 4 generate, gen_tests, install_format_hook, run, run_rustfmt,
12 Mode, Overwrite, Result, Test, Verify, project_root, run_fuzzer 5 Overwrite, Result, run_fuzzer,
13}; 6};
14 7
15const GRAMMAR_DIR: &str = "crates/ra_syntax/src/grammar";
16const OK_INLINE_TESTS_DIR: &str = "crates/ra_syntax/tests/data/parser/inline/ok";
17const ERR_INLINE_TESTS_DIR: &str = "crates/ra_syntax/tests/data/parser/inline/err";
18
19fn main() -> Result<()> { 8fn main() -> Result<()> {
20 let matches = App::new("tasks") 9 let matches = App::new("tasks")
21 .setting(clap::AppSettings::SubcommandRequiredElseHelp) 10 .setting(clap::AppSettings::SubcommandRequiredElseHelp)
22 .arg(
23 Arg::with_name("verify")
24 .long("--verify")
25 .help("Verify that generated code is up-to-date")
26 .global(true),
27 )
28 .subcommand(SubCommand::with_name("gen-syntax")) 11 .subcommand(SubCommand::with_name("gen-syntax"))
29 .subcommand(SubCommand::with_name("gen-tests")) 12 .subcommand(SubCommand::with_name("gen-tests"))
30 .subcommand(SubCommand::with_name("install-code")) 13 .subcommand(SubCommand::with_name("install-code"))
@@ -32,19 +15,14 @@ fn main() -> Result<()> {
32 .subcommand(SubCommand::with_name("format-hook")) 15 .subcommand(SubCommand::with_name("format-hook"))
33 .subcommand(SubCommand::with_name("fuzz-tests")) 16 .subcommand(SubCommand::with_name("fuzz-tests"))
34 .get_matches(); 17 .get_matches();
35 let mode = if matches.is_present("verify") {
36 Verify
37 } else {
38 Overwrite
39 };
40 match matches 18 match matches
41 .subcommand_name() 19 .subcommand_name()
42 .expect("Subcommand must be specified") 20 .expect("Subcommand must be specified")
43 { 21 {
44 "install-code" => install_code_extension()?, 22 "install-code" => install_code_extension()?,
45 "gen-tests" => gen_tests(mode)?, 23 "gen-tests" => gen_tests(Overwrite)?,
46 "gen-syntax" => generate(Overwrite)?, 24 "gen-syntax" => generate(Overwrite)?,
47 "format" => run_rustfmt(mode)?, 25 "format" => run_rustfmt(Overwrite)?,
48 "format-hook" => install_format_hook()?, 26 "format-hook" => install_format_hook()?,
49 "fuzz-tests" => run_fuzzer()?, 27 "fuzz-tests" => run_fuzzer()?,
50 _ => unreachable!(), 28 _ => unreachable!(),
@@ -52,101 +30,6 @@ fn main() -> Result<()> {
52 Ok(()) 30 Ok(())
53} 31}
54 32
55fn gen_tests(mode: Mode) -> Result<()> {
56 let tests = tests_from_dir(Path::new(GRAMMAR_DIR))?;
57 fn install_tests(tests: &HashMap<String, Test>, into: &str, mode: Mode) -> Result<()> {
58 let tests_dir = project_root().join(into);
59 if !tests_dir.is_dir() {
60 fs::create_dir_all(&tests_dir)?;
61 }
62 // ok is never actually read, but it needs to be specified to create a Test in existing_tests
63 let existing = existing_tests(&tests_dir, true)?;
64 for t in existing.keys().filter(|&t| !tests.contains_key(t)) {
65 panic!("Test is deleted: {}", t);
66 }
67
68 let mut new_idx = existing.len() + 1;
69 for (name, test) in tests {
70 let path = match existing.get(name) {
71 Some((path, _test)) => path.clone(),
72 None => {
73 let file_name = format!("{:04}_{}.rs", new_idx, name);
74 new_idx += 1;
75 tests_dir.join(file_name)
76 }
77 };
78 teraron::update(&path, &test.text, mode)?;
79 }
80 Ok(())
81 }
82 install_tests(&tests.ok, OK_INLINE_TESTS_DIR, mode)?;
83 install_tests(&tests.err, ERR_INLINE_TESTS_DIR, mode)
84}
85
86#[derive(Default, Debug)]
87struct Tests {
88 pub ok: HashMap<String, Test>,
89 pub err: HashMap<String, Test>,
90}
91
92fn tests_from_dir(dir: &Path) -> Result<Tests> {
93 let mut res = Tests::default();
94 for entry in ::walkdir::WalkDir::new(dir) {
95 let entry = entry.unwrap();
96 if !entry.file_type().is_file() {
97 continue;
98 }
99 if entry.path().extension().unwrap_or_default() != "rs" {
100 continue;
101 }
102 process_file(&mut res, entry.path())?;
103 }
104 let grammar_rs = dir.parent().unwrap().join("grammar.rs");
105 process_file(&mut res, &grammar_rs)?;
106 return Ok(res);
107 fn process_file(res: &mut Tests, path: &Path) -> Result<()> {
108 let text = fs::read_to_string(path)?;
109
110 for (_, test) in collect_tests(&text) {
111 if test.ok {
112 if let Some(old_test) = res.ok.insert(test.name.clone(), test) {
113 bail!("Duplicate test: {}", old_test.name)
114 }
115 } else {
116 if let Some(old_test) = res.err.insert(test.name.clone(), test) {
117 bail!("Duplicate test: {}", old_test.name)
118 }
119 }
120 }
121 Ok(())
122 }
123}
124
125fn existing_tests(dir: &Path, ok: bool) -> Result<HashMap<String, (PathBuf, Test)>> {
126 let mut res = HashMap::new();
127 for file in fs::read_dir(dir)? {
128 let file = file?;
129 let path = file.path();
130 if path.extension().unwrap_or_default() != "rs" {
131 continue;
132 }
133 let name = {
134 let file_name = path.file_name().unwrap().to_str().unwrap();
135 file_name[5..file_name.len() - 3].to_string()
136 };
137 let text = fs::read_to_string(&path)?;
138 let test = Test {
139 name: name.clone(),
140 text,
141 ok,
142 };
143 if let Some(old) = res.insert(name, (path, test)) {
144 println!("Duplicate test: {:?}", old);
145 }
146 }
147 Ok(res)
148}
149
150fn install_code_extension() -> Result<()> { 33fn install_code_extension() -> Result<()> {
151 run("cargo install --path crates/ra_lsp_server --force", ".")?; 34 run("cargo install --path crates/ra_lsp_server --force", ".")?;
152 if cfg!(windows) { 35 if cfg!(windows) {