about summary refs log tree commit diff
path: root/xtask/src
diff options
context:
space:
mode:
authorAleksey Kladov <aleksey.kladov@gmail.com>2020-01-10 11:23:11 +0100
committerAleksey Kladov <aleksey.kladov@gmail.com>2020-01-10 11:23:43 +0100
commitfd394ff424a8abde35f24643dfabbd5bd3f2f43c (patch)
tree23071b83610a2d0b61ffcb606f83c3e95a2809bd /xtask/src
parentb77a7e29a5b345a5dcdf427a0b332630147bcd5b (diff)
downloadrust-fd394ff424a8abde35f24643dfabbd5bd3f2f43c.tar.gz
rust-fd394ff424a8abde35f24643dfabbd5bd3f2f43c.zip
Use correct rustfmt for codegen
closes #1569
Diffstat (limited to 'xtask/src')
-rw-r--r--xtask/src/codegen.rs24
-rw-r--r--xtask/src/codegen/gen_assists_docs.rs2
-rw-r--r--xtask/src/codegen/gen_syntax.rs4
-rw-r--r--xtask/src/lib.rs37
4 files changed, 32 insertions, 35 deletions
diff --git a/xtask/src/codegen.rs b/xtask/src/codegen.rs
index 158cfc2d680..efa638e0603 100644
--- a/xtask/src/codegen.rs
+++ b/xtask/src/codegen.rs
@@ -9,15 +9,9 @@ mod gen_syntax;
 mod gen_parser_tests;
 mod gen_assists_docs;
 
-use std::{
-    fs,
-    io::Write,
-    mem,
-    path::Path,
-    process::{Command, Stdio},
-};
+use std::{fs, mem, path::Path};
 
-use crate::{project_root, Result};
+use crate::Result;
 
 pub use self::{
     gen_assists_docs::generate_assists_docs, gen_parser_tests::generate_parser_tests,
@@ -62,20 +56,6 @@ fn update(path: &Path, contents: &str, mode: Mode) -> Result<()> {
     }
 }
 
-fn reformat(text: impl std::fmt::Display) -> Result<String> {
-    let mut rustfmt = Command::new("rustfmt")
-        .arg("--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 extract_comment_blocks(text: &str) -> Vec<Vec<String>> {
     do_extract_comment_blocks(text, false)
 }
diff --git a/xtask/src/codegen/gen_assists_docs.rs b/xtask/src/codegen/gen_assists_docs.rs
index 05afda8f111..69f9b487288 100644
--- a/xtask/src/codegen/gen_assists_docs.rs
+++ b/xtask/src/codegen/gen_assists_docs.rs
@@ -102,7 +102,7 @@ r#####"
 
         buf.push_str(&test)
     }
-    let buf = codegen::reformat(buf)?;
+    let buf = crate::reformat(buf)?;
     codegen::update(&project_root().join(codegen::ASSISTS_TESTS), &buf, mode)
 }
 
diff --git a/xtask/src/codegen/gen_syntax.rs b/xtask/src/codegen/gen_syntax.rs
index 0f50ca56933..19fea67d8bf 100644
--- a/xtask/src/codegen/gen_syntax.rs
+++ b/xtask/src/codegen/gen_syntax.rs
@@ -152,7 +152,7 @@ fn generate_ast(grammar: AstSrc<'_>) -> Result<String> {
         #(#enums)*
     };
 
-    let pretty = codegen::reformat(ast)?;
+    let pretty = crate::reformat(ast)?;
     Ok(pretty)
 }
 
@@ -265,7 +265,7 @@ fn generate_syntax_kinds(grammar: KindsSrc<'_>) -> Result<String> {
         }
     };
 
-    codegen::reformat(ast)
+    crate::reformat(ast)
 }
 
 fn to_upper_snake_case(s: &str) -> String {
diff --git a/xtask/src/lib.rs b/xtask/src/lib.rs
index e46c21db727..0a569cf5dc6 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<String> {
+    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), ".")
 }