aboutsummaryrefslogtreecommitdiff
path: root/crates/tools/src/lib.rs
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2018-12-09 12:44:58 +0000
committerbors[bot] <bors[bot]@users.noreply.github.com>2018-12-09 12:44:58 +0000
commit3725276554fe63fdd9b63a10b86e794b3eb73158 (patch)
tree01de23f215e51459c67d2eb7f4bfb77d65afd1b7 /crates/tools/src/lib.rs
parentb9c17a6001aaf79e181a86218883fe96d9c95c09 (diff)
parentcbce28a348ebb5db646cfc5cd3305c6bce80e915 (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/src/lib.rs')
-rw-r--r--crates/tools/src/lib.rs21
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 @@
1use std::{ 1use 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
6use failure::bail; 8use 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
67pub fn project_root() -> PathBuf { 69pub 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
122pub 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}