-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathversionUtils.cake
104 lines (85 loc) · 2.85 KB
/
versionUtils.cake
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
#addin "Cake.Json"
#addin "Cake.FileHelpers"
//#tool nuget:?package=GitVersion.CommandLine
public class VersionUtils
{
private const string VersionFile = "version.json";
public static VersionInfo LoadVersion(ICakeContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
VersionInfo verInfo = null;
if (!string.IsNullOrEmpty(VersionFile) && context.FileExists(VersionFile))
{
verInfo = LoadVersionFromJson(context, VersionFile);
}
if (verInfo != null)
{
verInfo.CakeVersion = typeof(ICakeContext).Assembly.GetName().Version.ToString();
}
return verInfo;
}
private static VersionInfo LoadVersionFromJson(ICakeContext context, string versionFile)
{
context.Information("Loading Version Info From File: {0}", versionFile);
if (!context.FileExists(versionFile))
{
context.Error("Version File Does Not Exist");
return null;
}
var obj = context.DeserializeJsonFromFile<VersionInfo>(versionFile);
return obj;
}
public static void UpdateVersion(ICakeContext context, VersionInfo verInfo)
{
if (context == null)
throw new ArgumentNullException("context");
if (!string.IsNullOrEmpty(VersionFile) && context.FileExists(VersionFile))
{
context.Information("Updating Version File {0}", VersionFile);
context.SerializeJsonToFile(VersionFile, verInfo);
}
}
}
public class VersionInfo
{
[Newtonsoft.Json.JsonProperty("major")]
public int Major {get;set;}
[Newtonsoft.Json.JsonProperty("minor")]
public int Minor {get;set;}
[Newtonsoft.Json.JsonProperty("build")]
public int Build {get;set;}
[Newtonsoft.Json.JsonProperty("preRelease")]
public int? PreRelease {get;set;}
[Newtonsoft.Json.JsonProperty("releaseNotes")]
public string[] ReleaseNotes {get;set;}
[Newtonsoft.Json.JsonIgnore]
public string Semantic {get;set;}
[Newtonsoft.Json.JsonIgnore]
public string Milestone {get;set;}
[Newtonsoft.Json.JsonIgnore]
public string CakeVersion {get;set;}
[Newtonsoft.Json.JsonIgnore]
public bool IsPreRelease { get { return PreRelease != null && PreRelease != 0; } }
public string ToString(bool includePreRelease = true)
{
var str = string.Format("{0:#0}.{1:#0}.{2:#0}", Major, Minor, Build);
if (IsPreRelease && includePreRelease) str += string.Format("-pre{0:00}", PreRelease);
return str;
}
public void Display(ICakeContext context)
{
context.Information("Version:");
context.Information("\tMajor: {0}", Major);
context.Information("\tMinor: {0}", Minor);
context.Information("\tBuild: {0}", Build);
context.Information("\tIs PreRelease: {0}", IsPreRelease);
context.Information("\tPreRelease: {0}", PreRelease);
context.Information("\tSemantic: {0}", Semantic);
context.Information("\tMilestone: {0}", Milestone);
context.Information("\tCake Version: {0}", CakeVersion);
if (ReleaseNotes != null) context.Information("\tRelease Notes: {0}", ReleaseNotes);
}
}