Generate both build timestamp and version information
authorStefanos A <stapostol@gmail.com>
Sun, 15 Dec 2013 11:01:26 +0000 (12:01 +0100)
committerStefanos A <stapostol@gmail.com>
Sun, 15 Dec 2013 11:01:26 +0000 (12:01 +0100)
Installers/Nsis/Build.Installer.Nsis.csproj
Installers/Zip/Build.Installer.Zip.csproj
Source/Build.UpdateVersion/Build.UpdateVersion.csproj
Source/Build.UpdateVersion/Program.cs

index f58e10e..756f469 100644 (file)
@@ -49,7 +49,7 @@
   </ItemGroup>
   <Target Name="Build">
     <Exec Command="makensis opentk.nsi" />
-    <ReadLinesFromFile File="../../Version.txt">
+    <ReadLinesFromFile File="../../Timestamp.txt">
       <Output TaskParameter="Lines" ItemName="Version" />
     </ReadLinesFromFile>
     <Move SourceFiles=".\opentk.exe" DestinationFiles="@(Version-&gt;'../../opentk-%(Identity).exe')" />
index 79c8a9c..ad9d2c4 100644 (file)
@@ -51,7 +51,7 @@
     <!-- Zip the copy of the source tree -->
     <Exec Command="7z a -tzip opentk.zip ../../* -xr!opentk*.zip -xr!opentk*.exe -xr!.* -xr!obj -xr!bin -xr!Documentation/Source -xr!Automation -xr!*.suo -xr!*.pidb -xr!*.userprefs -xr!*vshost.exe* -xr!*.log" />
     <!-- Copy the zip file to the root directory and add a date stamp -->
-    <ReadLinesFromFile File="../../Version.txt">
+    <ReadLinesFromFile File="../../Timestamp.txt">
       <Output TaskParameter="Lines" ItemName="Version" />
     </ReadLinesFromFile>
     <Move SourceFiles=".\opentk.zip" DestinationFiles="@(Version-&gt;'..\..\opentk-%(Identity).zip')" />
index c4ff877..88783f2 100644 (file)
@@ -62,6 +62,7 @@
     <WarningLevel>4</WarningLevel>
   </PropertyGroup>
   <Target Name="BeforeBuild">
+    <Delete Files="..\..\Timestamp.txt" />
     <Delete Files="..\..\Version.txt" />
   </Target>
   <Target Name="AfterBuild">
index be3e6ba..6cdcdcf 100644 (file)
@@ -56,44 +56,41 @@ namespace Build.UpdateVersion
             }
 
             DateTime now = DateTime.UtcNow;
-            GenerateVersionInfo(now, Path.Combine(RootDirectory, "Version.txt"));
-            GenerateAssemblyInfo(now, Path.Combine(SourceDirectory, "GlobalAssemblyInfo.cs"));
-        }
+            string timestamp = now.ToString("u").Split(' ')[0];
+            // Build number is defined as the number of days since 1/1/2010.
+            double timespan = now.Subtract(new DateTime(2010, 1, 1)).TotalDays;
+            string build = ((int)timespan).ToString();
+            // Revision number is defined as the number of (git/svn/bzr) commits,
+            // or as the fraction of the current day, expressed in seconds, in case the
+            // working directory is not under source control.
+            string revision = RetrieveRevisionNumber(now);
 
-        static void GenerateVersionInfo(DateTime now, string file)
-        {
-            string version = null;
+            string major = Major;
+            string minor = Minor;
 
-            if (System.IO.File.Exists(file))
-            {
-                string[] lines = System.IO.File.ReadAllLines(file);
-                if (lines.Length > 0 && !String.IsNullOrEmpty(lines[0]))
-                {
-                    version = lines[0];
-                }
-            }
+            // Version is defined as {Major}.{Minor}.{Build}.{Revision}
+            string version = String.Format("{0}.{1}.{2}.{3}", major, minor, build, revision);
 
-            // If the file does not exist, create it.
-            if (version == null)
-            {
-                version = now.ToString("u").Split(' ')[0];
-                System.IO.File.WriteAllLines(file, new string[] { version });
-            }
+            Console.WriteLine("API compatibility key: {0}.{1}", major, minor);
+            Console.WriteLine("Build date: {0}", timestamp);
+
+            GenerateTimestamp(timestamp, Path.Combine(RootDirectory, "Timestamp.txt"));
+            GenerateVersion(version, Path.Combine(RootDirectory, "Version.txt"));
+            GenerateAssemblyInfo(major, minor, version, Path.Combine(SourceDirectory, "GlobalAssemblyInfo.cs"));
         }
 
-        static void GenerateAssemblyInfo(DateTime now, string file)
+        static void GenerateTimestamp(string timestamp, string file)
         {
-            // Build number is defined as the number of days since 1/1/2010.
-            // Revision number is defined as the fraction of the current day, expressed in seconds.
-            double timespan = now.Subtract(new DateTime(2010, 1, 1)).TotalDays;
-            string build = ((int)timespan).ToString();
+            System.IO.File.WriteAllLines(file, new string[] { timestamp });
+        }
 
-            string revision = RetrieveGitRevision() ?? RetrieveSvnRevision() ?? RetrieveBzrRevision() ?? RetrieveSeconds(timespan);
-            revision = revision.Trim();
-            
-            Console.WriteLine("Build timestamp was: " + build);
-            Console.WriteLine("Revision detected was: " + revision);
+        static void GenerateVersion(string version, string file)
+        {
+            File.WriteAllLines(file, new string[] { version });
+        }
 
+        static void GenerateAssemblyInfo(string major, string minor, string version, string file)
+        {
             File.WriteAllLines(file, new string[]
             {
                 "// This file is auto-generated through Source/Build.Tasks/GenerateAssemblyInfo.cs.",
@@ -109,11 +106,19 @@ namespace Build.UpdateVersion
                 "[assembly: AssemblyProduct(\"The Open Toolkit Library\")]",
                 "[assembly: AssemblyCopyright(\"Copyright © 2006 - 2013 the Open Toolkit Library\")]",
                 "[assembly: AssemblyTrademark(\"OpenTK\")]",
-                String.Format("[assembly: AssemblyVersion(\"{0}.{1}.0.0\")]", Major, Minor),
-                String.Format("[assembly: AssemblyFileVersion(\"{0}.{1}.{2}.{3}\")]", Major, Minor, build, revision),
+                String.Format("[assembly: AssemblyVersion(\"{0}.{1}.0.0\")]", major, minor),
+                String.Format("[assembly: AssemblyFileVersion(\"{0}\")]", version),
             });
         }
 
+        static string RetrieveRevisionNumber(DateTime now)
+        {
+            double timespan = now.Subtract(new DateTime(2010, 1, 1)).TotalDays;
+            string revision = RetrieveGitRevision() ?? RetrieveSvnRevision() ?? RetrieveBzrRevision() ?? RetrieveSeconds(timespan);
+            revision = revision.Trim();
+            return revision;
+        }
+
         static string RetrieveSeconds(double timespan)
         {
             string revision = ((int)((timespan - (int)timespan) * UInt16.MaxValue)).ToString();