-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSemVer.cs
More file actions
205 lines (180 loc) · 7.24 KB
/
Copy pathSemVer.cs
File metadata and controls
205 lines (180 loc) · 7.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
using System.Text.RegularExpressions;
namespace Dev;
public enum BumpPart { Major, Minor, Patch, Revision }
public readonly record struct BumpSpec(
BumpPart Part,
string? PreReleaseSuffix = null,
string? BuildMetadata = null);
public sealed partial class SemVer : IComparable<SemVer>, IEquatable<SemVer>
{
public readonly int Major;
public readonly int? Minor;
/// <summary>Third numeric component. Corresponds to the CLI <c>patch</c> bump part.</summary>
public readonly int? Build;
/// <summary>Fourth numeric component. Corresponds to the CLI <c>revision</c> bump part.</summary>
public readonly int? Fix;
public readonly string? Suffix, BuildVariables;
public readonly bool IsAny;
public SemVer(int major, int? minor, int? build, int? fix, string? suffix, string? buildVariables)
{
Major = major;
Minor = minor;
Build = build;
Fix = fix;
Suffix = suffix;
BuildVariables = buildVariables;
}
public SemVer(string src)
{
if (string.IsNullOrEmpty(src) || src == "*")
{
IsAny = true;
return;
}
var m = VersionRegex().Match(src);
if (!m.Success)
throw new FormatException($"Unparsable version string: {src}");
Major = int.Parse(m.Groups["major"].Value);
if (m.Groups["minor"].Success)
Minor = int.Parse(m.Groups["minor"].Value);
if (m.Groups["build"].Success)
Build = int.Parse(m.Groups["build"].Value);
if (m.Groups["fix"].Success)
Fix = int.Parse(m.Groups["fix"].Value);
if (m.Groups["suffix"].Success)
Suffix = m.Groups["suffix"].Value;
if (m.Groups["buildvars"].Success)
BuildVariables = m.Groups["buildvars"].Value;
}
public bool Equals(SemVer? other)
{
if (other is null) return false;
if (ReferenceEquals(this, other)) return true;
return Major == other.Major && Minor == other.Minor && Build == other.Build && Fix == other.Fix && string.Equals(Suffix, other.Suffix) && IsAny == other.IsAny;
}
public int CompareTo(SemVer? other)
{
if (ReferenceEquals(this, other)) return 0;
if (other is null) return 1;
var majorComparison = Major.CompareTo(other.Major);
if (majorComparison != 0) return majorComparison;
var minorComparison = Nullable.Compare(Minor, other.Minor);
if (minorComparison != 0) return minorComparison;
var buildComparison = Nullable.Compare(Build, other.Build);
if (buildComparison != 0) return buildComparison;
var fixComparison = Nullable.Compare(Fix, other.Fix);
if (fixComparison != 0) return fixComparison;
var suffixComparison = CompareSuffix(Suffix, other.Suffix);
return suffixComparison != 0 ? suffixComparison : IsAny.CompareTo(other.IsAny);
}
private static int CompareSuffix(string? a, string? b)
{
if (string.IsNullOrEmpty(a) && string.IsNullOrEmpty(b)) return 0;
if (string.IsNullOrEmpty(a)) return 1;
if (string.IsNullOrEmpty(b)) return -1;
var aparts = a.Split('.');
var bparts = b.Split('.');
for (int i = 0, l = Math.Max(aparts.Length, bparts.Length); i < l; i++)
{
if (aparts.Length <= i)
return -1;
if (bparts.Length <= i)
return 1;
bool ai = int.TryParse(aparts[i], out var aa),
bi = int.TryParse(bparts[i], out var bb);
if (ai && bi)
if (aa != bb)
return aa - bb;
else
continue;
if (ai) return -1;
if (bi) return 1;
var sc = string.Compare(aparts[i], bparts[i], StringComparison.Ordinal);
if (sc != 0)
return sc;
}
return 0;
}
public override bool Equals(object? obj)
{
if (obj is null) return false;
if (ReferenceEquals(this, obj)) return true;
return obj.GetType() == GetType() && Equals((SemVer)obj);
}
public override int GetHashCode() => HashCode.Combine(Major, Minor, Build, Fix, Suffix, IsAny);
private sealed class SemVerEqualityComparer : IEqualityComparer<SemVer>
{
public bool Equals(SemVer? x, SemVer? y)
{
if (ReferenceEquals(x, y)) return true;
if (x is null) return false;
if (y is null) return false;
if (x.GetType() != y.GetType()) return false;
return x.Major == y.Major && x.Minor == y.Minor && x.Build == y.Build && x.Fix == y.Fix && string.Equals(x.Suffix, y.Suffix) && x.IsAny == y.IsAny;
}
public int GetHashCode(SemVer obj)
{
unchecked
{
var hashCode = obj.Major;
hashCode = hashCode * 397 ^ obj.Minor.GetHashCode();
hashCode = hashCode * 397 ^ obj.Build.GetHashCode();
hashCode = hashCode * 397 ^ obj.Fix.GetHashCode();
hashCode = hashCode * 397 ^ (obj.Suffix != null ? obj.Suffix.GetHashCode() : 0);
hashCode = hashCode * 397 ^ obj.IsAny.GetHashCode();
return hashCode;
}
}
}
public static IEqualityComparer<SemVer> SemVerComparer { get; } = new SemVerEqualityComparer();
public override string ToString()
{
if (IsAny)
return "*";
var rv = Major.ToString();
if (Minor.HasValue)
{
rv += "." + Minor;
if (Build.HasValue)
{
rv += "." + Build;
if (Fix.HasValue)
rv += "." + Fix;
}
}
if (!string.IsNullOrEmpty(Suffix))
rv += "-" + Suffix;
if (!string.IsNullOrEmpty(BuildVariables))
rv += "+" + BuildVariables;
return rv;
}
[GeneratedRegex("^(?<major>[0-9]+)(\\.(?<minor>[0-9]+))?(\\.(?<build>[0-9]+))?(\\.(?<fix>[0-9]+))?(-(?<suffix>.*))?(\\+(?<buildvars>.*))?$", RegexOptions.IgnoreCase | RegexOptions.Compiled, "en-BE")]
private static partial Regex VersionRegex();
public SemVer Bump(BumpSpec spec)
{
var bumped = spec.Part switch
{
BumpPart.Major => new SemVer(Major + 1, 0, 0, Fix is null ? Fix : 0, Suffix, BuildVariables),
BumpPart.Minor => new SemVer(Major, Minor + 1, 0, Fix is null ? Fix : 0, Suffix, BuildVariables),
BumpPart.Patch => new SemVer(Major, Minor, Build + 1, Fix is null ? Fix : 0, Suffix, BuildVariables),
_ /* Revision */ => new SemVer(Major, Minor, Build, Fix + 1, Suffix, BuildVariables),
};
if (spec.PreReleaseSuffix is null && spec.BuildMetadata is null)
return bumped;
return new SemVer(
bumped.Major,
bumped.Minor,
bumped.Build,
bumped.Fix,
spec.PreReleaseSuffix ?? bumped.Suffix,
spec.BuildMetadata ?? bumped.BuildVariables);
}
public SemVer Bump(BumpPart part) => Bump(new BumpSpec(part));
public static BumpPart ParsePart(string? raw) => raw switch
{
"major" => BumpPart.Major,
"minor" => BumpPart.Minor,
"patch" => BumpPart.Patch,
_ => BumpPart.Revision,
};
}