aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDJMcNab <[email protected]>2018-12-09 12:27:13 +0000
committerDJMcNab <[email protected]>2018-12-09 12:27:13 +0000
commitcbce28a348ebb5db646cfc5cd3305c6bce80e915 (patch)
treee60851cedc361353b6ca4974dacdc97c8cd08140
parent1e554d551f1510780377158fda8d86ff8c8266fe (diff)
Reimplement format-hook using a rust binary
-rw-r--r--.cargo/config10
-rw-r--r--crates/tools/src/bin/pre-commit.rs37
-rw-r--r--crates/tools/src/lib.rs32
3 files changed, 52 insertions, 27 deletions
diff --git a/.cargo/config b/.cargo/config
index afb569b75..c319d33f2 100644
--- a/.cargo/config
+++ b/.cargo/config
@@ -1,10 +1,10 @@
1[alias] 1[alias]
2# Automatically generates the ast and syntax kinds files 2# Automatically generates the ast and syntax kinds files
3gen-syntax = "run --package tools -- gen-syntax" 3gen-syntax = "run --package tools --bin tools -- gen-syntax"
4gen-tests = "run --package tools -- gen-tests" 4gen-tests = "run --package tools --bin tools -- gen-tests"
5install-code = "run --package tools -- install-code" 5install-code = "run --package tools --bin tools -- install-code"
6format = "run --package tools -- format" 6format = "run --package tools --bin tools -- format"
7format-hook = "run --package tools -- format-hook" 7format-hook = "run --package tools --bin tools -- format-hook"
8 8
9render-test = "run --package ra_cli -- render-test" 9render-test = "run --package ra_cli -- render-test"
10parse = "run --package ra_cli -- parse" 10parse = "run --package ra_cli -- parse"
diff --git a/crates/tools/src/bin/pre-commit.rs b/crates/tools/src/bin/pre-commit.rs
new file mode 100644
index 000000000..ca1909479
--- /dev/null
+++ b/crates/tools/src/bin/pre-commit.rs
@@ -0,0 +1,37 @@
1use std::{
2 process::{Command},
3};
4
5use tools::{Result, run_rustfmt, run, project_root};
6use failure::bail;
7
8fn main() -> tools::Result<()> {
9 run_rustfmt(tools::Overwrite)?;
10 update_staged()
11}
12
13fn update_staged() -> Result<()> {
14 let root = project_root();
15 let output = Command::new("git")
16 .arg("diff")
17 .arg("--name-only")
18 .arg("--cached")
19 .current_dir(&root)
20 .output()?;
21 if !output.status.success() {
22 bail!(
23 "`git diff --name-only --cached` exited with {}",
24 output.status
25 );
26 }
27 for line in String::from_utf8(output.stdout)?.lines() {
28 run(
29 &format!(
30 "git update-index --add {}",
31 root.join(line).to_string_lossy()
32 ),
33 ".",
34 )?;
35 }
36 Ok(())
37}
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 @@
1use std::{ 1use 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)]
8use std::os::unix::fs::OpenOptionsExt;
9 7
10use failure::bail; 8use failure::bail;
11use itertools::Itertools; 9use itertools::Itertools;
@@ -69,7 +67,7 @@ pub fn generate(mode: Mode) -> Result<()> {
69} 67}
70 68
71pub fn project_root() -> PathBuf { 69pub 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
124pub fn install_format_hook() -> Result<()> { 122pub 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
138cargo format
139for path in $( git diff --name-only --cached ); do
140 git update-index --add $path
141done"#
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 }