From aeed29f248d70f88dbfb1115b4144b45070d3669 Mon Sep 17 00:00:00 2001 From: rzvxa <3788964+rzvxa@users.noreply.github.com> Date: Wed, 7 Aug 2024 22:11:34 +0000 Subject: [PATCH] chore(ci): generate `.generated_ast_watch_list.yml` (#4737) closes #4699 I also did some reordering to the code so it is easier to follow. --- .github/.generated_ast_watch_list.yml | 21 +++++ .github/workflows/ci.yml | 10 +- tasks/ast_codegen/src/main.rs | 130 +++++++++++++++++--------- 3 files changed, 108 insertions(+), 53 deletions(-) create mode 100644 .github/.generated_ast_watch_list.yml diff --git a/.github/.generated_ast_watch_list.yml b/.github/.generated_ast_watch_list.yml new file mode 100644 index 0000000000000..0b8a81ef34120 --- /dev/null +++ b/.github/.generated_ast_watch_list.yml @@ -0,0 +1,21 @@ +# To edit this generated file you have to edit `tasks/ast_codegen/src/main.rs` +# Auto-generated code, DO NOT EDIT DIRECTLY! + +src: + - 'crates/oxc_ast/src/ast/literal.rs' + - 'crates/oxc_ast/src/ast/js.rs' + - 'crates/oxc_ast/src/ast/ts.rs' + - 'crates/oxc_ast/src/ast/jsx.rs' + - 'crates/oxc_syntax/src/number.rs' + - 'crates/oxc_syntax/src/operator.rs' + - 'crates/oxc_span/src/span/types.rs' + - 'crates/oxc_span/src/source_type/types.rs' + - 'crates/oxc_ast/src/generated/assert_layouts.rs' + - 'crates/oxc_ast/src/generated/ast_kind.rs' + - 'crates/oxc_ast/src/generated/ast_builder.rs' + - 'crates/oxc_ast/src/generated/derive_clone_in.rs' + - 'crates/oxc_ast/src/generated/derive_get_span.rs' + - 'crates/oxc_ast/src/generated/derive_get_span_mut.rs' + - 'crates/oxc_ast/src/generated/visit.rs' + - 'crates/oxc_ast/src/generated/visit_mut.rs' + - 'tasks/ast_codegen/src/**' diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ab81b64d48204..5786ba3299149 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -274,15 +274,7 @@ jobs: - uses: dorny/paths-filter@v3 id: filter with: - filters: | - src: - - 'crates/oxc_ast/src/ast/*' # single star is intentional - - 'crates/oxc_ast/src/generated/**' # to potentially cause CI error if generated files are edited manually - - 'crates/oxc_syntax/src/number.rs' - - 'crates/oxc_syntax/src/operator.rs' - - 'crates/oxc_span/src/source_type/types.rs' - - 'crates/oxc_span/src/span/types.rs' - - 'tasks/ast_codegen/src/**' + filters: '.github/.generated_ast_watch_list.yml' - uses: Boshen/setup-rust@main if: steps.filter.outputs.src == 'true' diff --git a/tasks/ast_codegen/src/main.rs b/tasks/ast_codegen/src/main.rs index 48016b984eb48..bbf25b9cdcf48 100644 --- a/tasks/ast_codegen/src/main.rs +++ b/tasks/ast_codegen/src/main.rs @@ -25,14 +25,14 @@ use schema::{lower_ast_types, Schema, TypeDef}; use util::{write_all_to, NormalizeError}; static SOURCE_PATHS: &[&str] = &[ - "oxc_ast/src/ast/literal.rs", - "oxc_ast/src/ast/js.rs", - "oxc_ast/src/ast/ts.rs", - "oxc_ast/src/ast/jsx.rs", - "oxc_syntax/src/number.rs", - "oxc_syntax/src/operator.rs", - "oxc_span/src/span/types.rs", - "oxc_span/src/source_type/types.rs", + "crates/oxc_ast/src/ast/literal.rs", + "crates/oxc_ast/src/ast/js.rs", + "crates/oxc_ast/src/ast/ts.rs", + "crates/oxc_ast/src/ast/jsx.rs", + "crates/oxc_syntax/src/number.rs", + "crates/oxc_syntax/src/operator.rs", + "crates/oxc_span/src/span/types.rs", + "crates/oxc_span/src/source_type/types.rs", ]; const AST_CRATE: &str = "crates/oxc_ast"; @@ -45,6 +45,18 @@ type TypeTable = Vec; type DefTable = Vec; type IdentTable = HashMap; +#[derive(Debug, Bpaf)] +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, + /// Path of output `schema.json`. + schema: Option, +} + #[derive(Default)] struct AstCodegen { files: Vec, @@ -251,44 +263,12 @@ impl AstCodegen { } } -fn files() -> impl std::iter::Iterator { - SOURCE_PATHS.iter().map(|path| format!("crates/{path}")) -} - -fn write_generated_streams( - streams: impl IntoIterator, -) -> std::io::Result<()> { - for (path, stream) in streams { - let content = pprint(&stream); - write_all_to(content.as_bytes(), path.into_os_string().to_str().unwrap())?; - } - Ok(()) -} - -fn write_data_streams(streams: impl IntoIterator) -> std::io::Result<()> { - for (path, content) in streams { - write_all_to(&content, path.into_os_string().to_str().unwrap())?; - } - Ok(()) -} - -#[derive(Debug, Bpaf)] -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, - /// Path of output `schema.json`. - schema: Option, -} - #[allow(clippy::print_stdout)] fn main() -> std::result::Result<(), Box> { let cli_options = cli_options().run(); - let CodegenResult { outputs, schema } = files() + let CodegenResult { outputs, schema } = SOURCE_PATHS + .iter() .fold(AstCodegen::default(), AstCodegen::add_file) .pass(Linker) .pass(CalcLayout) @@ -309,8 +289,12 @@ fn main() -> std::result::Result<(), Box> { outputs.into_iter().partition(|it| matches!(it.1, GeneratorOutput::Data(_))); if !cli_options.dry_run { - write_generated_streams(streams.into_iter().map(|it| it.1.into_stream()))?; - write_data_streams(binaries.into_iter().map(|it| it.1.into_data()))?; + let side_effects = + write_generated_streams(streams.into_iter().map(|it| it.1.into_stream()))? + .into_iter() + .chain(write_data_streams(binaries.into_iter().map(|it| it.1.into_data()))?) + .collect(); + write_ci_filter(SOURCE_PATHS, side_effects, ".github/.generated_ast_watch_list.yml")?; } if !cli_options.no_fmt { @@ -329,3 +313,61 @@ fn main() -> std::result::Result<(), Box> { fn output(krate: &str, path: &str) -> PathBuf { std::path::PathBuf::from_iter(vec![krate, "src", "generated", path]) } + +/// Writes all streams and returns a vector pointing to side-effects written on the disk +fn write_generated_streams( + streams: impl IntoIterator, +) -> std::io::Result> { + streams + .into_iter() + .map(|(path, stream)| { + let path = path.into_os_string(); + let path = path.to_str().unwrap(); + let content = pprint(&stream); + write_all_to(content.as_bytes(), path)?; + Ok(path.to_string().replace('\\', "/")) + }) + .collect() +} + +/// Writes all streams and returns a vector pointing to side-effects written on the disk +fn write_data_streams( + streams: impl IntoIterator, +) -> std::io::Result> { + streams + .into_iter() + .map(|(path, stream)| { + let path = path.into_os_string(); + let path = path.to_str().unwrap(); + write_all_to(&stream, path)?; + Ok(path.to_string().replace('\\', "/")) + }) + .collect() +} + +fn write_ci_filter( + inputs: &[&str], + side_effects: Vec, + output_path: &str, +) -> std::io::Result<()> { + let file = file!().replace('\\', "/"); + let mut output = format!( + "\ + # To edit this generated file you have to edit `{file}`\n\ + # Auto-generated code, DO NOT EDIT DIRECTLY!\n\n\ + src:\n" + ); + let mut push_item = |path: &str| output.push_str(format!(" - '{path}'\n").as_str()); + + for input in inputs { + push_item(input); + } + + for side_effect in side_effects { + push_item(side_effect.as_str()); + } + + push_item("tasks/ast_codegen/src/**"); + + write_all_to(output.as_bytes(), output_path) +}