Commits

Mark Heath committed 85be1b7

refactoring, now justs sets AssemblyVersion by default

Comments (0)

Files changed (8)

SetVersionTask/CSharpUpdater.cs

+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.IO;
+using System.Text.RegularExpressions;
+
+namespace SetVersionTask
+{
+    public class CSharpUpdater
+    {
+        private VersionUpdateRule assemblyVersion;
+
+        public CSharpUpdater(VersionUpdateRule assemblyVersion)
+        {
+            this.assemblyVersion = assemblyVersion;
+        }
+
+        public void UpdateFile(string fileName)
+        {
+            string[] lines = File.ReadAllLines(fileName);
+            var outlines = new List<string>();
+            foreach (var line in lines)
+            {
+                outlines.Add(UpdateLine(line));
+            }
+            File.WriteAllLines(fileName, outlines.ToArray());
+        }
+
+        private string UpdateLine(string line)
+        {
+            VersionString v = null;
+            var g = GetVersionString(line, "AssemblyVersion");
+            if (g != null)
+            {
+                VersionString.TryParse(g.Value, out v);
+            }
+            if (v == null)
+            {
+                return line;
+            }
+            else
+            {
+                string newVersion = assemblyVersion.Update(v);
+                return line.Substring(0, g.Index) + newVersion + line.Substring(g.Index + g.Length);
+            }
+        }
+
+        // currently just works on AssemblyVersion
+        // AssemblyFileVersion
+        // AssemblyInformationalVersion
+        public static Group GetVersionString(string input, string attributeName)
+        {
+            var commentIndex = input.IndexOf("//");
+            if (commentIndex != -1)
+            {
+                input = input.Substring(0, commentIndex);
+            }
+            string attributeMatch = String.Format("(?:(?:{0})|(?:{0}Attribute))", attributeName);
+
+            string pattern = @"^\s*\[assembly: " + attributeMatch + @"\(""(?<Version>[0-9\.\*]+)""\)\]";
+            Regex regex = new Regex(pattern);
+            var m = regex.Match(input);
+            if (m.Success)
+            {
+                return m.Groups["Version"];
+            }
+            return null;
+        }
+    }
+}

SetVersionTask/SetVersion.cs

             {
                 if (this.FileName.EndsWith(".cs", StringComparison.OrdinalIgnoreCase))
                 {
-                    UpdateCSharp();
+                    var updater = new CSharpUpdater(new VersionUpdateRule(Version));
+                    updater.UpdateFile(FileName);
                 }
                 else if (this.FileName.EndsWith(".nuspec", StringComparison.OrdinalIgnoreCase))
                 {
 
         }
 
-        private void UpdateCSharp()
-        {
-            string[] lines = File.ReadAllLines(FileName);
-            var outlines = new List<string>();
-            foreach (var line in lines)
-            {
-                VersionString v = null;
-                var g = VersionUtils.GetVersionStringFromCSharp(line);
-                if (g != null)
-                {
-                    VersionString.TryParse(g.Value, out v);
-                }
-                if (v == null)
-                {
-                    outlines.Add(line);
-                }
-                else
-                {
-                    string newVersion = UpdateVersion(v);
-                    outlines.Add(line.Substring(0, g.Index) + newVersion + line.Substring(g.Index + g.Length));
-                }
-            }
-        }
-
-        private string UpdateVersion(VersionString v)
-        {
-            throw new NotImplementedException();
-        }
-
         private void ValidateArguments()
         {
 

SetVersionTask/SetVersionTask.csproj

     <Reference Include="System.Xml" />
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="CSharpUpdater.cs" />
     <Compile Include="SetVersion.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="VersionString.cs" />
     <Compile Include="VersionUpdateRule.cs" />
-    <Compile Include="VersionUtils.cs" />
   </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
   <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 

SetVersionTask/VersionUpdateRule.cs

             }
             return String.Join(".", outParts.ToArray());
         }
+
+
     }
 }

SetVersionTask/VersionUtils.cs

