aboutsummaryrefslogtreecommitdiff
path: root/xtask
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-11-18 12:22:51 +0000
committerAleksey Kladov <[email protected]>2019-11-18 12:27:04 +0000
commit76da22e66aaccda4a428e41e233ef7f3732463fd (patch)
tree4448743e0fff2822c3f6bde607e4a97374441be3 /xtask
parentb79d6789236bb53c5818949cc2960b5c4991cbeb (diff)
Don't create a separate bin for format hook
Diffstat (limited to 'xtask')
-rw-r--r--xtask/src/bin/pre-commit.rs31
-rw-r--r--xtask/src/help.rs2
-rw-r--r--xtask/src/lib.rs38
-rw-r--r--xtask/src/main.rs11
4 files changed, 35 insertions, 47 deletions
diff --git a/xtask/src/bin/pre-commit.rs b/xtask/src/bin/pre-commit.rs
deleted file mode 100644
index 44507fb74..000000000
--- a/xtask/src/bin/pre-commit.rs
+++ /dev/null
@@ -1,31 +0,0 @@
1//! FIXME: write short doc here
2
3use std::process::Command;
4
5use xtask::{codegen::Mode, project_root, run, run_rustfmt, Result};
6
7fn main() -> Result<()> {
8 run_rustfmt(Mode::Overwrite)?;
9 update_staged()
10}
11
12fn update_staged() -> Result<()> {
13 let root = project_root();
14 let output = Command::new("git")
15 .arg("diff")
16 .arg("--diff-filter=MAR")
17 .arg("--name-only")
18 .arg("--cached")
19 .current_dir(&root)
20 .output()?;
21 if !output.status.success() {
22 anyhow::bail!(
23 "`git diff --diff-filter=MAR --name-only --cached` exited with {}",
24 output.status
25 );
26 }
27 for line in String::from_utf8(output.stdout)?.lines() {
28 run(&format!("git update-index --add {}", root.join(line).to_string_lossy()), ".")?;
29 }
30 Ok(())
31}
diff --git a/xtask/src/help.rs b/xtask/src/help.rs
index 730eb5c61..f4e25dcde 100644
--- a/xtask/src/help.rs
+++ b/xtask/src/help.rs
@@ -10,7 +10,7 @@ FLAGS:
10 10
11SUBCOMMANDS: 11SUBCOMMANDS:
12 format 12 format
13 format-hook 13 install-pre-commit-hook
14 fuzz-tests 14 fuzz-tests
15 codegen 15 codegen
16 install 16 install
diff --git a/xtask/src/lib.rs b/xtask/src/lib.rs
index bfee2f9c8..7332a4072 100644
--- a/xtask/src/lib.rs
+++ b/xtask/src/lib.rs
@@ -83,19 +83,12 @@ pub fn install_rustfmt() -> Result<()> {
83 run(&format!("rustup component add rustfmt --toolchain {}", TOOLCHAIN), ".") 83 run(&format!("rustup component add rustfmt --toolchain {}", TOOLCHAIN), ".")
84} 84}
85 85
86pub fn install_format_hook() -> Result<()> { 86pub fn install_pre_commit_hook() -> Result<()> {
87 let result_path = Path::new(if cfg!(windows) { 87 let result_path =
88 "./.git/hooks/pre-commit.exe" 88 PathBuf::from(format!("./.git/hooks/pre-commit{}", std::env::consts::EXE_SUFFIX));
89 } else {
90 "./.git/hooks/pre-commit"
91 });
92 if !result_path.exists() { 89 if !result_path.exists() {
93 run("cargo build --package xtask --bin pre-commit", ".")?; 90 let me = std::env::current_exe()?;
94 if cfg!(windows) { 91 fs::copy(me, result_path)?;
95 fs::copy("./target/debug/pre-commit.exe", result_path)?;
96 } else {
97 fs::copy("./target/debug/pre-commit", result_path)?;
98 }
99 } else { 92 } else {
100 Err(IoError::new(ErrorKind::AlreadyExists, "Git hook already created"))?; 93 Err(IoError::new(ErrorKind::AlreadyExists, "Git hook already created"))?;
101 } 94 }
@@ -150,6 +143,27 @@ pub fn run_fuzzer() -> Result<()> {
150 run("rustup run nightly -- cargo fuzz run parser", "./crates/ra_syntax") 143 run("rustup run nightly -- cargo fuzz run parser", "./crates/ra_syntax")
151} 144}
152 145
146pub fn reformat_staged_files() -> Result<()> {
147 let root = project_root();
148 let output = Command::new("git")
149 .arg("diff")
150 .arg("--diff-filter=MAR")
151 .arg("--name-only")
152 .arg("--cached")
153 .current_dir(&root)
154 .output()?;
155 if !output.status.success() {
156 anyhow::bail!(
157 "`git diff --diff-filter=MAR --name-only --cached` exited with {}",
158 output.status
159 );
160 }
161 for line in String::from_utf8(output.stdout)?.lines() {
162 run(&format!("git update-index --add {}", root.join(line).to_string_lossy()), ".")?;
163 }
164 Ok(())
165}
166
153fn do_run<F>(cmdline: &str, dir: &str, mut f: F) -> Result<Output> 167fn do_run<F>(cmdline: &str, dir: &str, mut f: F) -> Result<Output>
154where 168where
155 F: FnMut(&mut Command), 169 F: FnMut(&mut Command),
diff --git a/xtask/src/main.rs b/xtask/src/main.rs
index f14e6c8ae..663e28103 100644
--- a/xtask/src/main.rs
+++ b/xtask/src/main.rs
@@ -16,7 +16,8 @@ use pico_args::Arguments;
16use std::{env, path::PathBuf}; 16use std::{env, path::PathBuf};
17use xtask::{ 17use xtask::{
18 codegen::{self, Mode}, 18 codegen::{self, Mode},
19 install_format_hook, run, run_clippy, run_fuzzer, run_rustfmt, run_with_output, Cmd, Result, 19 install_pre_commit_hook, reformat_staged_files, run, run_clippy, run_fuzzer, run_rustfmt,
20 run_with_output, Cmd, Result,
20}; 21};
21 22
22// Latest stable, feel free to send a PR if this lags behind. 23// Latest stable, feel free to send a PR if this lags behind.
@@ -36,6 +37,10 @@ struct ServerOpt {
36} 37}
37 38
38fn main() -> Result<()> { 39fn main() -> Result<()> {
40 if std::env::args().next().map(|it| it.contains("pre-commit")) == Some(true) {
41 return reformat_staged_files();
42 }
43
39 let subcommand = match std::env::args_os().nth(1) { 44 let subcommand = match std::env::args_os().nth(1) {
40 None => { 45 None => {
41 eprintln!("{}", help::GLOBAL_HELP); 46 eprintln!("{}", help::GLOBAL_HELP);
@@ -81,12 +86,12 @@ fn main() -> Result<()> {
81 } 86 }
82 run_rustfmt(Mode::Overwrite)? 87 run_rustfmt(Mode::Overwrite)?
83 } 88 }
84 "format-hook" => { 89 "install-pre-commit-hook" => {
85 if matches.contains(["-h", "--help"]) { 90 if matches.contains(["-h", "--help"]) {
86 help::print_no_param_subcommand_help(&subcommand); 91 help::print_no_param_subcommand_help(&subcommand);
87 return Ok(()); 92 return Ok(());
88 } 93 }
89 install_format_hook()? 94 install_pre_commit_hook()?
90 } 95 }
91 "lint" => { 96 "lint" => {
92 if matches.contains(["-h", "--help"]) { 97 if matches.contains(["-h", "--help"]) {