Cairngen Code Generator

Introducing Cairngen: Adobe Cairngorm Code Generator

Overview:
A tell tail sign of a well designed architectural framework is in it’s ability to facilitate code generation - and Adobe Cairngorm does just that.

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 this blog you will know that I am an 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.

Cairngen is a code generation tool for Adobe Cairngorm. I developed Cairngen in order to save time and rid myself of redundant copy / paste work I often found my team was dealing with while developing with Cairngorm. In addition to being very time consuming, copy / paste Event and Command creation is error prone. Cairngen addresses these issues. More importantly Cairngen allows developers to enforce consistency and best practices within their specific Cairngorm implementation.

Cairngen is built entirely in Ant, and provides a solution for generating Adobe Cairngorm classes from within the Eclipse IDE or the standalone Flex builder.

Getting started
You can get started with Cairngen simply by extracting the build directory to the root of your Flex project or by creating a generic Project in Eclipse and modifying the project.properties file to reflect your environment and project settings. 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 is completely configurable. Developers can modify the Cairngorm templates, properties files and build as needed to suite their particular needs. Users can view code generation in real time via the console. All code generation is written to a log file in your project’s root directory.

The Cairngen build file contains a default target [main] which will create the typical Cairngorm project structure as follows:

  • business
  • commands
  • control
  • events
  • model
  • view
  • vo
  • In addition, the main target will also create your applications ModelLocator, FrontController and Services.mxml and generate them to the appropriate packages.

    Sequences (use cases)
    Cairngen uses the term “Sequence” to identify the relationship between an Event, Command and Business Delegate. The Cairngen build.xml contains two targets for creating Sequences. One which creates and Event and Command and the other which creates an Event, Command and Business Delegate. When a sequence is generated the FrontController is modified with an additional addCommand invocation which automates the process of mapping an Event to it’s associated Command.


    Using Cairngen

    To get started with Cairngen follow these simple steps:

    1. Download Cairngen 2.1.
    2. Extract Cairngen to your Flex project directory, or create a new generic project and point it to the location where you extracted Cairngen 2.0
    3. Open the project.properties file and modify accordingly to reflect your projects environment.
    4. Right + Click the build.xml file and select Run as > Ant build. This will run the default target which generates the typical 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.
    5. To create sequences, edit the sequence.name property in the project.properties file (this is the name applied to the Event, Command and Business Delegate) and run either of the sequence targets.
    6. To create a ValueObject edit the vo.name property and double click the create-value-object target.

    All Cairngorm template classes can be modified as needed. The template files are located in the build/templates/cairngorm[release] directory.

    Feel free to share your thoughts and ideas in regards to making Cairngen a better tool.

    Cairngen is licensed under the MIT Licence.

    Cairngen utilizes Ant and Ant-contrib which are licensed under The Apache Software License

    Update 03.31.08: The Cairngen Project has be moved to Google Code.