From b4dbe62da00a7d312568733d97f98117ceab050f Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Tue, 27 Dec 2022 10:07:18 -0500 Subject: [PATCH] Add cargo +nightly dev generate-all (#1404) --- .github/workflows/ci.yaml | 8 ++--- CONTRIBUTING.md | 13 +++---- ruff_dev/src/generate_all.rs | 35 +++++++++++++++++++ ruff_dev/src/generate_check_code_prefix.rs | 2 +- ruff_dev/src/generate_json_schema.rs | 2 +- ruff_dev/src/generate_options.rs | 2 +- ruff_dev/src/generate_playground_options.rs | 2 +- ruff_dev/src/generate_rules_table.rs | 2 +- ruff_dev/src/lib.rs | 3 +- ruff_dev/src/main.rs | 15 ++++---- ...{generate_source_code.rs => round_trip.rs} | 0 11 files changed, 60 insertions(+), 24 deletions(-) create mode 100644 ruff_dev/src/generate_all.rs rename ruff_dev/src/{generate_source_code.rs => round_trip.rs} (100%) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 5e1a3806d4..8c389f4edd 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -38,15 +38,15 @@ jobs: - run: cargo build --all --release - run: ./target/release/ruff_dev generate-rules-table - run: ./target/release/ruff_dev generate-options - - run: git diff --quiet README.md || echo "::error file=README.md::This file is outdated. You may have to rerun 'cargo dev generate-options' and/or 'cargo dev generate-rules-table'." + - run: git diff --quiet README.md || echo "::error file=README.md::This file is outdated. Run 'cargo +nightly dev generate-all'." - run: ./target/release/ruff_dev generate-check-code-prefix - - run: git diff --quiet src/checks_gen.rs || echo "::error file=src/checks_gen.rs::This file is outdated. You may have to rerun 'cargo dev generate-check-code-prefix'." + - run: git diff --quiet src/checks_gen.rs || echo "::error file=src/checks_gen.rs::This file is outdated. Run 'cargo +nightly dev generate-all'." - run: git diff --exit-code -- README.md src/checks_gen.rs - run: ./target/release/ruff_dev generate-json-schema - - run: git diff --quiet ruff.schema.json || echo "::error file=ruff.schema.json::This file is outdated. You may have to rerun 'cargo dev generate-json-schema'." + - run: git diff --quiet ruff.schema.json || echo "::error file=ruff.schema.json::This file is outdated. Run 'cargo +nightly dev generate-all'." - run: git diff --exit-code -- ruff.schema.json - run: ./target/release/ruff_dev generate-playground-options - - run: git diff --quiet playground/src/ruff_options.rs || echo "::error file=playground/src/ruff_options.ts::This file is outdated. You may have to rerun 'cargo dev generate-playground-options'." + - run: git diff --quiet playground/src/ruff_options.rs || echo "::error file=playground/src/ruff_options.ts::This file is outdated. Run 'cargo +nightly dev generate-all'." - run: git diff --exit-code -- README.md src/checks_gen.rs playground/src/ruff_options.ts cargo-fmt: diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 899e88d73d..a2c7a06f65 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -59,9 +59,9 @@ pattern implemented therein. To trigger the rule, you'll likely want to augment the logic in `src/check_ast.rs`, which defines the Python AST visitor, responsible for iterating over the abstract syntax tree and collecting -lint-rule violations as it goes. If you need to inspect the AST, you can run `cargo dev print-ast` -with a Python file. Grep for the `Check::new` invocations to understand how other, similar rules -are implemented. +lint-rule violations as it goes. If you need to inspect the AST, you can run +`cargo +nightly dev print-ast` with a Python file. Grep for the `Check::new` invocations to +understand how other, similar rules are implemented. To add a test fixture, create a file under `resources/test/fixtures`, named to match the `CheckCode` you defined earlier (e.g., `E402.py`). This file should contain a variety of violations and @@ -79,9 +79,7 @@ Then, run `cargo test`. Your test will fail, but you'll be prompted to follow-up `cargo insta review`. Accept the generated snapshot, then commit the snapshot file alongside the rest of your changes. -Finally, to update the documentation, run `cargo dev generate-rules-table` from the repo root. To -update the generated prefix map, run `cargo +nightly dev generate-check-code-prefix`. Both of these commands -should be run whenever a new check is added to the codebase. +Finally, regenerate the documentation and generated code with `cargo +nightly dev generate-all`. ### Example: Adding a new configuration option @@ -105,8 +103,7 @@ You may also want to add the new configuration option to the `flake8-to-ruff` to responsible for converting `flake8` configuration files to Ruff's TOML format. This logic lives in `flake8_to_ruff/src/converter.rs`. -Run `cargo dev generate-options` to update the documentation for supported configuration options, -and `cargo dev generate-json-schema` to update the JSON schema for `tool.ruff` in `pyproject.toml`. +Finally, regenerate the documentation and generated code with `cargo +nightly dev generate-all`. ## Release process diff --git a/ruff_dev/src/generate_all.rs b/ruff_dev/src/generate_all.rs new file mode 100644 index 0000000000..2e00c444de --- /dev/null +++ b/ruff_dev/src/generate_all.rs @@ -0,0 +1,35 @@ +//! Run all code and documentation generation steps. + +use anyhow::Result; +use clap::Args; + +use crate::{ + generate_check_code_prefix, generate_json_schema, generate_options, + generate_playground_options, generate_rules_table, +}; + +#[derive(Args)] +pub struct Cli { + /// Write the generated artifacts to stdout (rather than to the filesystem). + #[arg(long)] + dry_run: bool, +} + +pub fn main(cli: &Cli) -> Result<()> { + generate_check_code_prefix::main(&generate_check_code_prefix::Cli { + dry_run: cli.dry_run, + })?; + generate_json_schema::main(&generate_json_schema::Cli { + dry_run: cli.dry_run, + })?; + generate_rules_table::main(&generate_rules_table::Cli { + dry_run: cli.dry_run, + })?; + generate_options::main(&generate_options::Cli { + dry_run: cli.dry_run, + })?; + generate_playground_options::main(&generate_playground_options::Cli { + dry_run: cli.dry_run, + })?; + Ok(()) +} diff --git a/ruff_dev/src/generate_check_code_prefix.rs b/ruff_dev/src/generate_check_code_prefix.rs index c8bbc6cd16..9ff30fc722 100644 --- a/ruff_dev/src/generate_check_code_prefix.rs +++ b/ruff_dev/src/generate_check_code_prefix.rs @@ -19,7 +19,7 @@ pub struct Cli { /// Write the generated source code to stdout (rather than to /// `src/checks_gen.rs`). #[arg(long)] - dry_run: bool, + pub(crate) dry_run: bool, } pub fn main(cli: &Cli) -> Result<()> { diff --git a/ruff_dev/src/generate_json_schema.rs b/ruff_dev/src/generate_json_schema.rs index fe825402df..1edf53ae1e 100644 --- a/ruff_dev/src/generate_json_schema.rs +++ b/ruff_dev/src/generate_json_schema.rs @@ -10,7 +10,7 @@ use schemars::schema_for; pub struct Cli { /// Write the generated table to stdout (rather than to `ruff.schema.json`). #[arg(long)] - dry_run: bool, + pub(crate) dry_run: bool, } pub fn main(cli: &Cli) -> Result<()> { diff --git a/ruff_dev/src/generate_options.rs b/ruff_dev/src/generate_options.rs index ccacbb8402..781cd22199 100644 --- a/ruff_dev/src/generate_options.rs +++ b/ruff_dev/src/generate_options.rs @@ -18,7 +18,7 @@ const END_PRAGMA: &str = ""; pub struct Cli { /// Write the generated table to stdout (rather than to `README.md`). #[arg(long)] - dry_run: bool, + pub(crate) dry_run: bool, } fn emit_field(output: &mut String, field: &OptionField, group_name: Option<&str>) { diff --git a/ruff_dev/src/generate_playground_options.rs b/ruff_dev/src/generate_playground_options.rs index 759c2fc5e6..8f833171fb 100644 --- a/ruff_dev/src/generate_playground_options.rs +++ b/ruff_dev/src/generate_playground_options.rs @@ -14,7 +14,7 @@ use ruff::settings::options_base::{ConfigurationOptions, OptionEntry, OptionFiel pub struct Cli { /// Write the generated table to stdout (rather than to `TODO`). #[arg(long)] - dry_run: bool, + pub(crate) dry_run: bool, } fn emit_field(output: &mut String, field: &OptionField) { diff --git a/ruff_dev/src/generate_rules_table.rs b/ruff_dev/src/generate_rules_table.rs index 9845aee44d..a6436f9f6b 100644 --- a/ruff_dev/src/generate_rules_table.rs +++ b/ruff_dev/src/generate_rules_table.rs @@ -21,7 +21,7 @@ const TOC_END_PRAGMA: &str = ""; pub struct Cli { /// Write the generated table to stdout (rather than to `README.md`). #[arg(long)] - dry_run: bool, + pub(crate) dry_run: bool, } pub fn main(cli: &Cli) -> Result<()> { diff --git a/ruff_dev/src/lib.rs b/ruff_dev/src/lib.rs index 8b53230bf6..4c09edf226 100644 --- a/ruff_dev/src/lib.rs +++ b/ruff_dev/src/lib.rs @@ -11,12 +11,13 @@ clippy::too_many_lines )] +pub mod generate_all; pub mod generate_check_code_prefix; pub mod generate_json_schema; pub mod generate_options; pub mod generate_playground_options; pub mod generate_rules_table; -pub mod generate_source_code; pub mod print_ast; pub mod print_cst; pub mod print_tokens; +pub mod round_trip; diff --git a/ruff_dev/src/main.rs b/ruff_dev/src/main.rs index 3f99f48609..67ae09b200 100644 --- a/ruff_dev/src/main.rs +++ b/ruff_dev/src/main.rs @@ -14,9 +14,9 @@ use anyhow::Result; use clap::{Parser, Subcommand}; use ruff_dev::{ - generate_check_code_prefix, generate_json_schema, generate_options, - generate_playground_options, generate_rules_table, generate_source_code, print_ast, print_cst, - print_tokens, + generate_all, generate_check_code_prefix, generate_json_schema, generate_options, + generate_playground_options, generate_rules_table, print_ast, print_cst, print_tokens, + round_trip, }; #[derive(Parser)] @@ -29,6 +29,8 @@ struct Cli { #[derive(Subcommand)] enum Commands { + /// Run all code and documentation generation steps. + GenerateAll(generate_all::Cli), /// Generate the `CheckCodePrefix` enum. GenerateCheckCodePrefix(generate_check_code_prefix::Cli), /// Generate JSON schema for the TOML configuration file. @@ -40,28 +42,29 @@ enum Commands { /// Generate typescript file defining options to be used by the web /// playground. GeneratePlaygroundOptions(generate_playground_options::Cli), - /// Run round-trip source code generation on a given Python file. - GenerateSourceCode(generate_source_code::Cli), /// Print the AST for a given Python file. PrintAST(print_ast::Cli), /// Print the LibCST CST for a given Python file. PrintCST(print_cst::Cli), /// Print the token stream for a given Python file. PrintTokens(print_tokens::Cli), + /// Run round-trip source code generation on a given Python file. + RoundTrip(round_trip::Cli), } fn main() -> Result<()> { let cli = Cli::parse(); match &cli.command { + Commands::GenerateAll(args) => generate_all::main(args)?, Commands::GenerateCheckCodePrefix(args) => generate_check_code_prefix::main(args)?, Commands::GenerateJSONSchema(args) => generate_json_schema::main(args)?, Commands::GenerateRulesTable(args) => generate_rules_table::main(args)?, - Commands::GenerateSourceCode(args) => generate_source_code::main(args)?, Commands::GenerateOptions(args) => generate_options::main(args)?, Commands::GeneratePlaygroundOptions(args) => generate_playground_options::main(args)?, Commands::PrintAST(args) => print_ast::main(args)?, Commands::PrintCST(args) => print_cst::main(args)?, Commands::PrintTokens(args) => print_tokens::main(args)?, + Commands::RoundTrip(args) => round_trip::main(args)?, } Ok(()) } diff --git a/ruff_dev/src/generate_source_code.rs b/ruff_dev/src/round_trip.rs similarity index 100% rename from ruff_dev/src/generate_source_code.rs rename to ruff_dev/src/round_trip.rs