diff options
author | DJMcNab <[email protected]> | 2018-12-09 10:29:13 +0000 |
---|---|---|
committer | DJMcNab <[email protected]> | 2018-12-09 10:29:13 +0000 |
commit | e823db06985d6782ff3803d3a4dea67a3c18426c (patch) | |
tree | b8377d95bdb62db3553d7bf3107c8c56052861a9 /crates/tools/src | |
parent | 904438e993b4cc3c1d9269a44436c1b112de16c0 (diff) |
Implement and test format hook
Diffstat (limited to 'crates/tools/src')
-rw-r--r-- | crates/tools/src/lib.rs | 29 | ||||
-rw-r--r-- | crates/tools/src/main.rs | 17 |
2 files changed, 39 insertions, 7 deletions
diff --git a/crates/tools/src/lib.rs b/crates/tools/src/lib.rs index 674b9d11f..d6c448f3b 100644 --- a/crates/tools/src/lib.rs +++ b/crates/tools/src/lib.rs | |||
@@ -1,7 +1,11 @@ | |||
1 | use std::{ | 1 | use std::{ |
2 | path::{Path, PathBuf}, | 2 | path::{Path, PathBuf}, |
3 | process::{Command, Stdio}, | 3 | process::{Command, Stdio}, |
4 | fs::OpenOptions, | ||
5 | io::{Write, Error, ErrorKind} | ||
4 | }; | 6 | }; |
7 | #[cfg(unix)] | ||
8 | use std::os::unix::fs::OpenOptionsExt; | ||
5 | 9 | ||
6 | use failure::bail; | 10 | use failure::bail; |
7 | use itertools::Itertools; | 11 | use itertools::Itertools; |
@@ -39,7 +43,7 @@ pub fn collect_tests(s: &str) -> Vec<(usize, Test)> { | |||
39 | let (start_line, name) = loop { | 43 | let (start_line, name) = loop { |
40 | match block.next() { | 44 | match block.next() { |
41 | Some((idx, line)) if line.starts_with("test ") => { | 45 | Some((idx, line)) if line.starts_with("test ") => { |
42 | break (idx, line["test ".len()..].to_string()) | 46 | break (idx, line["test ".len()..].to_string()); |
43 | } | 47 | } |
44 | Some(_) => (), | 48 | Some(_) => (), |
45 | None => continue 'outer, | 49 | None => continue 'outer, |
@@ -116,3 +120,26 @@ fn install_rustfmt() -> Result<()> { | |||
116 | ".", | 120 | ".", |
117 | ) | 121 | ) |
118 | } | 122 | } |
123 | |||
124 | pub fn install_format_hook() -> Result<()> { | ||
125 | let path = Path::new("./.git/hooks/pre-commit"); | ||
126 | if !path.exists() { | ||
127 | let mut open_options = OpenOptions::new(); | ||
128 | #[cfg(unix)] | ||
129 | { | ||
130 | // Set as executable | ||
131 | open_options.mode(0o770); | ||
132 | } | ||
133 | let mut file = open_options.write(true).create(true).open(path)?; | ||
134 | write!( | ||
135 | file, | ||
136 | r#"#!/bin/sh | ||
137 | |||
138 | cargo format | ||
139 | git update-index --add ."# | ||
140 | )?; | ||
141 | } else { | ||
142 | return Err(Error::new(ErrorKind::AlreadyExists, "Git hook already created").into()); | ||
143 | } | ||
144 | Ok(()) | ||
145 | } | ||
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(()) |