diff options
Diffstat (limited to 'crates/ra_tools/src/lib.rs')
-rw-r--r-- | crates/ra_tools/src/lib.rs | 43 |
1 files changed, 30 insertions, 13 deletions
diff --git a/crates/ra_tools/src/lib.rs b/crates/ra_tools/src/lib.rs index bb7845f7d..d47660369 100644 --- a/crates/ra_tools/src/lib.rs +++ b/crates/ra_tools/src/lib.rs | |||
@@ -1,3 +1,5 @@ | |||
1 | mod boilerplate_gen; | ||
2 | |||
1 | use std::{ | 3 | use std::{ |
2 | collections::HashMap, | 4 | collections::HashMap, |
3 | error::Error, | 5 | error::Error, |
@@ -9,7 +11,7 @@ use std::{ | |||
9 | 11 | ||
10 | use itertools::Itertools; | 12 | use itertools::Itertools; |
11 | 13 | ||
12 | pub use teraron::{Mode, Overwrite, Verify}; | 14 | pub use self::boilerplate_gen::generate_boilerplate; |
13 | 15 | ||
14 | pub type Result<T> = std::result::Result<T, Box<dyn Error>>; | 16 | pub type Result<T> = std::result::Result<T, Box<dyn Error>>; |
15 | 17 | ||
@@ -18,10 +20,17 @@ const GRAMMAR_DIR: &str = "crates/ra_parser/src/grammar"; | |||
18 | const OK_INLINE_TESTS_DIR: &str = "crates/ra_syntax/test_data/parser/inline/ok"; | 20 | const OK_INLINE_TESTS_DIR: &str = "crates/ra_syntax/test_data/parser/inline/ok"; |
19 | const ERR_INLINE_TESTS_DIR: &str = "crates/ra_syntax/test_data/parser/inline/err"; | 21 | const ERR_INLINE_TESTS_DIR: &str = "crates/ra_syntax/test_data/parser/inline/err"; |
20 | 22 | ||
21 | pub const SYNTAX_KINDS: &str = "crates/ra_parser/src/syntax_kind/generated.rs.tera"; | 23 | pub const SYNTAX_KINDS: &str = "crates/ra_parser/src/syntax_kind/generated.rs"; |
22 | pub const AST: &str = "crates/ra_syntax/src/ast/generated.rs.tera"; | 24 | pub const AST: &str = "crates/ra_syntax/src/ast/generated.rs"; |
23 | const TOOLCHAIN: &str = "stable"; | 25 | const TOOLCHAIN: &str = "stable"; |
24 | 26 | ||
27 | #[derive(Debug, PartialEq, Eq, Clone, Copy)] | ||
28 | pub enum Mode { | ||
29 | Overwrite, | ||
30 | Verify, | ||
31 | } | ||
32 | pub use Mode::*; | ||
33 | |||
25 | #[derive(Debug)] | 34 | #[derive(Debug)] |
26 | pub struct Test { | 35 | pub struct Test { |
27 | pub name: String, | 36 | pub name: String, |
@@ -66,15 +75,6 @@ pub fn collect_tests(s: &str) -> Vec<(usize, Test)> { | |||
66 | res | 75 | res |
67 | } | 76 | } |
68 | 77 | ||
69 | pub fn generate(mode: Mode) -> Result<()> { | ||
70 | let grammar = project_root().join(GRAMMAR); | ||
71 | let syntax_kinds = project_root().join(SYNTAX_KINDS); | ||
72 | let ast = project_root().join(AST); | ||
73 | teraron::generate(&syntax_kinds, &grammar, mode)?; | ||
74 | teraron::generate(&ast, &grammar, mode)?; | ||
75 | Ok(()) | ||
76 | } | ||
77 | |||
78 | pub fn project_root() -> PathBuf { | 78 | pub fn project_root() -> PathBuf { |
79 | Path::new(&env!("CARGO_MANIFEST_DIR")).ancestors().nth(2).unwrap().to_path_buf() | 79 | Path::new(&env!("CARGO_MANIFEST_DIR")).ancestors().nth(2).unwrap().to_path_buf() |
80 | } | 80 | } |
@@ -227,7 +227,7 @@ pub fn gen_tests(mode: Mode) -> Result<()> { | |||
227 | tests_dir.join(file_name) | 227 | tests_dir.join(file_name) |
228 | } | 228 | } |
229 | }; | 229 | }; |
230 | teraron::update(&path, &test.text, mode)?; | 230 | update(&path, &test.text, mode)?; |
231 | } | 231 | } |
232 | Ok(()) | 232 | Ok(()) |
233 | } | 233 | } |
@@ -311,3 +311,20 @@ fn existing_tests(dir: &Path, ok: bool) -> Result<HashMap<String, (PathBuf, Test | |||
311 | } | 311 | } |
312 | Ok(res) | 312 | Ok(res) |
313 | } | 313 | } |
314 | |||
315 | /// A helper to update file on disk if it has changed. | ||
316 | /// With verify = false, | ||
317 | pub fn update(path: &Path, contents: &str, mode: Mode) -> Result<()> { | ||
318 | match fs::read_to_string(path) { | ||
319 | Ok(ref old_contents) if old_contents == contents => { | ||
320 | return Ok(()); | ||
321 | } | ||
322 | _ => (), | ||
323 | } | ||
324 | if mode == Verify { | ||
325 | Err(format!("`{}` is not up-to-date", path.display()))?; | ||
326 | } | ||
327 | eprintln!("updating {}", path.display()); | ||
328 | fs::write(path, contents)?; | ||
329 | Ok(()) | ||
330 | } | ||