SemVersion.cs
21.2 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
/*
Copyright (c) 2013 Max Hauser
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
From: https://github.com/maxhauser/semver
*/
using System;
#if !NETSTANDARD
using System.Globalization;
using System.Runtime.Serialization;
using System.Security.Permissions;
#endif
using System.Text.RegularExpressions;
namespace Semver
{
/// <summary>
/// A semantic version implementation.
/// Conforms to v2.0.0 of http://semver.org/
/// </summary>
#if NETSTANDARD
public sealed class SemVersion : IComparable<SemVersion>, IComparable
#else
[Serializable]
internal sealed class SemVersion : IComparable<SemVersion>, IComparable, ISerializable
#endif
{
static Regex parseEx =
new Regex(@"^(?<major>\d+)" +
@"(\.(?<minor>\d+))?" +
@"(\.(?<patch>\d+))?" +
@"(\-(?<pre>[0-9A-Za-z\-\.]+))?" +
@"(\+(?<build>[0-9A-Za-z\-\.]+))?$",
#if NETSTANDARD
RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture);
#else
RegexOptions.CultureInvariant | RegexOptions.Compiled | RegexOptions.ExplicitCapture);
#endif
#if !NETSTANDARD
/// <summary>
/// Initializes a new instance of the <see cref="SemVersion" /> class.
/// </summary>
/// <param name="info"></param>
/// <param name="context"></param>
/// <exception cref="ArgumentNullException"></exception>
private SemVersion(SerializationInfo info, StreamingContext context)
{
if (info == null) throw new ArgumentNullException("info");
var semVersion = Parse(info.GetString("SemVersion"));
Major = semVersion.Major;
Minor = semVersion.Minor;
Patch = semVersion.Patch;
Prerelease = semVersion.Prerelease;
Build = semVersion.Build;
}
#endif
/// <summary>
/// Initializes a new instance of the <see cref="SemVersion" /> class.
/// </summary>
/// <param name="major">The major version.</param>
/// <param name="minor">The minor version.</param>
/// <param name="patch">The patch version.</param>
/// <param name="prerelease">The prerelease version (eg. "alpha").</param>
/// <param name="build">The build eg ("nightly.232").</param>
public SemVersion(int major, int minor = 0, int patch = 0, string prerelease = "", string build = "")
{
this.Major = major;
this.Minor = minor;
this.Patch = patch;
this.Prerelease = prerelease ?? "";
this.Build = build ?? "";
}
/// <summary>
/// Initializes a new instance of the <see cref="SemVersion"/> class.
/// </summary>
/// <param name="version">The <see cref="System.Version"/> that is used to initialize
/// the Major, Minor, Patch and Build properties.</param>
public SemVersion(Version version)
{
if (version == null)
throw new ArgumentNullException("version");
this.Major = version.Major;
this.Minor = version.Minor;
if (version.Revision >= 0)
{
this.Patch = version.Revision;
}
this.Prerelease = String.Empty;
if (version.Build > 0)
{
this.Build = version.Build.ToString();
}
else
{
this.Build = String.Empty;
}
}
/// <summary>
/// Parses the specified string to a semantic version.
/// </summary>
/// <param name="version">The version string.</param>
/// <param name="strict">If set to <c>true</c> minor and patch version are required, else they default to 0.</param>
/// <returns>The SemVersion object.</returns>
/// <exception cref="System.InvalidOperationException">When a invalid version string is passed.</exception>
public static SemVersion Parse(string version, bool strict = false)
{
var match = parseEx.Match(version);
if (!match.Success)
{
return new SemVersion(0);
}
#if NETSTANDARD
var major = int.Parse(match.Groups["major"].Value);
#else
var major = int.Parse(match.Groups["major"].Value, CultureInfo.InvariantCulture);
#endif
var minorMatch = match.Groups["minor"];
int minor = 0;
if (minorMatch.Success)
{
#if NETSTANDARD
minor = int.Parse(minorMatch.Value);
#else
minor = int.Parse(minorMatch.Value, CultureInfo.InvariantCulture);
#endif
}
else if (strict)
{
throw new InvalidOperationException("Invalid version (no minor version given in strict mode)");
}
var patchMatch = match.Groups["patch"];
int patch = 0;
if (patchMatch.Success)
{
#if NETSTANDARD
patch = int.Parse(patchMatch.Value);
#else
patch = int.Parse(patchMatch.Value, CultureInfo.InvariantCulture);
#endif
}
else if (strict)
{
throw new InvalidOperationException("Invalid version (no patch version given in strict mode)");
}
var prerelease = match.Groups["pre"].Value;
var build = match.Groups["build"].Value;
return new SemVersion(major, minor, patch, prerelease, build);
}
/// <summary>
/// Parses the specified string to a semantic version.
/// </summary>
/// <param name="version">The version string.</param>
/// <param name="semver">When the method returns, contains a SemVersion instance equivalent
/// to the version string passed in, if the version string was valid, or <c>null</c> if the
/// version string was not valid.</param>
/// <param name="strict">If set to <c>true</c> minor and patch version are required, else they default to 0.</param>
/// <returns><c>False</c> when a invalid version string is passed, otherwise <c>true</c>.</returns>
public static bool TryParse(string version, out SemVersion semver, bool strict = false)
{
try
{
semver = Parse(version, strict);
return true;
}
catch (Exception)
{
semver = null;
return false;
}
}
/// <summary>
/// Tests the specified versions for equality.
/// </summary>
/// <param name="versionA">The first version.</param>
/// <param name="versionB">The second version.</param>
/// <returns>If versionA is equal to versionB <c>true</c>, else <c>false</c>.</returns>
public static bool Equals(SemVersion versionA, SemVersion versionB)
{
if (ReferenceEquals(versionA, null))
return ReferenceEquals(versionB, null);
return versionA.Equals(versionB);
}
/// <summary>
/// Compares the specified versions.
/// </summary>
/// <param name="versionA">The version to compare to.</param>
/// <param name="versionB">The version to compare against.</param>
/// <returns>If versionA < versionB <c>< 0</c>, if versionA > versionB <c>> 0</c>,
/// if versionA is equal to versionB <c>0</c>.</returns>
public static int Compare(SemVersion versionA, SemVersion versionB)
{
if (ReferenceEquals(versionA, null))
return ReferenceEquals(versionB, null) ? 0 : -1;
return versionA.CompareTo(versionB);
}
/// <summary>
/// Make a copy of the current instance with optional altered fields.
/// </summary>
/// <param name="major">The major version.</param>
/// <param name="minor">The minor version.</param>
/// <param name="patch">The patch version.</param>
/// <param name="prerelease">The prerelease text.</param>
/// <param name="build">The build text.</param>
/// <returns>The new version object.</returns>
public SemVersion Change(int? major = null, int? minor = null, int? patch = null,
string prerelease = null, string build = null)
{
return new SemVersion(
major ?? this.Major,
minor ?? this.Minor,
patch ?? this.Patch,
prerelease ?? this.Prerelease,
build ?? this.Build);
}
/// <summary>
/// Gets the major version.
/// </summary>
/// <value>
/// The major version.
/// </value>
public int Major { get; private set; }
/// <summary>
/// Gets the minor version.
/// </summary>
/// <value>
/// The minor version.
/// </value>
public int Minor { get; private set; }
/// <summary>
/// Gets the patch version.
/// </summary>
/// <value>
/// The patch version.
/// </value>
public int Patch { get; private set; }
/// <summary>
/// Gets the pre-release version.
/// </summary>
/// <value>
/// The pre-release version.
/// </value>
public string Prerelease { get; private set; }
/// <summary>
/// Gets the build version.
/// </summary>
/// <value>
/// The build version.
/// </value>
public string Build { get; private set; }
/// <summary>
/// Returns a <see cref="System.String" /> that represents this instance.
/// </summary>
/// <returns>
/// A <see cref="System.String" /> that represents this instance.
/// </returns>
public override string ToString()
{
var version = "" + Major + "." + Minor + "." + Patch;
if (!String.IsNullOrEmpty(Prerelease))
version += "-" + Prerelease;
if (!String.IsNullOrEmpty(Build))
version += "+" + Build;
return version;
}
/// <summary>
/// Compares the current instance with another object of the same type and returns an integer that indicates
/// whether the current instance precedes, follows, or occurs in the same position in the sort order as the
/// other object.
/// </summary>
/// <param name="obj">An object to compare with this instance.</param>
/// <returns>
/// A value that indicates the relative order of the objects being compared.
/// The return value has these meanings: Value Meaning Less than zero
/// This instance precedes <paramref name="obj" /> in the sort order.
/// Zero This instance occurs in the same position in the sort order as <paramref name="obj" />. i
/// Greater than zero This instance follows <paramref name="obj" /> in the sort order.
/// </returns>
public int CompareTo(object obj)
{
return CompareTo((SemVersion)obj);
}
/// <summary>
/// Compares the current instance with another object of the same type and returns an integer that indicates
/// whether the current instance precedes, follows, or occurs in the same position in the sort order as the
/// other object.
/// </summary>
/// <param name="other">An object to compare with this instance.</param>
/// <returns>
/// A value that indicates the relative order of the objects being compared.
/// The return value has these meanings: Value Meaning Less than zero
/// This instance precedes <paramref name="other" /> in the sort order.
/// Zero This instance occurs in the same position in the sort order as <paramref name="other" />. i
/// Greater than zero This instance follows <paramref name="other" /> in the sort order.
/// </returns>
public int CompareTo(SemVersion other)
{
if (ReferenceEquals(other, null))
return 1;
var r = this.CompareByPrecedence(other);
if (r != 0)
return r;
r = CompareComponent(this.Build, other.Build);
return r;
}
/// <summary>
/// Compares to semantic versions by precedence. This does the same as a Equals, but ignores the build information.
/// </summary>
/// <param name="other">The semantic version.</param>
/// <returns><c>true</c> if the version precedence matches.</returns>
public bool PrecedenceMatches(SemVersion other)
{
return CompareByPrecedence(other) == 0;
}
/// <summary>
/// Compares to semantic versions by precedence. This does the same as a Equals, but ignores the build information.
/// </summary>
/// <param name="other">The semantic version.</param>
/// <returns>
/// A value that indicates the relative order of the objects being compared.
/// The return value has these meanings: Value Meaning Less than zero
/// This instance precedes <paramref name="other" /> in the version precedence.
/// Zero This instance has the same precedence as <paramref name="other" />. i
/// Greater than zero This instance has creater precedence as <paramref name="other" />.
/// </returns>
public int CompareByPrecedence(SemVersion other)
{
if (ReferenceEquals(other, null))
return 1;
var r = this.Major.CompareTo(other.Major);
if (r != 0) return r;
r = this.Minor.CompareTo(other.Minor);
if (r != 0) return r;
r = this.Patch.CompareTo(other.Patch);
if (r != 0) return r;
r = CompareComponent(this.Prerelease, other.Prerelease, true);
return r;
}
static int CompareComponent(string a, string b, bool lower = false)
{
var aEmpty = String.IsNullOrEmpty(a);
var bEmpty = String.IsNullOrEmpty(b);
if (aEmpty && bEmpty)
return 0;
if (aEmpty)
return lower ? 1 : -1;
if (bEmpty)
return lower ? -1 : 1;
var aComps = a.Split('.');
var bComps = b.Split('.');
var minLen = Math.Min(aComps.Length, bComps.Length);
for (int i = 0; i < minLen; i++)
{
var ac = aComps[i];
var bc = bComps[i];
int anum, bnum;
var isanum = Int32.TryParse(ac, out anum);
var isbnum = Int32.TryParse(bc, out bnum);
int r;
if (isanum && isbnum)
{
r = anum.CompareTo(bnum);
if (r != 0) return anum.CompareTo(bnum);
}
else
{
if (isanum)
return -1;
if (isbnum)
return 1;
r = String.CompareOrdinal(ac, bc);
if (r != 0)
return r;
}
}
return aComps.Length.CompareTo(bComps.Length);
}
/// <summary>
/// Determines whether the specified <see cref="System.Object" /> is equal to this instance.
/// </summary>
/// <param name="obj">The <see cref="System.Object" /> to compare with this instance.</param>
/// <returns>
/// <c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c>false</c>.
/// </returns>
public override bool Equals(object obj)
{
if (ReferenceEquals(obj, null))
return false;
if (ReferenceEquals(this, obj))
return true;
var other = (SemVersion)obj;
return this.Major == other.Major &&
this.Minor == other.Minor &&
this.Patch == other.Patch &&
string.Equals(this.Prerelease, other.Prerelease, StringComparison.Ordinal) &&
string.Equals(this.Build, other.Build, StringComparison.Ordinal);
}
/// <summary>
/// Returns a hash code for this instance.
/// </summary>
/// <returns>
/// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
/// </returns>
public override int GetHashCode()
{
unchecked
{
int result = this.Major.GetHashCode();
result = result * 31 + this.Minor.GetHashCode();
result = result * 31 + this.Patch.GetHashCode();
result = result * 31 + this.Prerelease.GetHashCode();
result = result * 31 + this.Build.GetHashCode();
return result;
}
}
#if !NETSTANDARD
[SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
if (info == null) throw new ArgumentNullException("info");
info.AddValue("SemVersion", ToString());
}
#endif
/// <summary>
/// Implicit conversion from string to SemVersion.
/// </summary>
/// <param name="version">The semantic version.</param>
/// <returns>The SemVersion object.</returns>
public static implicit operator SemVersion(string version)
{
return SemVersion.Parse(version);
}
/// <summary>
/// The override of the equals operator.
/// </summary>
/// <param name="left">The left value.</param>
/// <param name="right">The right value.</param>
/// <returns>If left is equal to right <c>true</c>, else <c>false</c>.</returns>
public static bool operator ==(SemVersion left, SemVersion right)
{
return SemVersion.Equals(left, right);
}
/// <summary>
/// The override of the un-equal operator.
/// </summary>
/// <param name="left">The left value.</param>
/// <param name="right">The right value.</param>
/// <returns>If left is not equal to right <c>true</c>, else <c>false</c>.</returns>
public static bool operator !=(SemVersion left, SemVersion right)
{
return !SemVersion.Equals(left, right);
}
/// <summary>
/// The override of the greater operator.
/// </summary>
/// <param name="left">The left value.</param>
/// <param name="right">The right value.</param>
/// <returns>If left is greater than right <c>true</c>, else <c>false</c>.</returns>
public static bool operator >(SemVersion left, SemVersion right)
{
return SemVersion.Compare(left, right) > 0;
}
/// <summary>
/// The override of the greater than or equal operator.
/// </summary>
/// <param name="left">The left value.</param>
/// <param name="right">The right value.</param>
/// <returns>If left is greater than or equal to right <c>true</c>, else <c>false</c>.</returns>
public static bool operator >=(SemVersion left, SemVersion right)
{
return left == right || left > right;
}
/// <summary>
/// The override of the less operator.
/// </summary>
/// <param name="left">The left value.</param>
/// <param name="right">The right value.</param>
/// <returns>If left is less than right <c>true</c>, else <c>false</c>.</returns>
public static bool operator <(SemVersion left, SemVersion right)
{
return SemVersion.Compare(left, right) < 0;
}
/// <summary>
/// The override of the less than or equal operator.
/// </summary>
/// <param name="left">The left value.</param>
/// <param name="right">The right value.</param>
/// <returns>If left is less than or equal to right <c>true</c>, else <c>false</c>.</returns>
public static bool operator <=(SemVersion left, SemVersion right)
{
return left == right || left < right;
}
}
}