Skip to content

Commit

Permalink
feat(ast_codegen): add cli arguments to use with oxc_ast_codegen ex…
Browse files Browse the repository at this point in the history
…ecutable. (#4117)

```
Usage: oxc_ast_codegen [--dry-run] [--no-fmt] [--schema=ARG]

Available options:
        --dry-run     Runs all generators but won't write anything down.
        --no-fmt      Don't run cargo fmt at the end
        --schema=ARG  Path of output `schema.json`.
    -h, --help        Prints help information
```
  • Loading branch information
rzvxa committed Jul 9, 2024
1 parent 6db630f commit 0b43310
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 9 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions tasks/ast_codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ quote = { workspace = true }
proc-macro2 = { workspace = true }
itertools = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
regex = { workspace = true }
prettyplease = { workspace = true }
lazy_static = { workspace = true }
convert_case = { workspace = true }
bpaf = { workspace = true, features = ["autocomplete", "bright-color", "derive"] }
40 changes: 31 additions & 9 deletions tasks/ast_codegen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use std::{
rc::Rc,
};

use bpaf::{Bpaf, Parser};
use fmt::{cargo_fmt, pprint};
use itertools::Itertools;
use proc_macro2::TokenStream;
Expand All @@ -26,6 +27,7 @@ use defs::TypeDef;
use generators::{AstBuilderGenerator, AstKindGenerator, VisitGenerator, VisitMutGenerator};
use linker::{linker, Linker};
use schema::{Inherit, Module, REnum, RStruct, RType, Schema};
use util::write_all_to;

use crate::generators::ImplGetSpanGenerator;

Expand Down Expand Up @@ -204,18 +206,29 @@ fn write_generated_streams(
let output_dir = output_dir()?;
for (name, stream) in streams {
let content = pprint(&stream);

let path = format!("{output_dir}/{name}.rs");
let mut file = fs::File::create(path)?;

file.write_all(content.as_bytes())?;
write_all_to(content.as_bytes(), path)?;
}
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<std::path::PathBuf>,
}

#[allow(clippy::print_stdout)]
fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
let CodegenResult { outputs, .. } = files()
let cli_options = cli_options().run();

let CodegenResult { outputs, schema } = files()
.fold(AstCodegen::default(), AstCodegen::add_file)
.with(AstKindGenerator)
.with(AstBuilderGenerator)
Expand All @@ -226,11 +239,20 @@ fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {

let (streams, _): (Vec<_>, Vec<_>) =
outputs.into_iter().partition(|it| matches!(it.1, GeneratorOutput::Stream(_)));
write_generated_streams(streams.into_iter().map(|it| it.1.into_stream()))?;

cargo_fmt(".")?;
if !cli_options.dry_run {
write_generated_streams(streams.into_iter().map(|it| it.1.into_stream()))?;
}

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).map_err(|e| e.to_string())?;
write_all_to(schema.as_bytes(), path)?;
}

// let schema = serde_json::to_string_pretty(&schema).map_err(|e| e.to_string())?;
// println!("{schema}");
Ok(())
}
7 changes: 7 additions & 0 deletions tasks/ast_codegen/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,10 @@ impl TokenStreamExt for TokenStream {
.collect()
}
}

pub fn write_all_to<S: AsRef<str>>(data: &[u8], path: S) -> std::io::Result<()> {
use std::{fs, io::Write};
let mut file = fs::File::create(path.as_ref())?;
file.write_all(data)?;
Ok(())
}

0 comments on commit 0b43310

Please sign in to comment.