2301: Don't create a separate bin for format hook r=matklad a=matklad



Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2019-11-18 12:41:59 +00:00 committed by GitHub
commit 789a0d2a64
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 36 additions and 48 deletions

View File

@ -55,7 +55,7 @@ We use Travis for CI. Most of the things, including formatting, are checked by
be green as well. We use bors-ng to enforce the [not rocket
science](https://graydon2.dreamwidth.org/1597.html) rule.
You can run `cargo format-hook` to install git-hook to run rustfmt on commit.
You can run `cargo xtask install-pre-commit-hook` to install git-hook to run rustfmt on commit.
# Code organization

View File

@ -1,31 +0,0 @@
//! FIXME: write short doc here
use std::process::Command;
use xtask::{codegen::Mode, project_root, run, run_rustfmt, Result};
fn main() -> Result<()> {
run_rustfmt(Mode::Overwrite)?;
update_staged()
}
fn update_staged() -> Result<()> {
let root = project_root();
let output = Command::new("git")
.arg("diff")
.arg("--diff-filter=MAR")
.arg("--name-only")
.arg("--cached")
.current_dir(&root)
.output()?;
if !output.status.success() {
anyhow::bail!(
"`git diff --diff-filter=MAR --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

@ -10,7 +10,7 @@
SUBCOMMANDS:
format
format-hook
install-pre-commit-hook
fuzz-tests
codegen
install

View File

@ -83,19 +83,12 @@ pub fn install_rustfmt() -> Result<()> {
run(&format!("rustup component add rustfmt --toolchain {}", TOOLCHAIN), ".")
}
pub fn install_format_hook() -> Result<()> {
let result_path = Path::new(if cfg!(windows) {
"./.git/hooks/pre-commit.exe"
} else {
"./.git/hooks/pre-commit"
});
pub fn install_pre_commit_hook() -> Result<()> {
let result_path =
PathBuf::from(format!("./.git/hooks/pre-commit{}", std::env::consts::EXE_SUFFIX));
if !result_path.exists() {
run("cargo build --package xtask --bin pre-commit", ".")?;
if cfg!(windows) {
fs::copy("./target/debug/pre-commit.exe", result_path)?;
} else {
fs::copy("./target/debug/pre-commit", result_path)?;
}
let me = std::env::current_exe()?;
fs::copy(me, result_path)?;
} else {
Err(IoError::new(ErrorKind::AlreadyExists, "Git hook already created"))?;
}
@ -150,6 +143,27 @@ pub fn run_fuzzer() -> Result<()> {
run("rustup run nightly -- cargo fuzz run parser", "./crates/ra_syntax")
}
pub fn reformat_staged_files() -> Result<()> {
let root = project_root();
let output = Command::new("git")
.arg("diff")
.arg("--diff-filter=MAR")
.arg("--name-only")
.arg("--cached")
.current_dir(&root)
.output()?;
if !output.status.success() {
anyhow::bail!(
"`git diff --diff-filter=MAR --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(())
}
fn do_run<F>(cmdline: &str, dir: &str, mut f: F) -> Result<Output>
where
F: FnMut(&mut Command),

View File

@ -16,7 +16,8 @@
use std::{env, path::PathBuf};
use xtask::{
codegen::{self, Mode},
install_format_hook, run, run_clippy, run_fuzzer, run_rustfmt, run_with_output, Cmd, Result,
install_pre_commit_hook, reformat_staged_files, run, run_clippy, run_fuzzer, run_rustfmt,
run_with_output, Cmd, Result,
};
// Latest stable, feel free to send a PR if this lags behind.
@ -36,6 +37,10 @@ struct ServerOpt {
}
fn main() -> Result<()> {
if std::env::args().next().map(|it| it.contains("pre-commit")) == Some(true) {
return reformat_staged_files();
}
let subcommand = match std::env::args_os().nth(1) {
None => {
eprintln!("{}", help::GLOBAL_HELP);
@ -81,12 +86,12 @@ fn main() -> Result<()> {
}
run_rustfmt(Mode::Overwrite)?
}
"format-hook" => {
"install-pre-commit-hook" => {
if matches.contains(["-h", "--help"]) {
help::print_no_param_subcommand_help(&subcommand);
return Ok(());
}
install_format_hook()?
install_pre_commit_hook()?
}
"lint" => {
if matches.contains(["-h", "--help"]) {