-
Notifications
You must be signed in to change notification settings - Fork 24
/
build.cake
349 lines (302 loc) · 13.5 KB
/
build.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
#tool nuget:?package=XamarinComponent
#addin nuget:?package=Octokit
#addin nuget:?package=Cake.AppVeyor
#addin nuget:?package=Cake.Xamarin
#addin nuget:?package=Cake.FileHelpers
#reference "tools/Addins/Octokit/lib/net45/Octokit.dll"
using System.Net;
using Octokit;
//////////////////////////////////////////////////////////////////////
// ARGUMENTS
//////////////////////////////////////////////////////////////////////
var target = Argument("target", "Default");
var configuration = Argument("configuration", "Release");
// special logic to tweak builds for platform limitations
var ForMacOnly = target.EndsWith("-Mac");
var ForWindowsOnly = target.EndsWith("-Windows");
var ForEverywhere = !ForMacOnly && !ForWindowsOnly;
var ForWindows = ForEverywhere || !ForMacOnly;
var ForMac = ForEverywhere || !ForWindowsOnly;
target = target.Replace("-Mac", string.Empty).Replace("-Windows", string.Empty);
Information("Building target '{0}' for {1}.", target, ForEverywhere ? "everywhere" : (ForWindowsOnly ? "Windows only" : "Mac only"));
//////////////////////////////////////////////////////////////////////
// PREPARATION
//////////////////////////////////////////////////////////////////////
FilePath XamarinComponentPath = "./tools/XamarinComponent/tools/xamarin-component.exe";
DirectoryPath outDir = "./output/";
FilePath outputZip = "output.zip";
if (!DirectoryExists(outDir)) {
CreateDirectory(outDir);
}
var sha = EnvironmentVariable("APPVEYOR_REPO_COMMIT") ?? EnvironmentVariable("TRAVIS_COMMIT");
var branch = EnvironmentVariable("APPVEYOR_REPO_BRANCH") ?? EnvironmentVariable("TRAVIS_BRANCH");
var tag = EnvironmentVariable("APPVEYOR_REPO_TAG_NAME") ?? EnvironmentVariable("TRAVIS_TAG");
var pull = EnvironmentVariable("APPVEYOR_PULL_REQUEST_NUMBER") ?? EnvironmentVariable("TRAVIS_PULL_REQUEST");
var buildType = "COMMIT";
if (!string.IsNullOrEmpty(pull) && !string.Equals(pull, "false", StringComparison.OrdinalIgnoreCase)) {
buildType = "PULL" + pull;
} else if (!string.IsNullOrEmpty(tag)) {
buildType = "TAG";
}
var tagOrBranch = branch;
if (!string.IsNullOrEmpty(tag)) {
tagOrBranch = tag;
}
var GitHubToken = EnvironmentVariable("GitHubToken");
var GitHubUser = "mattleibow";
var GitHubRepository = "Microsoft-Band-SDK-Bindings";
var GitHubBuildTag = "CI";
var GitHubUploadFilename = string.Format("{0}_{1}_{2}.zip", buildType, tagOrBranch, sha);
var AppVeyorToken = EnvironmentVariable("AppVeyorToken");
var Build = new Action<string>((solution) =>
{
if (IsRunningOnWindows()) {
MSBuild(solution, s => s.SetConfiguration(configuration).SetMSBuildPlatform(MSBuildPlatform.x86));
} else {
XBuild(solution, s => s.SetConfiguration(configuration));
}
});
//////////////////////////////////////////////////////////////////////
// TASKS
//////////////////////////////////////////////////////////////////////
Task("Clean")
.Does(() =>
{
var dirs = new [] {
"./output",
// source
"./packages",
"./Microsoft.Band/*/bin",
"./Microsoft.Band/*/obj",
"./Microsoft.Band.Portable/*/bin",
"./Microsoft.Band.Portable/*/obj",
// samples
"./Demos/*/packages",
"./Demos/*/*/bin",
"./Demos/*/*/obj",
};
foreach (var dir in dirs) {
CleanDirectories(dir);
}
});
Task("RestorePackages")
.Does(() =>
{
var solutions = new [] {
// source
"./Microsoft.Band.sln",
"./Microsoft.Band.Portable.sln",
// samples
"./Demos/Microsoft.Band.Sample/Microsoft.Band.Sample.sln",
"./Demos/Microsoft.Band.Portable.Sample/Microsoft.Band.Portable.Sample.sln",
"./Demos/RotatingHand/RotatingHand.sln",
};
foreach (var solution in solutions) {
Information("Restoring {0}...", solution);
NuGetRestore(solution, new NuGetRestoreSettings {
Source = new [] { ForWindows ? "https://api.nuget.org/v3/index.json" : "https://www.nuget.org/api/v2/" },
Verbosity = NuGetVerbosity.Detailed
});
}
});
Task("Build")
.IsDependentOn("RestorePackages")
.Does(() =>
{
var solutions = new [] {
ForEverywhere ? "./Microsoft.Band.sln" : (ForWindowsOnly ? "./Microsoft.Band.Windows.sln" : "./Microsoft.Band.Mac.sln"),
ForEverywhere ? "./Microsoft.Band.Portable.sln" : (ForWindowsOnly ? "./Microsoft.Band.Portable.Windows.sln" : "./Microsoft.Band.Portable.Mac.sln"),
};
foreach (var solution in solutions) {
Information("Building {0}...", solution);
Build(solution);
}
var outputs = new Dictionary<string, string>();
if (ForWindows) {
// native
outputs.Add("./Microsoft.Band/Microsoft.Band.Android/bin/{0}/Microsoft.Band.Android.dll", "android/Microsoft.Band.Android.dll");
// portable
outputs.Add("./Microsoft.Band.Portable/Microsoft.Band.Portable.Android/bin/{0}/Microsoft.Band.Portable.dll", "android/Microsoft.Band.Portable.dll");
outputs.Add("./Microsoft.Band.Portable/Microsoft.Band.Portable.Android/bin/{0}/Microsoft.Band.Portable.xml", "android/Microsoft.Band.Portable.xml");
outputs.Add("./Microsoft.Band.Portable/Microsoft.Band.Portable.Phone/bin/{0}/Microsoft.Band.Portable.dll", "wpa81/Microsoft.Band.Portable.dll");
outputs.Add("./Microsoft.Band.Portable/Microsoft.Band.Portable.Phone/bin/{0}/Microsoft.Band.Portable.xml", "wpa81/Microsoft.Band.Portable.xml");
outputs.Add("./Microsoft.Band.Portable/Microsoft.Band.Portable.Windows/bin/{0}/Microsoft.Band.Portable.dll", "netcore451/Microsoft.Band.Portable.dll");
outputs.Add("./Microsoft.Band.Portable/Microsoft.Band.Portable.Windows/bin/{0}/Microsoft.Band.Portable.xml", "netcore451/Microsoft.Band.Portable.xml");
outputs.Add("./Microsoft.Band.Portable/Microsoft.Band.Portable.UWP/bin/{0}/Microsoft.Band.Portable.dll", "uap10.0/Microsoft.Band.Portable.dll");
outputs.Add("./Microsoft.Band.Portable/Microsoft.Band.Portable.UWP/bin/{0}/Microsoft.Band.Portable.xml", "uap10.0/Microsoft.Band.Portable.xml");
outputs.Add("./Microsoft.Band.Portable/Microsoft.Band.Portable.UWP/bin/{0}/Microsoft.Band.Portable.pri", "uap10.0/Microsoft.Band.Portable.pri");
outputs.Add("./Microsoft.Band.Portable/Microsoft.Band.Portable/bin/{0}/Microsoft.Band.Portable.dll", "pcl/Microsoft.Band.Portable.dll");
outputs.Add("./Microsoft.Band.Portable/Microsoft.Band.Portable/bin/{0}/Microsoft.Band.Portable.xml", "pcl/Microsoft.Band.Portable.xml");
}
if (ForMac) {
// native
outputs.Add("./Microsoft.Band/Microsoft.Band.iOS/bin/{0}/Microsoft.Band.iOS.dll", "ios/Microsoft.Band.iOS.dll");
// portable
outputs.Add("./Microsoft.Band.Portable/Microsoft.Band.Portable.iOS/bin/{0}/Microsoft.Band.Portable.dll", "ios/Microsoft.Band.Portable.dll");
outputs.Add("./Microsoft.Band.Portable/Microsoft.Band.Portable.iOS/bin/{0}/Microsoft.Band.Portable.xml", "ios/Microsoft.Band.Portable.xml");
}
foreach (var output in outputs) {
var dest = outDir.CombineWithFilePath(string.Format(output.Value, configuration));
var dir = dest.GetDirectory();
if (!DirectoryExists(dir)) {
CreateDirectory(dir);
}
CopyFile(string.Format(output.Key, configuration), dest);
}
});
Task("BuildSamples")
.IsDependentOn("RestorePackages")
.Does(() =>
{
var solutions = new List<string> {
ForEverywhere ? "./Demos/Microsoft.Band.Sample/Microsoft.Band.Sample.sln" : (ForWindowsOnly ? "./Demos/Microsoft.Band.Sample/Microsoft.Band.Android.Sample.sln" : "./Demos/Microsoft.Band.Sample/Microsoft.Band.iOS.Sample.sln"),
ForEverywhere ? "./Demos/Microsoft.Band.Portable.Sample/Microsoft.Band.Portable.Sample.sln" : (ForWindowsOnly ? "./Demos/Microsoft.Band.Portable.Sample/Microsoft.Band.Portable.Sample.Windows.sln" : "./Demos/Microsoft.Band.Portable.Sample/Microsoft.Band.Portable.Sample.Mac.sln"),
};
if (ForWindows) {
solutions.Add("./Demos/RotatingHand/RotatingHand.sln");
}
foreach (var solution in solutions) {
Information("Building {0}...", solution);
Build(solution);
}
});
Task("PackageNuGet")
.WithCriteria(ForWindows)
.IsDependentOn("Build")
.Does(() =>
{
var nugets = new [] {
"./NuGet/Xamarin.Microsoft.Band.Native.nuspec",
"./NuGet/Xamarin.Microsoft.Band.nuspec",
};
foreach (var nuget in nugets) {
Information("Packing (NuGet) {0}...", nuget);
NuGetPack(nuget, new NuGetPackSettings {
OutputDirectory = outDir,
Verbosity = NuGetVerbosity.Detailed,
BasePath = IsRunningOnUnix() ? "././" : "./",
});
}
});
Task("PackageComponent")
.WithCriteria(ForWindows)
.IsDependentOn("Build")
.IsDependentOn("PackageNuGet")
.Does(() =>
{
Information("Packing Component...");
DeleteFiles("./Component/*.xam");
PackageComponent("./Component/", new XamarinComponentSettings { ToolPath = XamarinComponentPath });
DeleteFiles("./output/*.xam");
MoveFiles("./Component/*.xam", outDir);
});
Task("Package")
.IsDependentOn("Download")
.IsDependentOn("PackageNuGet")
.IsDependentOn("PackageComponent")
.Does(() =>
{
});
Task("Download")
.WithCriteria(!string.IsNullOrEmpty(sha))
.WithCriteria(!ForEverywhere)
.Does(() =>
{
if (ForWindowsOnly) {
Information("Connecting to GitHub...");
var client = new GitHubClient(new ProductHeaderValue("msband-sdk-ci"));
client.Credentials = new Credentials(GitHubToken);
Information("Loading releases...");
var releases = client.Release.GetAll(GitHubUser, GitHubRepository).Result;
var releaseId = releases.Single(r => r.TagName == GitHubBuildTag).Id;
Information("Loading CI release...");
Release release = null;
ReleaseAsset asset = null;
var waitSeconds = 0;
while (asset == null) {
release = client.Release.Get(GitHubUser, GitHubRepository, releaseId).Result;
Information("Loading asset...");
asset = release.Assets.SingleOrDefault(a => a.Name == GitHubUploadFilename);
if (asset == null) {
// only try for 15 minutes
if (waitSeconds > 15 * 60) {
throw new Exception("Unable to download assets, maybe the build has failed.");
}
Information("Asset not found, waiting another 30 seconds.");
waitSeconds += 30;
System.Threading.Thread.Sleep(1000 * 30);
}
}
Information("Found asset: {0}", asset.Id);
Information("Url: {0}", asset.BrowserDownloadUrl);
Information("Downloading asset...");
if (FileExists(outputZip)) {
DeleteFile(outputZip);
}
var url = string.Format("https://api.github.com/repos/{0}/{1}/releases/assets/{2}?access_token={3}", GitHubUser, GitHubRepository, asset.Id, GitHubToken);
var wc = new WebClient();
wc.Headers.Add("Accept", "application/octet-stream");
wc.Headers.Add("User-Agent", "msband-sdk-ci");
wc.DownloadFile(url, outputZip.FullPath);
Information("Extracting output...");
CleanDirectory(outDir);
Unzip(outputZip, outDir);
}
});
Task("Upload")
.WithCriteria(!string.IsNullOrEmpty(sha))
.WithCriteria(!ForEverywhere)
.IsDependentOn("Build")
.IsDependentOn("Package")
.Does(() =>
{
Information("Connecting to GitHub...");
var client = new GitHubClient(new ProductHeaderValue("msband-sdk"));
client.Credentials = new Credentials(GitHubToken);
Information("Loading releases...");
var releases = client.Release.GetAll(GitHubUser, GitHubRepository).Result;
var releaseId = releases.Single(r => r.TagName == GitHubBuildTag).Id;
Information("Loading CI release...");
var release = client.Release.Get(GitHubUser, GitHubRepository, releaseId).Result;
Information("Loading asset...");
var asset = release.Assets.SingleOrDefault(a => a.Name == GitHubUploadFilename);
if (asset != null) {
Information("Deleting asset...");
client.Release.DeleteAsset(GitHubUser, GitHubRepository, asset.Id).Wait();
} else {
Information("Asset not found.");
}
if (ForMacOnly) {
Information("Compressing output...");
if (FileExists(outputZip)) {
DeleteFile(outputZip);
}
Zip(outDir, outputZip);
Information("Creating asset...");
var archiveContents = System.IO.File.OpenRead(outputZip.FullPath);
var assetUpload = new ReleaseAssetUpload {
FileName = GitHubUploadFilename,
ContentType = "application/zip",
RawData = archiveContents
};
Information("Uploading asset...");
asset = client.Release.UploadAsset(release, assetUpload).Result;
Information("Uploaded asset: {0}", asset.Id);
Information("Url: {0}", asset.BrowserDownloadUrl);
// Information("Triggering AppVeyor build...");
// AppVeyorStartBuildSpecificCommit(AppVeyorToken, GitHubUser, GitHubRepository, sha, branch);
}
});
//////////////////////////////////////////////////////////////////////
// TASK TARGETS
//////////////////////////////////////////////////////////////////////
Task("Default")
.IsDependentOn("Download")
.IsDependentOn("Build")
.IsDependentOn("Package")
.IsDependentOn("BuildSamples")
.IsDependentOn("Upload");
//////////////////////////////////////////////////////////////////////
// EXECUTION
//////////////////////////////////////////////////////////////////////
RunTarget(target);