3 process::{Command, Stdio},
9 use itertools::Itertools;
11 pub use teraron::{Mode, Overwrite, Verify};
13 pub type Result<T> = std::result::Result<T, failure::Error>;
15 pub const GRAMMAR: &str = "crates/ra_syntax/src/grammar.ron";
16 pub const SYNTAX_KINDS: &str = "crates/ra_syntax/src/syntax_kinds/generated.rs.tera";
17 pub const AST: &str = "crates/ra_syntax/src/ast/generated.rs.tera";
18 const TOOLCHAIN: &str = "stable";
27 pub fn collect_tests(s: &str) -> Vec<(usize, Test)> {
30 let comment_blocks = s
34 .group_by(|(_idx, line)| line.starts_with(prefix));
36 'outer: for (is_comment, block) in comment_blocks.into_iter() {
40 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());
48 Some((idx, line)) if line.starts_with("test_err ") => {
50 break (idx, line["test_err ".len()..].to_string());
53 None => continue 'outer,
56 let text: String = itertools::join(
57 block.map(|(_, line)| line).chain(::std::iter::once("")),
60 assert!(!text.trim().is_empty() && text.ends_with('\n'));
61 res.push((start_line, Test { name, text, ok }))
66 pub fn generate(mode: Mode) -> Result<()> {
67 let grammar = project_root().join(GRAMMAR);
68 let syntax_kinds = project_root().join(SYNTAX_KINDS);
69 let ast = project_root().join(AST);
70 teraron::generate(&syntax_kinds, &grammar, mode)?;
71 teraron::generate(&ast, &grammar, mode)?;
75 pub fn project_root() -> PathBuf {
76 Path::new(&env!("CARGO_MANIFEST_DIR"))
83 pub fn run(cmdline: &str, dir: &str) -> Result<()> {
84 eprintln!("\nwill run: {}", cmdline);
85 let project_dir = project_root().join(dir);
86 let mut args = cmdline.split_whitespace();
87 let exec = args.next().unwrap();
88 let status = Command::new(exec)
90 .current_dir(project_dir)
92 if !status.success() {
93 bail!("`{}` exited with {}", cmdline, status);
98 pub fn run_rustfmt(mode: Mode) -> Result<()> {
99 match Command::new("rustup")
100 .args(&["run", TOOLCHAIN, "--", "cargo", "fmt", "--version"])
101 .stderr(Stdio::null())
102 .stdout(Stdio::null())
105 Ok(status) if status.success() => (),
106 _ => install_rustfmt()?,
111 &format!("rustup run {} -- cargo fmt -- --check", TOOLCHAIN),
115 run(&format!("rustup run {} -- cargo fmt", TOOLCHAIN), ".")?;
120 pub fn install_rustfmt() -> Result<()> {
121 run(&format!("rustup install {}", TOOLCHAIN), ".")?;
123 &format!("rustup component add rustfmt --toolchain {}", TOOLCHAIN),
128 pub fn install_format_hook() -> Result<()> {
129 let result_path = Path::new("./.git/hooks/pre-commit");
130 if !result_path.exists() {
131 run("cargo build --package tools --bin pre-commit", ".")?;
133 copy("./target/debug/pre-commit.exe", result_path)?;
135 copy("./target/debug/pre-commit", result_path)?;
138 return Err(Error::new(ErrorKind::AlreadyExists, "Git hook already created").into());
143 pub fn run_fuzzer() -> Result<()> {
144 match Command::new("cargo")
145 .args(&["fuzz", "--help"])
146 .stderr(Stdio::null())
147 .stdout(Stdio::null())
150 Ok(status) if status.success() => (),
151 _ => run("cargo install cargo-fuzz", ".")?,
155 "rustup run nightly -- cargo fuzz run parser",
156 "./crates/ra_syntax",