Incrementing The Project Version Number Using NAnt

The following code depends on getVersion.

<target name="incrementBuildNumber" description="Increments The Build Number" depends="getVersion">
    <script language="C#">
            <include name="System.dll"/>
            <include name="System.Xml.dll"/>
            <import namespace="System"/>
            <import namespace="System.IO"/>
            <import namespace="System.Text"/>
            <import namespace="System.Text.RegularExpressions"/>
        public static void ScriptMain(Project project)
            string file;

            using (StreamReader reader = File.OpenText(project.Properties["project.version.file"]))
                file = reader.ReadToEnd();

            string version = project.Properties["project.version"];
            string[] versions = version.Split('.');
            int buildNumber = int.Parse(versions[3]);
            string newVersion = string.Format("{0}.{1}.{2}.{3}", versions[0], versions[1], versions[2], ++buildNumber);

            project.Properties["project.version"] = newVersion;
            file = file.Replace(version, newVersion);

            using (StreamWriter writer = new StreamWriter(project.Properties["project.version.file"]))
    <call target="commitAssemblyInfo"/>

The script works simply by getting the current version number and then replacing it with the build number incremented (it assumes you use all four version number sections). It will also replace any other references to the version number in the file, keeping them consistent.

The script could be enhanced using regex trickery to target a specific instance of the version number (e.g. those between AssemblyVersion(”…”) etc), allow for use of only the first few number sections, or be self-dependent if desired.

Committing the changes to subversion:

<target name="commitAssemblyInfo" description="Commits the assembly information file to subversion">
    <exec program="${subversion.exe}" workingdir="./">
        <arg value="commit"/>
        <arg value="${project.version.file}"/>
        <arg value='-m "Incremented Version Number To ${project.version}"'/>

The ${subversion.exe} property should reference your tortoise subversion.exe file. Note: the user account calling the script will be the user account used to submit the file to the subversion repository – this means if you have a build server automating this process the service account must have access to commit changes.