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.

Adding Custom Toolbar Buttons To VS2008

So you’re a smart cookie and you’ve written yourself some scripts to build your DAL or perhaps to deploy or package for a different environment; However, when you want to run said scripts you have to open up a command window, navigate to the working folder and run the scripts. Not only is this a pain and waste of valuable time, it also inevitably results in a taskbar full of command prompt windows! Well, there is a quick and easy way to add custom buttons to your Visual Studio 2008 toolbar which will fire off your scripts and display the results in the output window.

The first step is to ensure you have a consistent approach to how your projects are set up in order to run your scripts. After all, you don’t want to have to create a unique toolbar button for every single application you’re developing. One simple approach to this is to create a batch file of the same name (e.g. GenerateDAL.bat) for each application which can then call of to project specific actions. Once you have a consistent pattern to your projects your custom toolbar buttons will become much more useful and save you lots of time.

Steps for creating a custom toolbar button:

  1. In Visual Studio 2008 go to Tools -> External Tools…
  2. Click Add -> (Complete the form)
    Title: (Name of the script e.g. Generate)
    Command: (Location of script e.g. $(SolutionDir)CodeSmithCode.Generate.cmd if your script is in the folder ‘CodeSmith’ of all your project)
    Arguments: (Leave this one empty if you wish)
    Initial directory: (Location of script or the folder level from which you would usually call your script e.g. $(SolutionDir)CodeSmith)
    (Check Use output window to display results in the output window)
  3. Click Ok.
  4. Right-Click on the toolbar -> Click Customize…
  5. Select the Toolbars tab -> Click New -> (Enter a suitable name e.g. Scripts)
  6. Select the Commands tab.
  7. Under Categories scroll down and select Tools -> Under Commands scroll down to External Command 3 (this assumes you only have to default 2 commands in Visual Studio — if you have more than the external command number will match the order of your external commands list)
  8. Drag-and-Drop “External Command 3″ and/or any other external commands you created previously.
  9. Click Close (At this point the button should automatically be renamed to the title of the external command they are linked to).
  10. Drag the new toolbar group to where you want it on the toolbar and enjoy 🙂