Reimplement format-hook using a rust binary

This commit is contained in:
DJMcNab 2018-12-09 12:27:13 +00:00
parent 1e554d551f
commit cbce28a348
3 changed files with 52 additions and 27 deletions

View File

@ -1,10 +1,10 @@
[alias] [alias]
# Automatically generates the ast and syntax kinds files # Automatically generates the ast and syntax kinds files
gen-syntax = "run --package tools -- gen-syntax" gen-syntax = "run --package tools --bin tools -- gen-syntax"
gen-tests = "run --package tools -- gen-tests" gen-tests = "run --package tools --bin tools -- gen-tests"
install-code = "run --package tools -- install-code" install-code = "run --package tools --bin tools -- install-code"
format = "run --package tools -- format" format = "run --package tools --bin tools -- format"
format-hook = "run --package tools -- format-hook" format-hook = "run --package tools --bin tools -- format-hook"
render-test = "run --package ra_cli -- render-test" render-test = "run --package ra_cli -- render-test"
parse = "run --package ra_cli -- parse" parse = "run --package ra_cli -- parse"

View File

@ -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(())
}

View File

@ -1,11 +1,9 @@
use std::{ use std::{
path::{Path, PathBuf}, path::{Path, PathBuf},
process::{Command, Stdio}, process::{Command, Stdio},
fs::OpenOptions, fs::copy,
io::{Write, Error, ErrorKind} io::{Error, ErrorKind}
}; };
#[cfg(unix)]
use std::os::unix::fs::OpenOptionsExt;
use failure::bail; use failure::bail;
use itertools::Itertools; use itertools::Itertools;
@ -69,7 +67,7 @@ pub fn generate(mode: Mode) -> Result<()> {
} }
pub fn project_root() -> PathBuf { pub fn project_root() -> PathBuf {
Path::new(&std::env::var("CARGO_MANIFEST_DIR").unwrap()) Path::new(&env!("CARGO_MANIFEST_DIR"))
.ancestors() .ancestors()
.nth(2) .nth(2)
.unwrap() .unwrap()
@ -122,24 +120,14 @@ fn install_rustfmt() -> Result<()> {
} }
pub fn install_format_hook() -> Result<()> { pub fn install_format_hook() -> Result<()> {
let path = Path::new("./.git/hooks/pre-commit"); let result_path = Path::new("./.git/hooks/pre-commit");
if !path.exists() { if !result_path.exists() {
let mut open_options = OpenOptions::new(); run("cargo build --package tools --bin pre-commit", ".")?;
#[cfg(unix)] if cfg!(windows) {
{ copy("./target/debug/pre-commit.exe", result_path)?;
// Set as executable } else {
open_options.mode(0o770); 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 { } else {
return Err(Error::new(ErrorKind::AlreadyExists, "Git hook already created").into()); return Err(Error::new(ErrorKind::AlreadyExists, "Git hook already created").into());
} }