-
Notifications
You must be signed in to change notification settings - Fork 652
/
Copy pathSemanticVersion.cs
230 lines (200 loc) · 7.87 KB
/
SemanticVersion.cs
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
namespace GitVersion
{
using System;
using System.Text.RegularExpressions;
public class SemanticVersion : IFormattable, IComparable<SemanticVersion>
{
public static SemanticVersion Empty = new SemanticVersion();
static Regex ParseSemVer = new Regex(
@"[vV]?(?<SemVer>(?<Major>\d+)(\.(?<Minor>\d+))?(\.(?<Patch>\d+))?)(\.(?<FourthPart>\d+))?(-(?<Tag>[^\+]*))?(\+(?<BuildMetaData>.*))?",
RegexOptions.Compiled);
public int Major;
public int Minor;
public int Patch;
public SemanticVersionPreReleaseTag PreReleaseTag;
public SemanticVersionBuildMetaData BuildMetaData;
public SemanticVersion()
{
PreReleaseTag = new SemanticVersionPreReleaseTag();
BuildMetaData = new SemanticVersionBuildMetaData();
}
public bool Equals(SemanticVersion obj)
{
if (obj == null)
{
return false;
}
return Major == obj.Major &&
Minor == obj.Minor &&
Patch == obj.Patch &&
PreReleaseTag == obj.PreReleaseTag &&
BuildMetaData == obj.BuildMetaData;
}
public bool IsEmpty()
{
return Equals(Empty);
}
public static bool operator ==(SemanticVersion v1, SemanticVersion v2)
{
if (ReferenceEquals(v1, null))
{
return ReferenceEquals(v2, null);
}
return v1.Equals(v2);
}
public static bool operator !=(SemanticVersion v1, SemanticVersion v2)
{
return !(v1 == v2);
}
public static bool operator >(SemanticVersion v1, SemanticVersion v2)
{
if (v1 == null)
throw new ArgumentNullException("v1");
if (v2 == null)
throw new ArgumentNullException("v2");
return v1.CompareTo(v2) > 0;
}
public static bool operator >=(SemanticVersion v1, SemanticVersion v2)
{
if (v1 == null)
throw new ArgumentNullException("v1");
if (v2 == null)
throw new ArgumentNullException("v2");
return v1.CompareTo(v2) >= 0;
}
public static bool operator <=(SemanticVersion v1, SemanticVersion v2)
{
if (v1 == null)
throw new ArgumentNullException("v1");
if (v2 == null)
throw new ArgumentNullException("v2");
return v1.CompareTo(v2) <= 0;
}
public static bool operator <(SemanticVersion v1, SemanticVersion v2)
{
if (v1 == null)
throw new ArgumentNullException("v1");
if (v2 == null)
throw new ArgumentNullException("v2");
return v1.CompareTo(v2) < 0;
}
public static SemanticVersion Parse(string version)
{
SemanticVersion semanticVersion;
if (!TryParse(version, out semanticVersion))
throw new WarningException(string.Format("Failed to parse {0} into a Semantic Version", version));
return semanticVersion;
}
public static bool TryParse(string version, out SemanticVersion semanticVersion)
{
var parsed = ParseSemVer.Match(version);
if (!parsed.Success)
{
semanticVersion = null;
return false;
}
var semanticVersionBuildMetaData = SemanticVersionBuildMetaData.Parse(parsed.Groups["BuildMetaData"].Value);
var fourthPart = parsed.Groups["FourthPart"];
if (fourthPart.Success && semanticVersionBuildMetaData.CommitsSinceTag == null)
{
semanticVersionBuildMetaData.CommitsSinceTag = int.Parse(fourthPart.Value);
}
semanticVersion = new SemanticVersion
{
Major = int.Parse(parsed.Groups["Major"].Value),
Minor = parsed.Groups["Minor"].Success ? int.Parse(parsed.Groups["Minor"].Value) : 0,
Patch = parsed.Groups["Patch"].Success ? int.Parse(parsed.Groups["Patch"].Value) : 0,
PreReleaseTag = SemanticVersionPreReleaseTag.Parse(parsed.Groups["Tag"].Value),
BuildMetaData = semanticVersionBuildMetaData
};
return true;
}
public int CompareTo(SemanticVersion value)
{
if (value == null)
{
return 1;
}
if (Major != value.Major)
{
if (Major > value.Major)
{
return 1;
}
return -1;
}
if (Minor != value.Minor)
{
if (Minor > value.Minor)
{
return 1;
}
return -1;
}
if (Patch != value.Patch)
{
if (Patch > value.Patch)
{
return 1;
}
return -1;
}
if (PreReleaseTag != value.PreReleaseTag)
{
if (PreReleaseTag > value.PreReleaseTag)
{
return 1;
}
return -1;
}
return 0;
}
public override string ToString()
{
return ToString(null);
}
/// <summary>
/// <para>s - Default SemVer [1.2.3-beta.4+5]</para>
/// <para>f - Full SemVer [1.2.3-beta.4+5]</para>
/// <para>i - Informational SemVer [1.2.3-beta.4+5.Branch.master.BranchType.Master.Sha.000000]</para>
/// <para>j - Just the SemVer part [1.2.3]</para>
/// <para>t - SemVer with the tag [1.2.3-beta.4]</para>
/// <para>l - Legacy SemVer tag for systems which do not support SemVer 2.0 properly [1.2.3-beta4]</para>
/// <para>lp - Legacy SemVer tag for systems which do not support SemVer 2.0 properly (padded) [1.2.3-beta0004]</para>
/// </summary>
public string ToString(string format, IFormatProvider formatProvider = null)
{
if (string.IsNullOrEmpty(format))
format = "s";
if (formatProvider != null)
{
var formatter = formatProvider.GetFormat(GetType()) as ICustomFormatter;
if (formatter != null)
return formatter.Format(format, this, formatProvider);
}
switch (format.ToLower())
{
case "j":
return string.Format("{0}.{1}.{2}", Major, Minor, Patch);
case "s":
return PreReleaseTag.HasTag() ? string.Format("{0}-{1}", ToString("j"), PreReleaseTag) : ToString("j");
case "l":
return PreReleaseTag.HasTag() ? string.Format("{0}-{1}", ToString("j"), PreReleaseTag.ToString("l")) : ToString("j");
case "lp":
return PreReleaseTag.HasTag() ? string.Format("{0}-{1}", ToString("j"), PreReleaseTag.ToString("lp")) : ToString("j");
case "f":
{
var buildMetadata = BuildMetaData.ToString();
return !string.IsNullOrEmpty(buildMetadata) ? string.Format("{0}+{1}", ToString("s"), buildMetadata) : ToString("s");
}
case "i":
{
var buildMetadata = BuildMetaData.ToString("f");
return !string.IsNullOrEmpty(buildMetadata) ? string.Format("{0}+{1}", ToString("s"), buildMetadata) : ToString("s");
}
default:
throw new ArgumentException(string.Format("Unrecognised format '{0}'", format), "format");
}
}
}
}