From cbce28a348ebb5db646cfc5cd3305c6bce80e915 Mon Sep 17 00:00:00 2001 From: DJMcNab <36049421+DJMcNab@users.noreply.github.com> Date: Sun, 9 Dec 2018 12:27:13 +0000 Subject: Reimplement format-hook using a rust binary --- .cargo/config | 10 +++++----- crates/tools/src/bin/pre-commit.rs | 37 +++++++++++++++++++++++++++++++++++++ crates/tools/src/lib.rs | 32 ++++++++++---------------------- 3 files changed, 52 insertions(+), 27 deletions(-) create mode 100644 crates/tools/src/bin/pre-commit.rs diff --git a/.cargo/config b/.cargo/config index afb569b75..c319d33f2 100644 --- a/.cargo/config +++ b/.cargo/config @@ -1,10 +1,10 @@ [alias] # Automatically generates the ast and syntax kinds files -gen-syntax = "run --package tools -- gen-syntax" -gen-tests = "run --package tools -- gen-tests" -install-code = "run --package tools -- install-code" -format = "run --package tools -- format" -format-hook = "run --package tools -- format-hook" +gen-syntax = "run --package tools --bin tools -- gen-syntax" +gen-tests = "run --package tools --bin tools -- gen-tests" +install-code = "run --package tools --bin tools -- install-code" +format = "run --package tools --bin tools -- format" +format-hook = "run --package tools --bin tools -- format-hook" render-test = "run --package ra_cli -- render-test" parse = "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 @@ +use std::{ + process::{Command}, +}; + +use tools::{Result, run_rustfmt, run, project_root}; +use failure::bail; + +fn main() -> tools::Result<()> { + run_rustfmt(tools::Overwrite)?; + update_staged() +} + +fn update_staged() -> Result<()> { + let root = project_root(); + let output = Command::new("git") + .arg("diff") + .arg("--name-only") + .arg("--cached") + .current_dir(&root) + .output()?; + if !output.status.success() { + bail!( + "`git diff --name-only --cached` exited with {}", + output.status + ); + } + for line in String::from_utf8(output.stdout)?.lines() { + run( + &format!( + "git update-index --add {}", + root.join(line).to_string_lossy() + ), + ".", + )?; + } + Ok(()) +} 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 @@ use std::{ path::{Path, PathBuf}, process::{Command, Stdio}, - fs::OpenOptions, - io::{Write, Error, ErrorKind} + fs::copy, + io::{Error, ErrorKind} }; -#[cfg(unix)] -use std::os::unix::fs::OpenOptionsExt; use failure::bail; use itertools::Itertools; @@ -69,7 +67,7 @@ pub fn generate(mode: Mode) -> Result<()> { } pub fn project_root() -> PathBuf { - Path::new(&std::env::var("CARGO_MANIFEST_DIR").unwrap()) + Path::new(&env!("CARGO_MANIFEST_DIR")) .ancestors() .nth(2) .unwrap() @@ -122,24 +120,14 @@ fn install_rustfmt() -> Result<()> { } pub fn install_format_hook() -> Result<()> { - let path = Path::new("./.git/hooks/pre-commit"); - if !path.exists() { - let mut open_options = OpenOptions::new(); - #[cfg(unix)] - { - // Set as executable - open_options.mode(0o770); + let result_path = Path::new("./.git/hooks/pre-commit"); + if !result_path.exists() { + run("cargo build --package tools --bin pre-commit", ".")?; + if cfg!(windows) { + copy("./target/debug/pre-commit.exe", result_path)?; + } else { + copy("./target/debug/pre-commit", result_path)?; } - let mut file = open_options.write(true).create(true).open(path)?; - write!( - file, - r#"#!/bin/sh - -cargo format -for path in $( git diff --name-only --cached ); do - git update-index --add $path -done"# - )?; } else { return Err(Error::new(ErrorKind::AlreadyExists, "Git hook already created").into()); } -- cgit v1.2.3