An Introduction to Gator-AS3-RobotLegs

Written by

NOTE There have been some pretty cool advancements made to Gator recently, however this has meant that gems and code bases are out of sync. I will update things here once we have resolved dependency issues.

I am a huge fan of generators as they for me, are a massive time saving tool in my day to day development. I have heard the arguments against them but personally, they speed up my work flow significantly for the repetitive tasks such as file/package creation and the initial injection of basic class code. As an added bonus my Unit Test classes also get generated and in some instances they are specific to the actual class type, it’s a no brainer for me to use these type of tools.

A little history

Up until now I have used Project Sprouts for both my project builds and generators and I still think its an amazing resource. However, there were a few minor issues and complexities (as a Ruby noob) I encountered on the generators which got myself and Dominic chatting on IM about potential solutions. Dominic highlighted an initial hurdle which resulted in Luke Bayes kindly taking time out to explain generators thus giving us an insight into the inner workings of Project Sprouts. The main problem with diving in and changing Project Sprouts was due to it being ultimately extremely powerful, but complex under the hood for someone like myself, simply put we just needed some classes generated.

Dominic found that Ruby on Rails also had generators and used a tool called Thor to facilitate this. Within a ridiculously short period of time Dominic had created a basic working model which he named Gator. This swiftly got segregated into a further library specifically for AS3 and MXML named gator-as3.

Gator-as3

The main use case scenario for generators in my development is when using the Robotlegs framework e.g. generating a view with it’s corresponding mediator plus unit tests. Another scenario is creating the typical directory structure of a project I use regularly, this became one of the first nuts Dominic cracked.

Project Templates

Please remember that gator and gator-as3 is all work in progress

Currently you can install something named a project template (naming conventions may change - I like project skeleton or Gator Bone). This basically is a directory structure which you instruct gator to install and then on creating a new project can be included and generated. Lets see this in action:

This article presumes you have Ruby installed on your dev environment, I use Ruby 1.9.2-p180.

1) Create a directory named RobotLegsTemplate and create the following directory structure including a simple README.md (this file can be omitted if you so choose):

1
2
3
4
5
6
7
8
RobotLegsTemplate/
  src/
      com/
          newtriks/
              controller/
              models/
              README.md
              views/

2) On command line install gator-as3 (this will also install gator):

1
gem install gator-as3 --pre

3) Install the template changing your path to the template accordingly:

1
gator project install /Users/newtriks/Development/dev/gator/RobotLegsTemplate

This will store your template in ~/.gator/templates/projects/.

4) Now create a new project based on this new template:

1
gator project new gator_example RobotLegsTemplate

Bam, you should now have a brand new project with the templates directory structure!

RobotLegs Generators

So the next step for me was to start porting across some of the generators I use in my daily development i.e. RobotLegs generators. I have created gator-as3-robotlegs on Github and so far covered the absolute basics needed for using RobotLegs within your project, this is all work in progress :)

1) Create a new directory named robotlegs_gator.

2) Gator projects need a configuration file where you (a) require the libraries i.e. gator-as3-robotlegs so they can be used within your project (b) define various project settings i.e. directory structure for Gator. So create the configuration file which has to be named gator.rb within your project root and add the following contents replacing your name and values as you see fit:

1
2
3
4
5
6
7
8
9
10
require "gator/as3/generators" 
require "gator/as3/generators/test/asunit4"
require "gator/as3/robotlegs/generators"
require "gator/as3/robotlegs/generators/test/asunit4"

project Gator::Project::ProjectBase.new
project.name = "MyProject"
project.options[:authors] = ["newtriks"]
project.layout[:source,:main,:as3] = "src"
project.layout[:source,:test,:as3] = "test"

This will tell Gator to use a directory structure as follows: [project root]/src & [project root]/test.

3) Install my gator-a3-robotlegs ruby gem:

1
gem install gator-as3-robotlegs --pre

4) Navigate into your project on command line and type the following to generate a view component (AS3) and mediator:

1
gator generate as3 robotlegs view_with_mediator com.newtriks.controller.CustomView --test

This should generate the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
gator_example/
  gator.rb
  src/
      com/
          newtriks/
              controller/
                  components/
                      CustomView.as
                  mediators/
                      CustomViewMediator.as
  test/
      AllTests.as
      com/
          newtriks/
              controller/
                  components/
                      CustomViewTest.as
                  mediators/
                      CustomViewMediatorTest.as

Nice huh?!

Currently I append certain names to particular templates generated i.e. Mediator, Service, Command so the following:

1
gator generate as3 robotlegs view_with_mediator com.newtriks.controller.CustomView --test

Appends the ‘Mediator’ string to the classname within the actual generator, this will be customisable in the future and with the ease generators and templates can be created you can custom make your own to your hearts content. Another point to add for the view/mediator generation is the views will be placed into a components directory and the mediator into mediators directory. This was something I pushed out on twitter recently and found that the general consensus leaned towards this architecture, but as above, this can easily be modified.

The –test parameter passed tells the generator to create the corresponding test, omit this to just create the class without a test.

More commands available for RobotLegs

Generate a RobotLegs Command Class:

1
gator generate as3 robotlegs command com.newtriks.controller.Startup --test

Generate a View Class:

1
gator generate as3 robotlegs view com.newtriks.views.CustomView --test

Generate a RobotLegs Mediator Class:

1
gator generate as3 robotlegs mediator com.newtriks.views.CustomMediator --test

Generate a RobotLegs Model (extends Actor) Class:

1
gator generate as3 robotlegs model com.newtriks.models.Login --test

Generate a RobotLegs Service (extends Actor) Class:

1
gator generate as3 robotlegs service com.newtriks.services.Remote --test

Command to generate a standard AS3 class:

1
gator generate as3 klass com.newtriks.Custom --test

TDD

As mentioned above passing in the value –test auto creates the corresponding Unit Test for the generated class. I use ASUnit4 for my testing and therefore have created a library of generators and templates as seen here. However, if you look on the gator-as3 repos you will see a FlexUnit set of generators and templates (in development). Simply changing the require in your gator.rb from:

1
require "gator/as3/generators/test/asunit4"

to:

1
require "gator/as3/generators/test/flexunit4"

Will instruct Gator to generate tests using FlexUnit4 instead of ASUnit4! How cool is that?! You can add whatever testing framework you want and structure your templates how you see fit…

I hope you will get some benefit from using Gator and have fun getting stuck in and having a play, any issues, requests or general feedback for anything Gator related then please go via the Gator Google group.

Further reading

Comments