forked from MvvmCross/MvvmCross
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.cake
459 lines (385 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
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
#tool nuget:?package=GitVersion.CommandLine&version=5.0.1
#tool nuget:?package=vswhere&version=2.7.1
#addin nuget:?package=Cake.Figlet&version=1.3.1
#addin nuget:?package=Cake.Git&version=0.21.0
#addin nuget:?package=Polly&version=7.1.1
using Polly;
var solutionName = "MvvmCross";
var repoName = "mvvmcross/mvvmcross";
var sln = new FilePath("./" + solutionName + ".sln");
var outputDir = new DirectoryPath("./artifacts");
var gitVersionLog = new FilePath("./artifacts/gitversion.log");
var nuspecDir = new DirectoryPath("./nuspec");
var nugetPackagesDir = new DirectoryPath("./nuget/packages");
var target = Argument("target", "Default");
var configuration = Argument("configuration", "Release");
var verbosityArg = Argument("verbosity", "Minimal");
var verbosity = Verbosity.Minimal;
var signingSecret = Argument("signing_secret", "");
var signingUser = Argument("signing_user", "");
var didSignPackages = false;
var nugetSource = Argument("package_source", "");
var nugetApiKey = Argument("package_apikey", "");
var githubToken = Argument("github_token", "");
var githubTokenEnv = EnvironmentVariable("CHANGELOG_GITHUB_TOKEN");
var sinceTag = Argument("since_tag", "");
var shouldPublishPackages = false;
var isRunningOnPipelines = TFBuild.IsRunningOnAzurePipelines || TFBuild.IsRunningOnAzurePipelinesHosted;
GitVersion versionInfo = null;
Setup(context =>
{
versionInfo = context.GitVersion(new GitVersionSettings
{
UpdateAssemblyInfo = true,
OutputType = GitVersionOutput.Json,
LogFilePath = gitVersionLog.MakeAbsolute(context.Environment)
});
if (isRunningOnPipelines)
{
var buildNumber = versionInfo.InformationalVersion + "-" + TFBuild.Environment.Build.Number;
buildNumber = buildNumber.Replace("/", "-");
TFBuild.Commands.UpdateBuildNumber(buildNumber);
}
var cakeVersion = typeof(ICakeContext).Assembly.GetName().Version.ToString();
Information(Figlet(solutionName));
Information("Building version {0}, ({1}, {2}) using version {3} of Cake.",
versionInfo.SemVer,
configuration,
target,
cakeVersion);
shouldPublishPackages = ShouldPushNugetPackages(versionInfo.BranchName, nugetSource);
Information("Will push NuGet packages {0}", shouldPublishPackages);
verbosity = (Verbosity) Enum.Parse(typeof(Verbosity), verbosityArg, true);
});
Task("Clean").Does(() =>
{
CleanDirectories("./**/bin");
CleanDirectories("./**/obj");
CleanDirectories(outputDir.FullPath);
CleanDirectories(nugetPackagesDir.FullPath);
EnsureDirectoryExists(outputDir);
});
FilePath msBuildPath;
Task("ResolveBuildTools")
.WithCriteria(() => IsRunningOnWindows())
.Does(() =>
{
var vsWhereSettings = new VSWhereLatestSettings
{
IncludePrerelease = true,
Requires = "Component.Xamarin"
};
var vsLatest = VSWhereLatest(vsWhereSettings);
msBuildPath = (vsLatest == null)
? null
: vsLatest.CombineWithFilePath("./MSBuild/Current/Bin/MSBuild.exe");
if (msBuildPath != null)
Information("Found MSBuild at {0}", msBuildPath.ToString());
});
Task("Restore")
.IsDependentOn("ResolveBuildTools")
.Does(() =>
{
var settings = GetDefaultBuildSettings()
.WithTarget("Restore");
MSBuild(sln, settings);
});
Task("PatchBuildProps")
.Does(() =>
{
var buildProp = new FilePath("./Directory.build.props");
XmlPoke(buildProp, "//Project/PropertyGroup/Version", versionInfo.SemVer);
});
Task("Build")
.IsDependentOn("ResolveBuildTools")
.IsDependentOn("Clean")
.IsDependentOn("PatchBuildProps")
.IsDependentOn("Restore")
.Does(() => {
var settings = GetDefaultBuildSettings()
.WithProperty("Version", versionInfo.SemVer)
.WithProperty("PackageVersion", versionInfo.SemVer)
.WithProperty("InformationalVersion", versionInfo.InformationalVersion)
.WithProperty("NoPackageAnalysis", "True")
.WithTarget("Build");
MSBuild(sln, settings);
});
Task("UnitTest")
.IsDependentOn("Build")
.Does(() =>
{
EnsureDirectoryExists(outputDir + "/Tests/");
var testPaths = GetFiles("./UnitTests/*.UnitTest/*.UnitTest.csproj");
var testsFailed = false;
var settings = new DotNetCoreTestSettings
{
Configuration = configuration,
NoBuild = true
};
foreach(var project in testPaths)
{
var projectName = project.GetFilenameWithoutExtension();
var testXml = MakeAbsolute(new FilePath(outputDir + "/Tests/" + projectName + ".xml"));
settings.Logger = $"xunit;LogFilePath={testXml.FullPath}";
try
{
DotNetCoreTest(project.ToString(), settings);
}
catch
{
testsFailed = true;
}
}
if (isRunningOnPipelines)
{
var data = new TFBuildPublishTestResultsData
{
Configuration = configuration,
TestResultsFiles = GetFiles(outputDir + "/Tests/*.xml").ToList(),
TestRunner = TFTestRunnerType.XUnit,
TestRunTitle = "MvvmCross Unit Tests",
MergeTestResults = true
};
TFBuild.Commands.PublishTestResults(data);
}
if (testsFailed)
throw new Exception("Tests failed :(");
});
Task("CopyPackages")
.IsDependentOn("Build")
.Does(() =>
{
var nugetFiles = GetFiles(solutionName + "*/**/bin/" + configuration + "/**/*.nupkg");
CopyFiles(nugetFiles, outputDir);
});
Task("SignPackages")
.WithCriteria(() => !BuildSystem.IsLocalBuild)
.WithCriteria(() => IsRepository(repoName))
.WithCriteria(() => !string.IsNullOrEmpty(signingSecret))
.WithCriteria(() => !string.IsNullOrEmpty(signingUser))
.WithCriteria(() => isRunningOnPipelines)
.WithCriteria(() => !TFBuild.Environment.PullRequest.IsPullRequest)
.IsDependentOn("Build")
.IsDependentOn("CopyPackages")
.Does(() =>
{
var settings = File("./signclient.json");
var files = GetFiles(outputDir + "/*.nupkg");
foreach(var file in files)
{
Information("Signing {0}...", file.FullPath);
// Build the argument list.
var arguments = new ProcessArgumentBuilder()
.Append("sign")
.AppendSwitchQuoted("-c", MakeAbsolute(settings.Path).FullPath)
.AppendSwitchQuoted("-i", MakeAbsolute(file).FullPath)
.AppendSwitchQuotedSecret("-s", signingSecret)
.AppendSwitchQuotedSecret("-r", signingUser)
.AppendSwitchQuoted("-n", "MvvmCross")
.AppendSwitchQuoted("-d", "MvvmCross is a cross platform MVVM framework.")
.AppendSwitchQuoted("-u", "https://mvvmcross.com");
// Sign the binary.
var result = StartProcess("SignClient.exe", new ProcessSettings { Arguments = arguments });
if (result != 0)
{
// We should not recover from this.
throw new InvalidOperationException("Signing failed!");
}
}
didSignPackages = true;
});
Task("PublishPackages")
.WithCriteria(() => !BuildSystem.IsLocalBuild)
.WithCriteria(() => IsRepository(repoName))
.WithCriteria(() => !string.IsNullOrEmpty(nugetSource))
.WithCriteria(() => !string.IsNullOrEmpty(nugetApiKey))
.WithCriteria(() => shouldPublishPackages)
.IsDependentOn("CopyPackages")
.IsDependentOn("SignPackages")
.Does (() =>
{
if (!didSignPackages)
{
Warning("Packages were not signed. Not publishing packages");
return;
}
var nugetPushSettings = new NuGetPushSettings
{
Source = nugetSource,
ApiKey = nugetApiKey
};
if (nugetSource.Contains("github.com"))
{
var nugetSourceSettings = new NuGetSourcesSettings
{
UserName = "Cheesebaron",
Password = nugetApiKey,
IsSensitiveSource = true
};
var feed = new
{
Name = "GitHub",
Source = nugetSource
};
NuGetAddSource(feed.Name, feed.Source, nugetSourceSettings);
nugetPushSettings = new NuGetPushSettings
{
Source = feed.Source
};
}
var nugetFiles = GetFiles(outputDir + "/*.nupkg");
var policy = Policy
.Handle<Exception>()
.WaitAndRetry(5, retryAttempt =>
TimeSpan.FromSeconds(Math.Pow(1.5, retryAttempt)));
foreach(var nugetFile in nugetFiles)
{
policy.Execute(() =>
NuGetPush(nugetFile, nugetPushSettings)
);
}
});
Task("UploadArtifacts")
.IsDependentOn("CopyPackages")
.WithCriteria(() => isRunningOnPipelines)
.Does(() =>
{
Information("Artifacts Dir: {0}", outputDir.FullPath);
var artifacts = GetFiles(outputDir.FullPath + "/**/*");
TFBuild.Commands.UploadArtifactDirectory(outputDir, "build artifacts");
});
Task("UpdateChangelog")
.Does(() =>
{
var arguments = new ProcessArgumentBuilder();
if (!string.IsNullOrEmpty(githubToken))
arguments.Append("--token {0}", githubToken);
else if (!string.IsNullOrEmpty(githubTokenEnv))
arguments.Append("--token {0}", githubTokenEnv);
// Exclude labels
var excludeLabels = new [] {
"t/question",
"s/wont-fix",
"s/duplicate",
"s/deprecated",
"s/invalid"
};
arguments.Append("--exclude-labels {0}", string.Join(",", excludeLabels));
// bug labels
arguments.Append("--bug-labels {0}", "t/bug");
// enhancement labels
arguments.Append("--enhancement-labels {0}", "t/feature");
// breaking labels (enable when github_changelog_generator 1.15 is released)
//arguments.Append("--breaking-labels {0}", "t/breaking");
arguments.Append("--max-issues 200");
if (!string.IsNullOrEmpty(sinceTag) && versionInfo.BranchName.Contains("release/"))
{
arguments.Append("--between-tags {0},{1}", sinceTag, versionInfo.MajorMinorPatch);
arguments.Append("--future-release {0}", versionInfo.MajorMinorPatch);
}
else
{
if (!string.IsNullOrEmpty(sinceTag))
arguments.Append("--since-tag {0}", sinceTag);
if (versionInfo.BranchName.Contains("release/"))
arguments.Append("--future-release {0}", versionInfo.MajorMinorPatch);
}
Information("Starting github_changelog_generator with arguments: {0}", arguments.Render());
using(var process = StartAndReturnProcess("github_changelog_generator",
new ProcessSettings { Arguments = arguments }))
{
process.WaitForExit();
Information("Exit code: {0}", process.GetExitCode());
}
});
Task("Default")
.IsDependentOn("Build")
.IsDependentOn("UnitTest")
.IsDependentOn("UploadArtifacts")
.IsDependentOn("PublishPackages")
.Does(() =>
{
});
RunTarget(target);
MSBuildSettings GetDefaultBuildSettings()
{
var settings = new MSBuildSettings
{
Configuration = configuration,
ToolPath = msBuildPath,
Verbosity = verbosity,
ArgumentCustomization = args => args.Append("/m"),
ToolVersion = MSBuildToolVersion.VS2019
};
// workaround for derped Java Home ENV vars
if (IsRunningOnWindows() && isRunningOnPipelines)
{
var javaSdkDir = EnvironmentVariable("JAVA_HOME_8_X64");
Information("Setting JavaSdkDirectory to: " + javaSdkDir);
settings = settings.WithProperty("JavaSdkDirectory", javaSdkDir);
}
return settings;
}
bool ShouldPushNugetPackages(string branchName, string nugetSource)
{
if (StringComparer.OrdinalIgnoreCase.Equals(branchName, "develop"))
return true;
var masterOrRelease = IsMasterOrReleases(branchName);
if (masterOrRelease && nugetSource != null && nugetSource.Contains("github.com"))
return true;
return masterOrRelease && IsTagged().Item1;
}
bool IsMasterOrReleases(string branchName)
{
if (StringComparer.OrdinalIgnoreCase.Equals(branchName, "master"))
return true;
if (branchName.StartsWith("release/", StringComparison.OrdinalIgnoreCase) ||
branchName.StartsWith("releases/", StringComparison.OrdinalIgnoreCase))
return true;
return false;
}
bool IsRepository(string repoName)
{
if (isRunningOnPipelines)
{
var buildEnvRepoName = TFBuild.Environment.Repository.RepoName;
Information("Checking repo name: {0} against build repo name: {1}", repoName, buildEnvRepoName);
return StringComparer.OrdinalIgnoreCase.Equals(repoName, buildEnvRepoName);
}
else
{
try
{
var path = MakeAbsolute(sln).GetDirectory().FullPath;
using (var repo = new LibGit2Sharp.Repository(path))
{
var origin = repo.Network.Remotes.FirstOrDefault(
r => r.Name.ToLowerInvariant() == "origin");
return origin.Url.ToLowerInvariant() ==
"https://github.com/" + repoName.ToLowerInvariant();
}
}
catch(Exception ex)
{
Information("Failed to lookup repository: {0}", ex);
return false;
}
}
}
Tuple<bool, string> IsTagged()
{
var path = MakeAbsolute(sln).GetDirectory().FullPath;
using (var repo = new LibGit2Sharp.Repository(path))
{
var head = repo.Head;
var headSha = head.Tip.Sha;
var tag = repo.Tags.FirstOrDefault(t => t.Target.Sha == headSha);
if (tag == null)
{
Debug("HEAD is not tagged");
return Tuple.Create<bool, string>(false, null);
}
Debug("HEAD is tagged: {0}", tag.FriendlyName);
return Tuple.Create<bool, string>(true, tag.FriendlyName);
}
}