diff options
Diffstat (limited to 'crates/tools/src/lib.rs')
-rw-r--r-- | crates/tools/src/lib.rs | 21 |
1 files changed, 19 insertions, 2 deletions
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 | } | ||