aboutsummaryrefslogtreecommitdiff
path: root/xtask/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'xtask/src/lib.rs')
-rw-r--r--xtask/src/lib.rs37
1 files changed, 27 insertions, 10 deletions
diff --git a/xtask/src/lib.rs b/xtask/src/lib.rs
index e46c21db7..0a569cf5d 100644
--- a/xtask/src/lib.rs
+++ b/xtask/src/lib.rs
@@ -10,6 +10,7 @@ mod ast_src;
10use anyhow::Context; 10use anyhow::Context;
11use std::{ 11use std::{
12 env, fs, 12 env, fs,
13 io::Write,
13 path::{Path, PathBuf}, 14 path::{Path, PathBuf},
14 process::{Command, Stdio}, 15 process::{Command, Stdio},
15}; 16};
@@ -31,15 +32,7 @@ pub fn project_root() -> PathBuf {
31} 32}
32 33
33pub fn run_rustfmt(mode: Mode) -> Result<()> { 34pub fn run_rustfmt(mode: Mode) -> Result<()> {
34 match Command::new("rustup") 35 ensure_rustfmt()?;
35 .args(&["run", TOOLCHAIN, "--", "cargo", "fmt", "--version"])
36 .stderr(Stdio::null())
37 .stdout(Stdio::null())
38 .status()
39 {
40 Ok(status) if status.success() => (),
41 _ => install_rustfmt().context("install rustfmt")?,
42 };
43 36
44 if mode == Mode::Verify { 37 if mode == Mode::Verify {
45 run(&format!("rustup run {} -- cargo fmt -- --check", TOOLCHAIN), ".")?; 38 run(&format!("rustup run {} -- cargo fmt -- --check", TOOLCHAIN), ".")?;
@@ -49,7 +42,31 @@ pub fn run_rustfmt(mode: Mode) -> Result<()> {
49 Ok(()) 42 Ok(())
50} 43}
51 44
52fn install_rustfmt() -> Result<()> { 45fn reformat(text: impl std::fmt::Display) -> Result<String> {
46 ensure_rustfmt()?;
47 let mut rustfmt = Command::new("rustup")
48 .args(&["run", TOOLCHAIN, "--", "rustfmt", "--config-path"])
49 .arg(project_root().join("rustfmt.toml"))
50 .stdin(Stdio::piped())
51 .stdout(Stdio::piped())
52 .spawn()?;
53 write!(rustfmt.stdin.take().unwrap(), "{}", text)?;
54 let output = rustfmt.wait_with_output()?;
55 let stdout = String::from_utf8(output.stdout)?;
56 let preamble = "Generated file, do not edit by hand, see `crate/ra_tools/src/codegen`";
57 Ok(format!("//! {}\n\n{}", preamble, stdout))
58}
59
60fn ensure_rustfmt() -> Result<()> {
61 match Command::new("rustup")
62 .args(&["run", TOOLCHAIN, "--", "cargo", "fmt", "--version"])
63 .stderr(Stdio::null())
64 .stdout(Stdio::null())
65 .status()
66 {
67 Ok(status) if status.success() => return Ok(()),
68 _ => (),
69 };
53 run(&format!("rustup toolchain install {}", TOOLCHAIN), ".")?; 70 run(&format!("rustup toolchain install {}", TOOLCHAIN), ".")?;
54 run(&format!("rustup component add rustfmt --toolchain {}", TOOLCHAIN), ".") 71 run(&format!("rustup component add rustfmt --toolchain {}", TOOLCHAIN), ".")
55} 72}