aboutsummaryrefslogtreecommitdiff
path: root/xtask
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-01-10 10:23:11 +0000
committerAleksey Kladov <[email protected]>2020-01-10 10:23:43 +0000
commitfd394ff424a8abde35f24643dfabbd5bd3f2f43c (patch)
tree23071b83610a2d0b61ffcb606f83c3e95a2809bd /xtask
parentb77a7e29a5b345a5dcdf427a0b332630147bcd5b (diff)
Use correct rustfmt for codegen
closes #1569
Diffstat (limited to 'xtask')
-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 158cfc2d6..efa638e06 100644
--- a/xtask/src/codegen.rs
+++ b/xtask/src/codegen.rs
@@ -9,15 +9,9 @@ mod gen_syntax;
9mod gen_parser_tests; 9mod gen_parser_tests;
10mod gen_assists_docs; 10mod gen_assists_docs;
11 11
12use std::{ 12use std::{fs, mem, path::Path};
13 fs,
14 io::Write,
15 mem,
16 path::Path,
17 process::{Command, Stdio},
18};
19 13
20use crate::{project_root, Result}; 14use crate::Result;
21 15
22pub use self::{ 16pub use self::{
23 gen_assists_docs::generate_assists_docs, gen_parser_tests::generate_parser_tests, 17 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<()> {
62 } 56 }
63} 57}
64 58
65fn reformat(text: impl std::fmt::Display) -> Result<String> {
66 let mut rustfmt = Command::new("rustfmt")
67 .arg("--config-path")
68 .arg(project_root().join("rustfmt.toml"))
69 .stdin(Stdio::piped())
70 .stdout(Stdio::piped())
71 .spawn()?;
72 write!(rustfmt.stdin.take().unwrap(), "{}", text)?;
73 let output = rustfmt.wait_with_output()?;
74 let stdout = String::from_utf8(output.stdout)?;
75 let preamble = "Generated file, do not edit by hand, see `crate/ra_tools/src/codegen`";
76 Ok(format!("//! {}\n\n{}", preamble, stdout))
77}
78
79fn extract_comment_blocks(text: &str) -> Vec<Vec<String>> { 59fn extract_comment_blocks(text: &str) -> Vec<Vec<String>> {
80 do_extract_comment_blocks(text, false) 60 do_extract_comment_blocks(text, false)
81} 61}
diff --git a/xtask/src/codegen/gen_assists_docs.rs b/xtask/src/codegen/gen_assists_docs.rs
index 05afda8f1..69f9b4872 100644
--- a/xtask/src/codegen/gen_assists_docs.rs
+++ b/xtask/src/codegen/gen_assists_docs.rs
@@ -102,7 +102,7 @@ r#####"
102 102
103 buf.push_str(&test) 103 buf.push_str(&test)
104 } 104 }
105 let buf = codegen::reformat(buf)?; 105 let buf = crate::reformat(buf)?;
106 codegen::update(&project_root().join(codegen::ASSISTS_TESTS), &buf, mode) 106 codegen::update(&project_root().join(codegen::ASSISTS_TESTS), &buf, mode)
107} 107}
108 108
diff --git a/xtask/src/codegen/gen_syntax.rs b/xtask/src/codegen/gen_syntax.rs
index 0f50ca569..19fea67d8 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> {
152 #(#enums)* 152 #(#enums)*
153 }; 153 };
154 154
155 let pretty = codegen::reformat(ast)?; 155 let pretty = crate::reformat(ast)?;
156 Ok(pretty) 156 Ok(pretty)
157} 157}
158 158
@@ -265,7 +265,7 @@ fn generate_syntax_kinds(grammar: KindsSrc<'_>) -> Result<String> {
265 } 265 }
266 }; 266 };
267 267
268 codegen::reformat(ast) 268 crate::reformat(ast)
269} 269}
270 270
271fn to_upper_snake_case(s: &str) -> String { 271fn to_upper_snake_case(s: &str) -> String {
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}