This repository has been archived by the owner on Jan 14, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
test-tools.cake
427 lines (389 loc) · 16 KB
/
test-tools.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
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#tool nuget:?package=XamarinComponent
#addin nuget:?package=Cake.Xamarin
#addin nuget:?package=Cake.FileHelpers&version=3.0.0
#addin nuget:?package=Newtonsoft.Json
using System;
using System.Collections.Generic;
using System.Net;
using Newtonsoft.Json.Linq;
// Task Target for build
var Target = Argument("target", Argument("t", "Default"));
string ArchiveDirectory = "archives";
bool IsMandatory = false;
string DistributionGroup = "Private Release Script Group";
string Token = EnvironmentVariable("APP_CENTER_API_TOKEN");
string BaseUrl = "https://api.appcenter.ms";
ApplicationInfo CurrentApp = null;
public enum Environment
{
Int,
Prod
}
public enum Platform
{
iOS,
Android,
UWP
}
public class ApplicationInfo
{
public static ICakeContext Context;
public static string OutputDirectory;
public Environment AppEnvironment { get; }
public Platform AppPlatform { get; }
public string AppOwner { get; }
public string AppId { get; }
public string AppPath
{
get
{
return OutputDirectory + "/" + AppId + "." + _appExtension;
}
}
public string ProjectPath
{
get
{
if (_projectPath == null)
{
_projectPath = Context.GetFiles("**/" + _projectFile).Single().ToString();
}
return _projectPath;
}
}
public string ProjectDirectory
{
get
{
return System.IO.Path.GetDirectoryName(ProjectPath);
}
}
private string _appExtension = null;
private string _projectPath = null;
private string _projectFile = null;
public ApplicationInfo(Environment environment, Platform platform, string appOwner, string appId, string projectFile, string appExtension)
{
AppOwner = appOwner;
AppId = appId;
AppEnvironment = environment;
AppPlatform = platform;
_projectFile = projectFile;
_appExtension = appExtension;
}
}
ApplicationInfo.Context = Context;
ApplicationInfo.OutputDirectory = ArchiveDirectory;
IList<ApplicationInfo> Applications = new List<ApplicationInfo>
{
new ApplicationInfo(Environment.Prod, Platform.iOS, "appcenter", "xamarin-demo-ios", "Contoso.Forms.Demo.iOS.csproj", "ipa"),
new ApplicationInfo(Environment.Prod, Platform.Android, "appcenter", "xamarin-demo-android", "Contoso.Forms.Demo.Droid.csproj", "apk"),
new ApplicationInfo(Environment.Prod, Platform.UWP, "appcenter-sdk", "UWP-Forms-Puppet", "Contoso.Forms.Demo.UWP.csproj", ""),
new ApplicationInfo(Environment.Int, Platform.iOS, "appcenter-sdk", "xamarin-puppet-ios", "Contoso.Forms.Puppet.iOS.csproj", "ipa"),
new ApplicationInfo(Environment.Int, Platform.Android, "appcenter-sdk", "xamarin-puppet-android-03", "Contoso.Forms.Puppet.Droid.csproj", "apk"),
new ApplicationInfo(Environment.Int, Platform.UWP, "appcenter-sdk", "xamarin-forms-puppet-uwp-2", "Contoso.Forms.Puppet.UWP.csproj", "")
};
Setup(context =>
{
// Arguments:
// -environment (-e): App "environment" ("prod" or "int") -- Default is "int"
// -group (-g): Distribution group name -- Default is "Private Release Script Group"
// -mandatory (-m): Should the release be mandatory ("true" or "false") -- Default is "false"
// -platform (-p): ios, android, or uwp -- Default is ios
// Read arguments
var environment = Environment.Prod;
if (Argument("Environment", "int") == "int")
{
environment = Environment.Int;
Token = EnvironmentVariable("APP_CENTER_INT_API_TOKEN");
BaseUrl = "https://api-gateway-core-integration.dev.avalanch.es";
}
var platformString = Argument<string>("Platform", "ios");
var platform = Platform.iOS;
if (platformString == "android")
{
platform = Platform.Android;
}
else if (platformString == "uwp")
{
platform = Platform.UWP;
}
CurrentApp = ( from app in Applications
where app.AppPlatform == platform &&
app.AppEnvironment == environment
select app
).Single();
DistributionGroup = Argument<string>("Group", DistributionGroup);
DistributionGroup = DistributionGroup.Replace('_', ' ');
IsMandatory = Argument<bool>("Mandatory", false);
});
// Distribution Tasks
Task("CreateIosArchive").IsDependentOn("IncreaseIosVersion").Does(()=>
{
MSBuild(CurrentApp.ProjectPath, settings => settings.SetConfiguration("Release")
.WithTarget("Build")
.WithProperty("Platform", "iPhone")
.WithProperty("BuildIpa", "true")
.WithProperty("OutputPath", "bin/Release/")
.WithProperty("AllowUnsafeBlocks", "true"));
var projectLocation = CurrentApp.ProjectDirectory;
var ipaLocation = projectLocation +
"/bin/Release/" +
System.IO.Path.GetFileNameWithoutExtension(CurrentApp.ProjectPath) +
".ipa";
EnsureDirectoryExists(ArchiveDirectory);
if (System.IO.File.Exists(CurrentApp.AppPath))
{
System.IO.File.Delete(CurrentApp.AppPath);
}
CopyFile(ipaLocation, CurrentApp.AppPath);
});
Task("CreateAndroidArchive").IsDependentOn("IncreaseAndroidVersion").Does(()=>
{
BuildAndroidApk(CurrentApp.ProjectPath, true, "Release", c => c.Configuration = "Release");
var projectLocation = CurrentApp.ProjectDirectory;
var apks = GetFiles(projectLocation + "/bin/Release/*.apk");
var unsignedApk = "";
foreach (var path in apks)
{
if (!path.ToString().EndsWith("-Signed.apk"))
{
unsignedApk = path.ToString();
break;
}
}
EnsureDirectoryExists(ArchiveDirectory);
if (System.IO.File.Exists(CurrentApp.AppPath))
{
System.IO.File.Delete(CurrentApp.AppPath);
}
CopyFile(unsignedApk, CurrentApp.AppPath);
});
Task("IncreaseIosVersion").Does(()=>
{
var infoPlistLocation = CurrentApp.ProjectDirectory + "/Info.plist";
var plist = File(infoPlistLocation);
var bundleVersionPattern = @"<key>CFBundleVersion<\/key>\s*<string>[^<]*<\/string>";
var match = FindRegexMatchInFile(File(infoPlistLocation), bundleVersionPattern, System.Text.RegularExpressions.RegexOptions.None);
var openTag = "<string>";
var closeTag = "</string>";
var currentVersion = match.Substring(match.IndexOf(openTag) + openTag.Length, match.IndexOf(closeTag) - match.IndexOf(openTag) - openTag.Length);
var newVersion = IncrementPatch(currentVersion);
var newBundleVersionString = "<key>CFBundleVersion</key>\n\t<string>" + newVersion + "</string>";
ReplaceRegexInFiles(infoPlistLocation, bundleVersionPattern, newBundleVersionString);
Information("iOS Version increased to " + newVersion);
});
Task("IncreaseAndroidVersion").Does(()=>
{
// Setup
var manifestLocation = CurrentApp.ProjectDirectory + "/Properties/AndroidManifest.xml";
var xmlNamespaces = new Dictionary<string, string> {{"android", "http://schemas.android.com/apk/res/android"}};
var peekSettings = new XmlPeekSettings();
peekSettings.Namespaces = xmlNamespaces;
var pokeSettings = new XmlPokeSettings();
pokeSettings.Namespaces = xmlNamespaces;
// Manifest version code
var versionCode = int.Parse(XmlPeek(manifestLocation, "manifest/@android:versionCode", peekSettings));
var newVersionCode = versionCode + 1;
XmlPoke(manifestLocation, "manifest/@android:versionCode", newVersionCode.ToString(), pokeSettings);
// Manifest version name
var versionName = XmlPeek(manifestLocation, "manifest/@android:versionName", peekSettings);
var suffix = "-macOS";
if (versionName.Contains(suffix))
{
versionName = versionName.Substring(0, versionName.IndexOf(suffix));
}
var newVersionName = IncrementPatch(versionName);
XmlPoke(manifestLocation, "manifest/@android:versionName", newVersionName, pokeSettings);
Information("Android version name changed to " + newVersionName + ", version code increased to " + newVersionCode);
});
Task("ReleaseApplication")
.Does(()=>
{
if (CurrentApp.AppPlatform == Platform.iOS)
{
RunTarget("CreateIosArchive");
}
else if (CurrentApp.AppPlatform == Platform.Android)
{
RunTarget("CreateAndroidArchive");
}
else
{
Error("Cannot distribute for this platform.");
return;
}
// Start the upload.
Information("Initiating distribution process...");
var startUploadUrl = GetApiUrl(BaseUrl, CurrentApp.AppOwner, CurrentApp.AppId, "release_uploads");
var startUploadRequest = GetWebRequest(startUploadUrl, Token);
var startUploadResponse = GetResponseJson(startUploadRequest);
// Upload the file to the given endpoint. The label "ipa" is correct for all platforms.
var uploadUrl = startUploadResponse["upload_url"].ToString();
HttpUploadFile(uploadUrl, CurrentApp.AppPath, "ipa");
// Commit the upload
Information("Committing distribution...");
var uploadId = startUploadResponse["upload_id"].ToString();
var commitRequestUrl = startUploadUrl + "/" + uploadId;
var commitRequest = GetWebRequest(commitRequestUrl, Token, "PATCH");
AttachJsonPayload(commitRequest,
new JObject(
new JProperty("status", "committed")));
var commitResponse = GetResponseJson(commitRequest);
var releaseUrl = BaseUrl + "/" + commitResponse["release_url"].ToString();
// Release the upload
Information("Finalizing release...");
var releaseRequest = GetWebRequest(releaseUrl, Token, "PATCH");
var releaseNotes = "This release has been created by the script test-tools.cake.";
AttachJsonPayload(releaseRequest,
new JObject(
new JProperty("destination_name", DistributionGroup),
new JProperty("release_notes", releaseNotes),
new JProperty("mandatory", IsMandatory.ToString().ToLower())));
releaseRequest.GetResponse().Dispose();
var mandatorySuffix = IsMandatory ? " as a mandatory update" : "";
Information("Successfully released " + CurrentApp.AppOwner +
"/" + CurrentApp.AppId + " to group " +
DistributionGroup + mandatorySuffix + ".");
});
// Push tasks
Task("SendPushNotification")
.Does(()=>
{
var name = "Test Notification";
var title = "Test Notification";
var timeSent = DateTime.Now.ToString();
var body = "Notification sent from test script at " + timeSent + ".";
var properties = new Dictionary<string, string> {{"time_sent", timeSent}};
var notificationJson = new JObject(
new JProperty("notification_content",
new JObject(
new JProperty("name", name),
new JProperty("title", title),
new JProperty("body", body),
new JProperty("custom_data",
new JObject(
from key in properties.Keys
select new JProperty(key, properties[key]))))));
Information("Sending notification:\n" + notificationJson.ToString());
var url = GetApiUrl(BaseUrl, CurrentApp.AppOwner, CurrentApp.AppId, "push/notifications");
var request = GetWebRequest(url, Token);
AttachJsonPayload(request, notificationJson);
var responseJson = GetResponseJson(request);
Information("Successfully sent push notification and received result:\n" + responseJson.ToString());
});
Task("BuildAppsInAppCenter").Does(() =>
{
CurrentApp = ( from app in Applications
where app.AppPlatform == Platform.iOS &&
app.AppEnvironment == Environment.Prod
select app
).Single();
BuildCurrentAppInAppCenter();
BuildCurrentAppInAppCenter();
CurrentApp = ( from app in Applications
where app.AppPlatform == Platform.Android &&
app.AppEnvironment == Environment.Prod
select app
).Single();
BuildCurrentAppInAppCenter();
BuildCurrentAppInAppCenter();
CurrentApp = ( from app in Applications
where app.AppPlatform == Platform.UWP &&
app.AppEnvironment == Environment.Prod
select app
).Single();
BuildCurrentAppInAppCenter();
});
void BuildCurrentAppInAppCenter()
{
Information("Triggering build in App Center... ");
var appCenterToken = Argument<string>("AppCenterToken");
var url = GetApiUrl(BaseUrl, CurrentApp.AppOwner, CurrentApp.AppId, "branches/master/builds");
var request = GetWebRequest(url, appCenterToken);
var responseJson = GetResponseJson(request);
Information("Successfully triggered build in App Center.");
}
// Helper methods
string GetApiUrl(string baseUrl, string appOwner, string appId, string apiName)
{
return string.Format("{0}/v0.1/apps/{1}/{2}/{3}", baseUrl, appOwner, appId, apiName);
}
JObject GetResponseJson(HttpWebRequest request)
{
using (var response = request.GetResponse())
using (var reader = new StreamReader(response.GetResponseStream()))
{
return JObject.Parse(reader.ReadToEnd());
}
}
HttpWebRequest GetWebRequest(string url, string token, string method = "POST")
{
Information(string.Format("About to call url '{0}'", url));
var request = (HttpWebRequest)WebRequest.Create(url);
request.Headers["X-API-Token"] = token;
request.ContentType = "application/json";
request.Accept = "application/json";
request.Method = method;
return request;
}
void AttachJsonPayload(HttpWebRequest request, JObject json)
{
using (var stream = request.GetRequestStream())
using (var sr = new StreamWriter(stream))
{
sr.Write(json.ToString());
}
}
string IncrementPatch(string semVer)
{
int patchIdx = 0;
for (int i = semVer.Length - 1; i >= 0; --i)
{
if (semVer[i] == '.')
{
patchIdx = i + 1;
break;
}
}
var newPatch = Convert.ToInt32(semVer.Substring(patchIdx, semVer.Length - patchIdx)) + 1;
return semVer.Substring(0, patchIdx) + newPatch;
}
// Adapted from https://stackoverflow.com/questions/566462/upload-files-with-httpwebrequest-multipart-form-data/2996904#2996904
void HttpUploadFile(string url, string file, string paramName)
{
Information(string.Format("Uploading {0} to {1}", file, url));
var boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
byte[] boundaryBytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
var request = (HttpWebRequest)WebRequest.Create(url);
request.ContentType = "multipart/form-data; boundary=" + boundary;
request.Method = "POST";
request.KeepAlive = true;
using (var requestStream = request.GetRequestStream())
{
requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);
var headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n\r\n";
var header = string.Format(headerTemplate, paramName, file);
byte[] headerBytes = System.Text.Encoding.UTF8.GetBytes(header);
requestStream.Write(headerBytes, 0, headerBytes.Length);
using (var fileStream = new FileStream(file, FileMode.Open, FileAccess.Read))
{
byte[] buffer = new byte[4096];
var bytesRead = 0;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
{
requestStream.Write(buffer, 0, bytesRead);
}
}
byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
requestStream.Write(trailer, 0, trailer.Length);
}
request.GetResponse().Dispose();
Information("File uploaded.");
}
Task("Default").Does(()=>
{
Error("Please run a specific target.");
});
RunTarget(Target);