Automated build numbers for Flash Applications with Sprouts

This article will demonstrate how to use compile time variables to set a build number and environment type. I will be using Project Sprouts to compile a swf with the mxmlc compiler.

We'll start by creating a new variable in our rakefile like this. Since this number will always show up when you run rakeI prefixed it with dev so my local build will never get confused others.

 
build_number = '"\'dev.1.1.1.1001\'"'  
 

Quark #1. It took me a few tries to get the quotes right. Since this var is passed through a command shell then into the mxmlc the triple quotes are necessary. If you don't you will be likely to see some strange results and your swf just not compiling at all. If you use the string above and just replace dev.1.1.1.1001 you should be in good shape.


The next step it to add our version number to either the project model or a task via the define method. I decided to add the define method to the task since I would need to change the production variable for the deploy task.

 
desc 'Compile and run the application for debugging'
debug :debug do |d|
  d.define = "CONFIG::production,false -define=CONFIG::version," + build_number
end
 
desc 'Compile and run the test harness'
unit :test do |d|
  d.define = "CONFIG::production,false -define=CONFIG::version," + build_number
end
 
desc 'Compile for deployment'
deploy :deploy  do |p|
  p.define = "CONFIG::production,true -define=CONFIG::version," + build_number
end
 

Did you notice how I'm passing multiple variables in the same line? This is because the define method can't be called more then one time. It doesn't look like this is by design and might be a little bug in the task. Getting around this is easy enough. You just need to append "-define=" for all other variables.

This next snippet is the ActionScript 3.0 code demonstrates how to access your variable.

 
package {
  class Constants {
    public static const VERSION:String        = String(CONFIG::version);
    public static const IS_PRODUCTION:Boolean = CONFIG::production;
  }
}
 

Now that you have these lines of code in your project the next time you try to run AsDoc everything is going to melt down. AsDoc is very finicky and doesn't like the CONFIG variable. I was able to get around this by using a c preprocessor to remove these lines before AsDoc executes.

Finally you'll want to have your continuous integration system insert the correct version number. For example I have Hudson use sed to insert the correct version number.

 
sed -i 's/dev.1.1.1.1001/{RELEASE_NUMBER}/' rakefile.rb
 

And now you never have to manually update version numbers again!

If your using Flex to profile your application you will have to add these additional compiler arguments to the project properties.

Comments are closed.