mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-25 12:33:33 +00:00
Don't create a separate bin for format hook
This commit is contained in:
parent
b79d678923
commit
76da22e66a
5 changed files with 36 additions and 48 deletions
|
@ -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
|
||||
|
||||
|
|
|
@ -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(())
|
||||
}
|
|
@ -10,7 +10,7 @@ FLAGS:
|
|||
|
||||
SUBCOMMANDS:
|
||||
format
|
||||
format-hook
|
||||
install-pre-commit-hook
|
||||
fuzz-tests
|
||||
codegen
|
||||
install
|
||||
|
|
|
@ -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),
|
||||
|
|
|
@ -16,7 +16,8 @@ use pico_args::Arguments;
|
|||
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"]) {
|
||||
|
|
Loading…
Reference in a new issue