mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-02 06:42:02 +00:00
Automatically write to src/checks_gen.rs (#604)
This commit is contained in:
parent
b335a6a5ec
commit
d5fbcd708d
1 changed files with 43 additions and 11 deletions
|
@ -1,13 +1,28 @@
|
||||||
//! Generate the CheckCodePrefix enum.
|
|
||||||
|
|
||||||
use std::collections::{BTreeMap, BTreeSet};
|
use std::collections::{BTreeMap, BTreeSet};
|
||||||
|
use std::fs::OpenOptions;
|
||||||
|
use std::io;
|
||||||
|
use std::io::Write;
|
||||||
|
|
||||||
|
use anyhow::Result;
|
||||||
|
use clap::Parser;
|
||||||
use codegen::{Scope, Type, Variant};
|
use codegen::{Scope, Type, Variant};
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use ruff::checks::CheckCode;
|
use ruff::checks::CheckCode;
|
||||||
use strum::IntoEnumIterator;
|
use strum::IntoEnumIterator;
|
||||||
|
|
||||||
fn main() {
|
#[derive(Parser)]
|
||||||
|
#[command(author, version, about, long_about = None)]
|
||||||
|
/// Generate the `CheckCodePrefix` enum.
|
||||||
|
struct Cli {
|
||||||
|
/// Write the generated source code to stdout (rather than to
|
||||||
|
/// `src/checks_gen.rs`).
|
||||||
|
#[arg(long)]
|
||||||
|
dry_run: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() -> Result<()> {
|
||||||
|
let cli = Cli::parse();
|
||||||
|
|
||||||
// Build up a map from prefix to matching CheckCodes.
|
// Build up a map from prefix to matching CheckCodes.
|
||||||
let mut prefix_to_codes: BTreeMap<String, BTreeSet<CheckCode>> = Default::default();
|
let mut prefix_to_codes: BTreeMap<String, BTreeSet<CheckCode>> = Default::default();
|
||||||
for check_code in CheckCode::iter() {
|
for check_code in CheckCode::iter() {
|
||||||
|
@ -100,12 +115,29 @@ fn main() {
|
||||||
}
|
}
|
||||||
gen.line("}");
|
gen.line("}");
|
||||||
|
|
||||||
println!("//! File automatically generated by examples/generate_check_code_prefix.rs.");
|
// Write the output to `src/checks_gen.rs`.
|
||||||
println!();
|
let mut writer = if cli.dry_run {
|
||||||
println!("use serde::{{Serialize, Deserialize}};");
|
Box::new(io::stdout()) as Box<dyn Write>
|
||||||
println!("use strum_macros::EnumString;");
|
} else {
|
||||||
println!();
|
let f = OpenOptions::new()
|
||||||
println!("use crate::checks::CheckCode;");
|
.write(true)
|
||||||
println!();
|
.truncate(true)
|
||||||
println!("{}", scope.to_string());
|
.open("src/checks_gen.rs")
|
||||||
|
.expect("unable to open file");
|
||||||
|
Box::new(f) as Box<dyn Write>
|
||||||
|
};
|
||||||
|
|
||||||
|
writeln!(
|
||||||
|
writer,
|
||||||
|
"//! File automatically generated by examples/generate_check_code_prefix.rs."
|
||||||
|
)?;
|
||||||
|
writeln!(writer)?;
|
||||||
|
writeln!(writer, "use serde::{{Serialize, Deserialize}};")?;
|
||||||
|
writeln!(writer, "use strum_macros::EnumString;")?;
|
||||||
|
writeln!(writer)?;
|
||||||
|
writeln!(writer, "use crate::checks::CheckCode;")?;
|
||||||
|
writeln!(writer)?;
|
||||||
|
writeln!(writer, "{}", scope.to_string())?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue