-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathbuild.cake
419 lines (366 loc) · 15.6 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
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
#tool nuget:?package=XamarinComponent&version=1.1.0.41
#addin nuget:?package=Octokit&version=0.21.1
#addin nuget:?package=Cake.Xamarin&version=1.3.0.3
#addin nuget:?package=Cake.FileHelpers&version=1.0.3.2
using System.Net;
using Octokit;
//////////////////////////////////////////////////////////////////////
// ARGUMENTS
//////////////////////////////////////////////////////////////////////
var target = Argument("target", "Default").ToUpper();
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
//////////////////////////////////////////////////////////////////////
// the tools
FilePath XamarinComponentPath = "./tools/XamarinComponent/tools/xamarin-component.exe";
FilePath NuGetPath = "./tools/nuget.exe";
// the output folder
DirectoryPath outDir = "./output/";
FilePath outputZip = "output.zip";
if (!DirectoryExists(outDir)) {
CreateDirectory(outDir);
}
// get CI variables
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");
// get the temporary build artifacts filename
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 TemporaryArtifactsFilename = string.Format("{0}_{1}_{2}.zip", buildType, tagOrBranch, sha);
// the GitHub communication (for storing the temporary build artifacts)
var GitHubToken = EnvironmentVariable("GitHubToken");
var GitHubUser = "mattleibow";
var GitHubRepository = "Flurry.Analytics";
var GitHubBuildTag = "CI";
// make sure we use the correct version of the NuGet feed
var NuGetSource = new [] { ForWindows ? "https://api.nuget.org/v3/index.json" : "https://www.nuget.org/api/v2/" };
//////////////////////////////////////////////////////////////////////
// FUNCTIONS
//////////////////////////////////////////////////////////////////////
var Build = new Action<FilePath>((solution) =>
{
if (IsRunningOnWindows()) {
MSBuild(solution, s => s.SetConfiguration(configuration).SetMSBuildPlatform(MSBuildPlatform.x86));
} else {
XBuild(solution, s => s.SetConfiguration(configuration));
}
});
void MergeDirectory(DirectoryPath source, DirectoryPath dest, bool replace)
{
var sourceDirName = source.FullPath;
var destDirName = dest.FullPath;
if (!DirectoryExists(source)) {
throw new DirectoryNotFoundException("Source directory does not exist or could not be found: " + source);
}
if (!DirectoryExists(dest)) {
CreateDirectory(dest);
}
DirectoryInfo dir = new DirectoryInfo(sourceDirName);
FileInfo[] files = dir.GetFiles();
foreach (FileInfo file in files) {
string temppath = dest.CombineWithFilePath(file.Name).FullPath;
if (FileExists(temppath)) {
if (replace) {
DeleteFile(temppath);
}
} else {
file.CopyTo(temppath);
}
}
DirectoryInfo[] dirs = dir.GetDirectories();
foreach (DirectoryInfo subdir in dirs) {
string temppath = dest.Combine(subdir.Name).FullPath;
MergeDirectory(subdir.FullName, temppath, replace);
}
}
//////////////////////////////////////////////////////////////////////
// TASKS
//////////////////////////////////////////////////////////////////////
Task("Clean")
.Does(() =>
{
// clean can be done on any platform
var dirs = new [] {
"./output",
// source
"./packages",
"./Flurry/binding/*/bin",
"./Flurry/binding/*/obj",
"./Portable/library/*/bin",
"./Portable/library/*/obj",
// samples
"./Flurry/samples/*/packages",
"./Flurry/samples/*/bin",
"./Flurry/samples/*/obj",
"./Portable/samples/packages",
"./Portable/samples/*/bin",
"./Portable/samples/*/obj",
};
foreach (var dir in dirs) {
CleanDirectories(dir);
}
});
Task("RestorePackages")
.Does(() =>
{
// restoring NuGets can be done on any platform
var solutions = new [] {
"./Flurry.sln",
"./Flurry.Analytics.Portable.sln",
"./Flurry/samples/FlurryAdsAndroidSample/FlurryAdsAndroidSample.sln",
"./Flurry/samples/FlurryAdsiOSSample/FlurryAdsiOSSample.sln",
"./Flurry/samples/FlurryAnalyticsAndroidSample/FlurryAnalyticsAndroidSample.sln",
"./Flurry/samples/FlurryAnalyticsiOSSample/FlurryAnalyticsiOSSample.sln",
"./Flurry/samples/FlurryAnalyticstvOSSample/FlurryAnalyticstvOSSample.sln",
"./Flurry/samples/FlurryAnalyticsWindowsPhoneSample/FlurryAnalyticsWindowsPhoneSample.sln",
"./Portable/samples/Flurry.Analytics.Portable.Samples.sln",
};
foreach (var solution in solutions) {
Information("Restoring {0}...", solution);
RestoreComponents(solution, new XamarinComponentRestoreSettings {
ToolPath = XamarinComponentPath
});
NuGetRestore(solution, new NuGetRestoreSettings {
Source = NuGetSource,
Verbosity = NuGetVerbosity.Detailed,
ToolPath = NuGetPath
});
}
});
Task("Build")
.IsDependentOn("RestorePackages")
.Does(() =>
{
// get the platform-specific solution name
FilePath solution = "./Flurry.sln";
if (ForWindowsOnly) {
solution = "./Flurry.Windows.sln";
} else if (ForMacOnly) {
solution = "./Flurry.Mac.sln";
}
// build the solution
Information("Building {0}...", solution);
Build(solution);
// get the outputs, making suer to only include the ones that built
var outputs = new Dictionary<string, string> {
{ "./Portable/library/Flurry.Analytics.Portable/bin/{0}/Flurry.Analytics.Portable.dll", "pcl" },
{ "./Portable/library/Flurry.Analytics.Portable/bin/{0}/Flurry.Analytics.Portable.xml", "pcl" },
};
if (ForWindows) {
outputs.Add("./Flurry/binding/Flurry.Ads.Android/bin/{0}/Flurry.Ads.Android.dll", "android");
outputs.Add("./Flurry/binding/Flurry.Analytics.Android/bin/{0}/Flurry.Analytics.Android.dll", "android");
outputs.Add("./Flurry/binding/Flurry.Analytics.WP8/FlurryWP8SDK.dll", "wp8");
outputs.Add("./Portable/library/Flurry.Analytics.Portable.Android/bin/{0}/Flurry.Analytics.Portable.dll", "android");
outputs.Add("./Portable/library/Flurry.Analytics.Portable.Android/bin/{0}/Flurry.Analytics.Portable.xml", "android");
outputs.Add("./Portable/library/Flurry.Analytics.Portable.WindowsPhone/bin/{0}/Flurry.Analytics.Portable.dll", "wp8");
outputs.Add("./Portable/library/Flurry.Analytics.Portable.WindowsPhone/bin/{0}/Flurry.Analytics.Portable.xml", "wp8");
}
if (ForMac) {
outputs.Add("./Flurry/binding/Flurry.Ads.iOS/bin/{0}/Flurry.Ads.iOS.dll", "ios-unified");
outputs.Add("./Flurry/binding/Flurry.Analytics.iOS/bin/{0}/Flurry.Analytics.iOS.dll", "ios-unified");
outputs.Add("./Flurry/binding/Flurry.Analytics.tvOS/bin/{0}/Flurry.Analytics.tvOS.dll", "tvos");
outputs.Add("./Portable/library/Flurry.Analytics.Portable.iOS/bin/{0}/Flurry.Analytics.Portable.dll", "ios-unified");
outputs.Add("./Portable/library/Flurry.Analytics.Portable.iOS/bin/{0}/Flurry.Analytics.Portable.xml", "ios-unified");
}
// copy the outputs
foreach (var output in outputs) {
FilePath source = string.Format(output.Key, configuration);
FilePath dest = outDir.Combine(output.Value).CombineWithFilePath(source.GetFilename());
DirectoryPath dir = dest.GetDirectory();
if (!DirectoryExists(dir)) {
CreateDirectory(dir);
}
CopyFile(source, dest);
}
});
Task("BuildSamples")
.IsDependentOn("RestorePackages")
.Does(() =>
{
if (ForWindows) {
Build("./Flurry/samples/FlurryAdsAndroidSample/FlurryAdsAndroidSample.sln");
Build("./Flurry/samples/FlurryAnalyticsAndroidSample/FlurryAnalyticsAndroidSample.sln");
Build("./Flurry/samples/FlurryAnalyticsWindowsPhoneSample/FlurryAnalyticsWindowsPhoneSample.sln");
Build("./Portable/samples/Flurry.Analytics.Portable.Samples.sln");
}
if (ForMac) {
Build("./Flurry/samples/FlurryAdsiOSSample/FlurryAdsiOSSample.sln");
Build("./Flurry/samples/FlurryAnalyticsiOSSample/FlurryAnalyticsiOSSample.sln");
Build("./Flurry/samples/FlurryAnalyticstvOSSample/FlurryAnalyticstvOSSample.sln");
}
});
Task("PackageNuGet")
.IsDependentOn("Build")
.WithCriteria(ForWindows)
.Does(() =>
{
var nugets = new [] {
"./nuget/Flurry.Ads.nuspec",
"./nuget/Flurry.Analytics.nuspec",
"./nuget/Flurry.Analytics.Portable.nuspec",
};
foreach (var nuget in nugets) {
NuGetPack(nuget, new NuGetPackSettings {
OutputDirectory = outDir,
Verbosity = NuGetVerbosity.Detailed,
BasePath = IsRunningOnUnix() ? "././" : "./",
ToolPath = NuGetPath
});
}
});
Task("PackageComponent")
.IsDependentOn("Build")
.IsDependentOn("PackageNuGet")
.IsDependentOn("BuildSamples")
.WithCriteria(ForWindows)
.Does(() =>
{
DeleteFiles("./component/Flurry.Ads/*.xam");
PackageComponent("./component/Flurry.Ads/", new XamarinComponentSettings {
ToolPath = XamarinComponentPath
});
DeleteFiles("./component/Flurry.Analytics.Portable/*.xam");
PackageComponent("./component/Flurry.Analytics.Portable/", new XamarinComponentSettings {
ToolPath = XamarinComponentPath
});
DeleteFiles("./output/*.xam");
MoveFiles("./component/Flurry.Ads/*.xam", outDir);
MoveFiles("./component/Flurry.Analytics.Portable/*.xam", outDir);
});
Task("Package")
.IsDependentOn("DownloadArtifacts")
.IsDependentOn("PackageNuGet")
.IsDependentOn("PackageComponent")
.Does(() =>
{
// although this can be done on any platform, the build order
// requires that the packaging is done on Windows (either after
// the iOS build is done, or if eveything thing was built on
// Windows using the iOS build host)
});
//////////////////////////////////////////////////////////////////////
// TEMPORARY ARTIFACT MANAGEMENT
//////////////////////////////////////////////////////////////////////
Task("DownloadArtifacts")
.WithCriteria(!string.IsNullOrEmpty(sha))
.WithCriteria(!ForEverywhere)
.Does(() =>
{
if (ForWindowsOnly) {
Information("Connecting to GitHub...");
var client = new GitHubClient(new ProductHeaderValue("Flurry.Analytics"));
client.Credentials = new Credentials(GitHubToken);
Information("Loading releases...");
var releases = client.Repository.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.Repository.Release.Get(GitHubUser, GitHubRepository, releaseId).Result;
Information("Loading asset...");
asset = release.Assets.SingleOrDefault(a => a.Name == TemporaryArtifactsFilename);
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", "Flurry.Analytics");
wc.DownloadFile(url, outputZip.FullPath);
Information("Extracting output...");
DirectoryPath tmp = "./temp-output/";
if (DirectoryExists(tmp)) {
CleanDirectory(tmp);
} else {
CreateDirectory(tmp);
}
Unzip(outputZip, tmp);
MergeDirectory(tmp, outDir, false);
}
});
Task("UploadArtifacts")
.WithCriteria(!string.IsNullOrEmpty(sha))
.WithCriteria(!ForEverywhere)
.Does(() =>
{
Information("Connecting to GitHub...");
var client = new GitHubClient(new ProductHeaderValue("Flurry.Analytics"));
client.Credentials = new Credentials(GitHubToken);
Information("Loading releases...");
var releases = client.Repository.Release.GetAll(GitHubUser, GitHubRepository).Result;
var releaseId = releases.Single(r => r.TagName == GitHubBuildTag).Id;
Information("Loading CI release...");
var release = client.Repository.Release.Get(GitHubUser, GitHubRepository, releaseId).Result;
Information("Loading asset...");
var asset = release.Assets.SingleOrDefault(a => a.Name == TemporaryArtifactsFilename);
if (asset != null) {
Information("Deleting asset...");
client.Repository.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 = TemporaryArtifactsFilename,
ContentType = "application/zip",
RawData = archiveContents
};
Information("Uploading asset...");
asset = client.Repository.Release.UploadAsset(release, assetUpload).Result;
Information("Uploaded asset: {0}", asset.Id);
Information("Url: {0}", asset.BrowserDownloadUrl);
}
});
//////////////////////////////////////////////////////////////////////
// TASK TARGETS
//////////////////////////////////////////////////////////////////////
Task("Default")
.IsDependentOn("Build")
.IsDependentOn("DownloadArtifacts")
.IsDependentOn("Package")
.IsDependentOn("UploadArtifacts");
//////////////////////////////////////////////////////////////////////
// EXECUTION
//////////////////////////////////////////////////////////////////////
RunTarget(target);