Generating source code with Ruby templates
September 17th, 2007 Posted in softwareImagine that you have to write function calls in C for 20 analog inputs and 20 driver outputs for your embedded control unit. Then there is another control unit with a similar - but not identical - configuration, but with a different pin setup. To write and support all function calls is a tedious work.
The configuration is setup in a XML sheet and edited using Eclipse. The first approach was to use one XSLT to transform the XML data to documentation and another to generate source code. However, using XSLT it was very difficult to write something that was a little more advanced than a simple list. After the initial transformation I want to generate interface layer functions with type control, and this was almost impossible in XSLT, especially if you consider that it shall be possible to maintain the code later.
Using Ruby templates - ERB - the problem is easily solved, even if it is sometimes necessary to mix ordinary source within the template as I on the other hand don’t want a zillion files either. I’m working on that…
Note: Python or Perl would have solved the issue just as easy as Ruby, in fact I tried Python first but then I decided that I liked Ruby better (actually I have used Python for 5 years and lately I felt that I want to try something different - just for fun).