mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-26 04:53:34 +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
|
be green as well. We use bors-ng to enforce the [not rocket
|
||||||
science](https://graydon2.dreamwidth.org/1597.html) rule.
|
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
|
# 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:
|
SUBCOMMANDS:
|
||||||
format
|
format
|
||||||
format-hook
|
install-pre-commit-hook
|
||||||
fuzz-tests
|
fuzz-tests
|
||||||
codegen
|
codegen
|
||||||
install
|
install
|
||||||
|
|
|
@ -83,19 +83,12 @@ pub fn install_rustfmt() -> Result<()> {
|
||||||
run(&format!("rustup component add rustfmt --toolchain {}", TOOLCHAIN), ".")
|
run(&format!("rustup component add rustfmt --toolchain {}", TOOLCHAIN), ".")
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn install_format_hook() -> Result<()> {
|
pub fn install_pre_commit_hook() -> Result<()> {
|
||||||
let result_path = Path::new(if cfg!(windows) {
|
let result_path =
|
||||||
"./.git/hooks/pre-commit.exe"
|
PathBuf::from(format!("./.git/hooks/pre-commit{}", std::env::consts::EXE_SUFFIX));
|
||||||
} else {
|
|
||||||
"./.git/hooks/pre-commit"
|
|
||||||
});
|
|
||||||
if !result_path.exists() {
|
if !result_path.exists() {
|
||||||
run("cargo build --package xtask --bin pre-commit", ".")?;
|
let me = std::env::current_exe()?;
|
||||||
if cfg!(windows) {
|
fs::copy(me, result_path)?;
|
||||||
fs::copy("./target/debug/pre-commit.exe", result_path)?;
|
|
||||||
} else {
|
|
||||||
fs::copy("./target/debug/pre-commit", result_path)?;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
Err(IoError::new(ErrorKind::AlreadyExists, "Git hook already created"))?;
|
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")
|
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>
|
fn do_run<F>(cmdline: &str, dir: &str, mut f: F) -> Result<Output>
|
||||||
where
|
where
|
||||||
F: FnMut(&mut Command),
|
F: FnMut(&mut Command),
|
||||||
|
|
|
@ -16,7 +16,8 @@ use pico_args::Arguments;
|
||||||
use std::{env, path::PathBuf};
|
use std::{env, path::PathBuf};
|
||||||
use xtask::{
|
use xtask::{
|
||||||
codegen::{self, Mode},
|
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.
|
// Latest stable, feel free to send a PR if this lags behind.
|
||||||
|
@ -36,6 +37,10 @@ struct ServerOpt {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> Result<()> {
|
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) {
|
let subcommand = match std::env::args_os().nth(1) {
|
||||||
None => {
|
None => {
|
||||||
eprintln!("{}", help::GLOBAL_HELP);
|
eprintln!("{}", help::GLOBAL_HELP);
|
||||||
|
@ -81,12 +86,12 @@ fn main() -> Result<()> {
|
||||||
}
|
}
|
||||||
run_rustfmt(Mode::Overwrite)?
|
run_rustfmt(Mode::Overwrite)?
|
||||||
}
|
}
|
||||||
"format-hook" => {
|
"install-pre-commit-hook" => {
|
||||||
if matches.contains(["-h", "--help"]) {
|
if matches.contains(["-h", "--help"]) {
|
||||||
help::print_no_param_subcommand_help(&subcommand);
|
help::print_no_param_subcommand_help(&subcommand);
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
install_format_hook()?
|
install_pre_commit_hook()?
|
||||||
}
|
}
|
||||||
"lint" => {
|
"lint" => {
|
||||||
if matches.contains(["-h", "--help"]) {
|
if matches.contains(["-h", "--help"]) {
|
||||||
|
|
Loading…
Reference in a new issue