diff --git a/.cargo/config b/.cargo/config index fd32471ea432..c0be661fd608 100644 --- a/.cargo/config +++ b/.cargo/config @@ -3,6 +3,7 @@ gen-syntax = "run --package tools -- gen-syntax" gen-tests = "run --package tools -- gen-tests" install-code = "run --package tools -- install-code" +format = "run --package tools -- format" render-test = "run --package ra_cli -- render-test" parse = "run --package ra_cli -- parse" diff --git a/crates/tools/src/lib.rs b/crates/tools/src/lib.rs index 7c5410d3c786..29c46c7c4d1b 100644 --- a/crates/tools/src/lib.rs +++ b/crates/tools/src/lib.rs @@ -4,17 +4,20 @@ extern crate teraron; use std::{ path::{Path, PathBuf}, + process::Command, }; use itertools::Itertools; +use failure::bail; pub use teraron::{Mode, Verify, Overwrite}; pub type Result = ::std::result::Result; -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"; +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"; +const TOOLCHAIN: &str = "beta-2018-10-30"; #[derive(Debug)] pub struct Test { @@ -75,7 +78,34 @@ pub fn generate(mode: Mode) -> Result<()> { pub fn project_root() -> PathBuf { Path::new(&std::env::var("CARGO_MANIFEST_DIR").unwrap()) - .parent() + .ancestors() + .nth(2) .unwrap() .to_path_buf() } + +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<()> { + run(&format!("rustup install {}", TOOLCHAIN), ".")?; + run(&format!("rustup component add rustfmt-preview --toolchain {}", TOOLCHAIN), ".")?; + if mode == Verify { + run(&format!("rustup run {} -- cargo fmt -- --check", TOOLCHAIN), ".")?; + } else { + run(&format!("rustup run {} -- cargo fmt", TOOLCHAIN), ".")?; + } + Ok(()) +} diff --git a/crates/tools/src/main.rs b/crates/tools/src/main.rs index fdb443690bd9..91675bbf0c80 100644 --- a/crates/tools/src/main.rs +++ b/crates/tools/src/main.rs @@ -1,5 +1,4 @@ extern crate clap; -#[macro_use] extern crate failure; extern crate tools; extern crate walkdir; @@ -10,11 +9,11 @@ use std::{ collections::HashMap, fs, path::{Path, PathBuf}, - process::Command, }; use tools::{ - collect_tests, Result, Test, generate, Mode, Overwrite, Verify, + collect_tests, Result, Test, generate, Mode, Overwrite, Verify, run, run_rustfmt, }; +use failure::bail; const GRAMMAR_DIR: &str = "./crates/ra_syntax/src/grammar"; const INLINE_TESTS_DIR: &str = "./crates/ra_syntax/tests/data/parser/inline"; @@ -31,6 +30,7 @@ fn main() -> Result<()> { .subcommand(SubCommand::with_name("gen-syntax")) .subcommand(SubCommand::with_name("gen-tests")) .subcommand(SubCommand::with_name("install-code")) + .subcommand(SubCommand::with_name("format")) .get_matches(); let mode = if matches.is_present("verify") { Verify @@ -41,6 +41,7 @@ fn main() -> Result<()> { ("install-code", _) => install_code_extension()?, ("gen-tests", _) => gen_tests(mode)?, ("gen-syntax", _) => generate(Overwrite)?, + ("format", _) => run_rustfmt(Overwrite)?, _ => unreachable!(), } Ok(()) @@ -143,23 +144,3 @@ fn install_code_extension() -> Result<()> { } Ok(()) } - -fn run(cmdline: &'static str, dir: &str) -> Result<()> { - eprintln!("\nwill run: {}", cmdline); - let manifest_dir = env!("CARGO_MANIFEST_DIR"); - let project_dir = Path::new(manifest_dir) - .ancestors() - .nth(2) - .unwrap() - .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(()) -} diff --git a/crates/tools/tests/cli.rs b/crates/tools/tests/cli.rs index 5de52fc2b8df..8c53a8230451 100644 --- a/crates/tools/tests/cli.rs +++ b/crates/tools/tests/cli.rs @@ -1,7 +1,7 @@ extern crate tools; use tools::{ - generate, Verify + generate, Verify, run_rustfmt, }; #[test] @@ -10,3 +10,10 @@ fn verify_template_generation() { panic!("{}. Please update it by running `cargo gen-syntax`", error); } } + +#[test] +fn check_code_formatting() { + if let Err(error) = run_rustfmt(Verify) { + panic!("{}. Please format the code by running `cargo format`", error); + } +}