Tutorial: Building Version Numbers into SWFs using ANT and SVN

A colleague suggested it would be useful to build in version numbers into SWFs so that you could simply right click and see just what version you were looking at. Think in terms of QA or a client on the phone. With continuous integration it is useful to…

A colleague suggested it would be useful to build in version numbers into SWFs so that you could simply right click and see just what version you were looking at. Think in terms of QA or a client on the phone. With continuous integration it is useful to be able to say “hey just right click… so it says r12345?”. The r12345 here would refer to the revision number in Subversion. This makes bug tracking say using Trac much more integrated.

Sample (zip at end):

The premise is simple. When you run your ANT build script from Eclipse to compile your SWF, it injects the current revision number into an AS file, which the swf then includes and uses to build a new context menu item containing said revision number. Here’s how to do it…

Installation and Pre-requisites:

You will need:

*Be sure you have the LATEST (1.4+) version of Subversion installed, not Tortoise, the Subversion client itself. Download from: http://subversion.tigris.org/project_packages.html

Step 1:

Assuming you already have Eclipse set up. Install svnANT by downloading the zip from the above URL and extracting the following two files:

  svnant.jar
  svnClientAdapter.jar

…into your ANT lib folder, e.g.:

  c:eclipsepluginsorg.apache.ant_1.6.5lib

Step 2:

Now before you can use your newly installed svn tasks in build files, you have to make sure these two JAR files are included in your external tool classpath. You can do this by clicking the downward arrow to the right of the External Tools icon that you normally press to run a build ( looks like the little red toolbox with the green icon run external tool ).

In the Classpath tab, add the two as external JARs. (see screenshot addSvnAnt.jpg).

adding svnAnt
(click to enlarge)

The alternative method is to just add the two JARs to your build file’s classpath, but this way saves that hassle. But if you do not wish to add these JARs to your ANT you must manually include them on a per instance basis in your ant task with the following line: <taskdef resource=”svntask.properties” classpathref=”project.classpath”/> where project.classpath is the folder they live in.

Now Restart Eclipse (use -clean flag if you experience problems).

The Build File:

You can now make use of the new tags available to you to perform any Subversion related tasks you wish. The docs are avaible as part of the svnANT download zip. In this case we will get the latest revision number for our src folder and then add that to a file which we can #include which we will include to add our context menu to our base SWF.

Step 3:

In the build file, add another target that looks like this:

<property name="src.revision" value="null" />
<tstamp><format property="today" pattern="d-MMMM-yyyy, hh:mm aa" locale="en"/></tstamp>
<target name="Include_SVN_Revision" >
<svn javahl="false" >
<status path="${main.file}" revisionProperty="src.revision" />
</svn>
<echo>Building revision::::: ${src.revision}</echo>
<copy file="${basedir}/revision_template.as" tofile="${source.dir}/revision.as" overwrite="true" >
<filterset>
<filter token="revision" value="${src.revision}" />
<filter token="timestamp" value="${src.revision}" />
<filter token="user" value="${user.name}" />
</filterset>
</copy>
</target>

( ${main.file} is just an ANT property pointing to your base/application FLA. )

You might just want to look at the build file in the download at the end. Basically all this is doing is injecting some meta data into an ActionScript file which we can later include…

Step 4:

In your FLA (or AS class), you just need to #include the revisions.as file, and add the following code to add the context menu:

var cm:ContextMenu = new ContextMenu();
var revisionCM:ContextMenuItem =
    new ContextMenuItem( "r"+_level0.svnRevision, function(){} );
cm.customItems.push( revisionCM );
this.menu = cm;

That’s it. Sample should be clearer than the above garb. Here’s a sample download and below is a sample swf:

3 thoughts on “Tutorial: Building Version Numbers into SWFs using ANT and SVN”

  1. Of course you may want to add in code to remove this when it recognises that it is running on a LIVE environment if you wish!

  2. Richard,

    You may be interested in checking our Parabuild it provides build numbers and Subversion change list numbers that a build runs at. This way there is no need to alter your build scripts to mess with Subversion.

    Accessing these variables from Ant as easy as

    Hope this helps.

    Regards,

    Slava Imeshev

Comments are closed.