Skip to content

Commit

Permalink
Merge pull request #173 from AshleighAdams/change-meta-ordering
Browse files Browse the repository at this point in the history
Update precedence rules such that "post release" versioning is possible for metadata
  • Loading branch information
AshleighAdams authored Feb 1, 2025
2 parents a61f020 + d9aaaec commit 44a6412
Show file tree
Hide file tree
Showing 13 changed files with 116 additions and 63 deletions.
4 changes: 1 addition & 3 deletions .github/shared.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ definitions:
dotnet-version: |
3.1.x
5.0.x
6.x
7.x
8.x
9.x
setup-nuget: &setup-nuget
name: Setup NuGet
Expand Down
20 changes: 5 additions & 15 deletions .github/workflows/continuous-delivery.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ jobs:
dotnet-version: |
3.1.x
5.0.x
6.x
7.x
8.x
9.x
- name: Restore
run: dotnet restore
Expand Down Expand Up @@ -97,9 +95,7 @@ jobs:
dotnet-version: |
3.1.x
5.0.x
6.x
7.x
8.x
9.x
- name: Publish
run: |
Expand Down Expand Up @@ -137,9 +133,7 @@ jobs:
dotnet-version: |
3.1.x
5.0.x
6.x
7.x
8.x
9.x
- name: Mutation Test
run: |
Expand Down Expand Up @@ -191,9 +185,7 @@ jobs:
dotnet-version: |
3.1.x
5.0.x
6.x
7.x
8.x
9.x
- name: Run Integration Tests
run: |
Expand All @@ -218,9 +210,7 @@ jobs:
dotnet-version: |
3.1.x
5.0.x
6.x
7.x
8.x
9.x
- name: Setup NuGet
run: |
Expand Down
16 changes: 4 additions & 12 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ jobs:
dotnet-version: |
3.1.x
5.0.x
6.x
7.x
8.x
9.x
- name: Restore
run: dotnet restore
Expand Down Expand Up @@ -97,9 +95,7 @@ jobs:
dotnet-version: |
3.1.x
5.0.x
6.x
7.x
8.x
9.x
- name: Publish
run: |
Expand Down Expand Up @@ -137,9 +133,7 @@ jobs:
dotnet-version: |
3.1.x
5.0.x
6.x
7.x
8.x
9.x
- name: Mutation Test
run: |
Expand Down Expand Up @@ -191,9 +185,7 @@ jobs:
dotnet-version: |
3.1.x
5.0.x
6.x
7.x
8.x
9.x
- name: Run Integration Tests
run: |
Expand Down
12 changes: 3 additions & 9 deletions .github/workflows/deploy-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ jobs:
dotnet-version: |
3.1.x
5.0.x
6.x
7.x
8.x
9.x
- name: Setup NuGet
run: |
Expand Down Expand Up @@ -98,9 +96,7 @@ jobs:
dotnet-version: |
3.1.x
5.0.x
6.x
7.x
8.x
9.x
- name: Publish
run: |
Expand Down Expand Up @@ -140,9 +136,7 @@ jobs:
dotnet-version: |
3.1.x
5.0.x
6.x
7.x
8.x
9.x
- name: Setup NuGet
run: |
Expand Down
5 changes: 3 additions & 2 deletions Verlite.sln
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29509.3
# Visual Studio Version 17
VisualStudioVersion = 17.12.35514.174 d17.12
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{D0F10406-C62D-49CE-A369-4CEDB84DC800}"
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
.gitattributes = .gitattributes
.gitignore = .gitignore
Directory.Build.props = Directory.Build.props
global.json = global.json
NuGet.config = NuGet.config
README.md = README.md
EndProjectSection
Expand Down
6 changes: 6 additions & 0 deletions global.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"sdk": {
"version": "9.0.0",
"rollForward": "latestFeature"
}
}
2 changes: 1 addition & 1 deletion src/Verlite.CLI/Verlite.CLI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<PropertyGroup Condition="'$(PublishAot)'=='true'">
<TargetName>verlite</TargetName>
<TargetExtension>elf</TargetExtension>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<SelfContained>true</SelfContained>
<TrimMode>Link</TrimMode>
<RootAllApplicationAssemblies>false</RootAllApplicationAssemblies>
Expand Down
2 changes: 2 additions & 0 deletions src/Verlite.Core/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
#nullable enable
static Verlite.SemVer.CompareMetadata(string? left, string? right) -> int
static Verlite.SemVer.ComparePrerelease(string? left, string? right) -> int
78 changes: 66 additions & 12 deletions src/Verlite.Core/SemVer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,61 @@ private static bool TryParseInt(string input, out int ret)
/// Compares the prerelease taking into account ordinals within the string.
/// </summary>
/// <returns><c>-1</c> if <paramref name="left"/> precedes <paramref name="right"/>, <c>0</c> if they have the same precedence, and <c>1</c> if <paramref name="right"/> precedes the <paramref name="left"/>.</returns>
public static int ComparePrerelease(string left, string right)
public static int ComparePrerelease(string? left, string? right)
{
int minLen = Math.Min(left.Length, right.Length);
if (left is null && right is null)
return 0;
if (left is null)
return 1;
if (right is null)
return -1;

int minLen = Math.Min(left.Length, right!.Length);

for (int i = 0; i < minLen; i++)
{
char l = left[i];
char r = right[i];

if (char.IsDigit(l) && char.IsDigit(r))
{
var (leftOrdinal, leftOrdinalLength) = SelectOrdinals(left.Substring(i));
var (rightOrdinal, rightOrdinalLength) = SelectOrdinals(right.Substring(i));

int cmpOrdinal = leftOrdinal.CompareTo(rightOrdinal);
if (cmpOrdinal != 0)
return cmpOrdinal;

Debug.Assert(leftOrdinalLength == rightOrdinalLength);
i += leftOrdinalLength - 1;
}

if (l != r)
return l < r ? -1 : 1;
}

return left.Length.CompareTo(right.Length) switch
{
0 => 0,
<0 => -1,
>0 => 1,
};
}

