2018-07-30 06:06:22 -05:00
|
|
|
extern crate clap;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate failure;
|
|
|
|
extern crate ron;
|
2018-07-30 06:08:06 -05:00
|
|
|
extern crate tera;
|
2018-07-30 08:16:58 -05:00
|
|
|
extern crate tools;
|
2018-07-31 07:40:40 -05:00
|
|
|
extern crate walkdir;
|
2018-07-30 14:17:33 -05:00
|
|
|
#[macro_use]
|
|
|
|
extern crate commandspec;
|
2018-07-30 06:06:22 -05:00
|
|
|
|
2018-07-30 08:16:58 -05:00
|
|
|
use clap::{App, Arg, SubCommand};
|
2018-07-31 07:40:40 -05:00
|
|
|
use std::{
|
|
|
|
collections::HashMap,
|
|
|
|
fs,
|
|
|
|
path::{Path, PathBuf},
|
|
|
|
};
|
2018-07-30 08:16:58 -05:00
|
|
|
use tools::{collect_tests, Test};
|
2018-07-30 06:06:22 -05:00
|
|
|
|
|
|
|
type Result<T> = ::std::result::Result<T, failure::Error>;
|
|
|
|
|
2018-07-31 15:38:19 -05:00
|
|
|
const GRAMMAR_DIR: &str = "./src/grammar";
|
2018-07-30 06:06:22 -05:00
|
|
|
const INLINE_TESTS_DIR: &str = "tests/data/parser/inline";
|
|
|
|
const GRAMMAR: &str = "./src/grammar.ron";
|
|
|
|
const SYNTAX_KINDS: &str = "./src/syntax_kinds/generated.rs";
|
|
|
|
const SYNTAX_KINDS_TEMPLATE: &str = "./src/syntax_kinds/generated.rs.tera";
|
2018-08-09 09:43:39 -05:00
|
|
|
const AST: &str = "./src/ast/generated.rs";
|
|
|
|
const AST_TEMPLATE: &str = "./src/ast/generated.rs.tera";
|
2018-07-30 06:06:22 -05:00
|
|
|
|
|
|
|
fn main() -> Result<()> {
|
|
|
|
let matches = App::new("tasks")
|
|
|
|
.setting(clap::AppSettings::SubcommandRequiredElseHelp)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("verify")
|
|
|
|
.long("--verify")
|
|
|
|
.help("Verify that generated code is up-to-date")
|
2018-07-30 06:08:06 -05:00
|
|
|
.global(true),
|
2018-07-30 06:06:22 -05:00
|
|
|
)
|
|
|
|
.subcommand(SubCommand::with_name("gen-kinds"))
|
|
|
|
.subcommand(SubCommand::with_name("gen-tests"))
|
2018-07-30 14:17:33 -05:00
|
|
|
.subcommand(SubCommand::with_name("install-code"))
|
2018-07-30 06:06:22 -05:00
|
|
|
.get_matches();
|
|
|
|
match matches.subcommand() {
|
2018-07-30 14:17:33 -05:00
|
|
|
("install-code", _) => install_code_extension()?,
|
2018-07-30 06:06:22 -05:00
|
|
|
(name, Some(matches)) => run_gen_command(name, matches.is_present("verify"))?,
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run_gen_command(name: &str, verify: bool) -> Result<()> {
|
|
|
|
match name {
|
2018-08-09 09:43:39 -05:00
|
|
|
"gen-kinds" => {
|
|
|
|
update(Path::new(SYNTAX_KINDS), &render_template(SYNTAX_KINDS_TEMPLATE)?, verify)?;
|
|
|
|
update(Path::new(AST), &render_template(AST_TEMPLATE)?, verify)?;
|
|
|
|
},
|
|
|
|
"gen-tests" => {
|
|
|
|
gen_tests(verify)?
|
|
|
|
},
|
2018-07-30 06:06:22 -05:00
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
2018-08-09 09:43:39 -05:00
|
|
|
Ok(())
|
2018-07-30 06:06:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn update(path: &Path, contents: &str, verify: bool) -> Result<()> {
|
|
|
|
match fs::read_to_string(path) {
|
|
|
|
Ok(ref old_contents) if old_contents == contents => {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
if verify {
|
|
|
|
bail!("`{}` is not up-to-date", path.display());
|
|
|
|
}
|
2018-07-30 07:32:19 -05:00
|
|
|
eprintln!("updating {}", path.display());
|
2018-07-30 06:06:22 -05:00
|
|
|
fs::write(path, contents)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-08-09 09:43:39 -05:00
|
|
|
fn render_template(template: &str) -> Result<String> {
|
|
|
|
let grammar: ron::value::Value = {
|
|
|
|
let text = fs::read_to_string(GRAMMAR)?;
|
|
|
|
ron::de::from_str(&text)?
|
|
|
|
};
|
|
|
|
let template = fs::read_to_string(template)?;
|
2018-07-30 10:02:13 -05:00
|
|
|
let mut tera = tera::Tera::default();
|
|
|
|
tera.add_raw_template("grammar", &template)
|
2018-07-30 09:46:50 -05:00
|
|
|
.map_err(|e| format_err!("template error: {:?}", e))?;
|
2018-07-30 10:02:13 -05:00
|
|
|
tera.register_global_function("concat", Box::new(concat));
|
2018-08-09 09:43:39 -05:00
|
|
|
tera.register_filter("camel", |arg, _| {
|
|
|
|
Ok(arg.as_str().unwrap()
|
|
|
|
.split("_")
|
|
|
|
.flat_map(|word| {
|
|
|
|
word.chars()
|
|
|
|
.next().unwrap()
|
|
|
|
.to_uppercase()
|
|
|
|
.chain(
|
|
|
|
word.chars().skip(1).flat_map(|c| c.to_lowercase())
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect::<String>()
|
|
|
|
.into())
|
|
|
|
});
|
2018-07-31 07:40:40 -05:00
|
|
|
let ret = tera
|
|
|
|
.render("grammar", &grammar)
|
2018-07-30 10:02:13 -05:00
|
|
|
.map_err(|e| format_err!("template error: {:?}", e))?;
|
|
|
|
return Ok(ret);
|
|
|
|
|
|
|
|
fn concat(args: HashMap<String, tera::Value>) -> tera::Result<tera::Value> {
|
|
|
|
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))
|
|
|
|
}
|
2018-07-30 06:06:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn gen_tests(verify: bool) -> Result<()> {
|
|
|
|
let tests = tests_from_dir(Path::new(GRAMMAR_DIR))?;
|
|
|
|
|
|
|
|
let inline_tests_dir = Path::new(INLINE_TESTS_DIR);
|
|
|
|
if !inline_tests_dir.is_dir() {
|
|
|
|
fs::create_dir_all(inline_tests_dir)?;
|
|
|
|
}
|
|
|
|
let existing = existing_tests(inline_tests_dir)?;
|
|
|
|
|
2018-07-31 07:30:11 -05:00
|
|
|
for t in existing.keys().filter(|&t| !tests.contains_key(t)) {
|
|
|
|
panic!("Test is deleted: {}", t);
|
2018-07-30 06:06:22 -05:00
|
|
|
}
|
|
|
|
|
2018-07-31 07:30:11 -05:00
|
|
|
let mut new_idx = existing.len() + 2;
|
|
|
|
for (name, test) in tests {
|
|
|
|
let path = match existing.get(&name) {
|
|
|
|
Some((path, _test)) => path.clone(),
|
|
|
|
None => {
|
|
|
|
let file_name = format!("{:04}_{}.rs", new_idx, name);
|
|
|
|
new_idx += 1;
|
|
|
|
inline_tests_dir.join(file_name)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
update(&path, &test.text, verify)?;
|
2018-07-30 06:06:22 -05:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-07-31 07:30:11 -05:00
|
|
|
fn tests_from_dir(dir: &Path) -> Result<HashMap<String, Test>> {
|
|
|
|
let mut res = HashMap::new();
|
2018-07-30 06:06:22 -05:00
|
|
|
for entry in ::walkdir::WalkDir::new(dir) {
|
|
|
|
let entry = entry.unwrap();
|
|
|
|
if !entry.file_type().is_file() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if entry.path().extension().unwrap_or_default() != "rs" {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let text = fs::read_to_string(entry.path())?;
|
|
|
|
|
2018-07-30 08:32:27 -05:00
|
|
|
for (_, test) in collect_tests(&text) {
|
2018-07-31 07:30:11 -05:00
|
|
|
if let Some(old_test) = res.insert(test.name.clone(), test) {
|
2018-07-30 06:06:22 -05:00
|
|
|
bail!("Duplicate test: {}", old_test.name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
|
2018-07-31 07:30:11 -05:00
|
|
|
fn existing_tests(dir: &Path) -> Result<HashMap<String, (PathBuf, Test)>> {
|
|
|
|
let mut res = HashMap::new();
|
2018-07-30 06:06:22 -05:00
|
|
|
for file in fs::read_dir(dir)? {
|
|
|
|
let file = file?;
|
|
|
|
let path = file.path();
|
|
|
|
if path.extension().unwrap_or_default() != "rs" {
|
|
|
|
continue;
|
|
|
|
}
|
2018-07-31 07:30:11 -05:00
|
|
|
let name = {
|
|
|
|
let file_name = path.file_name().unwrap().to_str().unwrap();
|
|
|
|
file_name[5..file_name.len() - 3].to_string()
|
|
|
|
};
|
2018-07-30 06:06:22 -05:00
|
|
|
let text = fs::read_to_string(&path)?;
|
2018-07-31 07:40:40 -05:00
|
|
|
let test = Test {
|
|
|
|
name: name.clone(),
|
|
|
|
text,
|
|
|
|
};
|
2018-07-31 07:30:11 -05:00
|
|
|
match res.insert(name, (path, test)) {
|
|
|
|
Some(old) => println!("Duplicate test: {:?}", old),
|
|
|
|
None => (),
|
|
|
|
}
|
2018-07-30 06:06:22 -05:00
|
|
|
}
|
|
|
|
Ok(res)
|
|
|
|
}
|
2018-07-30 14:17:33 -05:00
|
|
|
|
|
|
|
fn install_code_extension() -> Result<()> {
|
2018-07-31 07:40:40 -05:00
|
|
|
execute!(
|
|
|
|
r"
|
2018-07-30 14:17:33 -05:00
|
|
|
cd code
|
|
|
|
npm install
|
2018-07-31 07:40:40 -05:00
|
|
|
"
|
|
|
|
)?;
|
|
|
|
execute!(
|
|
|
|
r"
|
2018-07-30 14:17:33 -05:00
|
|
|
cd code
|
|
|
|
./node_modules/vsce/out/vsce package
|
2018-07-31 07:40:40 -05:00
|
|
|
"
|
|
|
|
)?;
|
|
|
|
execute!(
|
|
|
|
r"
|
2018-07-30 14:17:33 -05:00
|
|
|
cd code
|
|
|
|
code --install-extension ./libsyntax-rust-0.0.1.vsix
|
2018-07-31 07:40:40 -05:00
|
|
|
"
|
|
|
|
)?;
|
2018-07-30 14:17:33 -05:00
|
|
|
Ok(())
|
|
|
|
}
|