Repository navigation
Expand file tree
/
Copy pathbuild.cake
More file actions
601 lines (524 loc) · 21.3 KB
/
Copy pathbuild.cake
File metadata and controls
601 lines (524 loc) · 21.3 KB
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
// Install addins.
#addin "nuget:https://api.nuget.org/v3/index.json?package=Cake.Twitter&version=6.0.0"
// Install .NET Core Global tools.
#tool "dotnet:https://api.nuget.org/v3/index.json?package=GitVersion.Tool&version=6.7.0"
#tool "dotnet:https://api.nuget.org/v3/index.json?package=GitReleaseManager.Tool&version=0.20.0"
// Load other scripts.
#load "./build/parameters.cake"
#load "./build/verify.cake"
#load "./build/portable-rid-graph.cake"
///////////////////////////////////////////////////////////////////////////////
// SETUP / TEARDOWN
///////////////////////////////////////////////////////////////////////////////
Setup<BuildParameters>(context =>
{
var parameters = new BuildParameters(context);
// Increase verbosity?
if (parameters.IsMainCakeBranch && (context.Log.Verbosity != Verbosity.Diagnostic)) {
Information("Increasing verbosity to diagnostic.");
context.Log.Verbosity = Verbosity.Diagnostic;
}
Information("Building version {0} of Cake ({1}, {2}) using version {3} of Cake.\r\n\t(IsTagged: {4}, IsMainCakeRepo: {5}, IsMainCakeBranch: {6}, IsDevelopCakeBranch: {7})\r\n\t(ShouldPublish: {8}, ShouldPublishToAzureDevOps: {9}, ShouldSignPackages: {10}, IsPullRequest: {11})",
parameters.Version.SemVersion,
parameters.Configuration,
parameters.Target,
parameters.Version.CakeVersion,
parameters.IsTagged,
parameters.IsMainCakeRepo,
parameters.IsMainCakeBranch,
parameters.IsDevelopCakeBranch,
parameters.ShouldPublish,
parameters.ShouldPublishToAzureDevOps,
parameters.ShouldSignPackages,
parameters.IsPullRequest
);
if (parameters.ShouldSignPackages)
{
if (!parameters.CodeSigning.HasCredentials)
{
throw new CakeException("Code signing credentials are missing.");
}
context.Command(
parameters.SignCommandSettings,
out var standardOutput,
new ProcessArgumentBuilder()
.Append("--version")
);
Information("Sign tool version: {0}", standardOutput);
}
foreach (var assemblyInfo in GetFiles("./src/**/AssemblyInfo.cs"))
{
CreateAssemblyInfo(
assemblyInfo.ChangeExtension(".Generated.cs"),
new AssemblyInfoSettings { Description = parameters.Version.SemVersion });
}
return parameters;
});
Teardown<BuildParameters>((context, parameters) =>
{
Information("Starting Teardown...");
if (context.Successful)
{
if (parameters.ShouldPublish)
{
var message = $"Version {parameters.Version.SemVersion} of Cake has just been released, https://www.nuget.org/packages/Cake.Tool/{parameters.Version.SemVersion} 🎉";
if (parameters.CanPostToTwitter)
{
TwitterSendTweet(parameters.Twitter.ConsumerKey, parameters.Twitter.ConsumerSecret, parameters.Twitter.AccessToken, parameters.Twitter.AccessTokenSecret, message);
}
}
}
Information("Finished running tasks.");
});
//////////////////////////////////////////////////////////////////////
// TASKS
//////////////////////////////////////////////////////////////////////
Task("Update-Portable-Rid-Graph")
.Does(static context => UpdatePortableRidGraph(context));
Task("Clean")
.IsDependentOn("NuGet-Login")
.Does<BuildParameters>((context, parameters) =>
{
CleanDirectories("./src/**/bin/" + parameters.Configuration);
CleanDirectories("./src/**/obj");
CleanDirectories(parameters.Paths.Directories.ToClean);
});
Task("Restore-NuGet-Packages")
.IsDependentOn("Clean")
.Does<BuildParameters>((context, parameters) =>
{
DotNetRestore("./src/Cake.slnx", new DotNetRestoreSettings
{
Verbosity = DotNetVerbosity.Minimal,
Sources = new [] { "https://api.nuget.org/v3/index.json" },
MSBuildSettings = parameters.MSBuildSettings
});
});
Task("Build")
.IsDependentOn("Restore-NuGet-Packages")
.IsDependentOn("Update-Portable-Rid-Graph")
.Does<BuildParameters>((context, parameters) =>
{
// Build the solution.
var path = MakeAbsolute(new DirectoryPath("./src/Cake.slnx"));
DotNetBuild(path.FullPath, new DotNetBuildSettings
{
Configuration = parameters.Configuration,
NoRestore = true,
MSBuildSettings = parameters.MSBuildSettings
});
});
Task("Run-Unit-Tests")
.IsDependentOn("Build")
.DoesForEach<BuildParameters, FilePath>(
() => GetFiles("./src/**/*.Tests.csproj"),
(parameters, project, context) =>
{
foreach (var framework in new[] { "net10.0", "net11.0" })
{
var trxFileName = $"{project.GetFilenameWithoutExtension()}_{framework}_TestResults.trx";
DotNetTest(project.FullPath, new DotNetTestSettings
{
Framework = framework,
PathType = DotNetTestPathType.Project,
NoBuild = true,
NoRestore = true,
Configuration = parameters.Configuration,
ResultsDirectory = parameters.Paths.Directories.TestResults,
ArgumentCustomization = args => args
.Append("--report-trx")
.Append("--report-trx-filename")
.AppendQuoted(trxFileName)
});
}
})
.ReportError(async exception =>
{
try
{
await UploadVerifyReceivedFiles(Context, Context.Data.Get<BuildParameters>());
}
catch (Exception uploadException)
{
Error("Failed to upload Verify received files: {0}", uploadException.Message);
}
});
Task("Create-NuGet-Packages")
.IsDependentOn("Run-Unit-Tests")
.Does<BuildParameters>((context, parameters) =>
{
// Build libraries
var projects = GetFiles("./src/*/*.csproj");
foreach (var project in projects)
{
var name = project.GetDirectory().FullPath;
if (name.EndsWith("Tests") || name.EndsWith("Example"))
{
continue;
}
DotNetPack(project.FullPath, new DotNetPackSettings {
Configuration = parameters.Configuration,
OutputDirectory = parameters.Paths.Directories.NuGetRoot,
NoBuild = true,
NoRestore = true,
MSBuildSettings = parameters.MSBuildSettings
});
}
});
Task("Sign-Binaries")
.IsDependentOn("Create-NuGet-Packages")
.WithCriteria<BuildParameters>(static (context, parameters) => parameters.ShouldSignPackages)
.Does<BuildParameters>(static (context, parameters) =>
{
// Get the files to sign.
var files = context.GetFiles(string.Concat(parameters.Paths.Directories.NuGetRoot, "/", "*.nupkg"));
Parallel.ForEach(
files,
file => {
context.Information("Signing {0}...", file.FullPath);
// Build the argument list.
var arguments = new ProcessArgumentBuilder()
.Append("code")
.Append("azure-key-vault")
.AppendQuoted(file.FullPath)
.AppendSwitchQuoted("--file-list", parameters.Paths.SignFilterPath.FullPath)
.AppendSwitchQuoted("--publisher-name", "Cake")
.AppendSwitchQuoted("--description", "Cake (C# Make) is a cross platform build automation system.")
.AppendSwitchQuoted("--description-url", "https://cakebuild.net")
.AppendSwitchQuoted("--azure-credential-type", "azure-cli")
.AppendSwitchQuotedSecret("--azure-key-vault-certificate", parameters.CodeSigning.SignKeyVaultCertificate)
.AppendSwitchQuotedSecret("--azure-key-vault-url", parameters.CodeSigning.SignKeyVaultUrl);
context.Command(
parameters.SignCommandSettings,
arguments
);
context.Information("Done signing {0}.", file.FullPath);
});
});
Task("Upload-AppVeyor-Artifacts")
.IsDependentOn("Package")
.WithCriteria<BuildParameters>((context, parameters) => parameters.IsRunningOnAppVeyor)
.Does<BuildParameters>((context, parameters) =>
{
foreach (var package in GetFiles(parameters.Paths.Directories.NuGetRoot + "/*"))
{
AppVeyor.UploadArtifact(package);
}
});
Task("Upload-GitHubActions-Artifacts")
.IsDependentOn("Package")
.WithCriteria(BuildSystem.IsRunningOnGitHubActions, nameof(BuildSystem.IsRunningOnGitHubActions))
.Does<BuildParameters>(
static (context, parameters) => context
.GitHubActions() is var gh && gh != null
? gh.Commands
.UploadArtifact(parameters.Paths.Directories.NuGetRoot, $"Artifact_{gh.Environment.Runner.ImageOS ?? gh.Environment.Runner.OS}_{gh.Environment.Runner.Architecture}_{context.Environment.Runtime.BuiltFramework.Identifier}_{context.Environment.Runtime.BuiltFramework.Version}")
: throw new Exception("GitHubActions not available")
);
Task("Publish-AzureDevOps")
.IsDependentOn("Sign-Binaries")
.IsDependentOn("Package")
.WithCriteria<BuildParameters>((context, parameters) => parameters.ShouldPublishToAzureDevOps)
.Does<BuildParameters>((context, parameters) =>
{
string
apiKey = "AzureDevOps",
sourceName = "AzureDevOps",
userName = "AzureDevOps";
// Resolve the password .
var password = EnvironmentVariable("AZURE_DEVOPS_NUGET_API_KEY");
if (string.IsNullOrEmpty(password)) {
throw new InvalidOperationException("Could not resolve AzureDevOps password.");
}
// Resolve the API url.
var apiUrl = EnvironmentVariable("AZURE_DEVOPS_NUGET_API_URL");
if (string.IsNullOrEmpty(apiUrl)) {
throw new InvalidOperationException("Could not resolve AzureDevOps API url.");
}
DotNetNuGetAddSource(
sourceName,
new DotNetNuGetAddSourceSettings
{
UserName = userName,
Password = password,
Source = apiUrl
});
foreach (var package in parameters.Packages.NuGet)
{
// Push the package.
var settings = new DotNetNuGetPushSettings {
ApiKey = apiKey,
Source = apiUrl
};
DotNetNuGetPush(package.PackagePath.FullPath, settings);
}
})
.OnError<BuildParameters>((exception, parameters) =>
{
// Azure Artifacts not critical
Information("Publish-AzureDevOps Task failed, but continuing with next Task...");
});
Task("NuGet-Login")
.WithCriteria<BuildParameters>(static (_, parameters) => parameters.ShouldPublish)
.Does<BuildParameters>(static async (context, parameters) =>
{
ArgumentException.ThrowIfNullOrEmpty(parameters.NuGetApiUser);
context.Information("Logging in to NuGet...");
parameters.NuGetApiKey = await context.GitHubActions().Commands.NuGetLogin(parameters.NuGetApiUser);
ArgumentException.ThrowIfNullOrEmpty(parameters.NuGetApiKey);
});
Task("Publish-NuGet")
.IsDependentOn("Sign-Binaries")
.IsDependentOn("Package")
.IsDependentOn("NuGet-Login")
.WithCriteria<BuildParameters>((context, parameters) => parameters.ShouldPublish)
.Does<BuildParameters>((context, parameters) =>
{
// Resolve the API url.
var apiUrl = EnvironmentVariable("NUGET_API_URL");
if (string.IsNullOrEmpty(apiUrl)) {
throw new InvalidOperationException("Could not resolve NuGet API url.");
}
foreach (var package in parameters.Packages.NuGet)
{
// Push the package.
var settings = new DotNetNuGetPushSettings {
ApiKey = parameters.NuGetApiKey,
Source = apiUrl
};
DotNetNuGetPush(package.PackagePath.FullPath, settings);
}
})
.OnError<BuildParameters>((exception, parameters) =>
{
Information("Publish-NuGet Task failed, but continuing with next Task...");
parameters.PublishingError = true;
});
Task("Publish-GitHub-Release")
.WithCriteria<BuildParameters>((context, parameters) => parameters.ShouldPublish)
.Does<BuildParameters>((context, parameters) =>
{
foreach (var package in GetFiles(parameters.Paths.Directories.NuGetRoot + "/*"))
{
GitReleaseManagerAddAssets(parameters.GitHub.Token, "cake-build", "cake", parameters.Version.Milestone, package.FullPath);
}
GitReleaseManagerClose(parameters.GitHub.Token, "cake-build", "cake", parameters.Version.Milestone);
})
.OnError<BuildParameters>((exception, parameters) =>
{
Information("Publish-GitHub-Release Task failed, but continuing with next Task...");
parameters.PublishingError = true;
});
Task("Create-Release-Notes")
.Does<BuildParameters>((context, parameters) =>
{
GitReleaseManagerCreate(parameters.GitHub.Token, "cake-build", "cake", new GitReleaseManagerCreateSettings {
Milestone = parameters.Version.Milestone,
Name = parameters.Version.Milestone,
Prerelease = false,
TargetCommitish = "main"
});
});
Task("Prepare-Integration-Tests")
.IsDependentOn("Create-NuGet-Packages")
.Does<BuildParameters>((context, parameters) =>
{
Unzip(parameters.Paths.Directories.NuGetRoot.CombineWithFilePath($"Cake.Tool.{parameters.Version.SemVersion}.nupkg"),
parameters.Paths.Directories.IntegrationTestsBinTool);
});
Task("Frosting-Integration-Tests")
.WithCriteria(
() => !HasArgument("integration-tests-target"),
"Frosting suite when integration-tests-target is omitted or includes Run-All-Tests")
.DeferOnError()
.DoesForEach<BuildParameters, (string Framework, FilePath Project)>(
(parameters, context) => {
var project = context.MakeAbsolute(
new FilePath("tests/integration/Cake.Frosting/build/Build.csproj")
);
DotNetBuild(project.FullPath,
new DotNetBuildSettings
{
Verbosity = DotNetVerbosity.Quiet,
Configuration = parameters.Configuration,
MSBuildSettings = parameters.MSBuildSettings
});
context.Verbose("Peeking into {0}...", project);
var targetFrameworks = context.XmlPeek(
project,
"/Project/PropertyGroup/TargetFrameworks"
);
return targetFrameworks?.Split(';')
.Select(targetFramework => (targetFramework, project))
.ToArray();
},
(parameters, test, context) =>
{
string defaultVerbosity = EnvironmentVariable("RUNNER_DEBUG", "0") == "1" ? "diagnostic" : "quiet";
try
{
Information("Testing: {0}", test.Framework);
void RunFrosting(ProcessArgumentBuilder arguments, Dictionary<string, string> environmentVariables, int? expectedExitCode = null)
{
int? exitCode = null;
var settings = new DotNetRunSettings
{
Configuration = parameters.Configuration,
Framework = test.Framework,
NoRestore = true,
NoBuild = true,
EnvironmentVariables = environmentVariables
};
if (expectedExitCode is not null)
{
settings.HandleExitCode = code =>
{
exitCode = code;
return true;
};
}
DotNetRun(test.Project.FullPath, arguments, settings);
if (expectedExitCode is int expected && exitCode != expected)
{
throw new Exception($"Expected Frosting exit code {expected}, got {exitCode}.");
}
}
var baseEnvironment = new Dictionary<string, string>
{
["CAKE_INTEGRATIONTEST_ENVIRONMENT"] = bool.TrueString,
};
RunFrosting(
new ProcessArgumentBuilder()
.AppendSwitchQuoted("--verbosity", "=", Argument("integration-tests-verbosity", defaultVerbosity))
.AppendSwitchQuoted("--name", "=", "World")
.AppendSwitchQuoted("--IntegrationTest_Argument", "=", bool.TrueString),
baseEnvironment);
RunFrosting(
new ProcessArgumentBuilder()
.AppendSwitchQuoted("--target", "=", "Verbosity")
.AppendSwitchQuoted("--expected", "=", "Minimal"),
new Dictionary<string, string>(baseEnvironment)
{
["CAKE_SETTINGS_VERBOSITY"] = "Minimal"
});
RunFrosting(
new ProcessArgumentBuilder()
.AppendSwitchQuoted("--target", "=", "Verbosity")
.AppendSwitchQuoted("--verbosity", "=", "quiet")
.AppendSwitchQuoted("--expected", "=", "Quiet"),
new Dictionary<string, string>(baseEnvironment)
{
["CAKE_SETTINGS_VERBOSITY"] = "Diagnostic"
});
RunFrosting(
new ProcessArgumentBuilder()
.AppendSwitchQuoted("--target", "=", "Verbosity")
.AppendSwitchQuoted("--verbosity", "=", "normal")
.AppendSwitchQuoted("--expected", "=", "Normal"),
new Dictionary<string, string>(baseEnvironment)
{
["CAKE_SETTINGS_VERBOSITY"] = "Diagnostic"
});
RunFrosting(
new ProcessArgumentBuilder()
.AppendSwitchQuoted("--target", "=", "foobar")
.AppendSwitchQuoted("--verbosity", "=", Argument("integration-tests-verbosity", defaultVerbosity)),
baseEnvironment,
expectedExitCode: 1);
}
catch(Exception ex)
{
Error("While testing: {0}\r\n{1}", test.Framework, ex);
throw new Exception($"Exception while testing: {test.Framework}", ex);
}
finally
{
Information("Done testing: {0}", test.Framework);
}
});
Task("Run-Integration-Tests")
.IsDependentOn("Prepare-Integration-Tests")
.IsDependentOn("Frosting-Integration-Tests")
.DeferOnError()
.DoesForEach<BuildParameters, (FilePath CakeAssembly, string Verbosity)>(
parameters => {
string defaultVerbosity = EnvironmentVariable("RUNNER_DEBUG", "0") == "1" ? "diagnostic" : "quiet";
return [
(GetFiles($"{parameters.Paths.Directories.IntegrationTestsBinTool.FullPath}/**/net10.0/**/Cake.dll").Single(), defaultVerbosity),
(GetFiles($"{parameters.Paths.Directories.IntegrationTestsBinTool.FullPath}/**/net11.0/**/Cake.dll").Single(), defaultVerbosity)
];
},
(parameters, test, context) =>
{
var (cakeAssembly, verbosity) = test;
try
{
Information("Testing: {0}", cakeAssembly);
CakeExecuteScript("./tests/integration/build.cake",
new CakeSettings {
ToolPath = cakeAssembly,
EnvironmentVariables = {
["MyEnvironmentVariable"] = "Hello World",
["CAKE_INTEGRATION_TEST_ROOT"] = "../.."
},
ArgumentCustomization = args =>
Arguments<string>("integration-tests-target", ["Run-All-Tests"])
.Aggregate(args, (builder, target) => builder.AppendSwitchQuoted("--target", " ", target))
.AppendSwitchQuoted("--verbosity", " ", Argument("integration-tests-verbosity", verbosity))
.AppendSwitchQuoted("--platform", " ", parameters.IsRunningOnWindows ? "windows" : "posix")
.AppendSwitchQuoted("--customarg", " ", "hello")
.AppendSwitchQuoted("--multipleargs", "=", "a")
.AppendSwitchQuoted("--multipleargs", "=", "b")
.AppendSwitchQuoted("--testAssemblyDirectoryPath", "=", cakeAssembly.GetDirectory().FullPath)
.AppendSwitchQuoted("--testAssemblyFilePath", "=", cakeAssembly.FullPath)
.AppendSwitchQuoted("--testDotNetVerbosity", "=", "Diagnostic")
.AppendSwitchQuoted("--testDotNetRollForward", "=", "LatestMajor")
});
}
catch(Exception ex)
{
Error("While testing: {0}\r\n{1}", cakeAssembly, ex);
throw new Exception($"Exception while testing: {cakeAssembly}", ex);
}
finally
{
Information("Done testing: {0}", cakeAssembly);
}
});
//////////////////////////////////////////////////////////////////////
// TASK TARGETS
//////////////////////////////////////////////////////////////////////
Task("Package")
.IsDependentOn("Create-NuGet-Packages");
Task("Default")
.IsDependentOn("Package");
Task("AppVeyor")
.IsDependentOn("Upload-AppVeyor-Artifacts");
Task("GitHubActions")
.IsDependentOn("Upload-GitHubActions-Artifacts")
.IsDependentOn("Run-Integration-Tests")
.IsDependentOn("Publish-AzureDevOps");
Task("GitHubActions-Release")
.IsDependentOn("Upload-GitHubActions-Artifacts")
.IsDependentOn("Publish-AzureDevOps")
.IsDependentOn("Publish-NuGet")
.IsDependentOn("Publish-GitHub-Release")
.Does<BuildParameters>((context, parameters) =>
{
if (parameters.PublishingError)
{
throw new Exception("An error occurred during the publishing of Cake. All publishing tasks have been attempted.");
}
});
Task("Travis")
.IsDependentOn("Run-Unit-Tests");
Task("Rwx")
.IsDependentOn("Run-Integration-Tests");
Task("ReleaseNotes")
.IsDependentOn("Create-Release-Notes");
Task("AzureDevOps")
.IsDependentOn("Package");
//////////////////////////////////////////////////////////////////////
// EXECUTION
//////////////////////////////////////////////////////////////////////
RunTarget(Argument("target", "Default"));