forked from ThomasLebrun/XForms-Toolkit
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathversionUtils.cake
241 lines (196 loc) · 7.63 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
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
#addin "Cake.Json"
#addin "Cake.FileHelpers"
#addin "Newtonsoft.Json"
#tool nuget:?package=GitVersion.CommandLine
public class VersionUtils
{
public static VersionInfo LoadVersion(ICakeContext context, Settings settings)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
VersionInfo verInfo = null;
if (!string.IsNullOrEmpty(settings.Version.VersionFile) && context.FileExists(settings.Version.VersionFile))
{
verInfo = LoadVersionFromJson(context, settings.Version.VersionFile);
}
if (verInfo == null && !string.IsNullOrEmpty(settings.Version.AssemblyInfoFile) && context.FileExists(settings.Version.AssemblyInfoFile))
{
verInfo = LoadVersionFromAssemblyInfo(context, settings.Version.AssemblyInfoFile);
}
if (verInfo == null && settings.Version.LoadFromGit)
{
verInfo = LoadVersionFromGit(context);
}
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;
}
private static VersionInfo LoadVersionFromAssemblyInfo(ICakeContext context, string assemblyInfoFile)
{
context.Information("Fetching Version Info from AssemblyInfo File: {0}", assemblyInfoFile);
if (!context.FileExists(assemblyInfoFile))
{
context.Error("AssemblyInfo file does not exist");
return null;
}
try {
var assemblyInfo = context.ParseAssemblyInfo(assemblyInfoFile);
var v = Version.Parse(assemblyInfo.AssemblyVersion);
var verInfo = new VersionInfo {
Major = v.Major,
Minor = v.Minor,
Build = v.Build,
Semantic = assemblyInfo.AssemblyInformationalVersion,
Milestone = string.Concat("v", v.ToString())
};
return verInfo;
}
catch {}
return null;
}
private static VersionInfo LoadVersionFromGit(ICakeContext context)
{
context.Information("Fetching Verson Infop from Git");
try {
GitVersion assertedVersions = context.GitVersion(new GitVersionSettings
{
OutputType = GitVersionOutput.Json,
});
var verInfo = new VersionInfo {
Major = assertedVersions.Major,
Minor = assertedVersions.Minor,
Build = assertedVersions.Patch,
Semantic = assertedVersions.LegacySemVerPadded,
Milestone = string.Concat("v", assertedVersions.MajorMinorPatch)
};
context.Information("Calculated Semantic Version: {0}", verInfo.Semantic);
return verInfo;
} catch {}
return null;
}
public static void UpdateVersion(ICakeContext context, Settings settings, VersionInfo verInfo)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
if (!string.IsNullOrEmpty(settings.Version.VersionFile) && context.FileExists(settings.Version.VersionFile))
{
context.Information("Updating Version File {0}", settings.Version.VersionFile);
context.SerializeJsonToFile(settings.Version.VersionFile, verInfo);
}
if (!string.IsNullOrEmpty(settings.Version.AssemblyInfoFile) && context.FileExists(settings.Version.AssemblyInfoFile))
{
context.Information("Updating Assembly Info File {0}", settings.Version.AssemblyInfoFile);
context.ReplaceRegexInFiles(settings.Version.AssemblyInfoFile, "AssemblyVersion\\(.*\\)", string.Format("AssemblyVersion(\"{0}\")", verInfo.ToString(false)));
context.ReplaceRegexInFiles(settings.Version.AssemblyInfoFile, "AssemblyFileVersion\\(.*\\)", string.Format("AssemblyFileVersion(\"{0}\")", verInfo.ToString(false)));
}
}
public static void UpdateNuSpecVersion(ICakeContext context, Settings settings, VersionInfo verInfo, FilePath nuspecFile)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
var xpath = "/n:package/n:metadata/n:version"; // version of the package
context.Information("\tUpdating Version in Nuspec File {0} to {1}", nuspecFile, verInfo.ToString());
try {
context.XmlPoke(nuspecFile, xpath, verInfo.ToString(), new XmlPokeSettings {
PreserveWhitespace = true
, Namespaces = new Dictionary<string, string> {
{ /* Prefix */ "n", /* URI */ "http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd"}
}
});
}
catch (Exception ex) {
context.Warning("\t" + ex.Message);
} // Its ok to throw these away as it most likely means the file didn't exist or the XPath didn't find any nodes
}
public static void UpdateNuSpecVersionDependency(ICakeContext context, Settings settings, VersionInfo verInfo, FilePath nuspecFile)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
context.Information("\tUpdating dependencies in nuspec file {0}", nuspecFile);
if (string.IsNullOrEmpty(settings.NuGet.LibraryNamespaceBase))
return;
var xpq = string.Format("/n:package/n:metadata/n:dependencies//n:dependency[starts-with(@id, '{0}')]/@version", settings.NuGet.LibraryNamespaceBase);
var replacementStr = verInfo.ToString();
switch (settings.NuGet.VersionDependencyForLibrary)
{
case "none": break;
case "exact": replacementStr = string.Format("[{0}]", replacementStr); break;
case "greaterthan": replacementStr = string.Format("({0},)", replacementStr); break;
case "greaterthanorequal": break;
case "lessthan": replacementStr = string.Format("(,{0})", replacementStr); break;
}
context.Information("\tUpdating Version for {0} Namespace Assemblies in Nuspec File {1} to {2}", settings.NuGet.LibraryNamespaceBase, nuspecFile, replacementStr);
try {
context.XmlPoke(nuspecFile, xpq, replacementStr, new XmlPokeSettings {
PreserveWhitespace = true
, Namespaces = new Dictionary<string, string> {
{ /* Prefix */ "n", /* URI */ "http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd"}
}
});
}
catch (Exception ex) {
context.Warning("\t" + ex.Message);
} // Its ok to throw these away as it most likely means the file didn't exist or the XPath didn't find any nodes
}
}
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);
}
}