From 1d981bfa53057ea65016856e0b3bcb4d5e10e6d3 Mon Sep 17 00:00:00 2001 From: overlookmotel <557937+overlookmotel@users.noreply.github.com> Date: Fri, 25 Oct 2024 18:53:05 +0000 Subject: [PATCH] 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. --- tasks/ast_tools/src/fmt.rs | 23 ++++++++++++++++++----- tasks/ast_tools/src/main.rs | 8 -------- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/tasks/ast_tools/src/fmt.rs b/tasks/ast_tools/src/fmt.rs index 3a85c7256..08e47981d 100644 --- a/tasks/ast_tools/src/fmt.rs +++ b/tasks/ast_tools/src/fmt.rs @@ -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. diff --git a/tasks/ast_tools/src/main.rs b/tasks/ast_tools/src/main.rs index 9d9275f78..502ff3fec 100644 --- a/tasks/ast_tools/src/main.rs +++ b/tasks/ast_tools/src/main.rs @@ -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> { 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()?;