Jun 23

How to automatically version a Flex application

Tag: code, pythondenis @ 4:11 pm

I recently added automatic versioning to my Flex projects. This is quite useful when you want to know exactly which version of your application is currently running when it’s being debugged, deployed, tested, etc. This basic example will generate a version string containing a major version number, minor version number, svn revision number and a build number. The build number is incremented every time your application is built. It assumes you have python installed and use SVN as your version control system.

Storing your version information

We will use a simple text file to do that and we’ll call it .version. Its format will be:
major_no
minor_no
build_no
revision_no

Updating the version data

A small python script is used to do that. A build number is incremented each time and the revision number is retrieved from SVN. Here’s the versioner.py script (make sure it’s executable):

#!/usr/bin/python
v = open('.version')
major = int(v.readline().strip() or '2') # can change 2 for any major version number you'd like
minor = int(v.readline().strip() or '5') # idem
build = int(v.readline().strip() or '0') # idem
build += 1
import os, re
ri, ro = os.popen2("svn info", 'r')
rev = ro.read()
ri.close()
ro.close()
revre = re.compile('Revision: ([\d]+)')
revision = revre.search(rev).group(1)
o = open('.version', 'w')
o.write("%d\n%d\n%d\n%s\n" % (major, minor, build, revision))
o.close()

Running the version script

If you’re using Flex Builder, this is easily done:
Go to project properties.
Under the Builders tab, click New
Name it anything you like
Select the versioner.py script in location
Select the folder containing your .version file as the working directory
Check “During manual builds” and “During auto builds” in the Build Options
Once your builder is created, make sure it’s at the top.

Using the version information

You can embed it in your Flex application using the following code:

[Embed(source="../.version",mimeType="application/octet-stream")][Bindable]
 
public var version:Class;

and then you can access it at runtime using a function like this:

public function getVersion():String {var v:Array = String(new version).split('\n');
 
return v[0] + '.' + v[1] + '.' + v[3] + ' build ' + v[2];;
 
}

which will return something like ‘major.minor.revision build’.

Bookmark this page on: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • Reddit
  • StumbleUpon
  • Technorati

Leave a Reply