03/21/2023, 8:49 AM
Is there a way to influence/process the KotlinPoet output text after it has been generated and before it is written to the output directory? I would like to plugin some formatter on the generated code before it is written to disk but I cannot immediately find a clean way to do it.


03/21/2023, 8:57 AM
You can generate the files then reformat the generated files. You don’t need to plug in anything at generation time. That said, this is the API that KP uses to output code
public fun writeTo(out: Appendable) { ... }
You can always implement a delegating Appendable that pre formats the code.

Benoit Quenaudon

03/21/2023, 9:00 AM
What’s the goal of formatting it?


03/21/2023, 9:05 AM
I was exploring the options to see if we can nicely format the generated code using ktlint because part of the generated code (and it's a lot of files) are going to end up in the source tree and published in the repository. We currently have ktlint as an additional build step in the repository but it would be nice if we can do it one-shot all from the same parser/generator binary for endusers that use the generator by itself outside of our repo.
the Appendable is likely the easiest way to do it, although then we will have to do a little duplication of the mkdirs/createFile code. which isn't that much work, but I wanted to check if it could be plugged in easily.