Using ANT and ColdFusion to Emulate Rails Generators
One of the nice things about Rails is how it can automatically spit out common code for you. For example, if you need a new Model or Controller, Rails can spit one out for you, ready to be customized, with one line of code.
I've thought for a while that similar capabilities could be obtained using Apache ANT, which comes built in to Eclipse or can be downloaded separately and run from the command prompt. Last night I had a few minutes so I decided to try it for fun. And it seems to work pretty well!
For example, in a Model-Glue app, I could execute an ANT task called create_controller.xml. What this would do is prompt me for the name of the controller. Say I enter "StoreController". Then it copies the Controller from the Model-Glue application template folder into my application's controller folder and names it StoreController.cfc. Then it embeds new XML into the Model-Glue config XML file like:
<controller name="StoreController" type="deli.controller.Controller">
<message-listener message="OnRequestStart" function="OnRequestStart" />
<message-listener message="OnQueueComplete" function="OnQueueComplete" />
<message-listener message="OnRequestEnd" function="OnRequestEnd" />
</controller>
into the controllers block. It does this by doing some replace commands on the Model-Glue XML, as well as replacing the controller name and the application name. It does the same sort of replace within the CFC to embed the proper name and path.
I also did this with a create_event.xml file. This prompts me for the name of the event, and a view file name, and then creates the XML for the event, copies a view template into my views folder, and name it to match the view file name I specified.
If you've used the Model-Glue application template to create a new application, you know it already does some copying of files and some replacing. It wouldn't be hard to imagine it also copying and creating a set of build XML files to perform common tasks like this. These build XML files would all be set up specific to the newly generated app, with the proper paths and application name already embedded within them. And obviously the same sort of thing could be done with any framework (ColdBox, Fusebox, Mach-II, etc.)
These two examples were quite simple to create. And while they don't save a huge amount of time, it does save some time. I don't have to worry about creating new view or controller stub files, or creating the base XML in the config file. The build XML will do this for me in about one second. I could imagine making these do much more complex things if necessary, since ANT has a very large set of tasks that it can perform, including RegEx processing and replacement.
I was curious to see if anyone had thought about something like this before, and if this sounds like it would be useful.




Reference for <Cfant>
http://coolskool.blog-city.com/cfant__undocumented...
@Terrence: interesting idea but I'm a bit leery of relying on an undocumented tag. However, if you go the route of actually using CF to generate the target item, you could actually skip ANT altogether and just have the framework do the generation. For example, imagine in Model-Glue (or any framework), when in development mode I could run a special event like index.cfm?event=generate&item=controller&name=StoreController and the framework itself would copy or create the appropriate files as well as modify the config XML. Heck, maybe even a hidden div could always show up when you're in dev mode that you can open up. It would show a form that contains drop downs and text input fields to let you perform these actions any time you want. I suppose the real issue with all of the generation is how much control is one willing to give up over how things get generated, in exchange for making the generation easy?
I've had something similar in mind especially for Model-Glue apps. It's been on my list to do but I hadn't gotten to it yet. The idea was not just to generate a controller or events but the whole Model-Glue app from the template. This would include other files with the ColdSpring config and the Reactor config. A web interface would be kick ass to have. On top of that, generating new event handlers and refreshing the model-glue app would be awesome. Maybe we can work on something like that together if you are interested.
I'm actually writing the same for ColdBox ...
*runs of to code madly*
*thunder, evil laugh, lightening*
MD