forked from microsoft/appcenter-sdk-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathversion.cake
347 lines (306 loc) · 14.1 KB
/
version.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
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#addin nuget:?package=Cake.FileHelpers&version=3.0.0
#addin nuget:?package=Cake.Git&version=0.18.0
#addin nuget:?package=Cake.Incubator&version=2.0.2
#load "scripts/utility.cake"
#load "scripts/configuration/config-parser.cake"
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml.Linq;
using System.Xml.XPath;
// Task TARGET for build
var TARGET = Argument("target", Argument("t", ""));
// Need to read versions before setting url values
VersionReader.ReadVersions();
Task("IncrementRevisionNumberWithHash").Does(()=>
{
IncrementRevisionNumber(true);
});
Task("IncrementRevisionNumber").Does(()=>
{
IncrementRevisionNumber(false);
});
Task("SetReleaseVersion").Does(()=>
{
// Get base version of PCL core
var baseSemanticVersion = GetPCLBaseSemanticVersion();
// Replace versions in all non-demo app files
var informationalVersionPattern = @"AssemblyInformationalVersion\(" + "\".*\"" + @"\)";
ReplaceRegexInFilesWithExclusion("**/AssemblyInfo.cs", informationalVersionPattern, "AssemblyInformationalVersion(\"" + baseSemanticVersion + "\")", "Demo");
UpdateNewProjSdkVersion(baseSemanticVersion, baseSemanticVersion);
UpdateWrapperSdkVersion(baseSemanticVersion);
UpdateConfigFileSdkVersion(baseSemanticVersion);
});
Task("UpdateDemoVersion").Does(()=>
{
var newVersion = Argument<string>("DemoVersion");
// Replace version in all the demo application assemblies
var demoAssemblyInfoGlob = "Apps/**/*Demo*/**/AssemblyInfo.cs";
var informationalVersionPattern = @"AssemblyInformationalVersion\(" + "\".*\"" + @"\)";
ReplaceRegexInFiles(demoAssemblyInfoGlob, informationalVersionPattern, "AssemblyInformationalVersion(\"" + newVersion + "\")");
var newFileVersion = GetBaseVersion(newVersion) + "." + GetRevisionNumber(newVersion);
var fileVersionPattern = @"AssemblyFileVersion\(" + "\".*\"" + @"\)";
ReplaceRegexInFiles(demoAssemblyInfoGlob, fileVersionPattern, "AssemblyFileVersion(\"" + newFileVersion + "\")");
var csprojFiles = GetFiles("Apps/**/*Demo*/**/*.csproj");
foreach (var file in csprojFiles)
{
UpdateNewProjVersion(file, newVersion, newFileVersion);
}
// Replace android versions
var manifestGlob = "Apps/**/*Demo*/**/AndroidManifest.xml";
// Manifest version name tag
var versionNamePattern = "android:versionName=\"[^\"]+\"";
var newVersionName = "android:versionName=\"" + newVersion + "\"";
ReplaceRegexInFilesWithExclusion(manifestGlob, versionNamePattern, newVersionName, "/bin/", "/obj/");
// Manifest version code
var manifests = GetFiles("Apps/**/*Demo*/**/AndroidManifest.xml");
foreach (var manifest in manifests)
{
if (!manifest.FullPath.Contains("/bin/") &&
!manifest.FullPath.Contains("/obj/"))
{
IncrementManifestVersionCode(manifest);
}
}
// Replace UWP version
var uwpManifestGlob = "Apps/**/*Demo*/**/Package.appxmanifest";
var versionTagPattern = " Version=\"[^\"]+\"";
var newVersionTagText = " Version=\"" + newFileVersion + "\"";
ReplaceRegexInFiles(uwpManifestGlob, versionTagPattern, newVersionTagText);
// Replace iOS version
var bundleVersionPattern = @"<key>CFBundleVersion<\/key>\s*<string>[^<]*<\/string>";
var newBundleVersionString = "<key>CFBundleVersion</key>\n\t<string>" + newVersion + "</string>";
ReplaceRegexInFilesWithExclusion("Apps/**/*Demo*/**/Info.plist", bundleVersionPattern, newBundleVersionString, "/bin/", "/obj/");
var bundleShortVersionPattern = @"<key>CFBundleShortVersionString<\/key>\s*<string>[^<]*<\/string>";
var newBundleShortVersionString = "<key>CFBundleShortVersionString</key>\n\t<string>" + newVersion + "</string>";
ReplaceRegexInFilesWithExclusion("Apps/**/*Demo*/**/Info.plist", bundleShortVersionPattern, newBundleShortVersionString, "/bin/", "/obj/");
// Note: nuget update does not work with projects using project.json
// Replace version in all the demo application
ReplaceRegexInFiles("Apps/**/*Demo*/**/project.json", "(Microsoft.AppCenter[^\"]*\":[ ]+\")[^\"]+", "$1" + newVersion, RegexOptions.ECMAScript);
ReplaceRegexInFiles("Apps/**/*Demo*/**/*.csproj",
"<PackageReference Include=\"(Microsoft.AppCenter[^\"]*)\" Version=\"[^\"]+\" />",
"<PackageReference Include=\"$1\" Version=\"" + newVersion + "\" />", RegexOptions.ECMAScript);
});
Task("StartNewVersion").Does(()=>
{
var newVersion = Argument<string>("NewVersion");
var snapshotVersion = newVersion + "-SNAPSHOT";
var newFileVersion = newVersion + ".0";
// Replace version in all but the demo application assemblies
var assemblyInfoGlob = "**/AssemblyInfo.cs";
var informationalVersionPattern = @"AssemblyInformationalVersion\(" + "\".*\"" + @"\)";
ReplaceRegexInFilesWithExclusion(assemblyInfoGlob, informationalVersionPattern, "AssemblyInformationalVersion(\"" + snapshotVersion + "\")", "Demo");
var fileVersionPattern = @"AssemblyFileVersion\(" + "\".*\"" + @"\)";
ReplaceRegexInFilesWithExclusion(assemblyInfoGlob, fileVersionPattern, "AssemblyFileVersion(\"" + newFileVersion + "\")", "Demo");
UpdateConfigFileSdkVersion(snapshotVersion);
UpdateNewProjSdkVersion(snapshotVersion, newFileVersion);
UpdateWrapperSdkVersion(snapshotVersion);
// Replace android manifest version name tag
var androidManifestGlob = "**/AndroidManifest.xml";
var versionNamePattern = "android:versionName=\"[^\"]+\"";
var newVersionName = "android:versionName=\"" + snapshotVersion + "\"";
ReplaceRegexInFilesWithExclusion(androidManifestGlob, versionNamePattern, newVersionName, "Demo", "/bin/", "/obj/");
// Replace android manifest version code
var manifests = GetFiles(androidManifestGlob);
foreach (var manifest in manifests)
{
if (!manifest.FullPath.Contains("Demo") &&
!manifest.FullPath.Contains("SDK") &&
!manifest.FullPath.Contains("externals") &&
!manifest.FullPath.Contains("/bin/") &&
!manifest.FullPath.Contains("/obj/"))
{
IncrementManifestVersionCode(manifest);
}
}
// Replace UWP version
var uwpManifestGlob = "**/Package.appxmanifest";
var versionTagPattern = " Version=\"[^\"]+\"";
var newVersionTagText = " Version=\""+newVersion+".0\"";
ReplaceRegexInFilesWithExclusion(uwpManifestGlob, versionTagPattern, newVersionTagText, "Demo");
// Replace iOS version
var bundleVersionPattern = @"<key>CFBundleVersion<\/key>\s*<string>[^<]*<\/string>";
var newBundleVersionString = "<key>CFBundleVersion</key>\n\t<string>" + newVersion + "</string>";
ReplaceRegexInFilesWithExclusion("**/Info.plist", bundleVersionPattern, newBundleVersionString, "/bin/", "/obj/", "Demo");
var bundleShortVersionPattern = @"<key>CFBundleShortVersionString<\/key>\s*<string>[^<]*<\/string>";
var newBundleShortVersionString = "<key>CFBundleShortVersionString</key>\n\t<string>" + newVersion + "</string>";
ReplaceRegexInFilesWithExclusion("**/Info.plist", bundleShortVersionPattern, newBundleShortVersionString, "/bin/", "/obj/", "Demo");
});
void IncrementRevisionNumber(bool useHash)
{
// Get base version of PCL core
var baseSemanticVersion = GetPCLBaseSemanticVersion();
var nugetVer = GetLatestNuGetVersion();
var baseVersion = GetBaseVersion(nugetVer);
var newRevNum = baseSemanticVersion == baseVersion ? GetRevisionNumber(nugetVer) + 1 : 1;
var newRevString = GetPaddedString(newRevNum, 4);
var newVersion = baseSemanticVersion + "-r" + newRevString;
var newFileVersion = baseSemanticVersion + "." + newRevNum;
if (useHash)
{
newVersion += "-" + GetShortCommitHash();
}
// Replace AssemblyInformationalVersion in all AssemblyInfo files
var informationalVersionPattern = @"AssemblyInformationalVersion\(" + "\".*\"" + @"\)";
ReplaceRegexInFiles("**/AssemblyInfo.cs", informationalVersionPattern, "AssemblyInformationalVersion(\"" + newVersion + "\")");
// Increment revision number of AssemblyFileVersion
var fileVersionPattern = @"AssemblyFileVersion\(" + "\".*\"" + @"\)";
var files = FindRegexInFiles("**/AssemblyInfo.cs", fileVersionPattern);
foreach (var file in files)
{
var fileVersionTrimmedPattern = @"AssemblyFileVersion\("+ "\"" + @"([0-9]+.){3}";
var fullVersion = FindRegexMatchInFile(file, fileVersionPattern, RegexOptions.None);
var trimmedVersion = FindRegexMatchInFile(file, fileVersionTrimmedPattern, RegexOptions.None);
var newFileVersionTmp = trimmedVersion + newRevNum + "\")";
ReplaceTextInFiles(file.FullPath, fullVersion, newFileVersionTmp);
}
UpdateConfigFileSdkVersion(newVersion);
UpdateNewProjSdkVersion(newVersion, newFileVersion);
UpdateWrapperSdkVersion(newVersion);
}
string GetPCLBaseSemanticVersion()
{
return GetBaseVersion(VersionReader.SdkVersion);
}
string GetShortCommitHash()
{
var lastCommit = GitLogTip(".");
return lastCommit.Sha.Substring(0, 7);
}
string GetLatestNuGetVersion()
{
//Since password and feed id are secret variables in VSTS (and thus cannot be accessed like other environment variables),
//provide the option to pass them as parameters to the cake script
var nugetUser = EnvironmentVariable("NUGET_USER");
var nugetPassword = Argument("NuGetPassword", EnvironmentVariable("NUGET_PASSWORD"));
var nugetFeedId = Argument("NuGetFeedId", EnvironmentVariable("NUGET_FEED_ID"));
var url = "https://msmobilecenter.pkgs.visualstudio.com/_packaging/" + nugetFeedId + "/nuget/v2/Search()?$filter=IsAbsoluteLatestVersion+and+Id+eq+'Microsoft.AppCenter'&includePrerelease=true";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create (url);
request.Headers["X-NuGet-ApiKey"] = nugetPassword;
request.Credentials = new NetworkCredential(nugetUser, nugetPassword);
HttpWebResponse response = (HttpWebResponse)request.GetResponse ();
var responseString = String.Empty;
using (var reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
responseString = reader.ReadToEnd();
}
var startTag = "<d:Version>";
var endTag = "</d:Version>";
int start = responseString.IndexOf(startTag);
int end = responseString.IndexOf(endTag);
if (start == -1 || end == -1) {
return "0.0.0";
}
var tag = responseString.Substring(start + startTag.Length, end - start - startTag.Length);
return tag;
}
void IncrementManifestVersionCode(FilePath manifest)
{
var versionCodePattern = "android:versionCode=\"[^\"]+\"";
var versionCodeText = FindRegexMatchInFile(manifest, versionCodePattern, RegexOptions.None);
var firstPart = "android:versionCode=\"";
var length = versionCodeText.Length - 1 - firstPart.Length;
var versionCode = int.Parse(versionCodeText.Substring(firstPart.Length, length));
var newVersionCodeText = firstPart + (versionCode + 1) + "\"";
ReplaceRegexInFiles(manifest.FullPath, versionCodePattern, newVersionCodeText);
}
string GetBaseVersion(string fullVersion)
{
var indexDash = fullVersion.IndexOf("-");
if (indexDash == -1)
{
return fullVersion;
}
return fullVersion.Substring(0, indexDash);
}
// Changes the Version field in WrapperSdk.cs to the given version
void UpdateWrapperSdkVersion(string newVersion)
{
var patternString = "Version = \"[^\"]+\";";
var newString = "Version = \"" + newVersion + "\";";
ReplaceRegexInFiles("SDK/AppCenter/Microsoft.AppCenter.Shared/WrapperSdk.cs", patternString, newString);
}
void UpdateNewProjSdkVersion(string newVersion, string newFileVersion)
{
var csprojFiles = GetFiles("SDK/**/*.csproj");
foreach (var file in csprojFiles)
{
UpdateNewProjVersion(file, newVersion, newFileVersion);
}
}
void UpdateNewProjVersion(FilePath file, string newVersion, string newFileVersion)
{
var csproj = XDocument.Load(file.FullPath);
// Check if csproj with new format
if (csproj.Root.Attribute("Sdk")?.Value != "Microsoft.NET.Sdk")
{
return;
}
var version = csproj.XPathSelectElement("/Project/PropertyGroup/Version");
version.SetValue(newVersion);
var fileVersion = csproj.XPathSelectElement("/Project/PropertyGroup/FileVersion");
fileVersion.SetValue(newFileVersion);
csproj.Save(file.FullPath);
}
// Gets the revision number from a version string containing revision -r****
int GetRevisionNumber(string fullVersion)
{
var revStart = fullVersion.IndexOf("-r");
if (revStart == -1)
{
return 0;
}
var revEnd = fullVersion.IndexOf("-", revStart + 1);
if (revEnd == -1)
{
revEnd = fullVersion.Length;
}
var revString = fullVersion.Substring(revStart + 2, revEnd - revStart - 2);
try
{
return Int32.Parse(revString);
}
catch
{
return 0; //if the revision number could not be parsed, start new revision
}
}
// Returns the given integer as a string with a number of leading zeroes to
// pad the string to numDigits digits
string GetPaddedString(int num, int numDigits)
{
var numString = num.ToString();
while (numString.Length < numDigits)
{
numString = "0" + numString;
}
return numString;
}
// Run "ReplaceRegexInFiles" methods but exclude all file paths containing the strings in "excludeFilePathsContaining"
void ReplaceRegexInFilesWithExclusion(string globberPattern, string regEx, string replacement, params string[] excludeFilePathsContaining)
{
var files = GetFiles(globberPattern);
foreach (var file in files)
{
bool shouldReplace = true;
foreach (var excludeString in excludeFilePathsContaining)
{
if (file.FullPath.Contains(excludeString))
{
shouldReplace = false;
break;
}
}
if (shouldReplace)
{
ReplaceRegexInFiles(file.FullPath, regEx, replacement);
}
}
}
public void UpdateConfigFileSdkVersion(string newVersion)
{
ReplaceRegexInFiles(ConfigFile.Path, @"<sdkVersion>[\.|A-z|0-9|-]*<\/sdkVersion>", $"<sdkVersion>{newVersion}</sdkVersion>");
}
RunTarget(TARGET);