Explorar o código

Migrating Version Numbers to major.minor.release format to align with mobile app versioning

frogsoftware hai 2 semanas
pai
achega
43c873a7c4

+ 4 - 131
InABox.Database/DataUpdater.cs

@@ -2,139 +2,14 @@
 using InABox.Core;
 using InABox.Core;
 using Microsoft.CodeAnalysis.Scripting;
 using Microsoft.CodeAnalysis.Scripting;
 using System.Collections.Generic;
 using System.Collections.Generic;
-using System.Diagnostics.CodeAnalysis;
+using System.Collections.Immutable;
 using System.Linq;
 using System.Linq;
 using System.Text;
 using System.Text;
-using System.Text.RegularExpressions;
 using System.Threading.Tasks;
 using System.Threading.Tasks;
+using NPOI.XWPF.Usermodel;
 
 
 namespace InABox.Database;
 namespace InABox.Database;
 
 
-public class DatabaseVersion : BaseObject, IGlobalConfigurationSettings
-{
-    public string Version { get; set; }
-
-    public DatabaseVersion()
-    {
-        Version = "0.00";
-    }
-}
-
-public class VersionNumber
-{
-    public int MajorVersion { get; set; }
-    public int MinorVersion { get; set; }
-
-    public string Release { get; set; }
-
-    public bool IsDevelopmentVersion { get; set; }
-
-    public VersionNumber(int majorVersion, int minorVersion, string release = "", bool isDevelopmentVersion = false)
-    {
-        MajorVersion = majorVersion;
-        MinorVersion = minorVersion;
-        Release = release;
-        IsDevelopmentVersion = isDevelopmentVersion;
-    }
-
-    private static Regex _format = new(@"^(\d+)\.(\d+)([a-zA-Z]*)$");
-    public static VersionNumber Parse(string versionStr)
-    {
-        if(versionStr == "???")
-        {
-            return new(0, 0, "", true);
-        }
-        var match = _format.Match(versionStr);
-        if (!match.Success)
-            throw new FormatException($"'{versionStr}' is not a valid version!");
-        return new(int.Parse(match.Groups[1].Value), int.Parse(match.Groups[2].Value), match.Groups[3].Value, false);
-    }
-    public static bool TryParse(string versionStr, [NotNullWhen(true)] out VersionNumber? version)
-    {
-        if (versionStr == "???")
-        {
-            version = new(0, 0, "", true);
-            return true;
-        }
-        var match = _format.Match(versionStr);
-        if (!match.Success)
-        {
-            version = null;
-            return false;
-        }
-        version = new(int.Parse(match.Groups[1].Value), int.Parse(match.Groups[2].Value), match.Groups[3].Value, false);
-        return true;
-    }
-
-    public static bool operator <(VersionNumber a, VersionNumber b)
-    {
-        if (a.IsDevelopmentVersion)
-        {
-            return false;
-        }
-        else if (b.IsDevelopmentVersion)
-        {
-            return true;
-        }
-        return a.MajorVersion < b.MajorVersion || 
-            (a.MajorVersion == b.MajorVersion &&
-                (a.MinorVersion < b.MinorVersion || 
-                    (a.MinorVersion == b.MinorVersion && string.Compare(a.Release, b.Release, StringComparison.Ordinal) < 0)));
-    }
-    public static bool operator >(VersionNumber a, VersionNumber b)
-    {
-        return b < a;
-    }
-
-    public static bool operator <=(VersionNumber a, VersionNumber b)
-    {
-        return !(b < a);
-    }
-
-    public static bool operator >=(VersionNumber a, VersionNumber b)
-    {
-        return !(a < b);
-    }
-
-    public override bool Equals(object? obj)
-    {
-        if(obj is VersionNumber v)
-        {
-            return this == v;
-        }
-        return false;
-    }
-
-    public override int GetHashCode()
-    {
-        if (IsDevelopmentVersion)
-            return 0;
-        return MajorVersion ^ MinorVersion ^ Release.GetHashCode();
-    }
-
-    public static bool operator ==(VersionNumber a, VersionNumber b)
-    {
-        if (a.IsDevelopmentVersion)
-            return b.IsDevelopmentVersion;
-        if (b.IsDevelopmentVersion)
-            return false;
-        return a.MajorVersion == b.MajorVersion && a.MinorVersion == b.MinorVersion && a.Release == b.Release;
-    }
-    public static bool operator !=(VersionNumber a, VersionNumber b)
-    {
-        if (a.IsDevelopmentVersion)
-            return !b.IsDevelopmentVersion;
-        if (b.IsDevelopmentVersion)
-            return true;
-        return a.MajorVersion != b.MajorVersion || a.MinorVersion != b.MinorVersion || a.Release != b.Release;
-    }
-
-    public override string ToString()
-    {
-        return IsDevelopmentVersion ? "???" : $"{MajorVersion}.{MinorVersion:D2}{Release}";
-    }
-}
-
 public static class DataUpdater
 public static class DataUpdater
 {
 {
     private static Dictionary<VersionNumber, List<DatabaseUpdateScript>> updateScripts = new();
     private static Dictionary<VersionNumber, List<DatabaseUpdateScript>> updateScripts = new();
@@ -164,8 +39,8 @@ public static class DataUpdater
     private static bool MigrateDatabase(VersionNumber fromVersion, VersionNumber toVersion, out VersionNumber newVersion)
     private static bool MigrateDatabase(VersionNumber fromVersion, VersionNumber toVersion, out VersionNumber newVersion)
     {
     {
         var versionNumbers = updateScripts.Keys.ToList();
         var versionNumbers = updateScripts.Keys.ToList();
-        versionNumbers.Sort((x, y) => x == y ? 0 : x < y ? -1 : 1);
-
+        versionNumbers.Sort();
+        
         newVersion = fromVersion;
         newVersion = fromVersion;
 
 
         int? index = null;
         int? index = null;
@@ -207,9 +82,7 @@ public static class DataUpdater
     private static void UpdateVersionNumber(VersionNumber version)
     private static void UpdateVersionNumber(VersionNumber version)
     {
     {
         if (version.IsDevelopmentVersion)
         if (version.IsDevelopmentVersion)
-        {
             return;
             return;
-        }
         var dbVersion = DbFactory.GetVersionSettings();
         var dbVersion = DbFactory.GetVersionSettings();
         dbVersion.Version = version.ToString();
         dbVersion.Version = version.ToString();
 
 

+ 14 - 0
InABox.Database/DatabaseVersion.cs

@@ -0,0 +1,14 @@
+using InABox.Configuration;
+using InABox.Core;
+
+namespace InABox.Database;
+
+public class DatabaseVersion : BaseObject, IGlobalConfigurationSettings
+{
+    public string Version { get; set; }
+
+    public DatabaseVersion()
+    {
+        Version = "0.00";
+    }
+}

+ 186 - 0
InABox.Database/VersionNumber.cs

@@ -0,0 +1,186 @@
+using System.Diagnostics.CodeAnalysis;
+using System.Text.RegularExpressions;
+
+namespace InABox.Database;
+
+public class VersionNumber : IComparable
+{
+    public int MajorVersion { get; }
+    public int MinorVersion { get; }
+    public string Release { get; }
+    
+    public bool IsDevelopmentVersion { get; }
+    public bool IsNewStyle { get; private set; }
+
+    public VersionNumber()
+    {
+        IsDevelopmentVersion = true;
+        MajorVersion = 0;
+        MinorVersion = 0;
+        Release = "0";
+        IsNewStyle = true;
+    }
+    
+    public VersionNumber(int majorVersion, int minorVersion)
+    {
+        IsDevelopmentVersion = false;
+        MajorVersion = majorVersion;
+        MinorVersion = minorVersion;
+        Release = "0";
+        IsNewStyle = true;
+    }
+    
+    public VersionNumber(int majorVersion, int minorVersion, int release)
+    {
+        IsDevelopmentVersion = false;
+        MajorVersion = majorVersion;
+        MinorVersion = minorVersion;
+        Release = release.ToString();
+        IsNewStyle = true;
+    }
+
+    public VersionNumber(int majorVersion, int minorVersion, string release)
+    {
+        IsDevelopmentVersion = false;
+        MajorVersion = majorVersion;
+        MinorVersion = minorVersion;
+        Release = release;
+        IsNewStyle = false;
+    }
+
+    private static readonly Regex _format =
+        new Regex(
+            @"^(?<major>\d+)\.(?<minor>\d+)(?:(?<oldstyle>[A-Za-z]+)|\.(?<newstyle>\d+))?$",
+            RegexOptions.Compiled);
+
+    
+    public static VersionNumber Parse(string versionStr)
+    {
+        if (!TryParse(versionStr, out var _result))
+            throw new FormatException($"'{versionStr}' is not a valid version!");
+        return _result;
+    }
+    
+    public static bool TryParse(string versionStr, [NotNullWhen(true)] out VersionNumber? version)
+    {
+        if (versionStr == "???")
+        {
+            version = new();
+            return true;
+        }
+        var match = _format.Match(versionStr);
+        if (!match.Success)
+        {
+            version = null;
+            return false;
+        }
+        
+        if (match.Groups["oldstyle"].Success)
+        {
+            version = new(int.Parse(match.Groups["major"].Value),
+                int.Parse(match.Groups["minor"].Value),
+                match.Groups["release"].Value
+            );
+        }
+        
+        if (match.Groups["newstyle"].Success)
+        {
+            version = new(int.Parse(match.Groups["major"].Value),
+                int.Parse(match.Groups["minor"].Value),
+                int.Parse(match.Groups["newstyle"].Value)
+            );
+        }
+        version = new(int.Parse(match.Groups["major"].Value),
+            int.Parse(match.Groups["minor"].Value),
+            0
+        );
+        
+        return true;
+    }
+    
+    public int CompareTo(object? obj)
+    {
+        if (obj is not VersionNumber other)
+            throw new ArgumentException($"Object must be of type {nameof(VersionNumber)}");
+        var _result = this < other ? -1 : this == other ? 0 : 1;
+        return _result;
+    }
+
+    public static bool operator <(VersionNumber a, VersionNumber b)
+    {
+        if (a.IsDevelopmentVersion) return false;
+        if (b.IsDevelopmentVersion) return true;
+
+        if (a.MajorVersion != b.MajorVersion)
+            return a.MajorVersion <  b.MajorVersion;
+        if (a.MinorVersion != b.MinorVersion)
+            return a.MinorVersion <  b.MinorVersion;
+        if (a.IsNewStyle && b.IsNewStyle)
+            return int.Parse(a.Release) < int.Parse(b.Release);
+        return String.Compare(a.Release,b.Release, StringComparison.OrdinalIgnoreCase) < 0;
+    }
+    public static bool operator >(VersionNumber a, VersionNumber b)
+    {
+        return b < a;
+    }
+
+    public static bool operator <=(VersionNumber a, VersionNumber b)
+    {
+        return !(b < a);
+    }
+
+    public static bool operator >=(VersionNumber a, VersionNumber b)
+    {
+        return !(a < b);
+    }
+
+    public override bool Equals(object? obj)
+    {
+        if(obj is VersionNumber v)
+        {
+            return this == v;
+        }
+        return false;
+    }
+
+    public override int GetHashCode()
+    {
+        if (IsDevelopmentVersion)
+            return 0;
+        return MajorVersion ^ MinorVersion ^ Release.GetHashCode();
+    }
+
+    public static bool operator ==(VersionNumber a, VersionNumber b)
+    {
+        if (a.IsDevelopmentVersion)
+            return b.IsDevelopmentVersion;
+        if (b.IsDevelopmentVersion)
+            return false;
+        return a.MajorVersion == b.MajorVersion 
+               && a.MinorVersion == b.MinorVersion 
+               && String.Compare(a.Release,b.Release, StringComparison.OrdinalIgnoreCase) == 0;
+    }
+    public static bool operator !=(VersionNumber a, VersionNumber b)
+    {
+        if (a.IsDevelopmentVersion)
+            return !b.IsDevelopmentVersion;
+        if (b.IsDevelopmentVersion)
+            return true;
+        return a.MajorVersion != b.MajorVersion
+               || a.MinorVersion != b.MinorVersion
+               || String.Compare(a.Release, b.Release, StringComparison.OrdinalIgnoreCase) != 0;
+    }
+
+    public override string ToString()
+    {
+        return IsDevelopmentVersion
+            ? "???"
+            : IsNewStyle
+                ? !Release.Equals("0")
+                    ? $"{MajorVersion}.{MinorVersion:D2}.{Release}"
+                    : $"{MajorVersion}.{MinorVersion:D2}"
+                : $"{MajorVersion}.{MinorVersion:D2}{Release}";
+    }
+
+
+}