Thursday, October 14, 2004

Automating Versioning in Visual Studio.Net

The rules of the calculation of build numbers are fairly simple. Normally a version number will look like this: AA.BB.CCDD.EEFF, where AA is the major version and BB is the minor version. These two are untouched: you have to set them yourself. The CCDD.EEFF part is modified automatically. CC will be used to represent the number of months elapsed since the starting of the project, and DD the number of days after those months. EE and FF will be the hours and minutes the build was created in that day. Therefore for example, if the project started on Jan 10, 2003 with version 1.0 and you run this macro on Mar 15, 2003 at 14:00, this is the build number that comes out of the calculation: 1.0.0205.1400. (I was told that this is the Microsoft's way of numbering the build.)

Basically the macro, once triggered, tries to open the file AssemblyInfo.cs from a path you specify, and locates the version string (something like this in C#: [assembly:AssemblyVersion("1.0.0220.1536")]) using regular expression. It then updates the old revision and build numbers based on some rules, and writes the file back. At the very end, an optional Build.BuildSolution command is called. If you want to update the build number every time a new build process is kicked off, run this macro instead of using the normal Build->Build Solution menu item.