Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Postprocess extra generated file #94

Merged
merged 2 commits into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fs-err = "3.0.0"
heck = "0.5.0"
indexmap = "2.7.0"
itertools = "0.14.0"
minijinja = { version = "2.5.0", features = ["loader"] }
minijinja = { version = "2.8.0", features = ["loader"] }
schemars = "0.8.21"
serde = { version = "1.0.215", features = ["derive"] }
serde_json = "1.0.133"
Expand Down
6 changes: 5 additions & 1 deletion src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,13 @@ impl Generator<'_> {

let out_file = BufWriter::new(File::create(&file_path)?);

self.tpl.render_to_write(ctx, out_file)?;
let state = self.tpl.render_to_write(ctx, out_file)?;

if !self.flags.no_postprocess {
if let Some(extra_generated_file) = state.get_temp("extra_generated_file") {
self.postprocessor
.add_path(Utf8Path::new(extra_generated_file.as_str().unwrap()));
}
self.postprocessor.add_path(&file_path);
}

Expand Down
5 changes: 3 additions & 2 deletions src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use heck::{
ToLowerCamelCase as _, ToShoutySnakeCase as _, ToSnakeCase as _, ToUpperCamelCase as _,
};
use itertools::Itertools as _;
use minijinja::{path_loader, value::Kwargs, Value};
use minijinja::{path_loader, value::Kwargs, State, Value};

pub(crate) fn env(tpl_dir: &Utf8Path) -> Result<minijinja::Environment<'static>, minijinja::Error> {
let mut env = minijinja::Environment::new();
Expand Down Expand Up @@ -146,8 +146,9 @@ pub(crate) fn env(tpl_dir: &Utf8Path) -> Result<minijinja::Environment<'static>,
env.add_function(
// For java lib we need to create extra files.
"generate_extra_file",
|filename: Cow<'_, str>, file_contents: Cow<'_, str>| {
|state: &State, filename: Cow<'_, str>, file_contents: Cow<'_, str>| {
fs::write(&*filename, file_contents.as_bytes()).unwrap();
state.set_temp("extra_generated_file", filename.into());
},
);

Expand Down