I dedicated a lot of time to the development of AjGenesis, my open source code generation engine (from a free model). It’s time to write down what I consider the key points of its implementation, the parts that makes AjGenesis what it is.
First: the use of a free model on memory, that it’s managed like a common object in a programming language. You don’t need to traverse XML path or something else. You can visit and manipulated one or mode models like any other dynamic object. I didn’t adopt a type model or a model with a schema: the model is free, you can put on each model what you want to have in it. The model is deserialized from XML or from text files, or it could be injected for your program: AjGenesis can be hosted as a library in your own application.
Second: the use of a dynamic, simple language, to execute simple or complex task, with access to rich class library. In AjGenesis, I use AjBasic, a interpreter that can access .NET. It can use, create and change dynamic objects, like the models.
Third: the use of templates, with expressions and commands (i.e. loops) written using the dynamic language.
(Some early alternatives were: use XML with schemas for models, and XSLT for transformations. I rejected them because I felt they had shortcomings that could affect the clear development of the system).
Building an Application with AjGenesis
Code Generation with AjGenesis in a Real Project
Models for Code Generation in AjGenesis
AjBasic: An Open Source Basic-alike Interpreter
Generating Code Hello World with AjGenesis
Having these three pillars, I used AjGenesis for years, in different technologies, languages and framework (from Java to .NET, from JSP to ASP.NET to ASP.NET MVC, from PHP4 to PHP5 to .NET4 to .NET5, DDL for MySql, MS SQL Server database, and one user is generating code for Ruby and Rails!). Notably, I’m generating text artifacts for technologies that didn’t exist at the beginning of the project, with minimal changes to AjGenesis itself. And it can run in Linux and Mac OSX thanks to the Mono project (see Running AjGenesis Using Mono and Ubuntu).
Ideas to implement: the ability of inject the source of the model (you could write your own serializer); you could choose the dynamic language to use (I plan to add AjSharp as dynamic language for tasks and templates).
But if I have the three key points I enumerated above, I could implement AjGenesis. Then, as a proof of concept, I started to implement the same ideas under Ruby: dynamic objects, that can be loaded from YAML (or XML, or other formats), the use of ruby for task programming, and its template using ERB (or, maybe, other template engines).
You can check my progress at: https://github.com/ajlopez/AjGenesisRb