-
-
Notifications
You must be signed in to change notification settings - Fork 173
/
Copy pathVersionOracle.cs
463 lines (385 loc) · 21 KB
/
VersionOracle.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
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
namespace Nerdbank.GitVersioning
{
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using Validation;
/// <summary>
/// Assembles version information in a variety of formats.
/// </summary>
public class VersionOracle
{
/// <summary>
/// A regex that matches on numeric identifiers for prerelease or build metadata.
/// </summary>
private static readonly Regex NumericIdentifierRegex = new Regex(@"(?<![\w-])(\d+)(?![\w-])");
/// <summary>
/// The 0.0 version.
/// </summary>
private static readonly Version Version0 = new Version(0, 0);
/// <summary>
/// Initializes a new instance of the <see cref="VersionOracle"/> class.
/// </summary>
public static VersionOracle Create(string projectDirectory, string gitRepoDirectory = null, ICloudBuild cloudBuild = null, int? overrideBuildNumberOffset = null, string projectPathRelativeToGitRepoRoot = null)
{
Requires.NotNull(projectDirectory, nameof(projectDirectory));
if (string.IsNullOrEmpty(gitRepoDirectory))
{
gitRepoDirectory = projectDirectory;
}
using (var git = GitExtensions.OpenGitRepo(gitRepoDirectory))
{
return new VersionOracle(projectDirectory, git, null, cloudBuild, overrideBuildNumberOffset, projectPathRelativeToGitRepoRoot);
}
}
/// <summary>
/// Initializes a new instance of the <see cref="VersionOracle"/> class.
/// </summary>
public VersionOracle(string projectDirectory, LibGit2Sharp.Repository repo, ICloudBuild cloudBuild, int? overrideBuildNumberOffset = null, string projectPathRelativeToGitRepoRoot = null)
: this(projectDirectory, repo, null, cloudBuild, overrideBuildNumberOffset, projectPathRelativeToGitRepoRoot)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="VersionOracle"/> class.
/// </summary>
public VersionOracle(string projectDirectory, LibGit2Sharp.Repository repo, LibGit2Sharp.Commit head, ICloudBuild cloudBuild, int? overrideBuildNumberOffset = null, string projectPathRelativeToGitRepoRoot = null)
{
var repoRoot = repo?.Info?.WorkingDirectory?.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
var relativeRepoProjectDirectory = !string.IsNullOrWhiteSpace(repoRoot)
? (!string.IsNullOrEmpty(projectPathRelativeToGitRepoRoot)
? projectPathRelativeToGitRepoRoot
: projectDirectory.Substring(repoRoot.Length).TrimStart(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar))
: null;
var commit = head ?? repo?.Head.Commits.FirstOrDefault();
var committedVersion = VersionFile.GetVersion(commit, relativeRepoProjectDirectory);
var workingVersion = head != null ? VersionFile.GetVersion(head, relativeRepoProjectDirectory) : VersionFile.GetVersion(projectDirectory);
if (overrideBuildNumberOffset.HasValue)
{
if (committedVersion != null)
{
committedVersion.BuildNumberOffset = overrideBuildNumberOffset.Value;
}
if (workingVersion != null)
{
workingVersion.BuildNumberOffset = overrideBuildNumberOffset.Value;
}
}
this.VersionOptions = committedVersion ?? workingVersion;
this.GitCommitId = commit?.Id.Sha ?? cloudBuild?.GitCommitId ?? null;
this.VersionHeight = CalculateVersionHeight(relativeRepoProjectDirectory, commit, committedVersion, workingVersion);
this.BuildingRef = cloudBuild?.BuildingTag ?? cloudBuild?.BuildingBranch ?? repo?.Head.CanonicalName;
// Override the typedVersion with the special build number and revision components, when available.
if (repo != null)
{
this.Version = GetIdAsVersion(commit, committedVersion, workingVersion, this.VersionHeight);
}
else
{
this.Version = this.VersionOptions?.Version.Version ?? Version0;
}
this.VersionHeightOffset = this.VersionOptions?.BuildNumberOffsetOrDefault ?? 0;
this.PrereleaseVersion = ReplaceMacros(this.VersionOptions?.Version.Prerelease ?? string.Empty);
this.CloudBuildNumberOptions = this.VersionOptions?.CloudBuild?.BuildNumberOrDefault ?? VersionOptions.CloudBuildNumberOptions.DefaultInstance;
if (!string.IsNullOrEmpty(this.BuildingRef) && this.VersionOptions?.PublicReleaseRefSpec?.Length > 0)
{
this.PublicRelease = this.VersionOptions.PublicReleaseRefSpec.Any(
expr => Regex.IsMatch(this.BuildingRef, expr));
}
}
/// <summary>
/// Gets the BuildNumber to set the cloud build to (if applicable).
/// </summary>
public string CloudBuildNumber
{
get
{
var commitIdOptions = this.CloudBuildNumberOptions.IncludeCommitIdOrDefault;
bool includeCommitInfo = commitIdOptions.WhenOrDefault == VersionOptions.CloudBuildNumberCommitWhen.Always ||
(commitIdOptions.WhenOrDefault == VersionOptions.CloudBuildNumberCommitWhen.NonPublicReleaseOnly && !this.PublicRelease);
bool commitIdInRevision = includeCommitInfo && commitIdOptions.WhereOrDefault == VersionOptions.CloudBuildNumberCommitWhere.FourthVersionComponent;
bool commitIdInBuildMetadata = includeCommitInfo && commitIdOptions.WhereOrDefault == VersionOptions.CloudBuildNumberCommitWhere.BuildMetadata;
Version buildNumberVersion = commitIdInRevision ? this.Version : this.SimpleVersion;
string buildNumberMetadata = FormatBuildMetadata(commitIdInBuildMetadata ? this.BuildMetadataWithCommitId : this.BuildMetadata);
return buildNumberVersion + this.PrereleaseVersion + buildNumberMetadata;
}
}
/// <summary>
/// Gets a value indicating whether the cloud build number should be set.
/// </summary>
[Ignore]
public bool CloudBuildNumberEnabled => this.CloudBuildNumberOptions.EnabledOrDefault;
/// <summary>
/// Gets the build metadata identifiers, including the git commit ID as the first identifier if appropriate.
/// </summary>
[Ignore]
public IEnumerable<string> BuildMetadataWithCommitId
{
get
{
if (!string.IsNullOrEmpty(this.GitCommitId))
{
yield return $"g{this.GitCommitId.Substring(0, 10)}";
}
foreach (string identifier in this.BuildMetadata)
{
yield return identifier;
}
}
}
/// <summary>
/// Gets a value indicating whether a version.json or version.txt file was found.
/// </summary>
public bool VersionFileFound => this.VersionOptions != null;
/// <summary>
/// Gets the version options used to initialize this instance.
/// </summary>
private VersionOptions VersionOptions { get; }
/// <summary>
/// Gets the version string to use for the <see cref="System.Reflection.AssemblyVersionAttribute"/>.
/// </summary>
public Version AssemblyVersion => GetAssemblyVersion(this.Version, this.VersionOptions).EnsureNonNegativeComponents();
/// <summary>
/// Gets the version string to use for the <see cref="System.Reflection.AssemblyFileVersionAttribute"/>.
/// </summary>
public Version AssemblyFileVersion => this.Version;
/// <summary>
/// Gets the version string to use for the <see cref="System.Reflection.AssemblyInformationalVersionAttribute"/>.
/// </summary>
public string AssemblyInformationalVersion =>
$"{this.Version.ToStringSafe(3)}{this.PrereleaseVersion}{FormatBuildMetadata(this.BuildMetadataWithCommitId)}";
/// <summary>
/// Gets or sets a value indicating whether the project is building
/// in PublicRelease mode.
/// </summary>
public bool PublicRelease { get; set; }
/// <summary>
/// Gets the prerelease version information.
/// </summary>
public string PrereleaseVersion { get; }
/// <summary>
/// Gets the version information without a Revision component.
/// </summary>
public Version SimpleVersion => this.Version.Build >= 0
? new Version(this.Version.Major, this.Version.Minor, this.Version.Build)
: new Version(this.Version.Major, this.Version.Minor);
/// <summary>
/// Gets the build number (i.e. third integer, or PATCH) for this version.
/// </summary>
public int BuildNumber => Math.Max(0, this.Version.Build);
/// <summary>
/// Gets or sets the major.minor version string.
/// </summary>
/// <value>
/// The x.y string (no build number or revision number).
/// </value>
public Version MajorMinorVersion => new Version(this.Version.Major, this.Version.Minor);
/// <summary>
/// Gets the Git revision control commit id for HEAD (the current source code version).
/// </summary>
public string GitCommitId { get; }
/// <summary>
/// Gets the first several characters of the Git revision control commit id for HEAD (the current source code version).
/// </summary>
public string GitCommitIdShort => string.IsNullOrEmpty(this.GitCommitId) ? null : this.GitCommitId.Substring(0, 10);
/// <summary>
/// Gets the number of commits in the longest single path between
/// the specified commit and the most distant ancestor (inclusive)
/// that set the version to the value at HEAD.
/// </summary>
public int VersionHeight { get; }
/// <summary>
/// The offset to add to the <see cref="VersionHeight"/>
/// when calculating the integer to use as the <see cref="BuildNumber"/>
/// or elsewhere that the {height} macro is used.
/// </summary>
public int VersionHeightOffset { get; }
private string BuildingRef { get; }
/// <summary>
/// Gets the version for this project, with up to 4 components.
/// </summary>
public Version Version { get; }
/// <summary>
/// Gets a value indicating whether to set all cloud build variables prefaced with "NBGV_".
/// </summary>
[Ignore]
public bool CloudBuildAllVarsEnabled => this.VersionOptions?.CloudBuildOrDefault.SetAllVariablesOrDefault
?? VersionOptions.CloudBuildOptions.DefaultInstance.SetAllVariablesOrDefault;
/// <summary>
/// Gets a dictionary of all cloud build variables that applies to this project,
/// regardless of the current setting of <see cref="CloudBuildAllVarsEnabled"/>.
/// </summary>
[Ignore]
public IDictionary<string, string> CloudBuildAllVars
{
get
{
var variables = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
var properties = this.GetType().GetTypeInfo().GetProperties(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance);
foreach (var property in properties)
{
if (property.GetCustomAttribute<IgnoreAttribute>() == null)
{
var value = property.GetValue(this);
if (value != null)
{
variables.Add($"NBGV_{property.Name}", value.ToString());
}
}
}
return variables;
}
}
/// <summary>
/// Gets a value indicating whether to set cloud build version variables.
/// </summary>
[Ignore]
public bool CloudBuildVersionVarsEnabled => this.VersionOptions?.CloudBuildOrDefault.SetVersionVariablesOrDefault
?? VersionOptions.CloudBuildOptions.DefaultInstance.SetVersionVariablesOrDefault;
/// <summary>
/// Gets a dictionary of cloud build variables that applies to this project,
/// regardless of the current setting of <see cref="CloudBuildVersionVarsEnabled"/>.
/// </summary>
[Ignore]
public IDictionary<string, string> CloudBuildVersionVars
{
get
{
return new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
{
{ "GitAssemblyInformationalVersion", this.AssemblyInformationalVersion },
{ "GitBuildVersion", this.Version.ToString() },
{ "GitBuildVersionSimple", this.SimpleVersion.ToString() },
};
}
}
/// <summary>
/// Gets the list of build metadata identifiers to include in semver version strings.
/// </summary>
[Ignore]
public List<string> BuildMetadata { get; } = new List<string>();
/// <summary>
/// Gets the +buildMetadata fragment for the semantic version.
/// </summary>
public string BuildMetadataFragment => FormatBuildMetadata(this.BuildMetadataWithCommitId);
/// <summary>
/// Gets the version to use for NuGet packages.
/// </summary>
public string NuGetPackageVersion => this.VersionOptions?.NuGetPackageVersionOrDefault.SemVerOrDefault == 1 ? this.SemVer1 : this.SemVer2;
/// <summary>
/// Gets the version to use for NPM packages.
/// </summary>
public string NpmPackageVersion => this.SemVer1;
/// <summary>
/// Gets a SemVer 1.0 compliant string that represents this version, including the -gCOMMITID suffix
/// when <see cref="PublicRelease"/> is <c>false</c>.
/// </summary>
public string SemVer1 =>
$"{this.Version.ToStringSafe(3)}{this.PrereleaseVersionSemVer1}{this.SemVer1BuildMetadata}";
/// <summary>
/// Gets a SemVer 2.0 compliant string that represents this version, including a +gCOMMITID suffix
/// when <see cref="PublicRelease"/> is <c>false</c>.
/// </summary>
public string SemVer2 =>
$"{this.Version.ToStringSafe(3)}{this.PrereleaseVersion}{this.SemVer2BuildMetadata}";
/// <summary>
/// Gets the minimum number of digits to use for numeric identifiers in SemVer 1.
/// </summary>
public int SemVer1NumericIdentifierPadding => this.VersionOptions?.SemVer1NumericIdentifierPaddingOrDefault ?? 4;
private string SemVer1BuildMetadata =>
this.PublicRelease ? string.Empty : $"-g{this.GitCommitIdShort}";
/// <summary>
/// Gets the build metadata that is appropriate for SemVer2 use.
/// </summary>
/// <remarks>
/// We always put the commit ID in the -prerelease tag for non-public releases.
/// But for public releases, we don't include it in the +buildMetadata section since it may be confusing for NuGet.
/// See https://github.com/AArnott/Nerdbank.GitVersioning/pull/132#issuecomment-307208561
/// </remarks>
private string SemVer2BuildMetadata =>
(this.PublicRelease ? string.Empty : this.GitCommitIdShortForNonPublicPrereleaseTag) + FormatBuildMetadata(this.BuildMetadata);
private string PrereleaseVersionSemVer1 => MakePrereleaseSemVer1Compliant(this.PrereleaseVersion, SemVer1NumericIdentifierPadding);
private string GitCommitIdShortForNonPublicPrereleaseTag => (string.IsNullOrEmpty(this.PrereleaseVersion) ? "-" : ".") + $"g{this.GitCommitIdShort}";
private VersionOptions.CloudBuildNumberOptions CloudBuildNumberOptions { get; }
private int VersionHeightWithOffset => this.VersionHeight + this.VersionHeightOffset;
private static string FormatBuildMetadata(IEnumerable<string> identifiers) =>
(identifiers?.Any() ?? false) ? "+" + string.Join(".", identifiers) : string.Empty;
private static string FormatBuildMetadataSemVerV1(IEnumerable<string> identifiers) =>
(identifiers?.Any() ?? false) ? "-" + string.Join("-", identifiers) : string.Empty;
private static Version GetAssemblyVersion(Version version, VersionOptions versionOptions)
{
// If there is no repo, "version" could have uninitialized components (-1).
version = version.EnsureNonNegativeComponents();
var assemblyVersion = versionOptions?.AssemblyVersionOrDefault.Version ?? new System.Version(version.Major, version.Minor);
var precision = versionOptions?.AssemblyVersionOrDefault.PrecisionOrDefault;
assemblyVersion = new System.Version(
assemblyVersion.Major,
precision >= VersionOptions.VersionPrecision.Minor ? assemblyVersion.Minor : 0,
precision >= VersionOptions.VersionPrecision.Build ? version.Build : 0,
precision >= VersionOptions.VersionPrecision.Revision ? version.Revision : 0);
return assemblyVersion.EnsureNonNegativeComponents(4);
}
/// <summary>
/// Replaces any macros found in a prerelease or build metadata string.
/// </summary>
/// <param name="prereleaseOrBuildMetadata">The prerelease or build metadata.</param>
/// <returns>The specified string, with macros substituted for actual values.</returns>
private string ReplaceMacros(string prereleaseOrBuildMetadata) => prereleaseOrBuildMetadata?.Replace("{height}", this.VersionHeightWithOffset.ToString(CultureInfo.InvariantCulture));
/// <summary>
/// Converts a semver 2 compliant "-beta.5" prerelease tag to a semver 1 compatible one.
/// </summary>
/// <param name="prerelease">The semver 2 prerelease tag, including its leading hyphen.</param>
/// <param name="paddingSize">The minimum number of digits to use for any numeric identifier.</param>
/// <returns>A semver 1 compliant prerelease tag. For example "-beta-0005".</returns>
private static string MakePrereleaseSemVer1Compliant(string prerelease, int paddingSize)
{
if (string.IsNullOrEmpty(prerelease))
{
return prerelease;
}
string paddingFormatter = "{0:" + new string('0', paddingSize) + "}";
string semver1 = prerelease;
// Identify numeric identifiers and pad them.
Assumes.True(prerelease.StartsWith("-"));
semver1 = "-" + NumericIdentifierRegex.Replace(semver1.Substring(1), m => string.Format(CultureInfo.InvariantCulture, paddingFormatter, int.Parse(m.Groups[1].Value)));
semver1 = semver1.Replace('.', '-');
return semver1;
}
private static int CalculateVersionHeight(string relativeRepoProjectDirectory, LibGit2Sharp.Commit headCommit, VersionOptions committedVersion, VersionOptions workingVersion)
{
var headCommitVersion = committedVersion?.Version?.Version ?? Version0;
if (IsVersionFileChangedInWorkingTree(committedVersion, workingVersion))
{
var workingCopyVersion = workingVersion?.Version?.Version;
if (workingCopyVersion == null || !workingCopyVersion.Equals(headCommitVersion))
{
// The working copy has changed the major.minor version.
// So by definition the version height is 0, since no commit represents it yet.
return 0;
}
}
return headCommit?.GetHeight(c => c.CommitMatchesMajorMinorVersion(headCommitVersion, relativeRepoProjectDirectory)) ?? 0;
}
private static Version GetIdAsVersion(LibGit2Sharp.Commit headCommit, VersionOptions committedVersion, VersionOptions workingVersion, int versionHeight)
{
var version = IsVersionFileChangedInWorkingTree(committedVersion, workingVersion) ? workingVersion : committedVersion;
return headCommit.GetIdAsVersionHelper(version, versionHeight);
}
private static bool IsVersionFileChangedInWorkingTree(VersionOptions committedVersion, VersionOptions workingVersion)
{
if (workingVersion != null)
{
return !EqualityComparer<VersionOptions>.Default.Equals(workingVersion, committedVersion);
}
// A missing working version is a change only if it was previously commited.
return committedVersion != null;
}
[AttributeUsage(AttributeTargets.Property)]
private class IgnoreAttribute : Attribute
{
}
}
}