Geeks With Blogs
Michael Flanakin's blog Food for thought...

Download UpdateVersion

I've been looking at NAnt pretty hard lately. The biggest problem that I've had so far is creating and updating version numbers. NAntContrib has a <version> task that will read in and update a version number specified in a text file, but that doesn't quite give me what I'm looking for. NAnt 0.85 is supposed to come with a method for reading assembly the version, but again, this falls pretty short of what I'm looking for.

After looking around, I found a pretty good solution: UpdateVersion. Currently in version 1.2, this tool will read through your AssemblyInfo.cs (or .vb) file and update the version number based on specified criteria. The tool was pretty nice. I didn't quite like the code, but it got the job done. It can read through any input file (or stream) and write output to the same or another file or even the output stream. There was a “pinning” option (-p or --pin) to specify a particular version number, but that didn't work. I fixed that and extended it a little bit. I modified the code to allow any pinned version (i.e. 1.0, 1.0.0, or 1.0.0.0). Whatever you don't specify will be generated based on the other options you specify. I also added a write version (-w or --write) option to write the version number (and only the version number) to the output stream. This allows a user to catch the new version that updates the AssemblyInfo.cs file(s).

I didn't end up using the -w option that I setup because of the order of events that needed to happen in my build script. So, I just decided to update the version manually and use the pinning option to update the project assemblies. I probably could've used the <version> task included in the NAntContrib project, but there were two reasons I didn't: (1) I'm using one separate properties file and don't want to have to deal with another one for the version number; and, more importantly, (2) when I updated to a 0.85 nightly build (8/12/2004), NAnt wouldn't recognize my NAntContrib tasks.

First, I've specified a “startdate” and “build.version” property to represent the project start date and custom major.minor version number. Second, I run the following build script to update the “build.version” property.

 

<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;
      if (build == -1) {
        DateTime start = Convert.ToDateTime(project.Properties["startdate"]);
        Calendar calendar = Thread.CurrentThread.CurrentCulture.Calendar;
        int months = ((calendar.GetYear(DateTime.Today) 
          - calendar.GetYear(start)) * 12) 
          + calendar.GetMonth(DateTime.Today) 
          - calendar.GetMonth(start);
        int day = DateTime.Now.Day;
        build = (months * 100) + day;
      }
      if (revision == -1) {
        TimeSpan difference = DateTime.Now.Subtract(DateTime.Today);
        revision = (int)(difference.TotalSeconds / 10);
      }
      version = new Version(major, minor, build, revision);
      project.Properties["build.version"] = version.ToString();
    }
  ]]></code>
</script>

 

I hope this helps anyone else looking for a version numbering system for NAnt. After I updated the version number, everything else was pretty easy - I looped thru each AssemblyInfo.cs file in the solution directory and updated the versions using my modified UpdateVersion.

If anyone is interested in the UpdateVersion code, let me know. I sent an email to Matt about getting it updated, but haven't heard from him. I'll give it a few days.

Update (4/4/2008): Changed download link to CodePlex project

Posted on Monday, August 16, 2004 2:04 AM .NET , Development | Back to top

Copyright © Michael Flanakin | Powered by: GeeksWithBlogs.net