-
Notifications
You must be signed in to change notification settings - Fork 692
/
NuGetVersion.cs
199 lines (180 loc) · 7.66 KB
/
NuGetVersion.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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
namespace NuGet.Versioning
{
/// <summary>
/// A hybrid implementation of SemVer that supports semantic versioning as described at http://semver.org while
/// not strictly enforcing it to
/// allow older 4-digit versioning schemes to continue working.
/// </summary>
public partial class NuGetVersion : SemanticVersion
{
private readonly string _originalString;
/// <summary>
/// Creates a NuGetVersion using NuGetVersion.Parse(string)
/// </summary>
/// <param name="version">Version string</param>
public NuGetVersion(string version)
: this(Parse(version))
{
}
/// <summary>
/// Creates a NuGetVersion from an existing NuGetVersion
/// </summary>
public NuGetVersion(NuGetVersion version)
: this(version.Version, version.ReleaseLabels, version.Metadata, version.ToString())
{
}
/// <summary>
/// Creates a NuGetVersion from a .NET Version
/// </summary>
/// <param name="version">Version numbers</param>
/// <param name="releaseLabel">Prerelease label</param>
/// <param name="metadata">Build metadata</param>
public NuGetVersion(Version version, string releaseLabel = null, string metadata = null)
: this(version, ParseReleaseLabels(releaseLabel), metadata, GetLegacyString(version, ParseReleaseLabels(releaseLabel), metadata))
{
}
/// <summary>
/// Creates a NuGetVersion X.Y.Z
/// </summary>
/// <param name="major">X.y.z</param>
/// <param name="minor">x.Y.z</param>
/// <param name="patch">x.y.Z</param>
public NuGetVersion(int major, int minor, int patch)
: this(major, minor, patch, EmptyReleaseLabels, null)
{
}
/// <summary>
/// Creates a NuGetVersion X.Y.Z-alpha
/// </summary>
/// <param name="major">X.y.z</param>
/// <param name="minor">x.Y.z</param>
/// <param name="patch">x.y.Z</param>
/// <param name="releaseLabel">Prerelease label</param>
public NuGetVersion(int major, int minor, int patch, string releaseLabel)
: this(major, minor, patch, ParseReleaseLabels(releaseLabel), null)
{
}
/// <summary>
/// Creates a NuGetVersion X.Y.Z-alpha#build01
/// </summary>
/// <param name="major">X.y.z</param>
/// <param name="minor">x.Y.z</param>
/// <param name="patch">x.y.Z</param>
/// <param name="releaseLabel">Prerelease label</param>
/// <param name="metadata">Build metadata</param>
public NuGetVersion(int major, int minor, int patch, string releaseLabel, string metadata)
: this(major, minor, patch, ParseReleaseLabels(releaseLabel), metadata)
{
}
/// <summary>
/// Creates a NuGetVersion X.Y.Z-alpha.1.2#build01
/// </summary>
/// <param name="major">X.y.z</param>
/// <param name="minor">x.Y.z</param>
/// <param name="patch">x.y.Z</param>
/// <param name="releaseLabels">Prerelease labels</param>
/// <param name="metadata">Build metadata</param>
public NuGetVersion(int major, int minor, int patch, IEnumerable<string> releaseLabels, string metadata)
: this(new Version(major, minor, patch, 0), releaseLabels, metadata, null)
{
}
/// <summary>
/// Creates a NuGetVersion W.X.Y.Z
/// </summary>
/// <param name="major">W.x.y.z</param>
/// <param name="minor">w.X.y.z</param>
/// <param name="patch">w.x.Y.z</param>
/// <param name="revision">w.x.y.Z</param>
public NuGetVersion(int major, int minor, int patch, int revision)
: this(major, minor, patch, revision, EmptyReleaseLabels, null)
{
}
/// <summary>
/// Creates a NuGetVersion W.X.Y.Z-alpha#build01
/// </summary>
/// <param name="major">W.x.y.z</param>
/// <param name="minor">w.X.y.z</param>
/// <param name="patch">w.x.Y.z</param>
/// <param name="revision">w.x.y.Z</param>
/// <param name="releaseLabel">Prerelease label</param>
/// <param name="metadata">Build metadata</param>
public NuGetVersion(int major, int minor, int patch, int revision, string releaseLabel, string metadata)
: this(major, minor, patch, revision, ParseReleaseLabels(releaseLabel), metadata)
{
}
/// <summary>
/// Creates a NuGetVersion W.X.Y.Z-alpha.1#build01
/// </summary>
/// <param name="major">W.x.y.z</param>
/// <param name="minor">w.X.y.z</param>
/// <param name="patch">w.x.Y.z</param>
/// <param name="revision">w.x.y.Z</param>
/// <param name="releaseLabels">Prerelease labels</param>
/// <param name="metadata">Build metadata</param>
public NuGetVersion(int major, int minor, int patch, int revision, IEnumerable<string> releaseLabels, string metadata)
: this(new Version(major, minor, patch, revision), releaseLabels, metadata, null)
{
}
/// <summary>
/// Creates a NuGetVersion from a .NET Version with additional release labels, build metadata, and a
/// non-normalized version string.
/// </summary>
/// <param name="version">Version numbers</param>
/// <param name="releaseLabels">prerelease labels</param>
/// <param name="metadata">Build metadata</param>
/// <param name="originalVersion">Non-normalized original version string</param>
public NuGetVersion(Version version, IEnumerable<string> releaseLabels, string metadata, string originalVersion)
: base(version, releaseLabels, metadata)
{
_originalString = originalVersion;
}
/// <summary>
/// Returns the version string.
/// </summary>
/// <remarks>This method includes legacy behavior. Use ToNormalizedString() instead.</remarks>
/// <remarks>Versions with SemVer 2.0.0 components are automatically normalized.</remarks>
public override string ToString()
{
// Versions with SemVer 2.0.0 components are automatically normalized,
// non-normalized strings are only allowed for backcompat with older versions
// of nuget, and those did not support SemVer 2.0.0.
if (string.IsNullOrEmpty(_originalString) || IsSemVer2)
{
return ToNormalizedString();
}
return _originalString;
}
/// <summary>
/// A System.Version representation of the version without metadata or release labels.
/// </summary>
public Version Version
{
get { return _version; }
}
/// <summary>
/// True if the NuGetVersion is using legacy behavior.
/// </summary>
public virtual bool IsLegacyVersion
{
get { return Version.Revision > 0; }
}
/// <summary>
/// Revision version R (x.y.z.R)
/// </summary>
public int Revision
{
get { return _version.Revision; }
}
/// <summary>
/// Returns true if version is a SemVer 2.0.0 version
/// </summary>
public bool IsSemVer2
{
get { return (_releaseLabels != null && _releaseLabels.Length > 1) || HasMetadata; }
}
}
}