diff options
author | DJMcNab <[email protected]> | 2018-12-09 12:27:13 +0000 |
---|---|---|
committer | DJMcNab <[email protected]> | 2018-12-09 12:27:13 +0000 |
commit | cbce28a348ebb5db646cfc5cd3305c6bce80e915 (patch) | |
tree | e60851cedc361353b6ca4974dacdc97c8cd08140 /crates/tools/src/lib.rs | |
parent | 1e554d551f1510780377158fda8d86ff8c8266fe (diff) |
Reimplement format-hook using a rust binary
Diffstat (limited to 'crates/tools/src/lib.rs')
-rw-r--r-- | crates/tools/src/lib.rs | 32 |
1 files changed, 10 insertions, 22 deletions
diff --git a/crates/tools/src/lib.rs b/crates/tools/src/lib.rs index bc550c597..95d6e08f0 100644 --- a/crates/tools/src/lib.rs +++ b/crates/tools/src/lib.rs | |||
@@ -1,11 +1,9 @@ | |||
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, | 4 | fs::copy, |
5 | io::{Write, Error, ErrorKind} | 5 | io::{Error, ErrorKind} |
6 | }; | 6 | }; |
7 | #[cfg(unix)] | ||
8 | use std::os::unix::fs::OpenOptionsExt; | ||
9 | 7 | ||
10 | use failure::bail; | 8 | use failure::bail; |
11 | use itertools::Itertools; | 9 | use itertools::Itertools; |
@@ -69,7 +67,7 @@ pub fn generate(mode: Mode) -> Result<()> { | |||
69 | } | 67 | } |
70 | 68 | ||
71 | pub fn project_root() -> PathBuf { | 69 | pub fn project_root() -> PathBuf { |
72 | Path::new(&std::env::var("CARGO_MANIFEST_DIR").unwrap()) | 70 | Path::new(&env!("CARGO_MANIFEST_DIR")) |
73 | .ancestors() | 71 | .ancestors() |
74 | .nth(2) | 72 | .nth(2) |
75 | .unwrap() | 73 | .unwrap() |
@@ -122,24 +120,14 @@ fn install_rustfmt() -> Result<()> { | |||
122 | } | 120 | } |
123 | 121 | ||
124 | pub fn install_format_hook() -> Result<()> { | 122 | pub fn install_format_hook() -> Result<()> { |
125 | let path = Path::new("./.git/hooks/pre-commit"); | 123 | let result_path = Path::new("./.git/hooks/pre-commit"); |
126 | if !path.exists() { | 124 | if !result_path.exists() { |
127 | let mut open_options = OpenOptions::new(); | 125 | run("cargo build --package tools --bin pre-commit", ".")?; |
128 | #[cfg(unix)] | 126 | if cfg!(windows) { |
129 | { | 127 | copy("./target/debug/pre-commit.exe", result_path)?; |
130 | // Set as executable | 128 | } else { |
131 | open_options.mode(0o770); | 129 | copy("./target/debug/pre-commit", result_path)?; |
132 | } | 130 | } |
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 | for path in $( git diff --name-only --cached ); do | ||
140 | git update-index --add $path | ||
141 | done"# | ||
142 | )?; | ||
143 | } else { | 131 | } else { |
144 | return Err(Error::new(ErrorKind::AlreadyExists, "Git hook already created").into()); | 132 | return Err(Error::new(ErrorKind::AlreadyExists, "Git hook already created").into()); |
145 | } | 133 | } |