diff --git a/source/Octopus.Versioning.Tests/Docker/DockerVersionCompareTests.cs b/source/Octopus.Versioning.Tests/Docker/DockerVersionCompareTests.cs index 2530044..1b6b37a 100644 --- a/source/Octopus.Versioning.Tests/Docker/DockerVersionCompareTests.cs +++ b/source/Octopus.Versioning.Tests/Docker/DockerVersionCompareTests.cs @@ -45,7 +45,7 @@ public void MatchingVersionsShouldBeGroupedCorrectly(string version) var ver1 = VersionFactory.CreateDockerTag(version); var ver2 = VersionFactory.CreateDockerTag(version); - var items = new List {ver1, ver2}.GroupBy(i => i).ToList(); + var items = new List {ver1, ver2}.GroupBy(i => i).ToList(); Assert.AreEqual(1, items.Count); } diff --git a/source/Octopus.Versioning.Tests/Maven/MavenVersionCompareTests.cs b/source/Octopus.Versioning.Tests/Maven/MavenVersionCompareTests.cs index f74f0f7..68df331 100644 --- a/source/Octopus.Versioning.Tests/Maven/MavenVersionCompareTests.cs +++ b/source/Octopus.Versioning.Tests/Maven/MavenVersionCompareTests.cs @@ -15,7 +15,7 @@ public void TestMatchingVersionsAreGroupedCorrectly(string version) var ver1 = VersionFactory.CreateMavenVersion(version); var ver2 = VersionFactory.CreateMavenVersion(version); - var items = new List {ver1, ver2}.GroupBy(i => i).ToList(); + var items = new List {ver1, ver2}.GroupBy(i => i).ToList(); Assert.AreEqual(1, items.Count); } diff --git a/source/Octopus.Versioning.Tests/Maven/Ranges/RangeTests.cs b/source/Octopus.Versioning.Tests/Maven/Ranges/RangeTests.cs index a553240..1bd9bb1 100644 --- a/source/Octopus.Versioning.Tests/Maven/Ranges/RangeTests.cs +++ b/source/Octopus.Versioning.Tests/Maven/Ranges/RangeTests.cs @@ -691,25 +691,25 @@ void checkInvalidRange(string version) [Test] public void testContains() { - IVersion actualVersion = new MavenVersionParser().Parse("2.0.5"); - Assert.IsTrue(enforceVersion("2.0.5", actualVersion)); - Assert.IsTrue(enforceVersion("2.0.4", actualVersion)); - Assert.IsTrue(enforceVersion("[2.0.5]", actualVersion)); - Assert.IsFalse(enforceVersion("[2.0.6,)", actualVersion)); - Assert.IsFalse(enforceVersion("[2.0.6]", actualVersion)); - Assert.IsTrue(enforceVersion("[2.0,2.1]", actualVersion)); - Assert.IsFalse(enforceVersion("[2.0,2.0.3]", actualVersion)); - Assert.IsTrue(enforceVersion("[2.0,2.0.5]", actualVersion)); - Assert.IsFalse(enforceVersion("[2.0,2.0.5)", actualVersion)); + ISortableVersion actualSortableVersion = new MavenVersionParser().Parse("2.0.5"); + Assert.IsTrue(enforceVersion("2.0.5", actualSortableVersion)); + Assert.IsTrue(enforceVersion("2.0.4", actualSortableVersion)); + Assert.IsTrue(enforceVersion("[2.0.5]", actualSortableVersion)); + Assert.IsFalse(enforceVersion("[2.0.6,)", actualSortableVersion)); + Assert.IsFalse(enforceVersion("[2.0.6]", actualSortableVersion)); + Assert.IsTrue(enforceVersion("[2.0,2.1]", actualSortableVersion)); + Assert.IsFalse(enforceVersion("[2.0,2.0.3]", actualSortableVersion)); + Assert.IsTrue(enforceVersion("[2.0,2.0.5]", actualSortableVersion)); + Assert.IsFalse(enforceVersion("[2.0,2.0.5)", actualSortableVersion)); } - public bool enforceVersion(string requiredMavenVersionRange, IVersion actualVersion) + public bool enforceVersion(string requiredMavenVersionRange, ISortableVersion actualSortableVersion) { MavenVersionRange vr = null; vr = MavenVersionRange.CreateFromVersionSpec(requiredMavenVersionRange); - return vr.ContainsVersion(actualVersion); + return vr.ContainsVersion(actualSortableVersion); } [Test] diff --git a/source/Octopus.Versioning.Tests/Octopus/OctopusVersionTests.cs b/source/Octopus.Versioning.Tests/Octopus/OctopusSortableVersionTests.cs similarity index 99% rename from source/Octopus.Versioning.Tests/Octopus/OctopusVersionTests.cs rename to source/Octopus.Versioning.Tests/Octopus/OctopusSortableVersionTests.cs index df4c543..7e978db 100644 --- a/source/Octopus.Versioning.Tests/Octopus/OctopusVersionTests.cs +++ b/source/Octopus.Versioning.Tests/Octopus/OctopusSortableVersionTests.cs @@ -8,7 +8,7 @@ namespace Octopus.Versioning.Tests.Octopus { [TestFixture] - public class OctopusVersionTests + public class OctopusSortableVersionTests { static readonly Random Random = new Random(); static readonly OctopusVersionParser OctopusVersionParser = new OctopusVersionParser(); diff --git a/source/Octopus.Versioning.Tests/Octopus/OctopusVersionCompareTests.cs b/source/Octopus.Versioning.Tests/Octopus/OctopusVersionCompareTests.cs index c1b15a7..f879f30 100644 --- a/source/Octopus.Versioning.Tests/Octopus/OctopusVersionCompareTests.cs +++ b/source/Octopus.Versioning.Tests/Octopus/OctopusVersionCompareTests.cs @@ -132,7 +132,7 @@ public void MatchingVersionsShouldBeGroupedCorrectly(string version) var ver1 = VersionFactory.CreateOctopusVersion(version); var ver2 = VersionFactory.CreateOctopusVersion(version); - var items = new List {ver1, ver2}.GroupBy(i => i).ToList(); + var items = new List {ver1, ver2}.GroupBy(i => i).ToList(); Assert.AreEqual(1, items.Count); } diff --git a/source/Octopus.Versioning.Tests/Octopus/OctopusVersionMaskParserTests.cs b/source/Octopus.Versioning.Tests/Octopus/OctopusVersionMaskParserTests.cs index 98c2c05..a23198b 100644 --- a/source/Octopus.Versioning.Tests/Octopus/OctopusVersionMaskParserTests.cs +++ b/source/Octopus.Versioning.Tests/Octopus/OctopusVersionMaskParserTests.cs @@ -203,7 +203,7 @@ public void IsMask(string mask, bool isMask) [TestCase("1.i.i", "2.0.0", null)] public void GetLatestVersionMask(string version, string latestVersion, string expected) { - var latestVersions = new List + var latestVersions = new List { new OctopusVersionParser().Parse(latestVersion) }; diff --git a/source/Octopus.Versioning.Tests/PreviousImplementation/SemanticVersionMask.cs b/source/Octopus.Versioning.Tests/PreviousImplementation/SemanticVersionMask.cs index b642381..171caad 100644 --- a/source/Octopus.Versioning.Tests/PreviousImplementation/SemanticVersionMask.cs +++ b/source/Octopus.Versioning.Tests/PreviousImplementation/SemanticVersionMask.cs @@ -46,7 +46,7 @@ public static bool IsMask(string versionString) ); } - public static IVersion GetLatestMaskedVersion(string mask, List versions) + public static ISortableVersion GetLatestMaskedVersion(string mask, List versions) { var maskMatch = new MaskMatchedVersion(mask); @@ -87,18 +87,18 @@ public static IVersion GetLatestMaskedVersion(string mask, List versio .FirstOrDefault(); } - public static IVersion ApplyMask(string mask, IVersion currentVersion) + public static ISortableVersion ApplyMask(string mask, ISortableVersion currentSortableVersion) { var match = FormatRegex.Match(mask); if (!match.Success) return VersionFactory.CreateSemanticVersion(mask); - return currentVersion == null + return currentSortableVersion == null ? GenerateVersionFromMask(new MaskMatchedVersion(mask)) - : GenerateVersionFromCurrent(new MaskMatchedVersion(mask), new MaskMatchedVersion(currentVersion.ToString())); + : GenerateVersionFromCurrent(new MaskMatchedVersion(mask), new MaskMatchedVersion(currentSortableVersion.ToString())); } - static IVersion GenerateVersionFromMask(MaskMatchedVersion mask) + static ISortableVersion GenerateVersionFromMask(MaskMatchedVersion mask) { var result = new StringBuilder(); result.Append(mask.Major.EvaluateFromMask()); @@ -110,7 +110,7 @@ static IVersion GenerateVersionFromMask(MaskMatchedVersion mask) return VersionFactory.CreateSemanticVersion(result.ToString()); } - static IVersion GenerateVersionFromCurrent(MaskMatchedVersion mask, MaskMatchedVersion current) + static ISortableVersion GenerateVersionFromCurrent(MaskMatchedVersion mask, MaskMatchedVersion current) { var result = new StringBuilder(); result.Append(mask.Major.Substitute(current.Major)); diff --git a/source/Octopus.Versioning.Tests/Semver/SemanticVersionCompareTests.cs b/source/Octopus.Versioning.Tests/Semver/SemanticVersionCompareTests.cs index 3c8feb5..ccb6bc3 100644 --- a/source/Octopus.Versioning.Tests/Semver/SemanticVersionCompareTests.cs +++ b/source/Octopus.Versioning.Tests/Semver/SemanticVersionCompareTests.cs @@ -14,7 +14,7 @@ public void TestMatchingVersionsAreGroupedCorrectly() var ver1 = VersionFactory.CreateSemanticVersion(version); var ver2 = VersionFactory.CreateSemanticVersion(version); - var items = new List {ver1, ver2}.GroupBy(i => i).ToList(); + var items = new List {ver1, ver2}.GroupBy(i => i).ToList(); Assert.AreEqual(1, items.Count); } diff --git a/source/Octopus.Versioning.Tests/Unsortable/UnsortableVersionCompareTests.cs b/source/Octopus.Versioning.Tests/Unsortable/UnsortableVersionCompareTests.cs index dfa5ca5..a369478 100644 --- a/source/Octopus.Versioning.Tests/Unsortable/UnsortableVersionCompareTests.cs +++ b/source/Octopus.Versioning.Tests/Unsortable/UnsortableVersionCompareTests.cs @@ -7,39 +7,39 @@ public class UnsortableVersionCompareTests { static readonly UnsortableVersionParser UnsortableVersionParser = new(); - [Test] - [TestCase("release", "release", 0)] - [TestCase("release", "qrelease", 1)] - [TestCase("release", "srelease", -1)] - [TestCase("123", "123", 0)] - [TestCase("123", "100", 1)] - [TestCase("123", "321", -1)] - [TestCase("123Release", "123Release", 0)] - [TestCase("123Release", "100Release", 1)] - [TestCase("123Release", "321Release", -1)] - [TestCase("release-1", "release-1", 0)] - [TestCase("release-1", "release-0", 1)] - [TestCase("release-1", "release-2", -1)] - [TestCase("release.1", "release.1", 0)] - [TestCase("release.1", "release.0", 1)] - [TestCase("release.1", "release.2", -1)] - [TestCase("release_1", "release_1", 0)] - [TestCase("release_1", "release_0", 1)] - [TestCase("release_1", "release_2", -1)] - [TestCase("release-1", "release_1", 0)] - [TestCase("release.1", "release_1", 0)] - [TestCase("release-1", "release_0", 1)] - [TestCase("release.1", "release_2", -1)] - [TestCase("release+123", "release+321", 0)] - [TestCase("release-1+123", "release-1+321", 0)] - [TestCase("release-1+123", "release-0+321", 1)] - [TestCase("release-1+123", "release-2+321", -1)] - public void TestComparisons(string version1, string version2, int result) - { - var parsedVersion1 = UnsortableVersionParser.Parse(version1); - var parsedVersion2 = UnsortableVersionParser.Parse(version2); - Assert.AreEqual(result, parsedVersion1.CompareTo(parsedVersion2)); - } + // [Test] + // [TestCase("release", "release", 0)] + // [TestCase("release", "qrelease", 1)] + // [TestCase("release", "srelease", -1)] + // [TestCase("123", "123", 0)] + // [TestCase("123", "100", 1)] + // [TestCase("123", "321", -1)] + // [TestCase("123Release", "123Release", 0)] + // [TestCase("123Release", "100Release", 1)] + // [TestCase("123Release", "321Release", -1)] + // [TestCase("release-1", "release-1", 0)] + // [TestCase("release-1", "release-0", 1)] + // [TestCase("release-1", "release-2", -1)] + // [TestCase("release.1", "release.1", 0)] + // [TestCase("release.1", "release.0", 1)] + // [TestCase("release.1", "release.2", -1)] + // [TestCase("release_1", "release_1", 0)] + // [TestCase("release_1", "release_0", 1)] + // [TestCase("release_1", "release_2", -1)] + // [TestCase("release-1", "release_1", 0)] + // [TestCase("release.1", "release_1", 0)] + // [TestCase("release-1", "release_0", 1)] + // [TestCase("release.1", "release_2", -1)] + // [TestCase("release+123", "release+321", 0)] + // [TestCase("release-1+123", "release-1+321", 0)] + // [TestCase("release-1+123", "release-0+321", 1)] + // [TestCase("release-1+123", "release-2+321", -1)] + // public void TestComparisons(string version1, string version2, int result) + // { + // var parsedVersion1 = UnsortableVersionParser.Parse(version1); + // var parsedVersion2 = UnsortableVersionParser.Parse(version2); + // Assert.AreEqual(result, parsedVersion1.CompareTo(parsedVersion2)); + // } [Test] [TestCase("release-1", "release-1", true)] diff --git a/source/Octopus.Versioning.Tests/Unsortable/UnsortableVersionParserTests.cs b/source/Octopus.Versioning.Tests/Unsortable/UnsortableVersionParserTests.cs index cc2ad3a..7ddad4c 100644 --- a/source/Octopus.Versioning.Tests/Unsortable/UnsortableVersionParserTests.cs +++ b/source/Octopus.Versioning.Tests/Unsortable/UnsortableVersionParserTests.cs @@ -71,11 +71,11 @@ public void ShouldThrowExceptionOnFailureToParse() Assert.Catch(() => new UnsortableVersionParser().Parse(input)); } - void AssertVersionNumbersAreZero(UnsortableVersion version) + void AssertVersionNumbersAreZero(UnsortableSortableVersion sortableVersion) { - Assert.AreEqual(0, version.Major); - Assert.AreEqual(0, version.Minor); - Assert.AreEqual(0, version.Patch); - Assert.AreEqual(0, version.Revision); + Assert.AreEqual(0, sortableVersion.Major); + Assert.AreEqual(0, sortableVersion.Minor); + Assert.AreEqual(0, sortableVersion.Patch); + Assert.AreEqual(0, sortableVersion.Revision); } } \ No newline at end of file diff --git a/source/Octopus.Versioning/Docker/DockerTag.cs b/source/Octopus.Versioning/Docker/DockerTag.cs index 453796f..9a7b14b 100644 --- a/source/Octopus.Versioning/Docker/DockerTag.cs +++ b/source/Octopus.Versioning/Docker/DockerTag.cs @@ -4,22 +4,22 @@ namespace Octopus.Versioning.Docker { - public class DockerTag : OctopusVersion + public class DockerTag : OctopusSortableVersion { const string Latest = "latest"; bool IsLatest => string.Compare(OriginalString, Latest, StringComparison.Ordinal) == 0; - public DockerTag(OctopusVersion version) - : base(version.Prefix, - version.Major, - version.Minor, - version.Patch, - version.Revision, - version.Release, - version.ReleasePrefix, - version.ReleaseCounter, - version.Metadata, - version.OriginalString) + public DockerTag(OctopusSortableVersion sortableVersion) + : base(sortableVersion.Prefix, + sortableVersion.Major, + sortableVersion.Minor, + sortableVersion.Patch, + sortableVersion.Revision, + sortableVersion.Release, + sortableVersion.ReleasePrefix, + sortableVersion.ReleaseCounter, + sortableVersion.Metadata, + sortableVersion.OriginalString) { } diff --git a/source/Octopus.Versioning/ISortableVersion.cs b/source/Octopus.Versioning/ISortableVersion.cs new file mode 100644 index 0000000..53df82d --- /dev/null +++ b/source/Octopus.Versioning/ISortableVersion.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; + +namespace Octopus.Versioning +{ + /// + /// Represents the base components of a version recognised by Octopus. + /// The terminolgy here comes from SemVer, but can be mapped to other + /// versioning schemes like Maven. + /// + /// All classes should reference this interface, but there are some + /// exceptions. + /// + /// Some modelling classes need to reference the SemanticVersionConverter + /// to ensure that constructor paramaters and properties of the type + /// IVersion are converted to and from a SemanticVersion object. + /// + /// JSON converters like SemanticVersionConverter assume that + /// any version object being stored in the database is a SemanticVersion + /// object. This is because Octopus assumes the use of SemanticVersion + /// in all processes other than external feeds, which do not have + /// version information directly saved in the database. + /// + /// The version factory classes also need to know about the conrete + /// classes. + /// + /// Outside of those two use cases, all other references to a version + /// should be through this interface. + /// + public interface ISortableVersion : IVersion, IComparable + { + } +} \ No newline at end of file diff --git a/source/Octopus.Versioning/IVersion.cs b/source/Octopus.Versioning/IVersion.cs index b994f71..c0023e6 100644 --- a/source/Octopus.Versioning/IVersion.cs +++ b/source/Octopus.Versioning/IVersion.cs @@ -1,33 +1,8 @@ -using System; using System.Collections.Generic; namespace Octopus.Versioning { - /// - /// Represents the base components of a version recognised by Octopus. - /// The terminolgy here comes from SemVer, but can be mapped to other - /// versioning schemes like Maven. - /// - /// All classes should reference this interface, but there are some - /// exceptions. - /// - /// Some modelling classes need to reference the SemanticVersionConverter - /// to ensure that constructor paramaters and properties of the type - /// IVersion are converted to and from a SemanticVersion object. - /// - /// JSON converters like SemanticVersionConverter assume that - /// any version object being stored in the database is a SemanticVersion - /// object. This is because Octopus assumes the use of SemanticVersion - /// in all processes other than external feeds, which do not have - /// version information directly saved in the database. - /// - /// The version factory classes also need to know about the conrete - /// classes. - /// - /// Outside of those two use cases, all other references to a version - /// should be through this interface. - /// - public interface IVersion : IComparable + public interface IVersion { int Major { get; } int Minor { get; } diff --git a/source/Octopus.Versioning/IVersionComparer.cs b/source/Octopus.Versioning/IVersionComparer.cs index fbe9d32..dc59263 100644 --- a/source/Octopus.Versioning/IVersionComparer.cs +++ b/source/Octopus.Versioning/IVersionComparer.cs @@ -10,7 +10,7 @@ namespace Octopus.Versioning /// IVersionComparer represents a version comparer capable of sorting and determining the equality of /// SemanticVersion objects. /// - public interface IVersionComparer : IEqualityComparer, IComparer + public interface IVersionComparer : IEqualityComparer, IComparer { } } \ No newline at end of file diff --git a/source/Octopus.Versioning/Maven/MavenPackageID.cs b/source/Octopus.Versioning/Maven/MavenPackageID.cs index 82b7c7e..2a7a30b 100644 --- a/source/Octopus.Versioning/Maven/MavenPackageID.cs +++ b/source/Octopus.Versioning/Maven/MavenPackageID.cs @@ -84,15 +84,15 @@ public MavenPackageID([NotNull] public MavenPackageID([NotNull] string? id, - IVersion version) : this(id) + ISortableVersion sortableVersion) : this(id) { if (string.IsNullOrWhiteSpace(id) || id.Split(':').Length != 2) throw new ArgumentException("Package ID must be in the format Group:Artifact e.g. com.google.guava:guava or junit:junit."); - if (version == null) + if (sortableVersion == null) throw new ArgumentException("version can not be null"); - Version = version.ToString(); + Version = sortableVersion.ToString(); } /// @@ -154,7 +154,7 @@ public MavenPackageID([NotNull] public string? Classifier { get; } public string DisplayName => ToString(DISPLAY_DELIMITER); - public IVersion? SemanticVersion => Version == null ? null : new MavenVersionParser().Parse(Version); + public ISortableVersion? SemanticVersion => Version == null ? null : new MavenVersionParser().Parse(Version); /// /// The path to the metadata file for the artifact @@ -248,7 +248,7 @@ public string DefaultArtifactPath /// The optional version /// A MavenPackageID created to match the package id an optional packaging defined in the UI /// thrown if the input is not in the correct format - public static MavenPackageID CreatePackageIdFromOctopusInput(string input, IVersion? version = null) + public static MavenPackageID CreatePackageIdFromOctopusInput(string input, ISortableVersion? version = null) { var splitVersion = input.Split(':').ToList(); if (!(splitVersion.Count >= 2 && splitVersion.Count <= 4)) diff --git a/source/Octopus.Versioning/Maven/MavenVersion.cs b/source/Octopus.Versioning/Maven/MavenSortableVersion.cs similarity index 93% rename from source/Octopus.Versioning/Maven/MavenVersion.cs rename to source/Octopus.Versioning/Maven/MavenSortableVersion.cs index 4f6f387..fd4c151 100644 --- a/source/Octopus.Versioning/Maven/MavenVersion.cs +++ b/source/Octopus.Versioning/Maven/MavenSortableVersion.cs @@ -5,9 +5,9 @@ namespace Octopus.Versioning.Maven { - public class MavenVersion : IVersion + public class MavenSortableVersion : ISortableVersion { - public MavenVersion(int major, + public MavenSortableVersion(int major, int minor, int patch, int revision, @@ -63,7 +63,7 @@ public string Release public int CompareTo(object obj) { return new ComparableVersion(OriginalString) - .CompareTo(new ComparableVersion((obj as MavenVersion)?.OriginalString ?? "")); + .CompareTo(new ComparableVersion((obj as MavenSortableVersion)?.OriginalString ?? "")); } public override string ToString() diff --git a/source/Octopus.Versioning/Maven/MavenVersionParser.cs b/source/Octopus.Versioning/Maven/MavenVersionParser.cs index c00d3d4..11ffd98 100644 --- a/source/Octopus.Versioning/Maven/MavenVersionParser.cs +++ b/source/Octopus.Versioning/Maven/MavenVersionParser.cs @@ -27,7 +27,7 @@ public class MavenVersionParser public string? Qualifier { get; private set; } - public MavenVersion Parse(string version) + public MavenSortableVersion Parse(string version) { var matcherDigits = DIGITS.Match(version); if (matcherDigits.Success) @@ -40,7 +40,7 @@ public MavenVersion Parse(string version) Qualifier = version; } - return new MavenVersion( + return new MavenSortableVersion( Major, Minor, Patch, diff --git a/source/Octopus.Versioning/Maven/Ranges/MavenVersionRange.cs b/source/Octopus.Versioning/Maven/Ranges/MavenVersionRange.cs index d5f7237..aef362c 100644 --- a/source/Octopus.Versioning/Maven/Ranges/MavenVersionRange.cs +++ b/source/Octopus.Versioning/Maven/Ranges/MavenVersionRange.cs @@ -9,14 +9,14 @@ namespace Octopus.Versioning.Maven.Ranges public class MavenVersionRange { MavenVersionRange( - IVersion? recommendedVersion, + ISortableVersion? recommendedVersion, List restrictions ) { RecommendedVersion = recommendedVersion; Restrictions = restrictions; } - public IVersion? RecommendedVersion { get; } + public ISortableVersion? RecommendedVersion { get; } public List Restrictions { get; } @@ -56,9 +56,9 @@ public MavenVersionRange CloneOf() var restrictions = new List(); var process = spec; - IVersion? version = null; - IVersion? upperBound = null; - IVersion? lowerBound = null; + ISortableVersion? version = null; + ISortableVersion? upperBound = null; + ISortableVersion? lowerBound = null; while ( process.StartsWith( "[" ) || process.StartsWith( "(" ) ) { @@ -117,9 +117,9 @@ static Restriction ParseRestriction( string spec ) if ( !lowerBoundInclusive || !upperBoundInclusive ) throw new InvalidVersionSpecificationException( "Single version must be surrounded by []: " + spec ); - IVersion version = new MavenVersionParser().Parse( process ); + ISortableVersion sortableVersion = new MavenVersionParser().Parse( process ); - restriction = new Restriction( version, lowerBoundInclusive, version, upperBoundInclusive ); + restriction = new Restriction( sortableVersion, lowerBoundInclusive, sortableVersion, upperBoundInclusive ); } else { @@ -128,10 +128,10 @@ static Restriction ParseRestriction( string spec ) if ( lowerBound.Equals( upperBound ) ) throw new InvalidVersionSpecificationException( "Range cannot have identical boundaries: " + spec ); - IVersion? lowerVersion = null; + ISortableVersion? lowerVersion = null; if ( lowerBound.Length > 0 ) lowerVersion = new MavenVersionParser().Parse( lowerBound ); - IVersion? upperVersion = null; + ISortableVersion? upperVersion = null; if ( upperBound.Length > 0 ) upperVersion = new MavenVersionParser().Parse( upperBound ); @@ -189,7 +189,7 @@ public MavenVersionRange Restrict( MavenVersionRange restriction ) else restrictions = Intersection( r1, r2 ); - IVersion? version = null; + ISortableVersion? version = null; if ( restrictions.Count > 0 ) { foreach (var r in restrictions) @@ -248,8 +248,8 @@ List Intersection( List r1, List r2 ) if ( res1.UpperBound == null || res2.LowerBound == null || res1.UpperBound.CompareTo( res2.LowerBound ) >= 0 ) { - IVersion? lower; - IVersion? upper; + ISortableVersion? lower; + ISortableVersion? upper; bool lowerInclusive; bool upperInclusive; @@ -359,9 +359,9 @@ List Intersection( List r1, List r2 ) return restrictions; } - public IVersion? GetSelectedVersion( ) + public ISortableVersion? GetSelectedVersion( ) { - IVersion? version = null; + ISortableVersion? version = null; if ( RecommendedVersion != null ) { version = RecommendedVersion; @@ -397,11 +397,11 @@ public override string ToString() return string.Join(",", Restrictions); } - public IVersion? MatchVersion( List versions ) + public ISortableVersion? MatchVersion( List versions ) { // TODO could be more efficient by sorting the list and then moving along the restrictions in order? - IVersion? matched = null; + ISortableVersion? matched = null; foreach (var version in versions) if ( ContainsVersion( version ) ) // valid - check if it is greater than the currently matched version @@ -410,10 +410,10 @@ public override string ToString() return matched; } - public bool ContainsVersion( IVersion version ) + public bool ContainsVersion( ISortableVersion sortableVersion ) { foreach (var restriction in Restrictions) - if ( restriction.ContainsVersion( version ) ) + if ( restriction.ContainsVersion( sortableVersion ) ) return true; return false; } diff --git a/source/Octopus.Versioning/Maven/Ranges/Restriction.cs b/source/Octopus.Versioning/Maven/Ranges/Restriction.cs index 8ac8dd1..e39596c 100644 --- a/source/Octopus.Versioning/Maven/Ranges/Restriction.cs +++ b/source/Octopus.Versioning/Maven/Ranges/Restriction.cs @@ -11,9 +11,9 @@ public class Restriction public static readonly Restriction EVERYTHING = new Restriction(null, false, null, false); public Restriction( - IVersion? lowerBound, + ISortableVersion? lowerBound, bool lowerBoundInclusive, - IVersion? upperBound, + ISortableVersion? upperBound, bool upperBoundInclusive) { LowerBound = lowerBound; @@ -22,16 +22,16 @@ public Restriction( IsUpperBoundInclusive = upperBoundInclusive; } - public IVersion? LowerBound { get; } + public ISortableVersion? LowerBound { get; } public bool IsLowerBoundInclusive { get; } - public IVersion? UpperBound { get; } + public ISortableVersion? UpperBound { get; } public bool IsUpperBoundInclusive { get; } - public bool ContainsVersion(IVersion version) + public bool ContainsVersion(ISortableVersion sortableVersion) { if (LowerBound != null) { - var comparison = LowerBound.CompareTo(version); + var comparison = LowerBound.CompareTo(sortableVersion); if (comparison == 0 && !IsLowerBoundInclusive) return false; @@ -41,7 +41,7 @@ public bool ContainsVersion(IVersion version) if (UpperBound != null) { - var comparison = UpperBound.CompareTo(version); + var comparison = UpperBound.CompareTo(sortableVersion); if (comparison == 0 && !IsUpperBoundInclusive) return false; diff --git a/source/Octopus.Versioning/Octopus/OctopusVersion.cs b/source/Octopus.Versioning/Octopus/OctopusSortableVersion.cs similarity index 96% rename from source/Octopus.Versioning/Octopus/OctopusVersion.cs rename to source/Octopus.Versioning/Octopus/OctopusSortableVersion.cs index 2b2e540..50fd154 100644 --- a/source/Octopus.Versioning/Octopus/OctopusVersion.cs +++ b/source/Octopus.Versioning/Octopus/OctopusSortableVersion.cs @@ -4,9 +4,9 @@ namespace Octopus.Versioning.Octopus { - public class OctopusVersion : IVersion + public class OctopusSortableVersion : ISortableVersion { - public OctopusVersion(string? prefix, + public OctopusSortableVersion(string? prefix, int major, int minor, int patch, @@ -46,7 +46,7 @@ public OctopusVersion(string? prefix, public virtual int CompareTo(object obj) { - if (obj is IVersion objVersion) + if (obj is ISortableVersion objVersion) { if (Major.CompareTo(objVersion.Major) != 0) return Major.CompareTo(objVersion.Major); if (Minor.CompareTo(objVersion.Minor) != 0) return Minor.CompareTo(objVersion.Minor); @@ -83,7 +83,7 @@ public override string ToString() public override bool Equals(object obj) { - if (obj is IVersion objVersion) + if (obj is ISortableVersion objVersion) return CompareTo(objVersion) == 0; return false; diff --git a/source/Octopus.Versioning/Octopus/OctopusVersionMask.cs b/source/Octopus.Versioning/Octopus/OctopusVersionMask.cs index 0223ab5..6ed58c7 100644 --- a/source/Octopus.Versioning/Octopus/OctopusVersionMask.cs +++ b/source/Octopus.Versioning/Octopus/OctopusVersionMask.cs @@ -55,7 +55,7 @@ public OctopusVersionMask( public bool IsMask => DidParse && (Major.IsSubstitute || Minor.IsSubstitute || Patch.IsSubstitute || Release.IsSubstitute || Revision.IsSubstitute || Metadata.IsSubstitute); - public IVersion? GetLatestMaskedVersion(List versions) + public ISortableVersion? GetLatestMaskedVersion(List versions) { var maskMajor = Major.IsPresent && !Major.IsSubstitute ? int.Parse(Major.Value) : 0; var maskMinor = Minor.IsPresent && !Minor.IsSubstitute ? int.Parse(Minor.Value) : 0; @@ -96,7 +96,7 @@ public OctopusVersionMask( .FirstOrDefault(); } - public IVersion GenerateVersionFromMask() + public ISortableVersion GenerateVersionFromMask() { var result = new StringBuilder(); result.Append(Prefix); @@ -109,7 +109,7 @@ public IVersion GenerateVersionFromMask() return VersionFactory.CreateOctopusVersion(result.ToString()); } - public IVersion GenerateVersionFromCurrent(OctopusVersionMask current) + public ISortableVersion GenerateVersionFromCurrent(OctopusVersionMask current) { var result = new StringBuilder(); result.Append(Prefix); diff --git a/source/Octopus.Versioning/Octopus/OctopusVersionMaskParser.cs b/source/Octopus.Versioning/Octopus/OctopusVersionMaskParser.cs index 41d0cd8..74a84f4 100644 --- a/source/Octopus.Versioning/Octopus/OctopusVersionMaskParser.cs +++ b/source/Octopus.Versioning/Octopus/OctopusVersionMaskParser.cs @@ -47,7 +47,7 @@ public OctopusVersionMask Parse(string? version) new OctopusVersionMask.MetadataComponent(result.Groups[Meta])); } - public IVersion ApplyMask(string? mask, IVersion? currentVersion) + public ISortableVersion ApplyMask(string? mask, ISortableVersion? currentVersion) { var parsedMask = Parse(mask); diff --git a/source/Octopus.Versioning/Octopus/OctopusVersionParser.cs b/source/Octopus.Versioning/Octopus/OctopusVersionParser.cs index b391cbf..a005ae2 100644 --- a/source/Octopus.Versioning/Octopus/OctopusVersionParser.cs +++ b/source/Octopus.Versioning/Octopus/OctopusVersionParser.cs @@ -42,7 +42,7 @@ public class OctopusVersionParser // The metadata is everything after the plus $@"(?:\+(?<{Meta}>[A-Za-z0-9_\-.\\+]*?))?\s*$"); - public OctopusVersion Parse(string? version) + public OctopusSortableVersion Parse(string? version) { try { @@ -60,7 +60,7 @@ public OctopusVersion Parse(string? version) if (!result.Success) throw new ArgumentException("The supplied version was not valid"); - return new OctopusVersion( + return new OctopusSortableVersion( result.Groups[Prefix].Success ? result.Groups[Prefix].Value : string.Empty, result.Groups[Major].Success ? int.Parse(result.Groups[Major].Value) : 0, result.Groups[Minor].Success ? int.Parse(result.Groups[Minor].Value) : 0, @@ -78,16 +78,16 @@ public OctopusVersion Parse(string? version) } } - public bool TryParse(string version, out OctopusVersion parsedVersion) + public bool TryParse(string version, out OctopusSortableVersion parsedSortableVersion) { try { - parsedVersion = Parse(version); + parsedSortableVersion = Parse(version); return true; } catch { - parsedVersion = new OctopusVersion( + parsedSortableVersion = new OctopusSortableVersion( string.Empty, 0, 0, diff --git a/source/Octopus.Versioning/Semver/SemVerFactory.cs b/source/Octopus.Versioning/Semver/SemVerFactory.cs index 6fd3948..19b61a8 100644 --- a/source/Octopus.Versioning/Semver/SemVerFactory.cs +++ b/source/Octopus.Versioning/Semver/SemVerFactory.cs @@ -11,7 +11,7 @@ public class SemVerFactory { static readonly ISemanticVersionUtils utils = new SemanticVersionUtils(); - public static SemanticVersion CreateVersion(string input, bool preserveMissingComponents = false) + public static SemanticSortableVersion CreateVersion(string input, bool preserveMissingComponents = false) { var ver = TryCreateVersion(input, preserveMissingComponents); if (ver == null) @@ -20,7 +20,7 @@ public static SemanticVersion CreateVersion(string input, bool preserveMissingCo return ver; } - public static IVersion? CreateVersionOrNone(string input, bool preserveMissingComponents = false) + public static ISortableVersion? CreateVersionOrNone(string input, bool preserveMissingComponents = false) { return TryCreateVersion(input, preserveMissingComponents); } @@ -28,7 +28,7 @@ public static SemanticVersion CreateVersion(string input, bool preserveMissingCo /// /// Creates a NuGetVersion from a string representing the semantic version. /// - public static SemanticVersion Parse(string value, bool preserveMissingComponents = false) + public static SemanticSortableVersion Parse(string value, bool preserveMissingComponents = false) { if (string.IsNullOrEmpty(value)) throw new ArgumentException("Value cannot be null or an empty string", nameof(value)); @@ -44,7 +44,7 @@ public static SemanticVersion Parse(string value, bool preserveMissingComponents /// Parses a version string using loose semantic versioning rules that allows 2-4 version components followed /// by an optional special version. /// - public static SemanticVersion? TryCreateVersion(string value, bool preserveMissingComponents = false) + public static SemanticSortableVersion? TryCreateVersion(string value, bool preserveMissingComponents = false) { // trim the value before passing it in since we're not strict here value = value?.Trim(); @@ -89,7 +89,7 @@ public static SemanticVersion Parse(string value, bool preserveMissingComponents if (originalVersion.IndexOf(' ') > -1) originalVersion = value.Replace(" ", ""); - return new SemanticVersion(ver, + return new SemanticSortableVersion(ver, sections.Item2, sections.Item3 ?? string.Empty, originalVersion); @@ -103,11 +103,11 @@ public static SemanticVersion Parse(string value, bool preserveMissingComponents /// /// Parses a version string using strict SemVer rules. /// - public static SemanticVersion? TryParseStrict(string value) + public static SemanticSortableVersion? TryParseStrict(string value) { var semVer = TryCreateVersion(value); return semVer != null - ? new SemanticVersion(semVer.Major, + ? new SemanticSortableVersion(semVer.Major, semVer.Minor, semVer.Patch, 0, diff --git a/source/Octopus.Versioning/Semver/SemanticVersion.cs b/source/Octopus.Versioning/Semver/SemanticSortableVersion.cs similarity index 88% rename from source/Octopus.Versioning/Semver/SemanticVersion.cs rename to source/Octopus.Versioning/Semver/SemanticSortableVersion.cs index ce3442e..6a07abf 100644 --- a/source/Octopus.Versioning/Semver/SemanticVersion.cs +++ b/source/Octopus.Versioning/Semver/SemanticSortableVersion.cs @@ -12,7 +12,7 @@ namespace Octopus.Versioning.Semver /// not strictly enforcing it to /// allow older 4-digit versioning schemes to continue working. /// - public class SemanticVersion : StrictSemanticVersion + public class SemanticSortableVersion : StrictSemanticSortableVersion { static readonly SemVerFactory SemVerFactory = new SemVerFactory(); static readonly ISemanticVersionUtils utils = new SemanticVersionUtils(); @@ -22,7 +22,7 @@ public class SemanticVersion : StrictSemanticVersion /// Creates a NuGetVersion using NuGetVersion.Parse(string) /// /// Version string - public SemanticVersion(string version) + public SemanticSortableVersion(string version) : this(SemVerFactory.CreateVersion(version)) { } @@ -30,8 +30,8 @@ public SemanticVersion(string version) /// /// Creates a NuGetVersion from an existing NuGetVersion /// - public SemanticVersion(SemanticVersion version) - : this(version.Version, version.ReleaseLabels, version.Metadata, version.ToString()) + public SemanticSortableVersion(SemanticSortableVersion sortableVersion) + : this(sortableVersion.Version, sortableVersion.ReleaseLabels, sortableVersion.Metadata, sortableVersion.ToString()) { } @@ -41,7 +41,7 @@ public SemanticVersion(SemanticVersion version) /// Version numbers /// Prerelease label /// Build metadata - public SemanticVersion(Version version, string? releaseLabel = null, string? metadata = null) + public SemanticSortableVersion(Version version, string? releaseLabel = null, string? metadata = null) : this(version, utils.ParseReleaseLabels(releaseLabel), metadata, @@ -55,7 +55,7 @@ public SemanticVersion(Version version, string? releaseLabel = null, string? met /// X.y.z /// x.Y.z /// x.y.Z - public SemanticVersion(int major, int minor, int patch) + public SemanticSortableVersion(int major, int minor, int patch) : this(major, minor, patch, @@ -71,7 +71,7 @@ public SemanticVersion(int major, int minor, int patch) /// x.Y.z /// x.y.Z /// Prerelease label - public SemanticVersion(int major, int minor, int patch, string? releaseLabel) + public SemanticSortableVersion(int major, int minor, int patch, string? releaseLabel) : this(major, minor, patch, @@ -88,7 +88,7 @@ public SemanticVersion(int major, int minor, int patch, string? releaseLabel) /// x.y.Z /// Prerelease label /// Build metadata - public SemanticVersion(int major, + public SemanticSortableVersion(int major, int minor, int patch, string? releaseLabel, @@ -109,7 +109,7 @@ public SemanticVersion(int major, /// x.y.Z /// Prerelease labels /// Build metadata - public SemanticVersion(int major, + public SemanticSortableVersion(int major, int minor, int patch, IEnumerable? releaseLabels, @@ -125,7 +125,7 @@ public SemanticVersion(int major, /// w.X.y.z /// w.x.Y.z /// w.x.y.Z - public SemanticVersion(int major, int minor, int patch, int revision) + public SemanticSortableVersion(int major, int minor, int patch, int revision) : this(major, minor, patch, @@ -144,7 +144,7 @@ public SemanticVersion(int major, int minor, int patch, int revision) /// w.x.y.Z /// Prerelease label /// Build metadata - public SemanticVersion(int major, + public SemanticSortableVersion(int major, int minor, int patch, int revision, @@ -168,7 +168,7 @@ public SemanticVersion(int major, /// w.x.y.Z /// Prerelease labels /// Build metadata - public SemanticVersion(int major, + public SemanticSortableVersion(int major, int minor, int patch, int revision, @@ -187,7 +187,7 @@ public SemanticVersion(int major, /// Build metadata /// Non-normalized original version string /// Indicates whether to normalize to semantic version - public SemanticVersion(Version version, + public SemanticSortableVersion(Version version, IEnumerable? releaseLabels, string? metadata, string? originalVersion, @@ -220,7 +220,7 @@ public SemanticVersion(Version version, /// /// String represnetation of the version /// The SemanticVersion parsed from the supplied string - public static explicit operator SemanticVersion(string versionString) + public static explicit operator SemanticSortableVersion(string versionString) { return SemVerFactory.CreateVersion(versionString); } diff --git a/source/Octopus.Versioning/Semver/StrictSemanticVersion.cs b/source/Octopus.Versioning/Semver/StrictSemanticSortableVersion.cs similarity index 91% rename from source/Octopus.Versioning/Semver/StrictSemanticVersion.cs rename to source/Octopus.Versioning/Semver/StrictSemanticSortableVersion.cs index ea069ad..1df938e 100644 --- a/source/Octopus.Versioning/Semver/StrictSemanticVersion.cs +++ b/source/Octopus.Versioning/Semver/StrictSemanticSortableVersion.cs @@ -10,7 +10,7 @@ namespace Octopus.Versioning.Semver /// /// A strict SemVer implementation /// - public partial class StrictSemanticVersion : IVersion + public partial class StrictSemanticSortableVersion : ISortableVersion { static readonly ISemanticVersionUtils utils = new SemanticVersionUtils(); readonly IEnumerable? releaseLabels; @@ -22,7 +22,7 @@ public partial class StrictSemanticVersion : IVersion /// X.y.z /// x.Y.z /// x.y.Z - public StrictSemanticVersion(int major, int minor, int patch) + public StrictSemanticSortableVersion(int major, int minor, int patch) : this(major, minor, patch, @@ -39,7 +39,7 @@ public StrictSemanticVersion(int major, int minor, int patch) /// x.y.Z /// Release labels that have been split by the dot separator /// Build metadata - StrictSemanticVersion(int major, + StrictSemanticSortableVersion(int major, int minor, int patch, IEnumerable releaseLabels, @@ -48,7 +48,7 @@ public StrictSemanticVersion(int major, int minor, int patch) { } - public StrictSemanticVersion(Version version, IEnumerable? releaseLabels, string? metadata, bool preserveMissingComponents = false) + public StrictSemanticSortableVersion(Version version, IEnumerable? releaseLabels, string? metadata, bool preserveMissingComponents = false) { if (version == null) throw new ArgumentException("version can not be null"); diff --git a/source/Octopus.Versioning/Semver/StrictSemanticVersionBase.cs b/source/Octopus.Versioning/Semver/StrictSemanticVersionBase.cs index 61873c1..35f7fa8 100644 --- a/source/Octopus.Versioning/Semver/StrictSemanticVersionBase.cs +++ b/source/Octopus.Versioning/Semver/StrictSemanticVersionBase.cs @@ -8,7 +8,7 @@ namespace Octopus.Versioning.Semver /// /// A base version operations /// - public partial class StrictSemanticVersion : IFormattable, IComparable, IEquatable + public partial class StrictSemanticSortableVersion : IFormattable, IComparable, IEquatable { /// /// Gives a normalized representation of the version. @@ -42,20 +42,20 @@ public override int GetHashCode() public virtual int CompareTo(object obj) { - return CompareTo(obj as StrictSemanticVersion); + return CompareTo(obj as StrictSemanticSortableVersion); } - public virtual int CompareTo(StrictSemanticVersion? other) + public virtual int CompareTo(StrictSemanticSortableVersion? other) { return CompareTo(other, VersionComparison.Default); } public override bool Equals(object obj) { - return Equals(obj as StrictSemanticVersion); + return Equals(obj as StrictSemanticSortableVersion); } - public virtual bool Equals(StrictSemanticVersion? other) + public virtual bool Equals(StrictSemanticSortableVersion? other) { return Equals(other, VersionComparison.Default); } @@ -63,7 +63,7 @@ public virtual bool Equals(StrictSemanticVersion? other) /// /// True if the VersionBase objects are equal based on the given comparison mode. /// - public virtual bool Equals(StrictSemanticVersion? other, VersionComparison versionComparison) + public virtual bool Equals(StrictSemanticSortableVersion? other, VersionComparison versionComparison) { return CompareTo(other, versionComparison) == 0; } @@ -71,7 +71,7 @@ public virtual bool Equals(StrictSemanticVersion? other, VersionComparison versi /// /// Compares NuGetVersion objects using the given comparison mode. /// - public virtual int CompareTo(StrictSemanticVersion? other, VersionComparison versionComparison) + public virtual int CompareTo(StrictSemanticSortableVersion? other, VersionComparison versionComparison) { var comparer = new VersionComparer(versionComparison); return comparer.Compare(this, other); @@ -80,7 +80,7 @@ public virtual int CompareTo(StrictSemanticVersion? other, VersionComparison ver /// /// == /// - public static bool operator ==(StrictSemanticVersion? version1, StrictSemanticVersion? version2) + public static bool operator ==(StrictSemanticSortableVersion? version1, StrictSemanticSortableVersion? version2) { return Compare(version1, version2) == 0; } @@ -88,17 +88,17 @@ public virtual int CompareTo(StrictSemanticVersion? other, VersionComparison ver /// /// != /// - public static bool operator !=(StrictSemanticVersion? version1, StrictSemanticVersion? version2) + public static bool operator !=(StrictSemanticSortableVersion? version1, StrictSemanticSortableVersion? version2) { return Compare(version1, version2) != 0; } - public static bool operator <(StrictSemanticVersion? version1, StrictSemanticVersion? version2) + public static bool operator <(StrictSemanticSortableVersion? version1, StrictSemanticSortableVersion? version2) { return Compare(version1, version2) < 0; } - public static bool operator <=(StrictSemanticVersion? version1, StrictSemanticVersion? version2) + public static bool operator <=(StrictSemanticSortableVersion? version1, StrictSemanticSortableVersion? version2) { return Compare(version1, version2) <= 0; } @@ -106,7 +106,7 @@ public virtual int CompareTo(StrictSemanticVersion? other, VersionComparison ver /// /// > /// - public static bool operator >(StrictSemanticVersion? version1, StrictSemanticVersion? version2) + public static bool operator >(StrictSemanticSortableVersion? version1, StrictSemanticSortableVersion? version2) { return Compare(version1, version2) > 0; } @@ -114,12 +114,12 @@ public virtual int CompareTo(StrictSemanticVersion? other, VersionComparison ver /// /// >= /// - public static bool operator >=(StrictSemanticVersion? version1, StrictSemanticVersion? version2) + public static bool operator >=(StrictSemanticSortableVersion? version1, StrictSemanticSortableVersion? version2) { return Compare(version1, version2) >= 0; } - static int Compare(StrictSemanticVersion? version1, StrictSemanticVersion? version2) + static int Compare(StrictSemanticSortableVersion? version1, StrictSemanticSortableVersion? version2) { IVersionComparer comparer = new VersionComparer(); return comparer.Compare(version1, version2); diff --git a/source/Octopus.Versioning/Semver/VersionComparer.cs b/source/Octopus.Versioning/Semver/VersionComparer.cs index 579ea8d..af12691 100644 --- a/source/Octopus.Versioning/Semver/VersionComparer.cs +++ b/source/Octopus.Versioning/Semver/VersionComparer.cs @@ -54,7 +54,7 @@ public VersionComparer(VersionComparison versionComparison) /// /// Determines if both versions are equal. /// - public bool Equals(IVersion? x, IVersion? y) + public bool Equals(ISortableVersion? x, ISortableVersion? y) { return Compare(x, y) == 0; } @@ -62,7 +62,7 @@ public bool Equals(IVersion? x, IVersion? y) /// /// Compares the given versions using the VersionComparison mode. /// - public static int Compare(IVersion? version1, IVersion? version2, VersionComparison versionComparison) + public static int Compare(ISortableVersion? version1, ISortableVersion? version2, VersionComparison versionComparison) { IVersionComparer comparer = new VersionComparer(versionComparison); return comparer.Compare(version1, version2); @@ -71,7 +71,7 @@ public static int Compare(IVersion? version1, IVersion? version2, VersionCompari /// /// Gives a hash code based on the normalized version string. /// - public int GetHashCode(IVersion? version) + public int GetHashCode(ISortableVersion? version) { if (ReferenceEquals(version, null)) return 0; @@ -82,7 +82,7 @@ public int GetHashCode(IVersion? version) combiner.AddObject(version.Minor); combiner.AddObject(version.Patch); - var nuGetVersion = version as SemanticVersion; + var nuGetVersion = version as SemanticSortableVersion; if (nuGetVersion != null && nuGetVersion.Revision > 0) combiner.AddObject(nuGetVersion.Revision); @@ -102,7 +102,7 @@ public int GetHashCode(IVersion? version) /// /// Compare versions. /// - public int Compare(IVersion? x, IVersion? y) + public int Compare(ISortableVersion? x, ISortableVersion? y) { if (ReferenceEquals(x, y)) return 0; @@ -126,8 +126,8 @@ public int Compare(IVersion? x, IVersion? y) if (result != 0) return result; - var legacyX = x as SemanticVersion; - var legacyY = y as SemanticVersion; + var legacyX = x as SemanticSortableVersion; + var legacyY = y as SemanticSortableVersion; result = CompareLegacyVersion(legacyX, legacyY); if (result != 0) @@ -167,7 +167,7 @@ public int Compare(IVersion? x, IVersion? y) /// /// Compares the 4th digit of the version number. /// - static int CompareLegacyVersion(SemanticVersion? legacyX, SemanticVersion? legacyY) + static int CompareLegacyVersion(SemanticSortableVersion? legacyX, SemanticSortableVersion? legacyY) { var result = 0; diff --git a/source/Octopus.Versioning/Unsortable/UnsortableSortableVersion.cs b/source/Octopus.Versioning/Unsortable/UnsortableSortableVersion.cs new file mode 100644 index 0000000..c14f67f --- /dev/null +++ b/source/Octopus.Versioning/Unsortable/UnsortableSortableVersion.cs @@ -0,0 +1,136 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace Octopus.Versioning.Unsortable +{ + public class UnsortableSortableVersion: IVersion + { + public UnsortableSortableVersion(string release, string? metadata, string? originalString) + { + Metadata = metadata; + Release = release; + OriginalString = originalString ?? string.Empty; + } + + public int Major => 0; + public int Minor => 0; + public int Patch => 0; + public int Revision => 0; + public bool IsPrerelease => false; + public IEnumerable ReleaseLabels => Enumerable.Empty(); + public string? Metadata { get; } + public bool HasMetadata => !string.IsNullOrWhiteSpace(Metadata); + public string Release { get; } + public string OriginalString { get; } + + public VersionFormat Format => VersionFormat.Unsortable; + + // public int CompareTo(object obj) + // { + // if (!(obj is ISortableVersion objVersion)) + // return -1; + // + // if (string.Compare(Release.AlphaNumericOnly(), (objVersion.Release ?? string.Empty).AlphaNumericOnly(), StringComparison.Ordinal) != 0) + // return CompareReleaseLabels(Release.AlphaNumericOnly().Split('.', '-', '_'), (objVersion.Release ?? string.Empty).AlphaNumericOnly().Split('.', '-', '_')); + // + // return 0; + // } + // + // public override string ToString() + // { + // return OriginalString; + // } + // + // public override bool Equals(object obj) + // { + // if (obj is ISortableVersion objVersion) + // return CompareTo(objVersion) == 0; + // + // return false; + // } + // + // public override int GetHashCode() + // { + // return Release.GetHashCode(); + // } + // + // /// + // /// Compares sets of release labels. + // /// + // static int CompareReleaseLabels(IEnumerable version1, IEnumerable version2) + // { + // var result = 0; + // + // using var a = version1.GetEnumerator(); + // using var b = version2.GetEnumerator(); + // + // var aExists = a.MoveNext(); + // var bExists = b.MoveNext(); + // + // while (aExists || bExists) + // { + // if (!aExists && bExists) + // return -1; + // + // if (aExists && !bExists) + // return 1; + // + // // compare the labels + // result = CompareRelease(a.Current, b.Current); + // + // if (result != 0) + // return result; + // + // aExists = a.MoveNext(); + // bExists = b.MoveNext(); + // } + // + // return result; + // } + // + // /// + // /// Release labels are compared as numbers if they are numeric, otherwise they will be compared + // /// as strings. + // /// + // static int CompareRelease(string version1, string version2) + // { + // var version1Num = 0; + // var version2Num = 0; + // var result = 0; + // + // // check if the identifiers are numeric + // var v1IsNumeric = int.TryParse(version1, out version1Num); + // var v2IsNumeric = int.TryParse(version2, out version2Num); + // + // // if both are numeric compare them as numbers + // if (v1IsNumeric && v2IsNumeric) + // { + // result = version1Num.CompareTo(version2Num); + // } + // else if (v1IsNumeric || v2IsNumeric) + // { + // // numeric labels come before alpha labels + // if (v1IsNumeric) + // result = -1; + // else + // result = 1; + // } + // else + // { + // // Ignoring 2.0.0 case sensitive compare. Everything will be compared case insensitively as 2.0.1 specifies. + // var stringCompareResult = StringComparer.OrdinalIgnoreCase.Compare(version1, version2); + // if (stringCompareResult < 0) + // { + // result = -1; + // } + // else if (stringCompareResult > 0) + // { + // result = 1; + // } + // } + // + // return result; + // } + } +} \ No newline at end of file diff --git a/source/Octopus.Versioning/Unsortable/UnsortableVersion.cs b/source/Octopus.Versioning/Unsortable/UnsortableVersion.cs deleted file mode 100644 index 66ebf00..0000000 --- a/source/Octopus.Versioning/Unsortable/UnsortableVersion.cs +++ /dev/null @@ -1,136 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; - -namespace Octopus.Versioning.Unsortable -{ - public class UnsortableVersion: IVersion - { - public UnsortableVersion(string release, string? metadata, string? originalString) - { - Metadata = metadata; - Release = release; - OriginalString = originalString ?? string.Empty; - } - - public int Major => 0; - public int Minor => 0; - public int Patch => 0; - public int Revision => 0; - public bool IsPrerelease => false; - public IEnumerable ReleaseLabels => Enumerable.Empty(); - public string? Metadata { get; } - public bool HasMetadata => !string.IsNullOrWhiteSpace(Metadata); - public string Release { get; } - public string OriginalString { get; } - - public VersionFormat Format => VersionFormat.Unsortable; - - public int CompareTo(object obj) - { - if (!(obj is IVersion objVersion)) - return -1; - - if (string.Compare(Release.AlphaNumericOnly(), (objVersion.Release ?? string.Empty).AlphaNumericOnly(), StringComparison.Ordinal) != 0) - return CompareReleaseLabels(Release.AlphaNumericOnly().Split('.', '-', '_'), (objVersion.Release ?? string.Empty).AlphaNumericOnly().Split('.', '-', '_')); - - return 0; - } - - public override string ToString() - { - return OriginalString; - } - - public override bool Equals(object obj) - { - if (obj is IVersion objVersion) - return CompareTo(objVersion) == 0; - - return false; - } - - public override int GetHashCode() - { - return Release.GetHashCode(); - } - - /// - /// Compares sets of release labels. - /// - static int CompareReleaseLabels(IEnumerable version1, IEnumerable version2) - { - var result = 0; - - using var a = version1.GetEnumerator(); - using var b = version2.GetEnumerator(); - - var aExists = a.MoveNext(); - var bExists = b.MoveNext(); - - while (aExists || bExists) - { - if (!aExists && bExists) - return -1; - - if (aExists && !bExists) - return 1; - - // compare the labels - result = CompareRelease(a.Current, b.Current); - - if (result != 0) - return result; - - aExists = a.MoveNext(); - bExists = b.MoveNext(); - } - - return result; - } - - /// - /// Release labels are compared as numbers if they are numeric, otherwise they will be compared - /// as strings. - /// - static int CompareRelease(string version1, string version2) - { - var version1Num = 0; - var version2Num = 0; - var result = 0; - - // check if the identifiers are numeric - var v1IsNumeric = int.TryParse(version1, out version1Num); - var v2IsNumeric = int.TryParse(version2, out version2Num); - - // if both are numeric compare them as numbers - if (v1IsNumeric && v2IsNumeric) - { - result = version1Num.CompareTo(version2Num); - } - else if (v1IsNumeric || v2IsNumeric) - { - // numeric labels come before alpha labels - if (v1IsNumeric) - result = -1; - else - result = 1; - } - else - { - // Ignoring 2.0.0 case sensitive compare. Everything will be compared case insensitively as 2.0.1 specifies. - var stringCompareResult = StringComparer.OrdinalIgnoreCase.Compare(version1, version2); - if (stringCompareResult < 0) - { - result = -1; - } - else if (stringCompareResult > 0) - { - result = 1; - } - } - - return result; - } - } -} \ No newline at end of file diff --git a/source/Octopus.Versioning/Unsortable/UnsortableVersionParser.cs b/source/Octopus.Versioning/Unsortable/UnsortableVersionParser.cs index 79ba0e6..2fd3ed3 100644 --- a/source/Octopus.Versioning/Unsortable/UnsortableVersionParser.cs +++ b/source/Octopus.Versioning/Unsortable/UnsortableVersionParser.cs @@ -12,7 +12,7 @@ public class UnsortableVersionParser $@"(?:\+(?<{Meta}>[A-Za-z0-9_\-.\\+]*?))?\s*$" ); - public UnsortableVersion Parse(string? version) + public UnsortableSortableVersion Parse(string? version) { if (string.IsNullOrWhiteSpace(version)) throw new ArgumentException("The version can not be an empty string"); @@ -28,23 +28,23 @@ public UnsortableVersion Parse(string? version) if (!result.Success) throw new ArgumentException("The supplied version was not valid"); - return new UnsortableVersion( + return new UnsortableSortableVersion( result.Groups[Release].Success ? result.Groups[Release].Value : string.Empty, result.Groups[Meta].Success ? result.Groups[Meta].Value : string.Empty, noSpaces ); } - public bool TryParse(string version, out UnsortableVersion parsedVersion) + public bool TryParse(string version, out UnsortableSortableVersion parsedSortableVersion) { try { - parsedVersion = Parse(version); + parsedSortableVersion = Parse(version); return true; } catch { - parsedVersion = new UnsortableVersion( + parsedSortableVersion = new UnsortableSortableVersion( string.Empty, string.Empty, null diff --git a/source/Octopus.Versioning/VersionFactory.cs b/source/Octopus.Versioning/VersionFactory.cs index edcb258..e4787fc 100644 --- a/source/Octopus.Versioning/VersionFactory.cs +++ b/source/Octopus.Versioning/VersionFactory.cs @@ -44,37 +44,37 @@ public static IVersion CreateVersion(string input, VersionFormat format) } } - public static IVersion CreateMavenVersion(string input) + public static ISortableVersion CreateMavenVersion(string input) { return new MavenVersionParser().Parse(input); } - public static IVersion CreateSemanticVersion(string input, bool preserveMissingComponents = false) + public static ISortableVersion CreateSemanticVersion(string input, bool preserveMissingComponents = false) { return SemVerFactory.CreateVersion(input, preserveMissingComponents); } - public static IVersion CreateSemanticVersion(int major, int minor, int patch, string releaseLabel) + public static ISortableVersion CreateSemanticVersion(int major, int minor, int patch, string releaseLabel) { - return new SemanticVersion(major, minor, patch, releaseLabel); + return new SemanticSortableVersion(major, minor, patch, releaseLabel); } - public static IVersion CreateSemanticVersion(int major, int minor, int patch) + public static ISortableVersion CreateSemanticVersion(int major, int minor, int patch) { - return new SemanticVersion(major, minor, patch); + return new SemanticSortableVersion(major, minor, patch); } - public static IVersion CreateSemanticVersion(int major, int minor, int patch, int revision) + public static ISortableVersion CreateSemanticVersion(int major, int minor, int patch, int revision) { - return new SemanticVersion(major, minor, patch, revision); + return new SemanticSortableVersion(major, minor, patch, revision); } - public static IVersion CreateSemanticVersion(Version version, string? releaseLabel = null, string? metadata = null) + public static ISortableVersion CreateSemanticVersion(Version version, string? releaseLabel = null, string? metadata = null) { - return new SemanticVersion(version, releaseLabel, metadata); + return new SemanticSortableVersion(version, releaseLabel, metadata); } - public static IVersion CreateSemanticVersion(int major, + public static ISortableVersion CreateSemanticVersion(int major, int minor, int patch, int revision, @@ -82,7 +82,7 @@ public static IVersion CreateSemanticVersion(int major, string metadata, string originalVersion) { - return new SemanticVersion(major, + return new SemanticSortableVersion(major, minor, patch, revision, @@ -90,7 +90,7 @@ public static IVersion CreateSemanticVersion(int major, metadata); } - public static IVersion TryCreateMavenVersion(string input) + public static ISortableVersion TryCreateMavenVersion(string input) { /* * Any version is valid for Maven @@ -98,34 +98,34 @@ public static IVersion TryCreateMavenVersion(string input) return new MavenVersionParser().Parse(input); } - public static IVersion? TryCreateSemanticVersion(string input, bool preserveMissingComponents = false) + public static ISortableVersion? TryCreateSemanticVersion(string input, bool preserveMissingComponents = false) { return SemVerFactory.TryCreateVersion(input, preserveMissingComponents); } - public static IVersion? CreateSemanticVersionOrNone(string input, bool preserveMissingComponents = false) + public static ISortableVersion? CreateSemanticVersionOrNone(string input, bool preserveMissingComponents = false) { return SemVerFactory.CreateVersionOrNone(input, preserveMissingComponents); } - public static IVersion CreateSemanticVersion(Version version, + public static ISortableVersion CreateSemanticVersion(Version version, IEnumerable releaseLabels, string metadata, string originalVersion) { - return new SemanticVersion( + return new SemanticSortableVersion( version, releaseLabels, metadata, originalVersion); } - public static IVersion CreateDockerTag(string input) + public static ISortableVersion CreateDockerTag(string input) { return new DockerTag(new OctopusVersionParser().Parse(input)); } - public static IVersion? TryCreateDockerTag(string input) + public static ISortableVersion? TryCreateDockerTag(string input) { try { @@ -138,12 +138,12 @@ public static IVersion CreateDockerTag(string input) } } - public static IVersion CreateOctopusVersion(string input) + public static ISortableVersion CreateOctopusVersion(string input) { return new OctopusVersionParser().Parse(input); } - public static IVersion? TryCreateOctopusVersion(string input) + public static ISortableVersion? TryCreateOctopusVersion(string input) { try { diff --git a/source/Octopus.Versioning/VersionFormatter.cs b/source/Octopus.Versioning/VersionFormatter.cs index ab4c1e0..6d53a5a 100644 --- a/source/Octopus.Versioning/VersionFormatter.cs +++ b/source/Octopus.Versioning/VersionFormatter.cs @@ -21,7 +21,7 @@ public class VersionFormatter : IFormatProvider, ICustomFormatter if (argType == typeof(IFormattable)) formatted = ((IFormattable)arg).ToString(format, formatProvider); else if (!string.IsNullOrEmpty(format)) - if (arg is IVersion version) + if (arg is ISortableVersion version) { // single char identifiers if (format.Length == 1) @@ -52,62 +52,62 @@ public class VersionFormatter : IFormatProvider, ICustomFormatter public object? GetFormat(Type formatType) { if (formatType == typeof(ICustomFormatter) || - typeof(IVersion).IsAssignableFrom(formatType)) + typeof(ISortableVersion).IsAssignableFrom(formatType)) return this; return null; } - static string GetNormalizedString(IVersion version) + static string GetNormalizedString(ISortableVersion sortableVersion) { var sb = new StringBuilder(); - sb.Append(Format('V', version)); + sb.Append(Format('V', sortableVersion)); - if (version.IsPrerelease) + if (sortableVersion.IsPrerelease) { sb.Append('-'); - sb.Append(version.Release); + sb.Append(sortableVersion.Release); } - if (version.HasMetadata) + if (sortableVersion.HasMetadata) { sb.Append('+'); - sb.Append(version.Metadata); + sb.Append(sortableVersion.Metadata); } return sb.ToString(); } - static string? Format(char c, IVersion version) + static string? Format(char c, ISortableVersion sortableVersion) { string? s = null; switch (c) { case 'N': - s = GetNormalizedString(version); + s = GetNormalizedString(sortableVersion); break; case 'R': - s = version.Release; + s = sortableVersion.Release; break; case 'M': - s = version.Metadata; + s = sortableVersion.Metadata; break; case 'V': - s = FormatVersion(version); + s = FormatVersion(sortableVersion); break; case 'x': - s = string.Format(CultureInfo.InvariantCulture, "{0}", version.Major); + s = string.Format(CultureInfo.InvariantCulture, "{0}", sortableVersion.Major); break; case 'y': - s = string.Format(CultureInfo.InvariantCulture, "{0}", version.Minor); + s = string.Format(CultureInfo.InvariantCulture, "{0}", sortableVersion.Minor); break; case 'z': - s = string.Format(CultureInfo.InvariantCulture, "{0}", version.Patch); + s = string.Format(CultureInfo.InvariantCulture, "{0}", sortableVersion.Patch); break; case 'r': - var nuGetVersion = version as SemanticVersion; + var nuGetVersion = sortableVersion as SemanticSortableVersion; s = string.Format(CultureInfo.InvariantCulture, "{0}", nuGetVersion != null && nuGetVersion.IsLegacyVersion ? nuGetVersion.Version.Revision : 0); break; } @@ -115,16 +115,16 @@ static string GetNormalizedString(IVersion version) return s; } - static string FormatVersion(IVersion version) + static string FormatVersion(ISortableVersion sortableVersion) { - var nuGetVersion = version as SemanticVersion; + var nuGetVersion = sortableVersion as SemanticSortableVersion; var legacy = nuGetVersion != null && nuGetVersion.IsLegacyVersion; return string.Format(CultureInfo.InvariantCulture, "{0}.{1}.{2}{3}", - version.Major, - version.Minor, - version.Patch, + sortableVersion.Major, + sortableVersion.Minor, + sortableVersion.Patch, legacy ? string.Format(CultureInfo.InvariantCulture, ".{0}", nuGetVersion?.Version.Revision) : null); } }