diff options
-rw-r--r-- | Cargo.lock | 24 | ||||
-rw-r--r-- | crates/tools/Cargo.toml | 4 | ||||
-rw-r--r-- | crates/tools/src/lib.rs | 85 | ||||
-rw-r--r-- | crates/tools/src/main.rs | 36 | ||||
-rw-r--r-- | crates/tools/tests/cli.rs | 15 |
5 files changed, 53 insertions, 111 deletions
diff --git a/Cargo.lock b/Cargo.lock index 707257a31..79da84ebc 100644 --- a/Cargo.lock +++ b/Cargo.lock | |||
@@ -794,7 +794,7 @@ dependencies = [ | |||
794 | 794 | ||
795 | [[package]] | 795 | [[package]] |
796 | name = "ron" | 796 | name = "ron" |
797 | version = "0.3.0" | 797 | version = "0.4.0" |
798 | source = "registry+https://github.com/rust-lang/crates.io-index" | 798 | source = "registry+https://github.com/rust-lang/crates.io-index" |
799 | dependencies = [ | 799 | dependencies = [ |
800 | "base64 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)", | 800 | "base64 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)", |
@@ -1003,7 +1003,7 @@ dependencies = [ | |||
1003 | 1003 | ||
1004 | [[package]] | 1004 | [[package]] |
1005 | name = "tera" | 1005 | name = "tera" |
1006 | version = "0.11.17" | 1006 | version = "0.11.18" |
1007 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1007 | source = "registry+https://github.com/rust-lang/crates.io-index" |
1008 | dependencies = [ | 1008 | dependencies = [ |
1009 | "chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", | 1009 | "chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", |
@@ -1022,6 +1022,17 @@ dependencies = [ | |||
1022 | ] | 1022 | ] |
1023 | 1023 | ||
1024 | [[package]] | 1024 | [[package]] |
1025 | name = "teraron" | ||
1026 | version = "0.0.1" | ||
1027 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
1028 | dependencies = [ | ||
1029 | "failure 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", | ||
1030 | "heck 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", | ||
1031 | "ron 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", | ||
1032 | "tera 0.11.18 (registry+https://github.com/rust-lang/crates.io-index)", | ||
1033 | ] | ||
1034 | |||
1035 | [[package]] | ||
1025 | name = "termion" | 1036 | name = "termion" |
1026 | version = "1.5.1" | 1037 | version = "1.5.1" |
1027 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1038 | source = "registry+https://github.com/rust-lang/crates.io-index" |
@@ -1080,10 +1091,8 @@ version = "0.1.0" | |||
1080 | dependencies = [ | 1091 | dependencies = [ |
1081 | "clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)", | 1092 | "clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)", |
1082 | "failure 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", | 1093 | "failure 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", |
1083 | "heck 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", | ||
1084 | "itertools 0.7.8 (registry+https://github.com/rust-lang/crates.io-index)", | 1094 | "itertools 0.7.8 (registry+https://github.com/rust-lang/crates.io-index)", |
1085 | "ron 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", | 1095 | "teraron 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)", |
1086 | "tera 0.11.17 (registry+https://github.com/rust-lang/crates.io-index)", | ||
1087 | "walkdir 2.2.5 (registry+https://github.com/rust-lang/crates.io-index)", | 1096 | "walkdir 2.2.5 (registry+https://github.com/rust-lang/crates.io-index)", |
1088 | ] | 1097 | ] |
1089 | 1098 | ||
@@ -1345,7 +1354,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" | |||
1345 | "checksum regex-syntax 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "747ba3b235651f6e2f67dfa8bcdcd073ddb7c243cb21c442fc12395dfcac212d" | 1354 | "checksum regex-syntax 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "747ba3b235651f6e2f67dfa8bcdcd073ddb7c243cb21c442fc12395dfcac212d" |
1346 | "checksum relative-path 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)" = "e614f96449605730b4f7ad2c019e88c1652d730634b4eba07b810801856635e3" | 1355 | "checksum relative-path 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)" = "e614f96449605730b4f7ad2c019e88c1652d730634b4eba07b810801856635e3" |
1347 | "checksum remove_dir_all 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3488ba1b9a2084d38645c4c08276a1752dcbf2c7130d74f1569681ad5d2799c5" | 1356 | "checksum remove_dir_all 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3488ba1b9a2084d38645c4c08276a1752dcbf2c7130d74f1569681ad5d2799c5" |
1348 | "checksum ron 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a9fa11b7a38511d46ff1959ae46ebb60bd8a746f17bdd0206b4c8de7559ac47b" | 1357 | "checksum ron 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c48677d8a9247a4e0d1f3f9cb4b0a8e29167fdc3c04f383a5e669cd7a960ae0f" |
1349 | "checksum rowan 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4a1a7366ece9deee5e7df8316a136d585d5c5042854c2297f7f1aee3014c9130" | 1358 | "checksum rowan 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4a1a7366ece9deee5e7df8316a136d585d5c5042854c2297f7f1aee3014c9130" |
1350 | "checksum rustc-demangle 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "bcfe5b13211b4d78e5c2cadfebd7769197d95c639c35a50057eb4c05de811395" | 1359 | "checksum rustc-demangle 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "bcfe5b13211b4d78e5c2cadfebd7769197d95c639c35a50057eb4c05de811395" |
1351 | "checksum rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7540fc8b0c49f096ee9c961cda096467dce8084bec6bdca2fc83895fd9b28cb8" | 1360 | "checksum rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7540fc8b0c49f096ee9c961cda096467dce8084bec6bdca2fc83895fd9b28cb8" |
@@ -1371,7 +1380,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" | |||
1371 | "checksum syn 0.15.11 (registry+https://github.com/rust-lang/crates.io-index)" = "b036b7b35e846707c0e55c2c9441fa47867c0f87fca416921db3261b1d8c741a" | 1380 | "checksum syn 0.15.11 (registry+https://github.com/rust-lang/crates.io-index)" = "b036b7b35e846707c0e55c2c9441fa47867c0f87fca416921db3261b1d8c741a" |
1372 | "checksum synstructure 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "85bb9b7550d063ea184027c9b8c20ac167cd36d3e06b3a40bceb9d746dc1a7b7" | 1381 | "checksum synstructure 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "85bb9b7550d063ea184027c9b8c20ac167cd36d3e06b3a40bceb9d746dc1a7b7" |
1373 | "checksum tempdir 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)" = "15f2b5fb00ccdf689e0149d1b1b3c03fead81c2b37735d812fa8bddbbf41b6d8" | 1382 | "checksum tempdir 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)" = "15f2b5fb00ccdf689e0149d1b1b3c03fead81c2b37735d812fa8bddbbf41b6d8" |
1374 | "checksum tera 0.11.17 (registry+https://github.com/rust-lang/crates.io-index)" = "2829d259c4699fbbe8acb353d231e6da31ff4301c52244413ed29ff6093da412" | 1383 | "checksum tera 0.11.18 (registry+https://github.com/rust-lang/crates.io-index)" = "6c87cae42cc4fc480278c7583792cc5da2d51a25be916b7921cbb45c43063b8d" |
1384 | "checksum teraron 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0d89ad4617d1dec55331067fadaa041e813479e1779616f3d3ce9308bf46184e" | ||
1375 | "checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096" | 1385 | "checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096" |
1376 | "checksum text_unit 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "93fc86da66d0b9aa8d359b0ec31b4342c6bc52637eadef05b91b098551a9f8e9" | 1386 | "checksum text_unit 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "93fc86da66d0b9aa8d359b0ec31b4342c6bc52637eadef05b91b098551a9f8e9" |
1377 | "checksum textwrap 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "307686869c93e71f94da64286f9a9524c0f308a9e1c87a583de8e9c9039ad3f6" | 1387 | "checksum textwrap 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "307686869c93e71f94da64286f9a9524c0f308a9e1c87a583de8e9c9039ad3f6" |
diff --git a/crates/tools/Cargo.toml b/crates/tools/Cargo.toml index e2fecc60d..41b19c5c6 100644 --- a/crates/tools/Cargo.toml +++ b/crates/tools/Cargo.toml | |||
@@ -6,10 +6,8 @@ authors = ["Aleksey Kladov <[email protected]>"] | |||
6 | publish = false | 6 | publish = false |
7 | 7 | ||
8 | [dependencies] | 8 | [dependencies] |
9 | ron = "0.3.0" | 9 | teraron = "0.0.1" |
10 | walkdir = "2.1.3" | 10 | walkdir = "2.1.3" |
11 | itertools = "0.7.8" | 11 | itertools = "0.7.8" |
12 | tera = "0.11" | ||
13 | clap = "2.32.0" | 12 | clap = "2.32.0" |
14 | failure = "0.1.1" | 13 | failure = "0.1.1" |
15 | heck = "0.3.0" | ||
diff --git a/crates/tools/src/lib.rs b/crates/tools/src/lib.rs index 5d5d372bb..444745be5 100644 --- a/crates/tools/src/lib.rs +++ b/crates/tools/src/lib.rs | |||
@@ -1,25 +1,20 @@ | |||
1 | extern crate itertools; | 1 | extern crate itertools; |
2 | #[macro_use] | ||
3 | extern crate failure; | 2 | extern crate failure; |
4 | extern crate heck; | 3 | extern crate teraron; |
5 | extern crate ron; | ||
6 | extern crate tera; | ||
7 | 4 | ||
8 | use heck::{CamelCase, ShoutySnakeCase, SnakeCase}; | ||
9 | use itertools::Itertools; | ||
10 | use std::{ | 5 | use std::{ |
11 | collections::HashMap, | ||
12 | fs, | ||
13 | path::{Path, PathBuf}, | 6 | path::{Path, PathBuf}, |
14 | }; | 7 | }; |
15 | 8 | ||
9 | use itertools::Itertools; | ||
10 | |||
11 | pub use teraron::{Mode, Verify, Overwrite}; | ||
12 | |||
16 | pub type Result<T> = ::std::result::Result<T, failure::Error>; | 13 | pub type Result<T> = ::std::result::Result<T, failure::Error>; |
17 | 14 | ||
18 | const GRAMMAR: &str = "ra_syntax/src/grammar.ron"; | 15 | pub const GRAMMAR: &str = "ra_syntax/src/grammar.ron"; |
19 | pub const SYNTAX_KINDS: &str = "ra_syntax/src/syntax_kinds/generated.rs"; | 16 | pub const SYNTAX_KINDS: &str = "ra_syntax/src/syntax_kinds/generated.rs.tera"; |
20 | pub const SYNTAX_KINDS_TEMPLATE: &str = "ra_syntax/src/syntax_kinds/generated.rs.tera"; | 17 | pub const AST: &str = "ra_syntax/src/ast/generated.rs.tera"; |
21 | pub const AST: &str = "ra_syntax/src/ast/generated.rs"; | ||
22 | pub const AST_TEMPLATE: &str = "ra_syntax/src/ast/generated.rs.tera"; | ||
23 | 18 | ||
24 | #[derive(Debug)] | 19 | #[derive(Debug)] |
25 | pub struct Test { | 20 | pub struct Test { |
@@ -61,59 +56,23 @@ pub fn collect_tests(s: &str) -> Vec<(usize, Test)> { | |||
61 | res | 56 | res |
62 | } | 57 | } |
63 | 58 | ||
64 | pub fn update(path: &Path, contents: &str, verify: bool) -> Result<()> { | 59 | pub fn generate(mode: Mode) -> Result<()> { |
65 | match fs::read_to_string(path) { | 60 | let grammar = project_root().join(GRAMMAR); |
66 | Ok(ref old_contents) if old_contents == contents => { | 61 | let syntax_kinds = project_root().join(SYNTAX_KINDS); |
67 | return Ok(()); | 62 | let ast = project_root().join(AST); |
68 | } | 63 | teraron::generate( |
69 | _ => (), | 64 | &syntax_kinds, |
70 | } | 65 | &grammar, |
71 | if verify { | 66 | mode, |
72 | bail!("`{}` is not up-to-date", path.display()); | 67 | )?; |
73 | } | 68 | teraron::generate( |
74 | eprintln!("updating {}", path.display()); | 69 | &ast, |
75 | fs::write(path, contents)?; | 70 | &grammar, |
71 | mode, | ||
72 | )?; | ||
76 | Ok(()) | 73 | Ok(()) |
77 | } | 74 | } |
78 | 75 | ||
79 | pub fn render_template(template: &Path) -> Result<String> { | ||
80 | let grammar: ron::value::Value = { | ||
81 | let text = fs::read_to_string(project_root().join(GRAMMAR))?; | ||
82 | ron::de::from_str(&text)? | ||
83 | }; | ||
84 | let template = fs::read_to_string(template)?; | ||
85 | let mut tera = tera::Tera::default(); | ||
86 | tera.add_raw_template("grammar", &template) | ||
87 | .map_err(|e| format_err!("template error: {:?}", e))?; | ||
88 | tera.register_function("concat", Box::new(concat)); | ||
89 | tera.register_filter("camel", |arg, _| { | ||
90 | Ok(arg.as_str().unwrap().to_camel_case().into()) | ||
91 | }); | ||
92 | tera.register_filter("snake", |arg, _| { | ||
93 | Ok(arg.as_str().unwrap().to_snake_case().into()) | ||
94 | }); | ||
95 | tera.register_filter("SCREAM", |arg, _| { | ||
96 | Ok(arg.as_str().unwrap().to_shouty_snake_case().into()) | ||
97 | }); | ||
98 | let ret = tera | ||
99 | .render("grammar", &grammar) | ||
100 | .map_err(|e| format_err!("template error: {:?}", e))?; | ||
101 | return Ok(ret); | ||
102 | |||
103 | fn concat(args: HashMap<String, tera::Value>) -> tera::Result<tera::Value> { | ||
104 | let mut elements = Vec::new(); | ||
105 | for &key in ["a", "b", "c"].iter() { | ||
106 | let val = match args.get(key) { | ||
107 | Some(val) => val, | ||
108 | None => continue, | ||
109 | }; | ||
110 | let val = val.as_array().unwrap(); | ||
111 | elements.extend(val.iter().cloned()); | ||
112 | } | ||
113 | Ok(tera::Value::Array(elements)) | ||
114 | } | ||
115 | } | ||
116 | |||
117 | pub fn project_root() -> PathBuf { | 76 | pub fn project_root() -> PathBuf { |
118 | Path::new(&std::env::var("CARGO_MANIFEST_DIR").unwrap()) | 77 | Path::new(&std::env::var("CARGO_MANIFEST_DIR").unwrap()) |
119 | .parent() | 78 | .parent() |
diff --git a/crates/tools/src/main.rs b/crates/tools/src/main.rs index b662d78df..965bc7729 100644 --- a/crates/tools/src/main.rs +++ b/crates/tools/src/main.rs | |||
@@ -3,6 +3,7 @@ extern crate clap; | |||
3 | extern crate failure; | 3 | extern crate failure; |
4 | extern crate tools; | 4 | extern crate tools; |
5 | extern crate walkdir; | 5 | extern crate walkdir; |
6 | extern crate teraron; | ||
6 | 7 | ||
7 | use clap::{App, Arg, SubCommand}; | 8 | use clap::{App, Arg, SubCommand}; |
8 | use std::{ | 9 | use std::{ |
@@ -12,8 +13,7 @@ use std::{ | |||
12 | process::Command, | 13 | process::Command, |
13 | }; | 14 | }; |
14 | use tools::{ | 15 | use tools::{ |
15 | collect_tests, project_root, render_template, update, Result, Test, AST, AST_TEMPLATE, | 16 | collect_tests, Result, Test, generate, Mode, Overwrite, Verify, |
16 | SYNTAX_KINDS, SYNTAX_KINDS_TEMPLATE, | ||
17 | }; | 17 | }; |
18 | 18 | ||
19 | const GRAMMAR_DIR: &str = "./crates/ra_syntax/src/grammar"; | 19 | const GRAMMAR_DIR: &str = "./crates/ra_syntax/src/grammar"; |
@@ -32,35 +32,21 @@ fn main() -> Result<()> { | |||
32 | .subcommand(SubCommand::with_name("gen-tests")) | 32 | .subcommand(SubCommand::with_name("gen-tests")) |
33 | .subcommand(SubCommand::with_name("install-code")) | 33 | .subcommand(SubCommand::with_name("install-code")) |
34 | .get_matches(); | 34 | .get_matches(); |
35 | let mode = if matches.is_present("verify") { | ||
36 | Verify | ||
37 | } else { | ||
38 | Overwrite | ||
39 | }; | ||
35 | match matches.subcommand() { | 40 | match matches.subcommand() { |
36 | ("install-code", _) => install_code_extension()?, | 41 | ("install-code", _) => install_code_extension()?, |
37 | (name, Some(matches)) => run_gen_command(name, matches.is_present("verify"))?, | 42 | ("gen-tests", _) => gen_tests(mode)?, |
38 | _ => unreachable!(), | 43 | ("gen-kinds", _) => generate(Overwrite)?, |
39 | } | ||
40 | Ok(()) | ||
41 | } | ||
42 | |||
43 | fn run_gen_command(name: &str, verify: bool) -> Result<()> { | ||
44 | match name { | ||
45 | "gen-kinds" => { | ||
46 | update( | ||
47 | &project_root().join(SYNTAX_KINDS), | ||
48 | &render_template(&project_root().join(SYNTAX_KINDS_TEMPLATE))?, | ||
49 | verify, | ||
50 | )?; | ||
51 | update( | ||
52 | &project_root().join(AST), | ||
53 | &render_template(&project_root().join(AST_TEMPLATE))?, | ||
54 | verify, | ||
55 | )?; | ||
56 | } | ||
57 | "gen-tests" => gen_tests(verify)?, | ||
58 | _ => unreachable!(), | 44 | _ => unreachable!(), |
59 | } | 45 | } |
60 | Ok(()) | 46 | Ok(()) |
61 | } | 47 | } |
62 | 48 | ||
63 | fn gen_tests(verify: bool) -> Result<()> { | 49 | fn gen_tests(mode: Mode) -> Result<()> { |
64 | let tests = tests_from_dir(Path::new(GRAMMAR_DIR))?; | 50 | let tests = tests_from_dir(Path::new(GRAMMAR_DIR))?; |
65 | 51 | ||
66 | let inline_tests_dir = Path::new(INLINE_TESTS_DIR); | 52 | let inline_tests_dir = Path::new(INLINE_TESTS_DIR); |
@@ -83,7 +69,7 @@ fn gen_tests(verify: bool) -> Result<()> { | |||
83 | inline_tests_dir.join(file_name) | 69 | inline_tests_dir.join(file_name) |
84 | } | 70 | } |
85 | }; | 71 | }; |
86 | update(&path, &test.text, verify)?; | 72 | teraron::update(&path, &test.text, mode)?; |
87 | } | 73 | } |
88 | Ok(()) | 74 | Ok(()) |
89 | } | 75 | } |
diff --git a/crates/tools/tests/cli.rs b/crates/tools/tests/cli.rs index 16899bb5f..0bb5d15d8 100644 --- a/crates/tools/tests/cli.rs +++ b/crates/tools/tests/cli.rs | |||
@@ -1,23 +1,12 @@ | |||
1 | extern crate tools; | 1 | extern crate tools; |
2 | 2 | ||
3 | use tools::{ | 3 | use tools::{ |
4 | project_root, render_template, update, AST, AST_TEMPLATE, SYNTAX_KINDS, SYNTAX_KINDS_TEMPLATE, | 4 | generate, Verify |
5 | }; | 5 | }; |
6 | 6 | ||
7 | #[test] | 7 | #[test] |
8 | fn verify_template_generation() { | 8 | fn verify_template_generation() { |
9 | if let Err(error) = update( | 9 | if let Err(error) = generate(Verify) { |
10 | &project_root().join(SYNTAX_KINDS), | ||
11 | &render_template(&project_root().join(SYNTAX_KINDS_TEMPLATE)).unwrap(), | ||
12 | true, | ||
13 | ) { | ||
14 | panic!("{}. Please update it by running `cargo gen-kinds`", error); | ||
15 | } | ||
16 | if let Err(error) = update( | ||
17 | &project_root().join(AST), | ||
18 | &render_template(&project_root().join(AST_TEMPLATE)).unwrap(), | ||
19 | true, | ||
20 | ) { | ||
21 | panic!("{}. Please update it by running `cargo gen-kinds`", error); | 10 | panic!("{}. Please update it by running `cargo gen-kinds`", error); |
22 | } | 11 | } |
23 | } | 12 | } |