2 extern crate itertools;
7 process::{Command, Stdio},
11 use itertools::Itertools;
13 pub use teraron::{Mode, Overwrite, Verify};
15 pub type Result<T> = ::std::result::Result<T, failure::Error>;
17 pub const GRAMMAR: &str = "crates/ra_syntax/src/grammar.ron";
18 pub const SYNTAX_KINDS: &str = "crates/ra_syntax/src/syntax_kinds/generated.rs.tera";
19 pub const AST: &str = "crates/ra_syntax/src/ast/generated.rs.tera";
20 const TOOLCHAIN: &str = "beta-2018-10-30";
28 pub fn collect_tests(s: &str) -> Vec<(usize, Test)> {
31 let comment_blocks = s
35 .group_by(|(_idx, line)| line.starts_with(prefix));
37 'outer: for (is_comment, block) in comment_blocks.into_iter() {
41 let mut block = block.map(|(idx, line)| (idx, &line[prefix.len()..]));
43 let (start_line, name) = loop {
45 Some((idx, line)) if line.starts_with("test ") => {
46 break (idx, line["test ".len()..].to_string())
49 None => continue 'outer,
52 let text: String = itertools::join(
53 block.map(|(_, line)| line).chain(::std::iter::once("")),
56 assert!(!text.trim().is_empty() && text.ends_with('\n'));
57 res.push((start_line, Test { name, text }))
62 pub fn generate(mode: Mode) -> Result<()> {
63 let grammar = project_root().join(GRAMMAR);
64 let syntax_kinds = project_root().join(SYNTAX_KINDS);
65 let ast = project_root().join(AST);
66 teraron::generate(&syntax_kinds, &grammar, mode)?;
67 teraron::generate(&ast, &grammar, mode)?;
71 pub fn project_root() -> PathBuf {
72 Path::new(&std::env::var("CARGO_MANIFEST_DIR").unwrap())
79 pub fn run(cmdline: &str, dir: &str) -> Result<()> {
80 eprintln!("\nwill run: {}", cmdline);
81 let project_dir = project_root().join(dir);
82 let mut args = cmdline.split_whitespace();
83 let exec = args.next().unwrap();
84 let status = Command::new(exec)
86 .current_dir(project_dir)
88 if !status.success() {
89 bail!("`{}` exited with {}", cmdline, status);
94 pub fn run_rustfmt(mode: Mode) -> Result<()> {
95 match Command::new("rustup")
96 .args(&["run", TOOLCHAIN, "--", "cargo", "fmt", "--version"])
97 .stderr(Stdio::null())
98 .stdout(Stdio::null())
101 Ok(status) if status.success() => (),
102 _ => install_rustfmt()?,
107 &format!("rustup run {} -- cargo fmt -- --check", TOOLCHAIN),
111 run(&format!("rustup run {} -- cargo fmt", TOOLCHAIN), ".")?;
116 fn install_rustfmt() -> Result<()> {
117 run(&format!("rustup install {}", TOOLCHAIN), ".")?;
120 "rustup component add rustfmt-preview --toolchain {}",