-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Text.RegularExpressions;
-
-namespace SetVersionTask
-{
-    public static class VersionUtils
-    {
-
-        // AssemblyVersion
-        // AssemblyFileVersion
-        // AssemblyInformationalVersion
-
-        public static Group GetVersionStringFromCSharp(string input)
-        {
-            var commentIndex = input.IndexOf("//");
-            if (commentIndex != -1)
-            {
-                input = input.Substring(0, commentIndex);
-            }
-            string pattern = @"^\s*\[assembly: (?:(?:AssemblyFile)|(?:AssemblyInformational)|(?:Assembly))Version\(""(?<Version>[0-9\.\*]+)""\)\]";
-            Regex regex = new Regex(pattern);
-            var m = regex.Match(input);
-            if (m.Success)
-            {
-                return m.Groups["Version"];
-            }
-            return null;
-        }
-    }
-}

SetVersionTaskTests/CSharpUpdaterTests.cs

+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using NUnit.Framework;
+using SetVersionTask;
+
+namespace SetVersionTaskTests
+{
+    [TestFixture]
+    public class CSharpUpdaterTests
+    {
+        [TestCase("[assembly: AssemblyVersion(\"1.2.3.4\")]", "1.2.3.4")]
+        [TestCase("[assembly: AssemblyVersionAttribute(\"1.2.3.*\")]", "1.2.3.*")]
+        [TestCase("\t[assembly: AssemblyVersion(\"1.2.3\")] // some comment", "1.2.3")]
+        [TestCase("   [assembly: AssemblyVersion(\"1.2.*\")]", "1.2.*")]
+        [TestCase("[assembly: AssemblyVersion(\"11.22.33.44\")]", "11.22.33.44")]
+        public void CanFindAssemblyVersionAttribute(string input, string version)
+        {
+            var versionString = CSharpUpdater.GetVersionString(input, "AssemblyVersion");
+            Assert.NotNull(versionString, "Expected a match");
+            Assert.AreEqual(version, versionString.Value, "Version");
+        }
+
+        [TestCase("Version(\"11.22.33.44\")")]
+        [TestCase("//[assembly: AssemblyVersion(\"1.2.3.4\")]")]
+        [TestCase("[assembly: AssemblyCheeseVersion(\"11.22.33.44\")]")]
+        [TestCase("[assembly: Version(\"11.22.33.44\")]")]
+        [TestCase("[blah: AssemblyVersion(\"11.22.33.44\")]")]
+        public void ShouldNotMatch(string input)
+        {
+            var versionString = CSharpUpdater.GetVersionString(input, "AssemblyVersion");
+            Assert.Null(versionString, "Expected no match");
+        }
+    }
+}

SetVersionTaskTests/SetVersionTaskTests.csproj

   <ItemGroup>
     <Compile Include="VersionStringTests.cs" />
     <Compile Include="VersionUpdateRuleTests.cs" />
-    <Compile Include="VersionUtilsTests.cs" />
+    <Compile Include="CSharpUpdaterTests.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
   </ItemGroup>
   <ItemGroup>

SetVersionTaskTests/VersionUtilsTests.cs

-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using NUnit.Framework;
-using SetVersionTask;
-
-namespace SetVersionTaskTests
-{
-    [TestFixture]
-    public class VersionUtilsTests
-    {
-        [TestCase("[assembly: AssemblyVersion(\"1.2.3.4\")]", "1.2.3.4")]
-        [TestCase("[assembly: AssemblyFileVersion(\"1.2.3.*\")]", "1.2.3.*")]
-        [TestCase("[assembly: AssemblyInformationalVersion(\"1.2.3\")]", "1.2.3")]
-        [TestCase("   [assembly: AssemblyVersion(\"1.2.*\")]", "1.2.*")]
-        [TestCase("[assembly: AssemblyVersion(\"11.22.33.44\")]", "11.22.33.44")]
-        public void CanFindAssembleVersionAttribute(string input, string version)
-        {
-            var versionString = VersionUtils.GetVersionStringFromCSharp(input);
-            Assert.NotNull(versionString, "Expected a match");
-            Assert.AreEqual(version, versionString.Value, "Version");
-        }
-
-        [TestCase("Version(\"11.22.33.44\")")]
-        [TestCase("//[assembly: AssemblyVersion(\"1.2.3.4\")]")]
-        [TestCase("[assembly: AssemblyCheeseVersion(\"11.22.33.44\")]")]
-        [TestCase("[assembly: Version(\"11.22.33.44\")]")]
-        [TestCase("[blah: AssemblyVersion(\"11.22.33.44\")]")]
-        public void ShouldNotMatch(string input)
-        {
-            var versionString = VersionUtils.GetVersionStringFromCSharp(input);
-            Assert.Null(versionString, "Expected no match");
-        }
-    }
-}