From 514aa3cf853c3c4beef8f827c12328d626f977de Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 16 Oct 2018 20:28:43 +0300 Subject: extract teraron --- Cargo.lock | 22 +++++++--- crates/teraron/Cargo.toml | 13 ++++++ crates/teraron/src/lib.rs | 109 ++++++++++++++++++++++++++++++++++++++++++++++ crates/tools/Cargo.toml | 4 +- crates/tools/src/lib.rs | 50 ++------------------- crates/tools/src/main.rs | 45 +++++++++---------- 6 files changed, 164 insertions(+), 79 deletions(-) create mode 100644 crates/teraron/Cargo.toml create mode 100644 crates/teraron/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 707257a31..a4a07e004 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -794,7 +794,7 @@ dependencies = [ [[package]] name = "ron" -version = "0.3.0" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "base64 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1003,7 +1003,7 @@ dependencies = [ [[package]] name = "tera" -version = "0.11.17" +version = "0.11.18" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1021,6 +1021,16 @@ dependencies = [ "url 1.7.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "teraron" +version = "0.0.1" +dependencies = [ + "failure 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "heck 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "ron 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "tera 0.11.18 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "termion" version = "1.5.1" @@ -1080,10 +1090,8 @@ version = "0.1.0" dependencies = [ "clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)", "failure 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", - "heck 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "itertools 0.7.8 (registry+https://github.com/rust-lang/crates.io-index)", - "ron 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", - "tera 0.11.17 (registry+https://github.com/rust-lang/crates.io-index)", + "teraron 0.0.1", "walkdir 2.2.5 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1345,7 +1353,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum regex-syntax 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "747ba3b235651f6e2f67dfa8bcdcd073ddb7c243cb21c442fc12395dfcac212d" "checksum relative-path 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)" = "e614f96449605730b4f7ad2c019e88c1652d730634b4eba07b810801856635e3" "checksum remove_dir_all 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3488ba1b9a2084d38645c4c08276a1752dcbf2c7130d74f1569681ad5d2799c5" -"checksum ron 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a9fa11b7a38511d46ff1959ae46ebb60bd8a746f17bdd0206b4c8de7559ac47b" +"checksum ron 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c48677d8a9247a4e0d1f3f9cb4b0a8e29167fdc3c04f383a5e669cd7a960ae0f" "checksum rowan 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4a1a7366ece9deee5e7df8316a136d585d5c5042854c2297f7f1aee3014c9130" "checksum rustc-demangle 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "bcfe5b13211b4d78e5c2cadfebd7769197d95c639c35a50057eb4c05de811395" "checksum rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7540fc8b0c49f096ee9c961cda096467dce8084bec6bdca2fc83895fd9b28cb8" @@ -1371,7 +1379,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum syn 0.15.11 (registry+https://github.com/rust-lang/crates.io-index)" = "b036b7b35e846707c0e55c2c9441fa47867c0f87fca416921db3261b1d8c741a" "checksum synstructure 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "85bb9b7550d063ea184027c9b8c20ac167cd36d3e06b3a40bceb9d746dc1a7b7" "checksum tempdir 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)" = "15f2b5fb00ccdf689e0149d1b1b3c03fead81c2b37735d812fa8bddbbf41b6d8" -"checksum tera 0.11.17 (registry+https://github.com/rust-lang/crates.io-index)" = "2829d259c4699fbbe8acb353d231e6da31ff4301c52244413ed29ff6093da412" +"checksum tera 0.11.18 (registry+https://github.com/rust-lang/crates.io-index)" = "6c87cae42cc4fc480278c7583792cc5da2d51a25be916b7921cbb45c43063b8d" "checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096" "checksum text_unit 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "93fc86da66d0b9aa8d359b0ec31b4342c6bc52637eadef05b91b098551a9f8e9" "checksum textwrap 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "307686869c93e71f94da64286f9a9524c0f308a9e1c87a583de8e9c9039ad3f6" diff --git a/crates/teraron/Cargo.toml b/crates/teraron/Cargo.toml new file mode 100644 index 000000000..7ce9f1aa8 --- /dev/null +++ b/crates/teraron/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "teraron" +version = "0.0.1" +authors = ["Aleksey Kladov "] +repository = "https://github.com/rust-analyzer/teraron" +description = "Genrate Rust code from a .tera template and a .ron data" +license = "MIT OR Apache-2.0" + +[dependencies] +failure = "0.1.2" +tera = "0.11.18" +ron = "0.4.0" +heck = "0.3.0" diff --git a/crates/teraron/src/lib.rs b/crates/teraron/src/lib.rs new file mode 100644 index 000000000..e464ea080 --- /dev/null +++ b/crates/teraron/src/lib.rs @@ -0,0 +1,109 @@ +//! A simple tool to generate rust code by passing a ron value +//! to a tera template + +#[macro_use] +extern crate failure; +extern crate tera; +extern crate ron; +extern crate heck; + +use std::{ + fs, + collections::HashMap, + path::Path, +}; + +use heck::{CamelCase, ShoutySnakeCase, SnakeCase}; + +#[derive(Clone, Copy, PartialEq, Eq)] +pub enum Mode { + Overwrite, + Verify, +} + +pub use Mode::*; + +/// A helper to update file on disk if it has changed. +/// With verify = false, +pub fn update(path: &Path, contents: &str, mode: Mode) -> Result<(), failure::Error> { + match fs::read_to_string(path) { + Ok(ref old_contents) if old_contents == contents => { + return Ok(()); + } + _ => (), + } + if mode == Verify { + bail!("`{}` is not up-to-date", path.display()); + } + eprintln!("updating {}", path.display()); + fs::write(path, contents)?; + Ok(()) +} + +pub fn generate( + template: &Path, + src: &Path, + mode: Mode, +) -> Result<(), failure::Error> { + assert_eq!( + template.extension().and_then(|it| it.to_str()), Some("tera"), + "template file must have .rs.tera extension", + ); + let file_name = template.file_stem().unwrap().to_str().unwrap(); + assert!( + file_name.ends_with(".rs"), + "template file must have .rs.tera extension", + ); + let tgt = template.with_file_name(file_name); + let template = fs::read_to_string(template)?; + let src: ron::Value = { + let text = fs::read_to_string(src)?; + ron::de::from_str(&text)? + }; + let content = render(&template, src)?; + update( + &tgt, + &content, + mode, + ) +} + +pub fn render( + template: &str, + src: ron::Value, +) -> Result { + let mut tera = mk_tera(); + tera.add_raw_template("_src", template) + .map_err(|e| format_err!("template parsing error: {:?}", e))?; + let res = tera.render("_src", &src) + .map_err(|e| format_err!("template rendering error: {:?}", e))?; + return Ok(res); +} + +fn mk_tera() -> tera::Tera { + let mut res = tera::Tera::default(); + res.register_filter("camel", |arg, _| { + Ok(arg.as_str().unwrap().to_camel_case().into()) + }); + res.register_filter("snake", |arg, _| { + Ok(arg.as_str().unwrap().to_snake_case().into()) + }); + res.register_filter("SCREAM", |arg, _| { + Ok(arg.as_str().unwrap().to_shouty_snake_case().into()) + }); + res.register_function("concat", Box::new(concat)); + res +} + +fn concat(args: HashMap) -> tera::Result { + let mut elements = Vec::new(); + for &key in ["a", "b", "c"].iter() { + let val = match args.get(key) { + Some(val) => val, + None => continue, + }; + let val = val.as_array().unwrap(); + elements.extend(val.iter().cloned()); + } + Ok(tera::Value::Array(elements)) +} diff --git a/crates/tools/Cargo.toml b/crates/tools/Cargo.toml index e2fecc60d..ffd0ab1a5 100644 --- a/crates/tools/Cargo.toml +++ b/crates/tools/Cargo.toml @@ -6,10 +6,8 @@ authors = ["Aleksey Kladov "] publish = false [dependencies] -ron = "0.3.0" +teraron = { path = "../teraron"} walkdir = "2.1.3" itertools = "0.7.8" -tera = "0.11" clap = "2.32.0" failure = "0.1.1" -heck = "0.3.0" diff --git a/crates/tools/src/lib.rs b/crates/tools/src/lib.rs index 5d5d372bb..63ede5315 100644 --- a/crates/tools/src/lib.rs +++ b/crates/tools/src/lib.rs @@ -1,25 +1,18 @@ extern crate itertools; #[macro_use] extern crate failure; -extern crate heck; -extern crate ron; -extern crate tera; -use heck::{CamelCase, ShoutySnakeCase, SnakeCase}; use itertools::Itertools; use std::{ - collections::HashMap, fs, path::{Path, PathBuf}, }; pub type Result = ::std::result::Result; -const GRAMMAR: &str = "ra_syntax/src/grammar.ron"; -pub const SYNTAX_KINDS: &str = "ra_syntax/src/syntax_kinds/generated.rs"; -pub const SYNTAX_KINDS_TEMPLATE: &str = "ra_syntax/src/syntax_kinds/generated.rs.tera"; -pub const AST: &str = "ra_syntax/src/ast/generated.rs"; -pub const AST_TEMPLATE: &str = "ra_syntax/src/ast/generated.rs.tera"; +pub const GRAMMAR: &str = "ra_syntax/src/grammar.ron"; +pub const SYNTAX_KINDS: &str = "ra_syntax/src/syntax_kinds/generated.rs.tera"; +pub const AST: &str = "ra_syntax/src/ast/generated.rs.tera"; #[derive(Debug)] pub struct Test { @@ -76,43 +69,6 @@ pub fn update(path: &Path, contents: &str, verify: bool) -> Result<()> { Ok(()) } -pub fn render_template(template: &Path) -> Result { - let grammar: ron::value::Value = { - let text = fs::read_to_string(project_root().join(GRAMMAR))?; - ron::de::from_str(&text)? - }; - let template = fs::read_to_string(template)?; - let mut tera = tera::Tera::default(); - tera.add_raw_template("grammar", &template) - .map_err(|e| format_err!("template error: {:?}", e))?; - tera.register_function("concat", Box::new(concat)); - tera.register_filter("camel", |arg, _| { - Ok(arg.as_str().unwrap().to_camel_case().into()) - }); - tera.register_filter("snake", |arg, _| { - Ok(arg.as_str().unwrap().to_snake_case().into()) - }); - tera.register_filter("SCREAM", |arg, _| { - Ok(arg.as_str().unwrap().to_shouty_snake_case().into()) - }); - let ret = tera - .render("grammar", &grammar) - .map_err(|e| format_err!("template error: {:?}", e))?; - return Ok(ret); - - fn concat(args: HashMap) -> tera::Result { - let mut elements = Vec::new(); - for &key in ["a", "b", "c"].iter() { - let val = match args.get(key) { - Some(val) => val, - None => continue, - }; - let val = val.as_array().unwrap(); - elements.extend(val.iter().cloned()); - } - Ok(tera::Value::Array(elements)) - } -} pub fn project_root() -> PathBuf { Path::new(&std::env::var("CARGO_MANIFEST_DIR").unwrap()) diff --git a/crates/tools/src/main.rs b/crates/tools/src/main.rs index b662d78df..1bbc43123 100644 --- a/crates/tools/src/main.rs +++ b/crates/tools/src/main.rs @@ -3,6 +3,7 @@ extern crate clap; extern crate failure; extern crate tools; extern crate walkdir; +extern crate teraron; use clap::{App, Arg, SubCommand}; use std::{ @@ -12,9 +13,9 @@ use std::{ process::Command, }; use tools::{ - collect_tests, project_root, render_template, update, Result, Test, AST, AST_TEMPLATE, - SYNTAX_KINDS, SYNTAX_KINDS_TEMPLATE, + collect_tests, project_root, Result, Test, AST, SYNTAX_KINDS, GRAMMAR, }; +use teraron::{Mode, Verify, Overwrite}; const GRAMMAR_DIR: &str = "./crates/ra_syntax/src/grammar"; const INLINE_TESTS_DIR: &str = "./crates/ra_syntax/tests/data/parser/inline"; @@ -32,35 +33,35 @@ fn main() -> Result<()> { .subcommand(SubCommand::with_name("gen-tests")) .subcommand(SubCommand::with_name("install-code")) .get_matches(); + let mode = if matches.is_present("verify") { + Verify + } else { + Overwrite + }; match matches.subcommand() { ("install-code", _) => install_code_extension()?, - (name, Some(matches)) => run_gen_command(name, matches.is_present("verify"))?, - _ => unreachable!(), - } - Ok(()) -} - -fn run_gen_command(name: &str, verify: bool) -> Result<()> { - match name { - "gen-kinds" => { - update( - &project_root().join(SYNTAX_KINDS), - &render_template(&project_root().join(SYNTAX_KINDS_TEMPLATE))?, - verify, + ("gen-tests", _) => gen_tests(mode)?, + ("gen-kinds", _) => { + let grammar = project_root().join(GRAMMAR); + let syntax_kinds = project_root().join(SYNTAX_KINDS); + let ast = project_root().join(AST); + teraron::generate( + &syntax_kinds, + &grammar, + mode, )?; - update( - &project_root().join(AST), - &render_template(&project_root().join(AST_TEMPLATE))?, - verify, + teraron::generate( + &ast, + &grammar, + mode, )?; } - "gen-tests" => gen_tests(verify)?, _ => unreachable!(), } Ok(()) } -fn gen_tests(verify: bool) -> Result<()> { +fn gen_tests(mode: Mode) -> Result<()> { let tests = tests_from_dir(Path::new(GRAMMAR_DIR))?; let inline_tests_dir = Path::new(INLINE_TESTS_DIR); @@ -83,7 +84,7 @@ fn gen_tests(verify: bool) -> Result<()> { inline_tests_dir.join(file_name) } }; - update(&path, &test.text, verify)?; + teraron::update(&path, &test.text, mode)?; } Ok(()) } -- cgit v1.2.3