aboutsummaryrefslogtreecommitdiff
path: root/crates/tools
diff options
context:
space:
mode:
authorMuhammad Mominul Huque <[email protected]>2018-10-15 19:54:27 +0100
committerMuhammad Mominul Huque <[email protected]>2018-10-15 19:54:27 +0100
commit9d9e637ef39cbc00eaebad93294a60ccfd3405eb (patch)
treeec844156b4f2c867a860d802edfb463c156a2f37 /crates/tools
parentce73df065f89bb5aa17517de16c10f9e4d3abaeb (diff)
Refactor the constants
Diffstat (limited to 'crates/tools')
-rw-r--r--crates/tools/src/lib.rs16
-rw-r--r--crates/tools/src/main.rs11
-rw-r--r--crates/tools/tests/cli.rs12
3 files changed, 19 insertions, 20 deletions
diff --git a/crates/tools/src/lib.rs b/crates/tools/src/lib.rs
index ba7d10caa..548b157dd 100644
--- a/crates/tools/src/lib.rs
+++ b/crates/tools/src/lib.rs
@@ -8,13 +8,19 @@ extern crate heck;
8use std::{ 8use std::{
9 collections::HashMap, 9 collections::HashMap,
10 fs, 10 fs,
11 path::Path, 11 path::{Path, PathBuf},
12}; 12};
13use itertools::Itertools; 13use itertools::Itertools;
14use heck::{CamelCase, ShoutySnakeCase, SnakeCase}; 14use heck::{CamelCase, ShoutySnakeCase, SnakeCase};
15 15
16pub type Result<T> = ::std::result::Result<T, failure::Error>; 16pub type Result<T> = ::std::result::Result<T, failure::Error>;
17 17
18const GRAMMAR: &str = "ra_syntax/src/grammar.ron";
19pub const SYNTAX_KINDS: &str = "ra_syntax/src/syntax_kinds/generated.rs";
20pub const SYNTAX_KINDS_TEMPLATE: &str = "ra_syntax/src/syntax_kinds/generated.rs.tera";
21pub const AST: &str = "ra_syntax/src/ast/generated.rs";
22pub const AST_TEMPLATE: &str = "ra_syntax/src/ast/generated.rs.tera";
23
18#[derive(Debug)] 24#[derive(Debug)]
19pub struct Test { 25pub struct Test {
20 pub name: String, 26 pub name: String,
@@ -71,9 +77,9 @@ pub fn update(path: &Path, contents: &str, verify: bool) -> Result<()> {
71 Ok(()) 77 Ok(())
72} 78}
73 79
74pub fn render_template(template: &str) -> Result<String> { 80pub fn render_template(template: PathBuf) -> Result<String> {
75 let grammar: ron::value::Value = { 81 let grammar: ron::value::Value = {
76 let text = fs::read_to_string(format!("{}{}", Path::new(&std::env::var("CARGO_MANIFEST_DIR").unwrap()).parent().unwrap().to_str().unwrap(), "/ra_syntax/src/grammar.ron"))?; 82 let text = fs::read_to_string(project_root().join(GRAMMAR))?;
77 ron::de::from_str(&text)? 83 ron::de::from_str(&text)?
78 }; 84 };
79 let template = fs::read_to_string(template)?; 85 let template = fs::read_to_string(template)?;
@@ -108,3 +114,7 @@ pub fn render_template(template: &str) -> Result<String> {
108 Ok(tera::Value::Array(elements)) 114 Ok(tera::Value::Array(elements))
109 } 115 }
110} 116}
117
118pub fn project_root() -> PathBuf {
119 Path::new(&std::env::var("CARGO_MANIFEST_DIR").unwrap()).parent().unwrap().to_path_buf()
120}
diff --git a/crates/tools/src/main.rs b/crates/tools/src/main.rs
index aa6d964e6..549892bc6 100644
--- a/crates/tools/src/main.rs
+++ b/crates/tools/src/main.rs
@@ -11,15 +11,10 @@ use std::{
11 path::{Path, PathBuf}, 11 path::{Path, PathBuf},
12 process::Command, 12 process::Command,
13}; 13};
14use tools::{Test, collect_tests, render_template, update, Result}; 14use tools::{AST, AST_TEMPLATE, Result, SYNTAX_KINDS, SYNTAX_KINDS_TEMPLATE, Test, collect_tests, render_template, update, project_root};
15 15
16const GRAMMAR_DIR: &str = "./crates/ra_syntax/src/grammar"; 16const GRAMMAR_DIR: &str = "./crates/ra_syntax/src/grammar";
17const INLINE_TESTS_DIR: &str = "./crates/ra_syntax/tests/data/parser/inline"; 17const INLINE_TESTS_DIR: &str = "./crates/ra_syntax/tests/data/parser/inline";
18const GRAMMAR: &str = "./crates/ra_syntax/src/grammar.ron";
19const SYNTAX_KINDS: &str = "./crates/ra_syntax/src/syntax_kinds/generated.rs";
20const SYNTAX_KINDS_TEMPLATE: &str = "./crates/ra_syntax/src/syntax_kinds/generated.rs.tera";
21const AST: &str = "./crates/ra_syntax/src/ast/generated.rs";
22const AST_TEMPLATE: &str = "./crates/ra_syntax/src/ast/generated.rs.tera";
23 18
24fn main() -> Result<()> { 19fn main() -> Result<()> {
25 let matches = App::new("tasks") 20 let matches = App::new("tasks")
@@ -45,8 +40,8 @@ fn main() -> Result<()> {
45fn run_gen_command(name: &str, verify: bool) -> Result<()> { 40fn run_gen_command(name: &str, verify: bool) -> Result<()> {
46 match name { 41 match name {
47 "gen-kinds" => { 42 "gen-kinds" => {
48 update(Path::new(SYNTAX_KINDS), &render_template(SYNTAX_KINDS_TEMPLATE)?, verify)?; 43 update(&project_root().join(SYNTAX_KINDS), &render_template(project_root().join(SYNTAX_KINDS_TEMPLATE))?, verify)?;
49 update(Path::new(AST), &render_template(AST_TEMPLATE)?, verify)?; 44 update(&project_root().join(AST), &render_template(project_root().join(AST_TEMPLATE))?, verify)?;
50 }, 45 },
51 "gen-tests" => { 46 "gen-tests" => {
52 gen_tests(verify)? 47 gen_tests(verify)?
diff --git a/crates/tools/tests/cli.rs b/crates/tools/tests/cli.rs
index 9ff1eecd9..d0ed60f7a 100644
--- a/crates/tools/tests/cli.rs
+++ b/crates/tools/tests/cli.rs
@@ -1,19 +1,13 @@
1extern crate tools; 1extern crate tools;
2 2
3use std::path::Path; 3use tools::{AST, AST_TEMPLATE, SYNTAX_KINDS, SYNTAX_KINDS_TEMPLATE, render_template, update, project_root};
4use tools::{render_template, update};
5
6const SYNTAX_KINDS: &str = "../ra_syntax/src/syntax_kinds/generated.rs";
7const SYNTAX_KINDS_TEMPLATE: &str = "../ra_syntax/src/syntax_kinds/generated.rs.tera";
8const AST: &str = "../ra_syntax/src/ast/generated.rs";
9const AST_TEMPLATE: &str = "../ra_syntax/src/ast/generated.rs.tera";
10 4
11#[test] 5#[test]
12fn verify_template_generation() { 6fn verify_template_generation() {
13 if let Err(error) = update(Path::new(SYNTAX_KINDS), &render_template(SYNTAX_KINDS_TEMPLATE).unwrap(), true) { 7 if let Err(error) = update(&project_root().join(SYNTAX_KINDS), &render_template(project_root().join(SYNTAX_KINDS_TEMPLATE)).unwrap(), true) {
14 panic!("{}. Please update it by running `cargo gen-kinds`", error); 8 panic!("{}. Please update it by running `cargo gen-kinds`", error);
15 } 9 }
16 if let Err(error) = update(Path::new(AST), &render_template(AST_TEMPLATE).unwrap(), true) { 10 if let Err(error) = update(&project_root().join(AST), &render_template(project_root().join(AST_TEMPLATE)).unwrap(), true) {
17 panic!("{}. Please update it by running `cargo gen-kinds`", error); 11 panic!("{}. Please update it by running `cargo gen-kinds`", error);
18 } 12 }
19} 13}