-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVersionNumber.cs
More file actions
163 lines (139 loc) · 5.72 KB
/
VersionNumber.cs
File metadata and controls
163 lines (139 loc) · 5.72 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
using System;
using System.Collections.Generic;
using System.Text;
namespace H.Necessaire
{
public class VersionNumber : IEquatable<VersionNumber>, IComparable<VersionNumber>
{
public static readonly IComparer<VersionNumber> Comparer = new VersionNumberComparer();
public static readonly VersionNumber Unknown = new VersionNumber
{
Major = 0,
Minor = 0,
Patch = 0,
Build = 0,
Suffix = null,
};
public VersionNumber() { }
public VersionNumber(int major = 0, int minor = 0, int? patch = null, int? build = null, string suffix = null)
{
this.Major = major;
this.Minor = minor;
this.Patch = patch;
this.Build = build;
this.Suffix = suffix;
}
public int Major { get; set; } = 0;
public int Minor { get; set; } = 0;
public int? Patch { get; set; }
public int? Build { get; set; }
public string Suffix { get; set; }
public string Semantic => ToString();
public override string ToString()
{
StringBuilder versionString = new StringBuilder($"{this.Major}.{this.Minor}");
if (this.Patch != null)
{
versionString.Append($".{this.Patch}");
if (this.Build != null)
{
versionString.Append($".{this.Build}");
}
}
if (!string.IsNullOrWhiteSpace(this.Suffix))
{
versionString.Append($"-{this.Suffix}");
}
return versionString.ToString();
}
public static VersionNumber Parse(string versionNumber)
{
if (string.IsNullOrWhiteSpace(versionNumber) || !versionNumber.Contains("."))
{
throw new InvalidOperationException("The given version is not in semantic format");
}
string[] versionAndSuffixParts = versionNumber.Split(new char[] { '-' }, 2, StringSplitOptions.RemoveEmptyEntries);
var versionString = versionAndSuffixParts[0].ToLower().Replace("v", string.Empty).Trim();
string suffix = versionAndSuffixParts.Length > 1 ? versionAndSuffixParts[1].Trim() : null;
var versionParts = versionString.Split(new char[] { '.' }, 4, StringSplitOptions.RemoveEmptyEntries);
var major = int.Parse(versionParts[0].Trim());
var minor = int.Parse(versionParts[1].Trim());
var patch = versionParts.Length > 2 ? (int?)int.Parse(versionParts[2].Trim()) : null;
var build = versionParts.Length > 3 ? (int?)int.Parse(versionParts[3].Trim()) : null;
return new VersionNumber
{
Major = major,
Minor = minor,
Patch = patch,
Build = build,
Suffix = suffix,
};
}
public bool IsEqualWith(VersionNumber other)
{
if ((object)other is null)
return false;
return
Major == other.Major
&& Minor == other.Minor
&& Patch == other.Patch
&& Build == other.Build
&& Suffix == other.Suffix
;
}
public VersionNumber Clone()
{
return
new VersionNumber
{
Major = Major,
Minor = Minor,
Patch = Patch,
Build = Build,
Suffix = Suffix,
};
}
public override int GetHashCode()
{
return StringComparer.Ordinal.GetHashCode(Semantic);
}
public int CompareTo(VersionNumber other) => Comparer.Compare(this, other);
public override bool Equals(object other) => IsEqualWith(other as VersionNumber);
public bool Equals(VersionNumber other) => IsEqualWith(other);
public static bool operator ==(VersionNumber a, VersionNumber b) => (object)a is null ? (object)b is null : a.Equals(b);
public static bool operator !=(VersionNumber a, VersionNumber b) => (object)a is null ? !((object)b is null) : !a.Equals(b);
public static bool operator >(VersionNumber a, VersionNumber b) => Comparer.Compare(a, b) > 0;
public static bool operator <(VersionNumber a, VersionNumber b) => Comparer.Compare(a, b) < 0;
public static bool operator >=(VersionNumber a, VersionNumber b) => Comparer.Compare(a, b) >= 0;
public static bool operator <=(VersionNumber a, VersionNumber b) => Comparer.Compare(a, b) <= 0;
private class VersionNumberComparer : IComparer<VersionNumber>
{
public int Compare(VersionNumber x, VersionNumber y)
{
if (x is null && y is null)
return 0;
if (x is null && !(y is null))
return -1;
if (!(x is null) && y is null)
return 1;
if (x.Major < y.Major)
return -1;
if (x.Major > y.Major)
return 1;
if (x.Minor < y.Minor)
return -1;
if (x.Minor > y.Minor)
return 1;
if ((x.Patch ?? 0) < (y.Patch ?? 0))
return -1;
if ((x.Patch ?? 0) > (y.Patch ?? 0))
return 1;
if ((x.Build ?? 0) < (y.Build ?? 0))
return -1;
if ((x.Build ?? 0) > (y.Build ?? 0))
return 1;
return string.Compare(x.Suffix, y.Suffix, ignoreCase: true);
}
}
}
}