-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathBuildVersion.cs
59 lines (49 loc) · 1.69 KB
/
BuildVersion.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
using Cake.Common.Diagnostics;
using Cake.Common.Tools.GitVersion;
using Cake.Core;
public class BuildVersion
{
public string Prefix { get; set; }
public string Suffix { get; set; }
public string FullSemVer { get; set; }
public BuildVersion(string version, string suffix, string fullSemVer)
{
Prefix = version;
Suffix = suffix;
FullSemVer = fullSemVer;
if (string.IsNullOrWhiteSpace(Suffix))
{
Suffix = null;
}
}
public string GetSemanticVersion()
{
if (!string.IsNullOrWhiteSpace(Suffix))
{
return string.Concat(Prefix, "-", Suffix);
}
return Prefix;
}
public static BuildVersion Calculate(Context context)
{
string version = null;
string semVersion = null;
string fullSemVer = null;
context.Information("Calculating semantic version...");
if (!context.IsLocalBuild)
{
// Run to set the version properties inside the CI server
GitVersionRunner.Run(context, GitVersionOutput.BuildServer);
}
// Run in interactive mode to get the properties for the rest of the script
var assertedversions = GitVersionRunner.Run(context, GitVersionOutput.Json);
version = assertedversions.MajorMinorPatch;
semVersion = assertedversions.LegacySemVerPadded;
fullSemVer = assertedversions.FullSemVer;
if (string.IsNullOrWhiteSpace(version))
{
throw new CakeException("Could not calculate version of build.");
}
return new BuildVersion(version, semVersion.Substring(version.Length).TrimStart('-'), fullSemVer);
}
}