Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add format command to tools crate #163

Merged
merged 2 commits into from
Oct 31, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .cargo/config
Original file line number Diff line number Diff line change
Expand Up @@ -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"
9 changes: 5 additions & 4 deletions crates/tools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ pub use teraron::{Mode, Verify, Overwrite};

pub type Result<T> = ::std::result::Result<T, failure::Error>;

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";

#[derive(Debug)]
pub struct Test {
Expand Down Expand Up @@ -75,7 +75,8 @@ 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()
}
19 changes: 12 additions & 7 deletions crates/tools/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::{
process::Command,
};
use tools::{
collect_tests, Result, Test, generate, Mode, Overwrite, Verify,
collect_tests, Result, Test, generate, Mode, Overwrite, Verify, project_root,
};

const GRAMMAR_DIR: &str = "./crates/ra_syntax/src/grammar";
Expand All @@ -31,6 +31,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
Expand All @@ -41,6 +42,7 @@ fn main() -> Result<()> {
("install-code", _) => install_code_extension()?,
("gen-tests", _) => gen_tests(mode)?,
("gen-syntax", _) => generate(Overwrite)?,
("format", _) => run_rustfmt()?,
_ => unreachable!(),
}
Ok(())
Expand Down Expand Up @@ -146,12 +148,7 @@ fn install_code_extension() -> Result<()> {

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 project_dir = project_root().join(dir);
let mut args = cmdline.split_whitespace();
let exec = args.next().unwrap();
let status = Command::new(exec)
Expand All @@ -163,3 +160,11 @@ fn run(cmdline: &'static str, dir: &str) -> Result<()> {
}
Ok(())
}

fn run_rustfmt() -> Result<()> {
Copy link
Member

@matklad matklad Oct 30, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be cool to have an fn run_rustfmt_precommit, which formats all files changed in git. See this for the proper git command invocation and for how to make rustfmt format a single file: https://github.com/antiochp/grin/blob/121cce7e515f9eca8d8708a262a7cc262ee9b7e9/.hooks/pre-commit#L27

// Use beta toolchain for 2018 edition.
run("rustup install beta", ".")?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's pin this to a specific beta, rustup install beta-2018-10-30

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about we have a const value like:

const TOOLCHAIN: &str = "beta-2018-10-30";

and use it where we need?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

run("rustup component add rustfmt-preview --toolchain beta", ".")?;
run("rustup run beta -- cargo fmt", ".")?;
Ok(())
}