mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-11-10 07:04:22 +00:00
Reimplement format-hook using a rust binary
This commit is contained in:
parent
1e554d551f
commit
cbce28a348
3 changed files with 52 additions and 27 deletions
|
@ -1,10 +1,10 @@
|
|||
[alias]
|
||||
# Automatically generates the ast and syntax kinds files
|
||||
gen-syntax = "run --package tools -- gen-syntax"
|
||||
gen-tests = "run --package tools -- gen-tests"
|
||||
install-code = "run --package tools -- install-code"
|
||||
format = "run --package tools -- format"
|
||||
format-hook = "run --package tools -- format-hook"
|
||||
gen-syntax = "run --package tools --bin tools -- gen-syntax"
|
||||
gen-tests = "run --package tools --bin tools -- gen-tests"
|
||||
install-code = "run --package tools --bin tools -- install-code"
|
||||
format = "run --package tools --bin tools -- format"
|
||||
format-hook = "run --package tools --bin tools -- format-hook"
|
||||
|
||||
render-test = "run --package ra_cli -- render-test"
|
||||
parse = "run --package ra_cli -- parse"
|
||||
|
|
37
crates/tools/src/bin/pre-commit.rs
Normal file
37
crates/tools/src/bin/pre-commit.rs
Normal file
|
@ -0,0 +1,37 @@
|
|||
use std::{
|
||||
process::{Command},
|
||||
};
|
||||
|
||||
use tools::{Result, run_rustfmt, run, project_root};
|
||||
use failure::bail;
|
||||
|
||||
fn main() -> tools::Result<()> {
|
||||
run_rustfmt(tools::Overwrite)?;
|
||||
update_staged()
|
||||
}
|
||||
|
||||
fn update_staged() -> Result<()> {
|
||||
let root = project_root();
|
||||
let output = Command::new("git")
|
||||
.arg("diff")
|
||||
.arg("--name-only")
|
||||
.arg("--cached")
|
||||
.current_dir(&root)
|
||||
.output()?;
|
||||
if !output.status.success() {
|
||||
bail!(
|
||||
"`git diff --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(())
|
||||
}
|
|
@ -1,11 +1,9 @@
|
|||
use std::{
|
||||
path::{Path, PathBuf},
|
||||
process::{Command, Stdio},
|
||||
fs::OpenOptions,
|
||||
io::{Write, Error, ErrorKind}
|
||||
fs::copy,
|
||||
io::{Error, ErrorKind}
|
||||
};
|
||||
#[cfg(unix)]
|
||||
use std::os::unix::fs::OpenOptionsExt;
|
||||
|
||||
use failure::bail;
|
||||
use itertools::Itertools;
|
||||
|
@ -69,7 +67,7 @@ pub fn generate(mode: Mode) -> Result<()> {
|
|||
}
|
||||
|
||||
pub fn project_root() -> PathBuf {
|
||||
Path::new(&std::env::var("CARGO_MANIFEST_DIR").unwrap())
|
||||
Path::new(&env!("CARGO_MANIFEST_DIR"))
|
||||
.ancestors()
|
||||
.nth(2)
|
||||
.unwrap()
|
||||
|
@ -122,24 +120,14 @@ fn install_rustfmt() -> Result<()> {
|
|||
}
|
||||
|
||||
pub fn install_format_hook() -> Result<()> {
|
||||
let path = Path::new("./.git/hooks/pre-commit");
|
||||
if !path.exists() {
|
||||
let mut open_options = OpenOptions::new();
|
||||
#[cfg(unix)]
|
||||
{
|
||||
// Set as executable
|
||||
open_options.mode(0o770);
|
||||
let result_path = Path::new("./.git/hooks/pre-commit");
|
||||
if !result_path.exists() {
|
||||
run("cargo build --package tools --bin pre-commit", ".")?;
|
||||
if cfg!(windows) {
|
||||
copy("./target/debug/pre-commit.exe", result_path)?;
|
||||
} else {
|
||||
copy("./target/debug/pre-commit", result_path)?;
|
||||
}
|
||||
let mut file = open_options.write(true).create(true).open(path)?;
|
||||
write!(
|
||||
file,
|
||||
r#"#!/bin/sh
|
||||
|
||||
cargo format
|
||||
for path in $( git diff --name-only --cached ); do
|
||||
git update-index --add $path
|
||||
done"#
|
||||
)?;
|
||||
} else {
|
||||
return Err(Error::new(ErrorKind::AlreadyExists, "Git hook already created").into());
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue