It has been awhile since I released Cairngen to the Flex community as an open source project. Cairngen, a one-shot code generation tool for Adobe Cairngorm was a tool I created to save time and rid myself of redundant copy / paste work I often found myself dealing with while developing with Cairngorm.
I have since realized that using Cairngen as a solution simply involved to much effort as developers were required to install Apache, PHP and AMFPHP as well as start the application server and run the Cairngen.swf every time there was a need to generate classes for a project built on Cairngorm. This complexity seemed out of sync with the simplicity which Cairngorm provides.
Cairngorm, as you may know, is a micro-architecture which provides a consistent architectural solution for developing mid to large scale applications with Adobe Flex. If you read my blog you will know that I am a huge advocate of Adobe Cairngorm and believe it to be the most effective way to build scalable Flex applications. The beauty of Cairngorm is in it’s simplicity – yes simplicity. Despite certain opinions, in my experience I have found that once you fully understand Cairngorm you begin to realize just how simple it really is. Eventually you no longer have to give any thought as to how to go about building an application in Flex, you get to a point where all you ever need to think about is what is most important to your application – business logic and display logic. Cairngorm takes care of all of the plumbing, but more importantly, Cairngorm trains you to think consistently when building applications. It is through this simplicity which Cairngorm provides that I realized how ineffective the original version of Cairngen was. But like I said, my original intension was to share it with the community “as is”, if it helps great, if it’s to much effort, then don’t use it.
There has also been many other useful tools that are similar to Cairngen which provide various solutions as well. I had mentioned in a previous post that part of my intension when releasing Cairngen was that someone with enough time available would build a proper eclipse plug in, which I think we all can agree would be the ideal solution.
With that being said, I still have been facing the same dilemma – I need the ability to create Cairngorm project directories, Events, Commands Delegates and so forth with minimal effort, in any environment, and have as much configurability as possible. My solution? ANT! I don’t know why I didn’t think of this originally as it makes perfect sense for many different reasons. Best of all it only took me about an hour or so to develop.
Cairngen 1.0
Cairngen 1.0 is built entirely in Ant and provides a solution for one-shot code generation of Adobe Cairngorm 2.2 classes. You can get started with Cairngen 1.0 simply by extracting the cairngen folder to the root of your Flex project and modifying the config.properties file to reflect your environment and project. If you are familiar with ANT, this will be very straight forward and you should be up and running in no time at all. Cairngen 1.0 is completely configurable. Developers can modify the Cairngorm templates, config.properties file and build.xml document as needed to suite their particular needs. Users can view code generation in real time via the console in Flex builder.
Cairngen 1.0 contains a default target which will create the typical Cairngorm project structure as follows:
business
commands
control
events
model
view
vo
Cairngen 1.0 will create a stub class for the applications ModelLocator, FrontController and Service.mxml for Cairngorm 2.2.
I have also retained the notion of a “Sequence” in order to identify the relationship between an Event, Command and Business Delegate. The Cairngen build.xml contains two targets for creating Sequences. In addition, developers can create Value Objects as well.
Targets
Below is a description of all targets available in Cairngen 1.0 build.xml:
main: cleans the project structure and generates project directories, the Cairngorm folder structure and default Cairngorm classes. Executes in approximately 750 milliseconds.
clean: deletes Cairngorm project directories and creates Cairngorm project directories. Executes in approximately 450 milliseconds.
delete-cairngorm-directories: deletes previously created Cairngorm project directories. Executes in approximately 380 milliseconds.
create-cairngorm-directories: creates Cairngorm project directories. Executes in approximately 385 milliseconds.
create-model-locator: creates the applications ModelLocator. Executes in approximately 450 milliseconds.
create-front-controller: creates the applications FrontController. Executes in approximately 450 milliseconds.
create-services: creates the applications ServiceLocator. Executes in approximately 450 milliseconds.
create-sequence-include-delegate: creates an Event – Command – Delegate sequence. Executes in approximately 482 milliseconds.
create-sequence-exclude-delegate: creates an Event – Command sequence, in which the Command will not implement mx.rpc.IResponder. Executes in approximately 460 milliseconds.
create-value-object: creates a typical Cairngorm value object. Executes in approximately 400 milliseconds.
Usage
To use Cairngen follow these simple steps:
- Download Cairngen 1.0.
- Extract Cairngen to your Flex project directory and refresh.
- Open the config.properties file and modify accordingly to reflect your projects environment.
- Right + Click the build.xml file and select Run as > Ant build. This will run the default target which generates the com.domain.project directories as well as the default Cairngorm folders, ModelLocator, ServiceLocator and FrontController. Ideally, if you are using Eclipse select Window > Show View > Other > Ant, select Ant and click OK. In the Ant panel select the “Add Buildfiles” icon (first icon on left). Select Cairngen > build.xml. Expand and you will see all tasks. double click main.
- To create sequences, edit the sequence property in the config.properties file (this is the name of the event, command and delegate) and run either of the sequence targets, make sure to refresh your project in order to see the generated files.
- To create a ValueObject edit the vo.name property and double click the create-value-object target. Refresh your project in order to see the generated files.
All Cairngorm-stub classes can be modified as needed. The template files are located in the templates/cairngorm2.2 directory.
I have a small amount of re-factoring which I plan to complete within the next few days, at which point I will post an update of the source. In the interim, there is enough to get you started. I welcome your thoughts and ideas in regards to making Cairngen a better tool while we await a release of an Eclipse plugin for Cairngorm. If you wish to contribute to Cairngen feel free to contact me.
Cairngen is released under the MIT license.
Sweet Eric. Thanks for the efforts. Now i need to think of something useful to make with Cairngorm!
Cheers