From 91f9bc2b866fc87dbaadb29a8c72eba10ae57c5c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 7 Jan 2020 14:42:56 +0100 Subject: Refactor xtasks --- xtask/src/pre_commit.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 xtask/src/pre_commit.rs (limited to 'xtask/src/pre_commit.rs') diff --git a/xtask/src/pre_commit.rs b/xtask/src/pre_commit.rs new file mode 100644 index 000000000..7984ba963 --- /dev/null +++ b/xtask/src/pre_commit.rs @@ -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(()) +} -- cgit v1.2.3 From 5e7995eeb7b7ab4cf0d80ddfa2d20e506216f895 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 7 Jan 2020 15:33:51 +0100 Subject: Cleanup --- xtask/src/pre_commit.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'xtask/src/pre_commit.rs') diff --git a/xtask/src/pre_commit.rs b/xtask/src/pre_commit.rs index 7984ba963..88e868ca6 100644 --- a/xtask/src/pre_commit.rs +++ b/xtask/src/pre_commit.rs @@ -4,7 +4,7 @@ use std::{fs, path::PathBuf}; use anyhow::{bail, Result}; -use crate::{project_root, run, run_rustfmt, run_with_output, Mode}; +use crate::{cmd::run_with_output, project_root, run, run_rustfmt, Mode}; // FIXME: if there are changed `.ts` files, also reformat TypeScript (by // shelling out to `npm fmt`). -- cgit v1.2.3