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