Commits

Mark Heath committed 59d0bfd

Included ability to update both AssemblyVersion and AssemblyFileVersion

  • Participants
  • Parent commits 0d027ff

Comments (0)

Files changed (7)

SetVersionTask/CSharpUpdater.cs

 
 namespace SetVersionTask
 {
+    public class CSharpVersionUpdateRule
+    {
+        private VersionUpdateRule updateRule;
+        public CSharpVersionUpdateRule(string attributeName, string updateRule)
+        {
+            this.AttributeName = attributeName;
+            this.updateRule = new VersionUpdateRule(updateRule);
+        }
+        public string AttributeName { get; private set; }
+        public string Update(VersionString v) { return this.updateRule.Update(v); }
+    }
+
     public class CSharpUpdater
     {
-        private VersionUpdateRule assemblyVersionUpdateRule;
+        private List<CSharpVersionUpdateRule> updateRules;
 
-        public CSharpUpdater(VersionUpdateRule assemblyVersionUpdateRule)
+        public CSharpUpdater(string newAssemblyVersion, string newAssemblyFileVersion = null)
         {
-            this.assemblyVersionUpdateRule = assemblyVersionUpdateRule;
+            this.updateRules = new List<CSharpVersionUpdateRule>();
+            if (!String.IsNullOrEmpty(newAssemblyVersion))
+            {
+                this.updateRules.Add(new CSharpVersionUpdateRule("AssemblyVersion", newAssemblyVersion));
+            }
+            if (!String.IsNullOrEmpty(newAssemblyFileVersion))
+            {
+                this.updateRules.Add(new CSharpVersionUpdateRule("AssemblyFileVersion", newAssemblyFileVersion));
+            }
+            // n.b. there is also AssemblyInformationalVersion
         }
 
         public void UpdateFile(string fileName)
 
         private string UpdateLine(string line)
         {
+            foreach (var rule in updateRules)
+            {
+                if (UpdateLineWithRule(ref line, rule))
+                {
+                    break;
+                }
+            }
+            return line;
+        }
+
+        public static bool UpdateLineWithRule(ref string line, CSharpVersionUpdateRule rule)
+        {
             VersionString v = null;
-            var g = GetVersionString(line, "AssemblyVersion");
+            bool updated = false;
+            var g = GetVersionString(line, rule.AttributeName);
             if (g != null)
             {
                 VersionString.TryParse(g.Value, out v);
             }
-            if (v == null)
+            if (v != null)
             {
-                return line;
+                string newVersion = rule.Update(v);
+                line = line.Substring(0, g.Index) + newVersion + line.Substring(g.Index + g.Length);
+                updated = true;
             }
-            else
-            {
-                string newVersion = assemblyVersionUpdateRule.Update(v);
-                return line.Substring(0, g.Index) + newVersion + line.Substring(g.Index + g.Length);
-            }
+            return updated;
         }
 
-        // currently just works on AssemblyVersion
-        // AssemblyFileVersion
-        // AssemblyInformationalVersion
         public static Group GetVersionString(string input, string attributeName)
         {
             var commentIndex = input.IndexOf("//");

SetVersionTask/Properties/AssemblyInfo.cs

 // General Information about an assembly is controlled through the following 
 // set of attributes. Change these attribute values to modify the information
 // associated with an assembly.
-[assembly: AssemblyTitle("MsBuildSetVersion")]
+[assembly: AssemblyTitle("SetVersion MSBuild Task")]
 [assembly: AssemblyDescription("")]
 [assembly: AssemblyConfiguration("")]
 [assembly: AssemblyCompany("")]
-[assembly: AssemblyProduct("MsBuildSetVersion")]
-[assembly: AssemblyCopyright("Copyright ©  2011")]
+[assembly: AssemblyProduct("SetVersion MSBuild Task")]
+[assembly: AssemblyCopyright("Copyright © Mark Heath 2011")]
 [assembly: AssemblyTrademark("")]
 [assembly: AssemblyCulture("")]
 
 // You can specify all the values or you can default the Build and Revision Numbers 
 // by using the '*' as shown below:
 // [assembly: AssemblyVersion("1.0.*")]
-[assembly: AssemblyVersion("1.0.10.0")]
-[assembly: AssemblyFileVersion("1.0.0.0")]
+[assembly: AssemblyVersion("1.0.14.0")]
+[assembly: AssemblyFileVersion("1.0.14.0")]

SetVersionTask/SetVersion.cs

         [Required]
         public string FileName { get; set; }
 
-        [Required]
-        public string Version { get; set; }
+        public string AssemblyVersion { get; set; }
+        public string AssemblyFileVersion { get; set; }
 
         public override bool Execute()
         {
             {
                 if (this.FileName.EndsWith(".cs", StringComparison.OrdinalIgnoreCase))
                 {
-                    var updater = new CSharpUpdater(new VersionUpdateRule(Version));
+                    var updater = new CSharpUpdater(AssemblyVersion, AssemblyFileVersion);
                     updater.UpdateFile(FileName);
                 }
                 else if (this.FileName.EndsWith(".nuspec", StringComparison.OrdinalIgnoreCase))

SetVersionTask/SetVersionTask.csproj

     <SetVersionPath>..\Tools\</SetVersionPath>
   </PropertyGroup>
   <UsingTask TaskName="SetVersion" AssemblyFile="$(SetVersionPath)SetVersionTask.dll" />
-  <Target Name="UpdateVersionNumber" Condition="Exists('$(SetVersionPath)SetVersionTask.dll')">
+  <Target Name="UpdateVersionNumber" Condition="Exists('$(SetVersionPath)SetVersionTask.dll') AND '$(Configuration)' == 'Release'">
     <Message Text="Updating Version..." />
-    <SetVersion FileName="Properties\AssemblyInfo.cs" Version="=.=.+.=" Condition="'$(Configuration)' == 'Release'"/>
+    <SetVersion FileName="Properties\AssemblyInfo.cs" AssemblyVersion="=.=.+.=" AssemblyFileVersion="=.=.+.=" />
   </Target>
   <Target Name="BeforeBuild" DependsOnTargets="UpdateVersionNumber">
   
   </Target>
-  <Target Name="UpdateTools">
-    <!-- need to do a release build first, but can't include that here because MSBuild will be referencing the target DLL -->
-    <Copy SourceFiles="bin\Release\SetVersionTask.dll" DestinationFolder="$(SetVersionPath)" />
-  </Target>
   <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
        Other similar extension points exist, see Microsoft.Common.targets.
   <Target Name="AfterBuild">

SetVersionTaskTests/CSharpUpdaterTests.cs

             var versionString = CSharpUpdater.GetVersionString(input, "AssemblyVersion");
             Assert.Null(versionString, "Expected no match");
         }
+
+        [TestCase("[assembly: AssemblyVersion(\"1.2.3.4\")]", "=.=.+.=", "AssemblyVersion", "[assembly: AssemblyVersion(\"1.2.4.4\")]")]
+        [TestCase("[assembly: AssemblyFileVersion(\"1.2.3.4\")]", "+.5.+.7", "AssemblyFileVersion", "[assembly: AssemblyFileVersion(\"2.5.4.7\")]")]
+        public void CanUpdateRuleWithString(string inLine, string rule, string attributeName, string outLine)
+        {
+            string line = inLine;
+            CSharpUpdater.UpdateLineWithRule(ref line, new CSharpVersionUpdateRule(attributeName, rule));
+            Assert.AreEqual(outLine, line);
+        }
     }
 }

Tools/SetVersionTask.dll

Binary file added.
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" DefaultTargets="UpdateTools" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+    <Target Name="UpdateTools">
+        <Copy SourceFiles="SetVersionTask\bin\Release\SetVersionTask.dll" DestinationFolder="Tools\" />
+    </Target>
+</Project>