Skip to content

Commit

Permalink
Various changes
Browse files Browse the repository at this point in the history
Pin to a specific toolchain version
Format checking functionality
Add a test to check the code formatting.
Remove macro_use attribute
  • Loading branch information
mominul authored and matklad committed Oct 31, 2018
1 parent d14610d commit 857c165
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 28 deletions.
29 changes: 29 additions & 0 deletions crates/tools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ extern crate teraron;

use std::{
path::{Path, PathBuf},
process::Command,
};

use itertools::Itertools;
use failure::bail;

pub use teraron::{Mode, Verify, Overwrite};

Expand All @@ -15,6 +17,7 @@ pub type Result<T> = ::std::result::Result<T, failure::Error>;
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 {
Expand Down Expand Up @@ -80,3 +83,29 @@ pub fn project_root() -> PathBuf {
.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(())
}
30 changes: 3 additions & 27 deletions crates/tools/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
extern crate clap;
#[macro_use]
extern crate failure;
extern crate tools;
extern crate walkdir;
Expand All @@ -10,11 +9,11 @@ use std::{
collections::HashMap,
fs,
path::{Path, PathBuf},
process::Command,
};
use tools::{
collect_tests, Result, Test, generate, Mode, Overwrite, Verify, project_root,
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";
Expand Down Expand Up @@ -42,7 +41,7 @@ fn main() -> Result<()> {
("install-code", _) => install_code_extension()?,
("gen-tests", _) => gen_tests(mode)?,
("gen-syntax", _) => generate(Overwrite)?,
("format", _) => run_rustfmt()?,
("format", _) => run_rustfmt(Overwrite)?,
_ => unreachable!(),
}
Ok(())
Expand Down Expand Up @@ -145,26 +144,3 @@ fn install_code_extension() -> Result<()> {
}
Ok(())
}

fn run(cmdline: &'static 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(())
}

fn run_rustfmt() -> Result<()> {
// Use beta toolchain for 2018 edition.
run("rustup install beta", ".")?;
run("rustup component add rustfmt-preview --toolchain beta", ".")?;
run("rustup run beta -- cargo fmt", ".")?;
Ok(())
}
9 changes: 8 additions & 1 deletion crates/tools/tests/cli.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
extern crate tools;

use tools::{
generate, Verify
generate, Verify, run_rustfmt,
};

#[test]
Expand All @@ -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);
}
}

0 comments on commit 857c165

Please sign in to comment.