I’ve been spending some quality time with NAnt over the past couple of days. I’m putting together a comprehensive set of build scripts that I’ll be using to build CLAW. The first set(s) of problems that I decided to tackle are related to the build process, build numbers, and Visual SourceSafe integration.
I’ve spent quite a bit of time reading various bits and pieces of documentation about N/Ant. What drives me crazy about all of the documentation that I’ve read is that none of them pay any serious attention to version control and build numbers. These two issues are critical in any software development process, particularly once you into situations where many different builds of your software can be found “in the wild”.
I have two very simple requirements for my NAnt script:
- For my daily build scenarios, I want to have the script auto-generate a new build number, get the latest source code files from VSS, compile everything, check the compilation artifiacts (DLL’s, EXE’s, PDB’s) back into VSS, and label everything using that build number.
- For debugging scenarios, I want to be able to re-build any version of my code. This involes getting the source code files that correspond to Version X, and compile everything.
The first thing that I had to do was implement a new NAnt task that will auto-generate build numbers at runtime. The build number is the third octet in a typical 64-bit FILEVERSION version number: major.minor.build_number.revsion_number.
My VersionTask implementation can use one of two algorithms to generate build numbers. The first algorithm simply increments the build number field of a FILEVERSION version number that can be found in the build.number file that resides in the same directory as the NAnt build file.
The second algorithm calculates a 4 digit build number. The first two digits are the number of months since the start of the project, and the last two digits are the current day. This is identical to the build number algorithm used by the .NET Frameworks team.
If you’re interested in looking at my alpha code for my VersionTask implementation, you can download it. Note that I revised this code since this message. See the updated entry in my weblog.
I also wanted to stamp the FILEVERSION of my file using the auto-generated build number. To make this happen, I need to inject the build number into an AssemblyInfo.cs file that would resemble:
`using System.Reflection; using System.Runtime.CompilerServices;
[ assembly: AssemblyFileVersion( “@version@” ) ]`
Note that I’m using an AssemblyFileVersion attribute to stamp the FILEVERSION resource in the generated assembly. Also notice that the version ID is encoded as “@version@”. To make this happen, I needed to implement Ant’s filtering capabilities.
The way I structured my build file, I checked the above file into VSS as AssemblyInfo.txt. Next, I defined a filter for the version number:
<filter token="version" value="${sys.version}"/>
Then, I used my modified CopyTask to generate the AssemblyInfo.cs file:
<copy file="${src}\AssemblyInfo.txt" tofile="${src}\AssemblyInfo.cs" filtered="true"/>
Note that the filtered attribute is set to true. In this case, I read the entire file into memory, and use Regex.Replace to replace all occurrances of the @version@ token with the version ID property.
If you’re interested in taking a peek at the code that I wrote to make this all possible in NAnt, you can download the bits from here. I’ll be testing these bits for a while first before submitting them to the good folks on the NAnt dev team for inclusion in the real distribution. Special thanks go out to Scott Hernandez, who patiently answered a number of dumb NAnt questions that I sent him.
2 archived reader comments
Riley (2003-06-18)
John - What is the status of this? Has it been included in NANT yet? Thx
Andrew (2003-07-23)
This is excellent stuff. Just what I was looking for. The only problem is that integrating the new source files with Nant is not so trivial. I notice that the Project.cs file is considerably different to the one that ships with Nant. Do you have any suggestions as I think that the Nant world is not going to move as fast as you did.
Many thanks
Andrew