blog.jj5.net (2003 to 2005)

Versioning build outputs with nant

Sat Feb 5 18:08:00 UTC+1100 2005

Categories:

Version format:

[n digit major starting at 0].
[0 or 2 digit minor starting at 10].
[(count of days since (last century UTC modulo 20 years)) + 1000].
[(count of 10 second intervals since midnight UTC) + 1000]

Repositry layout:

svn://terminator.matrix.jj5.net/proj/company-name/client-name/project-name/dev/major.minor

Working copy layout:

f:\file\proj\company-name\client-name\project-name\dev\major.minor

NAnt bits for updating the file containing the assembly version (this example uses AssemblyInfo.cs, although I don't use that)

    <property name="version" value="" />
    <script language="C#">
      <code><![CDATA[
        public static void ScriptMain( Project project ) {

          Regex majorMinorExpression = new Regex( @"([0-9]+)\.([0-9]+)" );
          Match majorMinorMatch = majorMinorExpression.Match( project.BaseDirectory );

          String majorMinor = majorMinorMatch.Result( "$1.$2" );

          String sourcePath = Path.Combine( project.BaseDirectory, "src" );
          String versionFile = Path.Combine( sourcePath, "AssemblyInfo.cs" );
          String newVersionFile = Path.GetTempFileName();

          StreamWriter writer = new StreamWriter( newVersionFile );
          StreamReader reader = File.OpenText( versionFile );
         
          Regex expression = new Regex(@"^\s*\[assembly:\s*AssemblyVersion\(.*");
          String line = reader.ReadLine();

          try {

            while ( line != null ) {

              Match match = expression.Match( line );

              if ( match.Success ) {

                DateTime baseYear = new DateTime( 2000, 1, 1, 0, 0, 0, 0, System.Globalization.CultureInfo.InvariantCulture.Calendar );
                DateTime pointInTime = DateTime.Now.ToUniversalTime();

                TimeSpan period = pointInTime.Date - baseYear;
                Int32 majorInterval = period.Days + 1000;

                period = pointInTime.TimeOfDay.Duration();
                Int32 minorInterval = ( ( (Int32) period.TotalSeconds ) / 10 ) + 1000;

                String version = majorMinor + String.Format( ".{0}.{1}", majorInterval, minorInterval );
                project.Properties[ "version" ] = version;

                writer.WriteLine( String.Format( "[assembly: AssemblyVersion( \"{0}\" )]", version ) );

              }
              else {

                writer.WriteLine( line );

              }

              line = reader.ReadLine();

            }
          }
          finally {
            writer.Close();
            reader.Close();
          }

          File.Copy( newVersionFile, versionFile, true );
          File.Delete( newVersionFile );

        }
      ]]></code>
    </script>

    <echo message="Version: ${version}" />


Copyright © 2003-2005 John Elliot