refactor(ast_tools): run rustfmt only on generated files (#6898)

Run `rustfmt` only on individual files which codegen generates, rather than on the whole repo. This speeds up local development of `ast_tools` when you're on a slower machine.
This commit is contained in:
overlookmotel 2024-10-25 18:53:05 +00:00
parent 5c299bc10c
commit 1d981bfa53
2 changed files with 18 additions and 13 deletions

View file

@ -1,4 +1,7 @@
use std::process::Command;
use std::{
io::Write,
process::{Command, Stdio},
};
use lazy_static::lazy_static;
use proc_macro2::TokenStream;
@ -9,12 +12,22 @@ use syn::parse_file;
pub fn pretty_print(input: &TokenStream) -> String {
let result = prettyplease::unparse(&parse_file(input.to_string().as_str()).unwrap());
let result = COMMENT_REGEX.replace_all(&result, CommentReplacer).to_string();
result
rust_fmt(&result)
}
/// Run `cargo fmt`
pub fn cargo_fmt() {
Command::new("cargo").arg("fmt").status().unwrap();
pub fn rust_fmt(source_text: &str) -> String {
let mut rustfmt = Command::new("rustfmt")
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()
.expect("Failed to run rustfmt (is it installed?)");
let stdin = rustfmt.stdin.as_mut().unwrap();
stdin.write_all(source_text.as_bytes()).unwrap();
stdin.flush().unwrap();
let output = rustfmt.wait_with_output().unwrap();
String::from_utf8(output.stdout).unwrap()
}
/// Replace doc comments which start with `@` with plain comments or line breaks.

View file

@ -21,7 +21,6 @@ use derives::{
DeriveCloneIn, DeriveContentEq, DeriveContentHash, DeriveESTree, DeriveGetSpan,
DeriveGetSpanMut,
};
use fmt::cargo_fmt;
use generators::{
AssertLayouts, AstBuilderGenerator, AstKindGenerator, Generator, GeneratorOutput,
TypescriptGenerator, VisitGenerator, VisitMutGenerator,
@ -53,9 +52,6 @@ pub struct CliOptions {
/// Runs all generators but won't write anything down.
#[bpaf(switch)]
dry_run: bool,
/// Don't run cargo fmt at the end
#[bpaf(switch)]
no_fmt: bool,
/// Prints no logs.
quiet: bool,
/// Path of output `schema.json`.
@ -102,10 +98,6 @@ fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
write_ci_filter(SOURCE_PATHS, side_effects, ".github/.generated_ast_watch_list.yml")?;
}
if !cli_options.no_fmt {
cargo_fmt();
}
if let CliOptions { schema: Some(schema_path), dry_run: false, .. } = cli_options {
let path = schema_path.to_str().expect("invalid path for schema output.");
let schema = serde_json::to_string_pretty(&schema.defs).normalize()?;