2018-10-14 09:32:57 -05:00
|
|
|
use std::{
|
2018-10-15 13:54:27 -05:00
|
|
|
path::{Path, PathBuf},
|
2018-10-31 15:57:38 -05:00
|
|
|
process::{Command, Stdio},
|
2018-12-09 06:27:13 -06:00
|
|
|
fs::copy,
|
|
|
|
io::{Error, ErrorKind}
|
2018-10-14 09:32:57 -05:00
|
|
|
};
|
|
|
|
|
2018-10-31 14:50:43 -05:00
|
|
|
use failure::bail;
|
2018-10-31 15:41:43 -05:00
|
|
|
use itertools::Itertools;
|
2018-10-16 12:53:19 -05:00
|
|
|
|
2018-10-31 15:41:43 -05:00
|
|
|
pub use teraron::{Mode, Overwrite, Verify};
|
2018-10-16 12:53:19 -05:00
|
|
|
|
2018-12-06 11:42:03 -06:00
|
|
|
pub type Result<T> = std::result::Result<T, failure::Error>;
|
2018-07-30 08:16:58 -05:00
|
|
|
|
2018-10-26 08:08:21 -05:00
|
|
|
pub const GRAMMAR: &str = "crates/ra_syntax/src/grammar.ron";
|
|
|
|
pub const SYNTAX_KINDS: &str = "crates/ra_syntax/src/syntax_kinds/generated.rs.tera";
|
|
|
|
pub const AST: &str = "crates/ra_syntax/src/ast/generated.rs.tera";
|
2018-12-06 11:22:06 -06:00
|
|
|
const TOOLCHAIN: &str = "1.31.0";
|
2018-10-15 13:54:27 -05:00
|
|
|
|
2018-07-31 07:30:11 -05:00
|
|
|
#[derive(Debug)]
|
2018-07-30 08:16:58 -05:00
|
|
|
pub struct Test {
|
|
|
|
pub name: String,
|
|
|
|
pub text: String,
|
|
|
|
}
|
|
|
|
|
2018-07-30 08:32:27 -05:00
|
|
|
pub fn collect_tests(s: &str) -> Vec<(usize, Test)> {
|
2018-07-30 08:16:58 -05:00
|
|
|
let mut res = vec![];
|
|
|
|
let prefix = "// ";
|
|
|
|
let comment_blocks = s
|
|
|
|
.lines()
|
|
|
|
.map(str::trim_left)
|
|
|
|
.enumerate()
|
2018-07-30 08:25:10 -05:00
|
|
|
.group_by(|(_idx, line)| line.starts_with(prefix));
|
2018-07-30 08:16:58 -05:00
|
|
|
|
|
|
|
'outer: for (is_comment, block) in comment_blocks.into_iter() {
|
|
|
|
if !is_comment {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let mut block = block.map(|(idx, line)| (idx, &line[prefix.len()..]));
|
|
|
|
|
|
|
|
let (start_line, name) = loop {
|
|
|
|
match block.next() {
|
|
|
|
Some((idx, line)) if line.starts_with("test ") => {
|
2018-12-09 04:29:13 -06:00
|
|
|
break (idx, line["test ".len()..].to_string());
|
2018-07-31 07:40:40 -05:00
|
|
|
}
|
2018-07-30 08:16:58 -05:00
|
|
|
Some(_) => (),
|
|
|
|
None => continue 'outer,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let text: String = itertools::join(
|
2018-07-31 07:40:40 -05:00
|
|
|
block.map(|(_, line)| line).chain(::std::iter::once("")),
|
|
|
|
"\n",
|
2018-07-30 08:16:58 -05:00
|
|
|
);
|
2018-10-16 10:45:10 -05:00
|
|
|
assert!(!text.trim().is_empty() && text.ends_with('\n'));
|
2018-07-31 07:40:40 -05:00
|
|
|
res.push((start_line, Test { name, text }))
|
2018-07-30 08:16:58 -05:00
|
|
|
}
|
|
|
|
res
|
|
|
|
}
|
2018-10-14 09:32:57 -05:00
|
|
|
|
2018-10-16 12:53:19 -05:00
|
|
|
pub fn generate(mode: Mode) -> Result<()> {
|
|
|
|
let grammar = project_root().join(GRAMMAR);
|
|
|
|
let syntax_kinds = project_root().join(SYNTAX_KINDS);
|
|
|
|
let ast = project_root().join(AST);
|
2018-10-31 15:41:43 -05:00
|
|
|
teraron::generate(&syntax_kinds, &grammar, mode)?;
|
|
|
|
teraron::generate(&ast, &grammar, mode)?;
|
2018-10-14 09:32:57 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-10-15 13:54:27 -05:00
|
|
|
pub fn project_root() -> PathBuf {
|
2018-12-09 06:27:13 -06:00
|
|
|
Path::new(&env!("CARGO_MANIFEST_DIR"))
|
2018-10-26 08:08:21 -05:00
|
|
|
.ancestors()
|
|
|
|
.nth(2)
|
2018-10-15 16:44:23 -05:00
|
|
|
.unwrap()
|
|
|
|
.to_path_buf()
|
2018-10-15 13:54:27 -05:00
|
|
|
}
|
2018-10-31 14:50:43 -05:00
|
|
|
|
|
|
|
pub fn run(cmdline: &str, dir: &str) -> Result<()> {
|
|
|
|
eprintln!("\nwill run: {}", cmdline);
|
|
|
|
let project_dir = project_root().join(dir);
|
|
|
|
let mut args = cmdline.split_whitespace();
|
|
|
|
let exec = args.next().unwrap();
|
|
|
|
let status = Command::new(exec)
|
|
|
|
.args(args)
|
|
|
|
.current_dir(project_dir)
|
|
|
|
.status()?;
|
|
|
|
if !status.success() {
|
|
|
|
bail!("`{}` exited with {}", cmdline, status);
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn run_rustfmt(mode: Mode) -> Result<()> {
|
2018-10-31 15:57:38 -05:00
|
|
|
match Command::new("rustup")
|
|
|
|
.args(&["run", TOOLCHAIN, "--", "cargo", "fmt", "--version"])
|
|
|
|
.stderr(Stdio::null())
|
|
|
|
.stdout(Stdio::null())
|
|
|
|
.status()
|
|
|
|
{
|
|
|
|
Ok(status) if status.success() => (),
|
|
|
|
_ => install_rustfmt()?,
|
|
|
|
};
|
|
|
|
|
2018-10-31 14:50:43 -05:00
|
|
|
if mode == Verify {
|
2018-10-31 15:41:43 -05:00
|
|
|
run(
|
|
|
|
&format!("rustup run {} -- cargo fmt -- --check", TOOLCHAIN),
|
|
|
|
".",
|
|
|
|
)?;
|
2018-10-31 14:50:43 -05:00
|
|
|
} else {
|
|
|
|
run(&format!("rustup run {} -- cargo fmt", TOOLCHAIN), ".")?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2018-10-31 15:57:38 -05:00
|
|
|
|
|
|
|
fn install_rustfmt() -> Result<()> {
|
|
|
|
run(&format!("rustup install {}", TOOLCHAIN), ".")?;
|
|
|
|
run(
|
2018-12-09 03:08:10 -06:00
|
|
|
&format!("rustup component add rustfmt --toolchain {}", TOOLCHAIN),
|
2018-10-31 15:57:38 -05:00
|
|
|
".",
|
|
|
|
)
|
|
|
|
}
|
2018-12-09 04:29:13 -06:00
|
|
|
|
|
|
|
pub fn install_format_hook() -> Result<()> {
|
2018-12-09 06:27:13 -06:00
|
|
|
let result_path = Path::new("./.git/hooks/pre-commit");
|
|
|
|
if !result_path.exists() {
|
|
|
|
run("cargo build --package tools --bin pre-commit", ".")?;
|
|
|
|
if cfg!(windows) {
|
|
|
|
copy("./target/debug/pre-commit.exe", result_path)?;
|
|
|
|
} else {
|
|
|
|
copy("./target/debug/pre-commit", result_path)?;
|
2018-12-09 04:29:13 -06:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Err(Error::new(ErrorKind::AlreadyExists, "Git hook already created").into());
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|