Here’s a short introduction to a tool I wrote that may be of interest to others here.
Textrude sits firmly in the ‘templating’ part of the code-generation spectrum. That is to say that it transforms simple data models into textual output via a template containing a mixture of textual scaffolding and control logic. It’s really intended as a “front end” for a compiler; I typically use it to generate “boilerplate” source code from data-sets such as lists of error codes.
If that sounds a bit abstract, here’s an actual example:
Model:
name,cost
laptop,1000
monitor,400
Template:
{{for item in model}}
const int {{item.name}} = {{item.cost}};
{{end}}
Output:
const int laptop = 1000;
const int monitor = 400;
The first notable thing here is that the model schema is automatically derived and made available to the template engine. I used a CSV model here but Textrude also understands JSON and YAML and supports “dotting into” structured models.
The template language is Scriban which provides a reasonable trade-off between text/control switching and power. Textrude supports template function libraries so it’s feasible to build up a pseudo-DSL to reduce the amount of 'surface template code.
The tool aims to address a couple of my pet peeves with code-generation in “real world” environments.
Firstly, it’s designed with build-system integration in mind; it has its own (weak) dependency checking mechanism built in and templates can easily access environment variables (e.g. to stamp generated code with a commit or branch id).
Secondly, it aims to dramatically reduce the length of the feedback cycle when developing templates and models. The UI version of the tool allows output to be generated on every change to the model or template. It’s therefore possible to evaluate the correctness of the template in real-time as it is edited.
If this sounds interesting, you can check it out on github. It’s just a “spare time” project so has a few rough edges but feel free to raise an issue or even submit a PR if you want.