19 October 2006
Tutorial: Building Version Numbers into SWFs using ANT and SVN
Published on October 19th, 2006 @ 03:05:01 am, using 711 words, 2275 views
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:
- Eclipse IDE (usually with FDT/ASDT)
- Subversion (+optionally TortoiseSVN)*
- svnANT from http://subclipse.tigris.org/svnant.html
- 1 ANT build file
- 1 egg
*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:\eclipse\plugins\org.apache.ant_1.6.5\lib
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
).
In the Classpath tab, add the two as external JARs. (see screenshot addSvnAnt.jpg).
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
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:
