Refactor xtasks

This commit is contained in:
Aleksey Kladov 2020-01-07 14:42:56 +01:00
parent b02576d562
commit 91f9bc2b86
5 changed files with 288 additions and 279 deletions

36
xtask/src/pre_commit.rs Normal file
View file

@ -0,0 +1,36 @@
//! pre-commit hook for code formatting.
use std::{fs, path::PathBuf};
use anyhow::{bail, Result};
use crate::{project_root, run, run_rustfmt, run_with_output, Mode};
// FIXME: if there are changed `.ts` files, also reformat TypeScript (by
// shelling out to `npm fmt`).
pub fn run_hook() -> Result<()> {
run_rustfmt(Mode::Overwrite)?;
let diff = run_with_output("git diff --diff-filter=MAR --name-only --cached", ".")?;
let root = project_root();
for line in String::from_utf8(diff.stdout)?.lines() {
run(&format!("git update-index --add {}", root.join(line).to_string_lossy()), ".")?;
}
Ok(())
}
pub fn install_hook() -> Result<()> {
let hook_path: PathBuf =
format!("./.git/hooks/pre-commit{}", std::env::consts::EXE_SUFFIX).into();
if hook_path.exists() {
bail!("Git hook already created");
}
let me = std::env::current_exe()?;
fs::copy(me, hook_path)?;
Ok(())
}