diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2018-12-09 12:44:58 +0000 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2018-12-09 12:44:58 +0000 |
commit | 3725276554fe63fdd9b63a10b86e794b3eb73158 (patch) | |
tree | 01de23f215e51459c67d2eb7f4bfb77d65afd1b7 /crates/tools | |
parent | b9c17a6001aaf79e181a86218883fe96d9c95c09 (diff) | |
parent | cbce28a348ebb5db646cfc5cd3305c6bce80e915 (diff) |
Merge #271
271: Implement format hook r=matklad a=DJMcNab
Tentatively: fixes #155.
However, this does add all changes in staged files, which might not be desirable. However, I think we can't solve that without explicit support in rustfmt for it, so it should be fine.
Co-authored-by: DJMcNab <[email protected]>
Diffstat (limited to 'crates/tools')
-rw-r--r-- | crates/tools/src/bin/pre-commit.rs | 37 | ||||
-rw-r--r-- | crates/tools/src/lib.rs | 21 | ||||
-rw-r--r-- | crates/tools/src/main.rs | 17 |
3 files changed, 67 insertions, 8 deletions
diff --git a/crates/tools/src/bin/pre-commit.rs b/crates/tools/src/bin/pre-commit.rs new file mode 100644 index 000000000..ca1909479 --- /dev/null +++ b/crates/tools/src/bin/pre-commit.rs | |||
@@ -0,0 +1,37 @@ | |||
1 | use std::{ | ||
2 | process::{Command}, | ||
3 | }; | ||
4 | |||
5 | use tools::{Result, run_rustfmt, run, project_root}; | ||
6 | use failure::bail; | ||
7 | |||
8 | fn main() -> tools::Result<()> { | ||
9 | run_rustfmt(tools::Overwrite)?; | ||
10 | update_staged() | ||
11 | } | ||
12 | |||
13 | fn update_staged() -> Result<()> { | ||
14 | let root = project_root(); | ||
15 | let output = Command::new("git") | ||
16 | .arg("diff") | ||
17 | .arg("--name-only") | ||
18 | .arg("--cached") | ||
19 | .current_dir(&root) | ||
20 | .output()?; | ||
21 | if !output.status.success() { | ||
22 | bail!( | ||
23 | "`git diff --name-only --cached` exited with {}", | ||
24 | output.status | ||
25 | ); | ||
26 | } | ||
27 | for line in String::from_utf8(output.stdout)?.lines() { | ||
28 | run( | ||
29 | &format!( | ||
30 | "git update-index --add {}", | ||
31 | root.join(line).to_string_lossy() | ||
32 | ), | ||
33 | ".", | ||
34 | )?; | ||
35 | } | ||
36 | Ok(()) | ||
37 | } | ||
diff --git a/crates/tools/src/lib.rs b/crates/tools/src/lib.rs index 674b9d11f..95d6e08f0 100644 --- a/crates/tools/src/lib.rs +++ b/crates/tools/src/lib.rs | |||
@@ -1,6 +1,8 @@ | |||
1 | use std::{ | 1 | use std::{ |
2 | path::{Path, PathBuf}, | 2 | path::{Path, PathBuf}, |
3 | process::{Command, Stdio}, | 3 | process::{Command, Stdio}, |
4 | fs::copy, | ||
5 | io::{Error, ErrorKind} | ||
4 | }; | 6 | }; |
5 | 7 | ||
6 | use failure::bail; | 8 | use failure::bail; |
@@ -39,7 +41,7 @@ pub fn collect_tests(s: &str) -> Vec<(usize, Test)> { | |||
39 | let (start_line, name) = loop { | 41 | let (start_line, name) = loop { |
40 | match block.next() { | 42 | match block.next() { |
41 | Some((idx, line)) if line.starts_with("test ") => { | 43 | Some((idx, line)) if line.starts_with("test ") => { |
42 | break (idx, line["test ".len()..].to_string()) | 44 | break (idx, line["test ".len()..].to_string()); |
43 | } | 45 | } |
44 | Some(_) => (), | 46 | Some(_) => (), |
45 | None => continue 'outer, | 47 | None => continue 'outer, |
@@ -65,7 +67,7 @@ pub fn generate(mode: Mode) -> Result<()> { | |||
65 | } | 67 | } |
66 | 68 | ||
67 | pub fn project_root() -> PathBuf { | 69 | pub fn project_root() -> PathBuf { |
68 | Path::new(&std::env::var("CARGO_MANIFEST_DIR").unwrap()) | 70 | Path::new(&env!("CARGO_MANIFEST_DIR")) |
69 | .ancestors() | 71 | .ancestors() |
70 | .nth(2) | 72 | .nth(2) |
71 | .unwrap() | 73 | .unwrap() |
@@ -116,3 +118,18 @@ fn install_rustfmt() -> Result<()> { | |||
116 | ".", | 118 | ".", |
117 | ) | 119 | ) |
118 | } | 120 | } |
121 | |||
122 | pub fn install_format_hook() -> Result<()> { | ||
123 | let result_path = Path::new("./.git/hooks/pre-commit"); | ||
124 | if !result_path.exists() { | ||
125 | run("cargo build --package tools --bin pre-commit", ".")?; | ||
126 | if cfg!(windows) { | ||
127 | copy("./target/debug/pre-commit.exe", result_path)?; | ||
128 | } else { | ||
129 | copy("./target/debug/pre-commit", result_path)?; | ||
130 | } | ||
131 | } else { | ||
132 | return Err(Error::new(ErrorKind::AlreadyExists, "Git hook already created").into()); | ||
133 | } | ||
134 | Ok(()) | ||
135 | } | ||
diff --git a/crates/tools/src/main.rs b/crates/tools/src/main.rs index 36a7c83e2..9e90ac5c2 100644 --- a/crates/tools/src/main.rs +++ b/crates/tools/src/main.rs | |||
@@ -7,7 +7,7 @@ use std::{ | |||
7 | use clap::{App, Arg, SubCommand}; | 7 | use clap::{App, Arg, SubCommand}; |
8 | use failure::bail; | 8 | use failure::bail; |
9 | 9 | ||
10 | use tools::{collect_tests, generate, run, run_rustfmt, Mode, Overwrite, Result, Test, Verify}; | 10 | use tools::{collect_tests, generate, install_format_hook, run, run_rustfmt, Mode, Overwrite, Result, Test, Verify}; |
11 | 11 | ||
12 | const GRAMMAR_DIR: &str = "./crates/ra_syntax/src/grammar"; | 12 | const GRAMMAR_DIR: &str = "./crates/ra_syntax/src/grammar"; |
13 | const INLINE_TESTS_DIR: &str = "./crates/ra_syntax/tests/data/parser/inline"; | 13 | const INLINE_TESTS_DIR: &str = "./crates/ra_syntax/tests/data/parser/inline"; |
@@ -25,17 +25,22 @@ fn main() -> Result<()> { | |||
25 | .subcommand(SubCommand::with_name("gen-tests")) | 25 | .subcommand(SubCommand::with_name("gen-tests")) |
26 | .subcommand(SubCommand::with_name("install-code")) | 26 | .subcommand(SubCommand::with_name("install-code")) |
27 | .subcommand(SubCommand::with_name("format")) | 27 | .subcommand(SubCommand::with_name("format")) |
28 | .subcommand(SubCommand::with_name("format-hook")) | ||
28 | .get_matches(); | 29 | .get_matches(); |
29 | let mode = if matches.is_present("verify") { | 30 | let mode = if matches.is_present("verify") { |
30 | Verify | 31 | Verify |
31 | } else { | 32 | } else { |
32 | Overwrite | 33 | Overwrite |
33 | }; | 34 | }; |
34 | match matches.subcommand() { | 35 | match matches |
35 | ("install-code", _) => install_code_extension()?, | 36 | .subcommand_name() |
36 | ("gen-tests", _) => gen_tests(mode)?, | 37 | .expect("Subcommand must be specified") |
37 | ("gen-syntax", _) => generate(Overwrite)?, | 38 | { |
38 | ("format", _) => run_rustfmt(Overwrite)?, | 39 | "install-code" => install_code_extension()?, |
40 | "gen-tests" => gen_tests(mode)?, | ||
41 | "gen-syntax" => generate(Overwrite)?, | ||
42 | "format" => run_rustfmt(mode)?, | ||
43 | "format-hook" => install_format_hook()?, | ||
39 | _ => unreachable!(), | 44 | _ => unreachable!(), |
40 | } | 45 | } |
41 | Ok(()) | 46 | Ok(()) |