Skip to content

Commit 046e466

Browse files
bors[bot]matklad
andauthored
Merge #2782
2782: Use correct rustfmt for codegen r=matklad a=matklad Co-authored-by: Aleksey Kladov <[email protected]>
2 parents b77a7e2 + fd394ff commit 046e466

File tree

4 files changed

+32
-35
lines changed

4 files changed

+32
-35
lines changed

xtask/src/codegen.rs

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,9 @@ mod gen_syntax;
99
mod gen_parser_tests;
1010
mod gen_assists_docs;
1111

12-
use std::{
13-
fs,
14-
io::Write,
15-
mem,
16-
path::Path,
17-
process::{Command, Stdio},
18-
};
12+
use std::{fs, mem, path::Path};
1913

20-
use crate::{project_root, Result};
14+
use crate::Result;
2115

2216
pub use self::{
2317
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<()> {
6256
}
6357
}
6458

65-
fn 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-
7959
fn extract_comment_blocks(text: &str) -> Vec<Vec<String>> {
8060
do_extract_comment_blocks(text, false)
8161
}

xtask/src/codegen/gen_assists_docs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ r#####"
102102

103103
buf.push_str(&test)
104104
}
105-
let buf = codegen::reformat(buf)?;
105+
let buf = crate::reformat(buf)?;
106106
codegen::update(&project_root().join(codegen::ASSISTS_TESTS), &buf, mode)
107107
}
108108

xtask/src/codegen/gen_syntax.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ fn generate_ast(grammar: AstSrc<'_>) -> Result<String> {
152152
#(#enums)*
153153
};
154154

155-
let pretty = codegen::reformat(ast)?;
155+
let pretty = crate::reformat(ast)?;
156156
Ok(pretty)
157157
}
158158

@@ -265,7 +265,7 @@ fn generate_syntax_kinds(grammar: KindsSrc<'_>) -> Result<String> {
265265
}
266266
};
267267

268-
codegen::reformat(ast)
268+
crate::reformat(ast)
269269
}
270270

271271
fn to_upper_snake_case(s: &str) -> String {

xtask/src/lib.rs

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ mod ast_src;
1010
use anyhow::Context;
1111
use std::{
1212
env, fs,
13+
io::Write,
1314
path::{Path, PathBuf},
1415
process::{Command, Stdio},
1516
};
@@ -31,15 +32,7 @@ pub fn project_root() -> PathBuf {
3132
}
3233

3334
pub fn run_rustfmt(mode: Mode) -> Result<()> {
34-
match Command::new("rustup")
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-
};
35+
ensure_rustfmt()?;
4336

4437
if mode == Mode::Verify {
4538
run(&format!("rustup run {} -- cargo fmt -- --check", TOOLCHAIN), ".")?;
@@ -49,7 +42,31 @@ pub fn run_rustfmt(mode: Mode) -> Result<()> {
4942
Ok(())
5043
}
5144

52-
fn install_rustfmt() -> Result<()> {
45+
fn 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+
60+
fn 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+
};
5370
run(&format!("rustup toolchain install {}", TOOLCHAIN), ".")?;
5471
run(&format!("rustup component add rustfmt --toolchain {}", TOOLCHAIN), ".")
5572
}

0 commit comments

Comments
 (0)