Notably, this adds a SemVer shim assembly wrapping Hive.Versioning.pull/72/head
@ -0,0 +1,78 @@ | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Linq; | |||
using Hive.Versioning; | |||
using HVersion = Hive.Versioning.Version; | |||
namespace SemVer | |||
{ | |||
[Obsolete("Use Hive.Versioning.VersionRange instead.")] | |||
public class Range : IEquatable<Range>, IEquatable<VersionRange> | |||
{ | |||
public VersionRange UnderlyingRange { get; } | |||
private Range(VersionRange real) => UnderlyingRange = real; | |||
public Range(string rangeSpec, bool loose = false) : this(new(rangeSpec)) | |||
=> _ = loose; // loose is ignored because Hive doesn't have an equivalent | |||
public static Range ForHiveRange(VersionRange real) => new(real); | |||
public bool IsSatisfied(Version version) => IsSatisfied(version.UnderlyingVersion); | |||
public bool IsSatisfied(HVersion version) => UnderlyingRange.Matches(version); | |||
public bool IsSatisfied(string versionString, bool loose = false) => IsSatisfied(new Version(versionString, loose)); | |||
public IEnumerable<Version> Satisfying(IEnumerable<Version> versions) => versions.Where(IsSatisfied); | |||
public IEnumerable<string> Satisfying(IEnumerable<string> versions, bool loose = false) | |||
=> versions.Where(v => IsSatisfied(v, loose)); | |||
public Version? MaxSatisfying(IEnumerable<Version> versions) => Satisfying(versions).Max(); | |||
public string? MaxSatisfying(IEnumerable<string> versionStrings, bool loose = false) | |||
=> MaxSatisfying(ValidVersions(versionStrings, loose))?.ToString(); | |||
public Range Intersect(Range other) => new(UnderlyingRange & other.UnderlyingRange); // the conjunction is the intersection | |||
public override string ToString() => UnderlyingRange.ToString(); | |||
public bool Equals(Range? other) => UnderlyingRange.Equals(other?.UnderlyingRange); | |||
public bool Equals(VersionRange? other) => UnderlyingRange.Equals(other); | |||
public override bool Equals(object? obj) | |||
=> obj switch | |||
{ | |||
Range r => Equals(r), | |||
VersionRange vr => Equals(vr), | |||
_ => false | |||
}; | |||
public static bool operator ==(Range? a, Range? b) => a?.Equals(b) ?? b is null; | |||
public static bool operator !=(Range? a, Range? b) => !(a == b); | |||
public override int GetHashCode() => UnderlyingRange.GetHashCode(); | |||
public static bool IsSatisfied(string rangeSpec, string versionString, bool loose = false) | |||
=> new Range(rangeSpec, loose).IsSatisfied(versionString, loose); | |||
public static IEnumerable<string> Satisfying(string rangeSpec, IEnumerable<string> versions, bool loose = false) | |||
=> new Range(rangeSpec, loose).Satisfying(versions, loose); | |||
public static string? MaxSatisfying(string rangeSpec, IEnumerable<string> versions, bool loose = false) | |||
=> new Range(rangeSpec, loose).MaxSatisfying(versions, loose); | |||
private IEnumerable<Version> ValidVersions(IEnumerable<string> versionStrings, bool loose) | |||
{ | |||
foreach (string versionString in versionStrings) | |||
{ | |||
Version? version = null; | |||
try | |||
{ | |||
version = new Version(versionString, loose); | |||
} | |||
catch (ArgumentException) | |||
{ | |||
} | |||
if (version is not null) | |||
{ | |||
yield return version; | |||
} | |||
} | |||
} | |||
} | |||
} |
@ -0,0 +1,16 @@ | |||
<Project Sdk="Microsoft.NET.Sdk"> | |||
<Import Project="..\Common.props" /> | |||
<PropertyGroup> | |||
<TargetFramework>net461</TargetFramework> | |||
<Nullable>enable</Nullable> | |||
</PropertyGroup> | |||
<ItemGroup> | |||
<Reference Include="Hive.Versioning" Version="0.1.0"> | |||
<HintPath>$(MSBuildThisFileDirectory)..\Libs\thirdparty\Hive.Versioning.dll</HintPath> | |||
</Reference> | |||
</ItemGroup> | |||
</Project> |
@ -0,0 +1,70 @@ | |||
using System; | |||
using System.Linq; | |||
using HVersion = Hive.Versioning.Version; | |||
namespace SemVer | |||
{ | |||
[Obsolete("Use Hive.Versioning.Version instead.")] | |||
public class Version : IComparable<Version>, IComparable<HVersion>, IComparable, IEquatable<Version>, IEquatable<HVersion> | |||
{ | |||
public HVersion UnderlyingVersion { get; } | |||
private Version(HVersion real) => UnderlyingVersion = real; | |||
public static Version ForHiveVersion(HVersion real) => new(real); | |||
public Version(string input, bool loose = false) : this(new HVersion(input)) | |||
=> _ = loose; // specifically unused because Hive has no equivalent (by design) | |||
public Version(int major, int minor, int patch, string? preRelease = null, string? build = null) | |||
: this(new HVersion(major, minor, patch, | |||
preRelease is null ? Enumerable.Empty<string>() : preRelease.Split('.'), | |||
build is null ? Enumerable.Empty<string>() : build.Split('.'))) | |||
{ | |||
} | |||
public int Major => (int)UnderlyingVersion.Major; | |||
public int Minor => (int)UnderlyingVersion.Minor; | |||
public int Patch => (int)UnderlyingVersion.Patch; | |||
public string PreRelease => string.Join(".", UnderlyingVersion.PreReleaseIds); | |||
public string Build => string.Join(".", UnderlyingVersion.BuildIds); | |||
public Version BaseVersion() => new(new(UnderlyingVersion.Major, UnderlyingVersion.Minor, UnderlyingVersion.Patch)); | |||
public override string ToString() => UnderlyingVersion.ToString(); | |||
public string Clean() => ToString(); // normally this is the other way around kek | |||
public override int GetHashCode() => UnderlyingVersion.GetHashCode(); | |||
public bool Equals(Version? other) => UnderlyingVersion.Equals(other?.UnderlyingVersion); | |||
public bool Equals(HVersion? other) => UnderlyingVersion.Equals(other); | |||
public override bool Equals(object? obj) | |||
=> obj switch | |||
{ | |||
Version v => Equals(v), | |||
HVersion h => Equals(h), | |||
_ => false | |||
}; | |||
public int CompareTo(Version? other) => UnderlyingVersion.CompareTo(other?.UnderlyingVersion); | |||
public int CompareTo(HVersion? other) => UnderlyingVersion.CompareTo(other); | |||
public int CompareTo(object? obj) | |||
=> obj switch | |||
{ | |||
null => 1, | |||
Version v => CompareTo(v), | |||
HVersion h => CompareTo(h), | |||
_ => throw new ArgumentException("Object is not a Version") | |||
}; | |||
public static bool operator ==(Version? a, Version? b) | |||
=> a?.UnderlyingVersion == b?.UnderlyingVersion; | |||
public static bool operator !=(Version? a, Version? b) | |||
=> a?.UnderlyingVersion != b?.UnderlyingVersion; | |||
public static bool operator >(Version? a, Version? b) | |||
=> a is null ? b is not null && b.CompareTo(a) < 0 : a.CompareTo(b) > 0; | |||
public static bool operator >=(Version? a, Version? b) | |||
=> !(a < b); | |||
public static bool operator <(Version? a, Version? b) | |||
=> a is null ? b is not null && b.CompareTo(a) > 0 : a.CompareTo(b) < 0; | |||
public static bool operator <=(Version? a, Version? b) | |||
=> !(a > b); | |||
} | |||
} |