aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/tools/src/lib.rs29
-rw-r--r--crates/tools/src/main.rs30
-rw-r--r--crates/tools/tests/cli.rs9
3 files changed, 40 insertions, 28 deletions
diff --git a/crates/tools/src/lib.rs b/crates/tools/src/lib.rs
index 91c35b9e1..29c46c7c4 100644
--- a/crates/tools/src/lib.rs
+++ b/crates/tools/src/lib.rs
@@ -4,9 +4,11 @@ extern crate teraron;
4 4
5use std::{ 5use std::{
6 path::{Path, PathBuf}, 6 path::{Path, PathBuf},
7 process::Command,
7}; 8};
8 9
9use itertools::Itertools; 10use itertools::Itertools;
11use failure::bail;
10 12
11pub use teraron::{Mode, Verify, Overwrite}; 13pub use teraron::{Mode, Verify, Overwrite};
12 14
@@ -15,6 +17,7 @@ pub type Result<T> = ::std::result::Result<T, failure::Error>;
15pub const GRAMMAR: &str = "crates/ra_syntax/src/grammar.ron"; 17pub const GRAMMAR: &str = "crates/ra_syntax/src/grammar.ron";
16pub const SYNTAX_KINDS: &str = "crates/ra_syntax/src/syntax_kinds/generated.rs.tera"; 18pub const SYNTAX_KINDS: &str = "crates/ra_syntax/src/syntax_kinds/generated.rs.tera";
17pub const AST: &str = "crates/ra_syntax/src/ast/generated.rs.tera"; 19pub const AST: &str = "crates/ra_syntax/src/ast/generated.rs.tera";
20const TOOLCHAIN: &str = "beta-2018-10-30";
18 21
19#[derive(Debug)] 22#[derive(Debug)]
20pub struct Test { 23pub struct Test {
@@ -80,3 +83,29 @@ pub fn project_root() -> PathBuf {
80 .unwrap() 83 .unwrap()
81 .to_path_buf() 84 .to_path_buf()
82} 85}
86
87pub fn run(cmdline: &str, dir: &str) -> Result<()> {
88 eprintln!("\nwill run: {}", cmdline);
89 let project_dir = project_root().join(dir);
90 let mut args = cmdline.split_whitespace();
91 let exec = args.next().unwrap();
92 let status = Command::new(exec)
93 .args(args)
94 .current_dir(project_dir)
95 .status()?;
96 if !status.success() {
97 bail!("`{}` exited with {}", cmdline, status);
98 }
99 Ok(())
100}
101
102pub fn run_rustfmt(mode: Mode) -> Result<()> {
103 run(&format!("rustup install {}", TOOLCHAIN), ".")?;
104 run(&format!("rustup component add rustfmt-preview --toolchain {}", TOOLCHAIN), ".")?;
105 if mode == Verify {
106 run(&format!("rustup run {} -- cargo fmt -- --check", TOOLCHAIN), ".")?;
107 } else {
108 run(&format!("rustup run {} -- cargo fmt", TOOLCHAIN), ".")?;
109 }
110 Ok(())
111}
diff --git a/crates/tools/src/main.rs b/crates/tools/src/main.rs
index bc5ad6fa8..91675bbf0 100644
--- a/crates/tools/src/main.rs
+++ b/crates/tools/src/main.rs
@@ -1,5 +1,4 @@
1extern crate clap; 1extern crate clap;
2#[macro_use]
3extern crate failure; 2extern crate failure;
4extern crate tools; 3extern crate tools;
5extern crate walkdir; 4extern crate walkdir;
@@ -10,11 +9,11 @@ use std::{
10 collections::HashMap, 9 collections::HashMap,
11 fs, 10 fs,
12 path::{Path, PathBuf}, 11 path::{Path, PathBuf},
13 process::Command,
14}; 12};
15use tools::{ 13use tools::{
16 collect_tests, Result, Test, generate, Mode, Overwrite, Verify, project_root, 14 collect_tests, Result, Test, generate, Mode, Overwrite, Verify, run, run_rustfmt,
17}; 15};
16use failure::bail;
18 17
19const GRAMMAR_DIR: &str = "./crates/ra_syntax/src/grammar"; 18const GRAMMAR_DIR: &str = "./crates/ra_syntax/src/grammar";
20const INLINE_TESTS_DIR: &str = "./crates/ra_syntax/tests/data/parser/inline"; 19const INLINE_TESTS_DIR: &str = "./crates/ra_syntax/tests/data/parser/inline";
@@ -42,7 +41,7 @@ fn main() -> Result<()> {
42 ("install-code", _) => install_code_extension()?, 41 ("install-code", _) => install_code_extension()?,
43 ("gen-tests", _) => gen_tests(mode)?, 42 ("gen-tests", _) => gen_tests(mode)?,
44 ("gen-syntax", _) => generate(Overwrite)?, 43 ("gen-syntax", _) => generate(Overwrite)?,
45 ("format", _) => run_rustfmt()?, 44 ("format", _) => run_rustfmt(Overwrite)?,
46 _ => unreachable!(), 45 _ => unreachable!(),
47 } 46 }
48 Ok(()) 47 Ok(())
@@ -145,26 +144,3 @@ fn install_code_extension() -> Result<()> {
145 } 144 }
146 Ok(()) 145 Ok(())
147} 146}
148
149fn run(cmdline: &'static str, dir: &str) -> Result<()> {
150 eprintln!("\nwill run: {}", cmdline);
151 let project_dir = project_root().join(dir);
152 let mut args = cmdline.split_whitespace();
153 let exec = args.next().unwrap();
154 let status = Command::new(exec)
155 .args(args)
156 .current_dir(project_dir)
157 .status()?;
158 if !status.success() {
159 bail!("`{}` exited with {}", cmdline, status);
160 }
161 Ok(())
162}
163
164fn run_rustfmt() -> Result<()> {
165 // Use beta toolchain for 2018 edition.
166 run("rustup install beta", ".")?;
167 run("rustup component add rustfmt-preview --toolchain beta", ".")?;
168 run("rustup run beta -- cargo fmt", ".")?;
169 Ok(())
170}
diff --git a/crates/tools/tests/cli.rs b/crates/tools/tests/cli.rs
index 5de52fc2b..8c53a8230 100644
--- a/crates/tools/tests/cli.rs
+++ b/crates/tools/tests/cli.rs
@@ -1,7 +1,7 @@
1extern crate tools; 1extern crate tools;
2 2
3use tools::{ 3use tools::{
4 generate, Verify 4 generate, Verify, run_rustfmt,
5}; 5};
6 6
7#[test] 7#[test]
@@ -10,3 +10,10 @@ fn verify_template_generation() {
10 panic!("{}. Please update it by running `cargo gen-syntax`", error); 10 panic!("{}. Please update it by running `cargo gen-syntax`", error);
11 } 11 }
12} 12}
13
14#[test]
15fn check_code_formatting() {
16 if let Err(error) = run_rustfmt(Verify) {
17 panic!("{}. Please format the code by running `cargo format`", error);
18 }
19}