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" />
</exec>
</target>

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$" />
</project>

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">
<imports>
<import namespace="System.Reflection" />
<import namespace="System.Runtime.InteropServices" />
</imports>
<attributes>
<attribute type="AssemblyCompanyAttribute" value="Company Name" />
<attribute type="AssemblyVersionAttribute" value="${build.version}" />
<attribute type="AssemblyFileVersionAttribute" value="${build.version}" />
</attributes>
</asminfo>
</target>

<target name="GenerateBuildNumber">
<script language="C#">
<imports>
<import name="System.Globalization" />
<import name="System.Threading" />
</imports>
<code>
<![CDATA[
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();

}

]]>
</code>
</script>
<delete file="revision.include" />
</target>


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!!