From fd394ff424a8abde35f24643dfabbd5bd3f2f43c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 10 Jan 2020 11:23:11 +0100 Subject: Use correct rustfmt for codegen closes #1569 --- xtask/src/lib.rs | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) (limited to 'xtask/src/lib.rs') 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; use anyhow::Context; use std::{ env, fs, + io::Write, path::{Path, PathBuf}, process::{Command, Stdio}, }; @@ -31,15 +32,7 @@ pub fn project_root() -> PathBuf { } pub fn run_rustfmt(mode: Mode) -> Result<()> { - match Command::new("rustup") - .args(&["run", TOOLCHAIN, "--", "cargo", "fmt", "--version"]) - .stderr(Stdio::null()) - .stdout(Stdio::null()) - .status() - { - Ok(status) if status.success() => (), - _ => install_rustfmt().context("install rustfmt")?, - }; + ensure_rustfmt()?; if mode == Mode::Verify { run(&format!("rustup run {} -- cargo fmt -- --check", TOOLCHAIN), ".")?; @@ -49,7 +42,31 @@ pub fn run_rustfmt(mode: Mode) -> Result<()> { Ok(()) } -fn install_rustfmt() -> Result<()> { +fn reformat(text: impl std::fmt::Display) -> Result { + ensure_rustfmt()?; + let mut rustfmt = Command::new("rustup") + .args(&["run", TOOLCHAIN, "--", "rustfmt", "--config-path"]) + .arg(project_root().join("rustfmt.toml")) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .spawn()?; + write!(rustfmt.stdin.take().unwrap(), "{}", text)?; + let output = rustfmt.wait_with_output()?; + let stdout = String::from_utf8(output.stdout)?; + let preamble = "Generated file, do not edit by hand, see `crate/ra_tools/src/codegen`"; + Ok(format!("//! {}\n\n{}", preamble, stdout)) +} + +fn ensure_rustfmt() -> Result<()> { + match Command::new("rustup") + .args(&["run", TOOLCHAIN, "--", "cargo", "fmt", "--version"]) + .stderr(Stdio::null()) + .stdout(Stdio::null()) + .status() + { + Ok(status) if status.success() => return Ok(()), + _ => (), + }; run(&format!("rustup toolchain install {}", TOOLCHAIN), ".")?; run(&format!("rustup component add rustfmt --toolchain {}", TOOLCHAIN), ".") } -- cgit v1.2.3