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

5 Responses to “How to automatically version a Flex application”

  1. volte says:

    So, I tried all your steps except I made my script a shell script (running OS X here) and the script never seems to get called. I’m not sure if I’m setting my “Builders” up correctly or not, and there is little to no information about this aspect of Flex Builder on google, mostly because the name is too close to “Flex Builder” so saying “Flex Builders” is just completely drowned out.

  2. How to automatically version a Flex application « szisti’s blog says:

    [...] found a great solution for this at denislaprise.com (thank’s for [...]

  3. Andrew D. Goodfellow says:

    Thanks Denis! Very helpful.

    -Andy

  4. Sylvain Donnet says:

    Thanks Denis for the idea… I have not Python on my Vista workstation, so I rewrite it (only the build number, not the SVN version mgmt) under VBS, which is onto every Windows, in a ‘buildnumber.vbs’ file :

    Const ForReading = 1
    Const ForWriting = 2

    Set objFSO = CreateObject(”Scripting.FileSystemObject”)
    Set objFile = objFSO.OpenTextFile(”buildnumber.txt”, ForReading)

    BuildNumber = objFile.ReadLine
    objFile.Close

    BuildNumber = BuildNumber + 1

    Set objFile = objFSO.OpenTextFile(”buildnumber.txt”, ForWriting)
    objFile.WriteLine(BuildNumber)
    objFile.Close

    Other steps are quite yours : write a “buildnumber.txt” file at the Flex project root, declare in Flex Builders (use C:\Windows\System32\cscript.exe as the builder executable, and ${workspace_loc:/PixSoftware/buildversion.vbs} as argument.

    The only trouble I still have is that the counter goes forward 5 by 5 and not 1 by 1. As if the script was called several times per ‘Flex Build’…

  5. Dennis says:

    Great stuff! However, I did find that I had to split on “\r\n” to make it work… but invaluable information nonetheless!

Leave a Reply