Tuesday, January 22, 2008

Include SVN Revision number in the Assembly Info using Nant

Recently, I decided to include the revision number from the SVN repository into the assembly major/minor build information (e.g., 1.0.1234.0, where 1234 is the SVN revision number). The reason I had to do this, our applications gets installed locally at multiple user's computers primarily for testing/demo purposes and over time, it was getting hard to track the problems/bugs resported as it was too hard to figure which version was installed on their machine. And, nant is used as the primary build tool.

First, let's create a target to retrieve the latest SVN revision number.

<target name="RetriveSVNRevisionNumber" description="Retreiving the latest revision number of the working copy">
<echo message="Retrieving Subversion revision number"/>
<exec program="${tortoisesvn.dir}/SubWCRev.exe" >
<arg value="." />
<arg value="revision.include.default" />
<arg value="revision.include" />

The above section retrieves the version number into the local project. For more details, refer this link.

In our case, the contents of revision.include.default file looks like:

<project name="RevisionSpecificProperties">
<property name="revision.value" overwrite="true" value="$WCREV$" />

Ok, now after retrieving the version number, we need to update our global SolutionInfo.cs file that will hold the assembly information.

<target name="UpdateAssemblyInfo" depends="GenerateBuildNumber" description="Updating the Assembly Information by overwriting the SolutionInfo.cs file with a new one." >
<asminfo output="SolutionInfo.cs" language="CSharp">
<import namespace="System.Reflection" />
<import namespace="System.Runtime.InteropServices" />
<attribute type="AssemblyCompanyAttribute" value="Company Name" />
<attribute type="AssemblyVersionAttribute" value="${build.version}" />
<attribute type="AssemblyFileVersionAttribute" value="${build.version}" />

<target name="GenerateBuildNumber">
<script language="C#">
<import name="System.Globalization" />
<import name="System.Threading" />
public static void ScriptMain(Project project) {
Version version = new Version(project.Properties["build.version"]);
int major = version.Major;
int minor = version.Minor;
int build = version.Build;
int revision = version.Revision;

build = Convert.ToInt32(project.Properties["revision.value"]);

version = new Version(major, minor, build, revision);
project.Properties["build.version"] = version.ToString();


<delete file="revision.include" />

The above targets, creates/updates an existing SolutionInfo.cs file which can be linked to all the projects in the solution so, all assemblies gets updated with the latest revision number in their build info.

1 comment:

propecia said...

Hello friend amazing and very interesting blog about Include SVN Revision number in the Assembly Info using Nant I really enjoyed reading this blog and I would like to have any update about it, thanks for sharing!!