public static VersionInfo From(VersionWithSuffix version, bool strict = true) {
            if((version.Major == 0) && (version.Minor != 0)) {
                return new VersionInfo(
                    int.MinValue + version.Minor - 1,
                    (version.Build != -1) ? version.Build : 0,
                    (version.Revision != -1) ? (int?)version.Revision : null,
                    version.Suffix
                );
            }

            // revision is not supported in this format since it requires 4 numbers to be stored
            if(strict && (version.Revision != -1)) {
                throw new ArgumentException("unsupported format", nameof(version));
            }
            return new VersionInfo(
                version.Major,
                version.Minor,
                (version.Build != -1) ? (int?)version.Build : null,
                version.Suffix
            );
        }
 public static bool TryParse(string text, out VersionWithSuffix?version)
 {
     if (TryParse(text, out var major, out var minor, out var build, out var revision, out var suffix))
     {
         if (build == -1)
         {
             version = new VersionWithSuffix(new Version(major, minor), suffix);
         }
         else if (revision == -1)
         {
             version = new VersionWithSuffix(new Version(major, minor, build), suffix);
         }
         else
         {
             version = new VersionWithSuffix(new Version(major, minor, build, revision), suffix);
         }
         return(true);
     }
     version = null;
     return(false);
 }
Exemple #3
0
 public bool IsEqualToVersion(VersionWithSuffix other) => CompareToVersion(other) == 0;
Exemple #4
0
 public bool IsGreaterOrEqualThanVersion(VersionWithSuffix other) => CompareToVersion(other) >= 0;
Exemple #5
0
 public bool IsGreaterThanVersion(VersionWithSuffix other) => CompareToVersion(other) > 0;
Exemple #6
0
 public bool IsLessOrEqualThanVersion(VersionWithSuffix other) => CompareToVersion(other) <= 0;
Exemple #7
0
 public bool IsLessThanVersion(VersionWithSuffix other) => CompareToVersion(other) < 0;
Exemple #8
0
        //--- Methods ---
        public int?CompareToVersion(VersionWithSuffix other)
        {
            if (object.ReferenceEquals(other, null))
            {
                return(null);
            }

            // version number dominates other comparisions
            var result = Major - other.Major;

            if (result != 0)
            {
                return(Sign(result));
            }
            result = Minor - other.Minor;
            if (result != 0)
            {
                return(Sign(result));
            }
            result = Math.Max(0, Build) - Math.Max(0, other.Build);
            if (result != 0)
            {
                return(Sign(result));
            }
            result = Math.Max(0, Revision) - Math.Max(0, other.Revision);
            if (result != 0)
            {
                return(Sign(result));
            }

            // a suffix indicates a pre-release version, which is always less than the stable version
            if ((Suffix == "") && (other.Suffix != ""))
            {
                return(1);
            }
            if ((Suffix != "") && (other.Suffix == ""))
            {
                return(-1);
            }
            if (Suffix == other.Suffix)
            {
                return(0);
            }

            // check if the suffixes have a trailing number that can be compared
            var shortestLength = Math.Min(Suffix.Length, other.Suffix.Length);
            var i = 1;

            for (; (i < shortestLength) && char.IsLetter(Suffix[i]) && (Suffix[i] == other.Suffix[i]); ++i)
            {
                ;
            }
            if (
                int.TryParse(Suffix.Substring(i, Suffix.Length - i), out var leftSuffixValue) &&
                int.TryParse(other.Suffix.Substring(i, other.Suffix.Length - i), out var rightSuffixVersion)
                )
            {
                if (leftSuffixValue > rightSuffixVersion)
                {
                    return(1);
                }
                if (leftSuffixValue < rightSuffixVersion)
                {
                    return(-1);
                }
                return(0);
            }

            // versions cannot be compared
            return(null);

            // local functions
            int Sign(int value) => (value < 0) ? -1 : ((value > 0) ? 1 : 0);
        }
        public static bool IsValidLambdaSharpAssemblyReferenceForToolVersion(VersionInfo toolVersion, string projectFramework, string lambdaSharpAssemblyVersion, out bool outdated)
        {
            // extract assembly version pattern without wildcard
            VersionWithSuffix libraryVersion;

            if (lambdaSharpAssemblyVersion.EndsWith(".*", StringComparison.Ordinal))
            {
                libraryVersion = VersionWithSuffix.Parse(lambdaSharpAssemblyVersion.Substring(0, lambdaSharpAssemblyVersion.Length - 2));
            }
            else
            {
                libraryVersion = VersionWithSuffix.Parse(lambdaSharpAssemblyVersion);
            }

            // compare based on selected framework
            bool valid;

            switch (projectFramework)
            {
            case "netstandard2.1":

                // .NET Standard 2.1 projects (Blazor) require 0.8.1.* or 0.8.2.*
                valid = (libraryVersion.Major == 0) &&
                        (libraryVersion.Minor == 8) &&
                        (
                    (libraryVersion.Build == 1) ||
                    (libraryVersion.Build == 2)
                        );
                break;

            case "netcoreapp2.1":

                // .NET Core 2.1 projects (Lambda) require 0.8.0.* or 0.8.1.*
                valid = (libraryVersion.Major == 0) &&
                        (libraryVersion.Minor == 8) &&
                        (
                    (libraryVersion.Build == 0) ||
                    (libraryVersion.Build == 1)
                        );
                break;

            case "netcoreapp3.1":

                // .NET Core 3.1 projects (Lambda) require 0.8.0.*, 0.8.1.*, or 0.8.2.*
                valid = (libraryVersion.Major == 0) &&
                        (libraryVersion.Minor == 8) &&
                        (
                    (libraryVersion.Build == 0) ||
                    (libraryVersion.Build == 1) ||
                    (libraryVersion.Build == 2)
                        );
                break;

            case "net5":
            case "net5.0":

                // .NET 5 projects require 0.8.2.*
                valid = (libraryVersion.Major == 0) &&
                        (libraryVersion.Minor == 8) &&
                        (libraryVersion.Build == 2);
                break;

            default:
                throw new VersionInfoCompatibilityUnsupportedFrameworkException(projectFramework);
            }
            outdated = valid && VersionInfo.From(libraryVersion, strict: false).IsLessThanVersion(toolVersion.GetMajorMinorVersion());
            return(valid);
        }