/// <summary>
/// Compares the meta taking into account ordinals within the string.
/// </summary>
/// <returns><c>-1</c> if <paramref name="left"/> precedes <paramref name="right"/>, <c>0</c> if they have the same precedence, and <c>1</c> if <paramref name="right"/> precedes the <paramref name="left"/>.</returns>
public static int CompareMetadata(string? left, string? right)
{
if (left is null && right is null)
return 0;
if (left is null)
return -1;
if (right is null)
return 1;

int minLen = Math.Min(left.Length, right!.Length);

for (int i = 0; i < minLen; i++)
{
Expand All @@ -199,10 +251,15 @@ public static int ComparePrerelease(string left, string right)
}

if (l != r)
return l.CompareTo(r);
return l < r ? -1 : 1;
}

return left.Length.CompareTo(right.Length);
return left.Length.CompareTo(right.Length) switch
{
0 => 0,
< 0 => -1,
> 0 => 1,
};
}

/// <summary>
Expand Down Expand Up @@ -266,14 +323,11 @@ static bool areDifferent(int left, int right, out int result)
if (areDifferent(Patch, other.Patch, out compareResult))
return compareResult;

if (Prerelease is null && other.Prerelease is null)
return 0;
else if (other.Prerelease is null)
return -1;
else if (Prerelease is null)
return 1;
else
return ComparePrerelease(Prerelease, other.Prerelease);
compareResult = ComparePrerelease(Prerelease, other.Prerelease);
if (compareResult != 0)
return compareResult;

return CompareMetadata(BuildMetadata, other.BuildMetadata);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
Expand Down
23 changes: 23 additions & 0 deletions tests/UnitTests/PrecedenceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,28 @@ public void PrereleaseComesBeforeRelease()
new SemVer(1, 0, 0, "alpha.101").Should().BeLessThan(new SemVer(1, 0, 0, "alpha.102"));
new SemVer(1, 0, 0, "alpha.aaa").Should().BeLessThan(new SemVer(1, 0, 0, "alpha.aab"));
}

[Fact]
public void OrderedMetaversions()
{
new SemVer(1, 0, 2, null, "abc").Should().BeGreaterThan(new SemVer(1, 0, 1));
new SemVer(1, 0, 0, null, "abc").Should().BeGreaterThan(new SemVer(1, 0, 0));
new SemVer(1, 0, 0, null, "abc").Should().BeGreaterThan(new SemVer(1, 0, 0));
new SemVer(1, 0, 1, null, "abc").Should().BeGreaterThan(new SemVer(1, 0, 0));
new SemVer(1, 0, 0, null, "abc").Should().BeLessThan(new SemVer(1, 0, 0, null, "alpha.0"));
new SemVer(1, 0, 0, null, "abc").Should().BeLessThan(new SemVer(1, 0, 0, null, "alpha.1"));
new SemVer(1, 0, 0, null, "abc.1").Should().BeLessThan(new SemVer(1, 0, 0, null, "alpha.2"));
new SemVer(1, 0, 0, null, "abc.2").Should().BeLessThan(new SemVer(1, 0, 0, null, "alpha.10"));
new SemVer(1, 0, 0, null, "abc.2.1").Should().BeLessThan(new SemVer(1, 0, 0, null, "alpha.100"));
new SemVer(1, 0, 0, null, "abc.101").Should().BeLessThan(new SemVer(1, 0, 0, null, "alpha.102"));
new SemVer(1, 0, 0, null, "abc.aaa").Should().BeLessThan(new SemVer(1, 0, 0, null, "alpha.aab"));

new SemVer(1, 0, 0, null, "abc").Should().BeGreaterThan(new SemVer(1, 0, 0, "def"));
new SemVer(1, 0, 0, "def", "abc").Should().BeGreaterThan(new SemVer(1, 0, 0, "def"));
new SemVer(1, 0, 0, "def", "abc").Should().BeGreaterThan(new SemVer(1, 0, 0, "def"));
new SemVer(1, 0, 0, "def", "b").Should().BeGreaterThan(new SemVer(1, 0, 0, "def", "a"));
new SemVer(1, 0, 0, "def", "rev.13").Should().BeGreaterThan(new SemVer(1, 0, 0, "def", "rev.1"));
new SemVer(1, 0, 0, "def", "aa").Should().BeGreaterThan(new SemVer(1, 0, 0, "def", "a"));
}
}
}
2 changes: 1 addition & 1 deletion tests/UnitTests/UnitTests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<IsPackable>false</IsPackable>
<!-- CA1014: Unit tests don't need to express thier CLS complience -->
<!-- CS1591: Unit tests don't need xmldoc comments. -->
Expand Down
7 changes: 0 additions & 7 deletions tests/UnitTests/VersionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,6 @@ public void CompreableWorks()
(new SemVer(1, 0, 0).CompareTo(new SemVer(1, 0, 0, "alpha"))).Should().Be(1);
}

[Fact]
public void CompreableBuildInfoIgnored()
{
(new SemVer(1, 0, 0, "alpha.1", "abc").CompareTo(new SemVer(1, 0, 0, "alpha.1", "def"))).Should().Be(0);
(new SemVer(1, 0, 0, "alpha.1", "abc").CompareTo(new SemVer(1, 0, 0, "alpha.2", "def"))).Should().Be(-1);
}

[Fact]
public void CoreVersionFunctions()
{
Expand Down

0 comments on commit 44a6412

Please sign in to comment.