forked from MahApps/MahApps.Metro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.cake
475 lines (402 loc) · 16.9 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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
///////////////////////////////////////////////////////////////////////////////
// TOOLS / ADDINS
///////////////////////////////////////////////////////////////////////////////
#tool dotnet:?package=NuGetKeyVaultSignTool&version=3.2.3
#tool dotnet:?package=AzureSignTool&version=4.0.1
#tool dotnet:?package=GitReleaseManager.Tool&version=0.15.0
#tool dotnet:?package=XamlStyler.Console&version=3.2404.2
#tool nuget:?package=GitVersion.CommandLine&version=5.12.0
#tool nuget:?package=NUnit.ConsoleRunner&version=3.18.3
#addin nuget:?package=Cake.Figlet&version=2.0.1
///////////////////////////////////////////////////////////////////////////////
// ARGUMENTS
///////////////////////////////////////////////////////////////////////////////
var target = Argument("target", "Default");
///////////////////////////////////////////////////////////////////////////////
// PREPARATION
///////////////////////////////////////////////////////////////////////////////
var repoName = "MahApps.Metro";
var baseDir = MakeAbsolute(Directory(".")).ToString();
var srcDir = baseDir + "/src";
var solution = srcDir + "/MahApps.Metro.sln";
var publishDir = baseDir + "/Publish";
var testResultsDir = Directory(baseDir + "/TestResults");
var styler = Context.Tools.Resolve("xstyler.exe");
var stylerFile = baseDir + "/Settings.XAMLStyler";
public class BuildData
{
public string Configuration { get; }
public Verbosity Verbosity { get; }
public DotNetVerbosity DotNetVerbosity { get; }
public bool IsLocalBuild { get; set; }
public bool IsPullRequest { get; set; }
public bool IsDevelopBranch { get; set; }
public bool IsReleaseBranch { get; set; }
public GitVersion GitVersion { get; set; }
public BuildData(
string configuration,
Verbosity verbosity,
DotNetVerbosity dotNetVerbosity
)
{
Configuration = configuration;
Verbosity = verbosity;
DotNetVerbosity = dotNetVerbosity;
}
public void SetGitVersion(GitVersion gitVersion)
{
GitVersion = gitVersion;
IsDevelopBranch = StringComparer.OrdinalIgnoreCase.Equals("develop", GitVersion.BranchName);
IsReleaseBranch = StringComparer.OrdinalIgnoreCase.Equals("main", GitVersion.BranchName);
}
}
///////////////////////////////////////////////////////////////////////////////
// SETUP / TEARDOWN
///////////////////////////////////////////////////////////////////////////////
Setup<BuildData>(ctx =>
{
if (!IsRunningOnWindows())
{
throw new NotImplementedException($"{repoName} will only build on Windows because it's not possible to target WPF and Windows Forms from UNIX.");
}
Information(Figlet(repoName));
var gitVersionPath = Context.Tools.Resolve("gitversion.exe");
Information("GitVersion : {0}", gitVersionPath);
var buildData = new BuildData(
configuration: Argument("configuration", "Release"),
verbosity: Argument("verbosity", Verbosity.Minimal),
dotNetVerbosity: Argument("dotNetVerbosity", DotNetVerbosity.Minimal)
)
{
IsLocalBuild = BuildSystem.IsLocalBuild,
IsPullRequest =
(BuildSystem.GitHubActions.IsRunningOnGitHubActions && BuildSystem.GitHubActions.Environment.PullRequest.IsPullRequest)
|| (BuildSystem.AppVeyor.IsRunningOnAppVeyor && BuildSystem.AppVeyor.Environment.PullRequest.IsPullRequest)
};
// Set build version for CI
if (buildData.IsLocalBuild == false || buildData.Verbosity == Verbosity.Verbose)
{
GitVersion(new GitVersionSettings { ToolPath = gitVersionPath, OutputType = GitVersionOutput.BuildServer });
}
buildData.SetGitVersion(GitVersion(new GitVersionSettings { ToolPath = gitVersionPath, OutputType = GitVersionOutput.Json }));
Information("Branch : {0}", buildData.GitVersion.BranchName);
Information("Configuration : {0}", buildData.Configuration);
Information("IsLocalBuild : {0}", buildData.IsLocalBuild);
Information("Informational Version: {0}", buildData.GitVersion.InformationalVersion);
Information("SemVer Version: {0}", buildData.GitVersion.SemVer);
Information("AssemblySemVer Version: {0}", buildData.GitVersion.AssemblySemVer);
Information("MajorMinorPatch Version: {0}", buildData.GitVersion.MajorMinorPatch);
Information("NuGet Version: {0}", buildData.GitVersion.NuGetVersion);
Information("Verbosity : {0}", buildData.Verbosity);
return buildData;
});
Teardown(ctx =>
{
});
///////////////////////////////////////////////////////////////////////////////
// TASKS
///////////////////////////////////////////////////////////////////////////////
Task("Clean")
.ContinueOnError()
.Does(() =>
{
var filesToDelete = GetFiles("**/*_wpftmp.csproj")
;
DeleteFiles(filesToDelete);
var directoriesToDelete = GetDirectories("./**/obj")
.Concat(GetDirectories("./**/bin"))
.Concat(GetDirectories("./**/Publish"))
;
DeleteDirectories(directoriesToDelete, new DeleteDirectorySettings { Recursive = true, Force = true });
});
Task("Restore")
.Does<BuildData>(data =>
{
DotNetRestore(solution);
});
Task("Build")
.Does<BuildData>(data =>
{
var msbuildSettings = new DotNetMSBuildSettings
{
MaxCpuCount = 0,
Version = data.IsReleaseBranch ? data.GitVersion.MajorMinorPatch : data.GitVersion.NuGetVersion,
AssemblyVersion = data.GitVersion.AssemblySemVer,
FileVersion = data.GitVersion.AssemblySemFileVer,
InformationalVersion = data.GitVersion.InformationalVersion,
ContinuousIntegrationBuild = data.IsReleaseBranch,
ArgumentCustomization = args => args.Append("/m").Append("/nr:false") // The /nr switch tells msbuild to quite once it's done
};
// msbuildSettings.FileLoggers.Add(
// new MSBuildFileLoggerSettings
// {
// LogFile = buildLogFile,
// AppendToLogFile = true,
// Verbosity = data.DotNetVerbosity
// }
// );
var settings = new DotNetBuildSettings
{
MSBuildSettings = msbuildSettings,
Configuration = data.Configuration,
Verbosity = data.DotNetVerbosity,
NoRestore = true
};
DotNetBuild(solution, settings);
});
Task("Pack")
.ContinueOnError()
.Does<BuildData>(data =>
{
EnsureDirectoryExists(Directory(publishDir));
var msbuildSettings = new DotNetMSBuildSettings
{
MaxCpuCount = 0,
Version = data.IsReleaseBranch ? data.GitVersion.MajorMinorPatch : data.GitVersion.NuGetVersion,
AssemblyVersion = data.GitVersion.AssemblySemVer,
FileVersion = data.GitVersion.AssemblySemFileVer,
InformationalVersion = data.GitVersion.InformationalVersion
}
.WithProperty("IncludeBuildOutput", "true")
.WithProperty("RepositoryBranch", data.GitVersion.BranchName)
.WithProperty("RepositoryCommit", data.GitVersion.Sha)
.WithProperty("Description", "The goal of MahApps.Metro is to allow devs to quickly and easily cobble together a 'Modern' UI for their WPF apps (.Net 4.6.2 and later), with minimal effort.")
;
// msbuildSettings.FileLoggers.Add(
// new MSBuildFileLoggerSettings
// {
// LogFile = buildLogFile,
// AppendToLogFile = true,
// Verbosity = DotNetVerbosity.Minimal
// }
// );
var settings = new DotNetPackSettings
{
Configuration = data.Configuration,
OutputDirectory = MakeAbsolute(Directory(publishDir)).FullPath,
MSBuildSettings = msbuildSettings,
NoBuild = true,
NoRestore = true
};
var project = "./src/MahApps.Metro/MahApps.Metro.csproj";
DotNetPack(project, settings);
});
Task("Sign")
.WithCriteria<BuildData>((context, data) => !data.IsPullRequest)
.ContinueOnError()
.Does(() =>
{
var files = GetFiles("./src/MahApps.Metro/bin/**/*/MahApps.Metro*.dll");
SignFiles(files, "MahApps.Metro, a toolkit for creating Metro / Modern UI styled WPF applications.");
files = GetFiles("./src/MahApps.Metro.Samples/**/bin/**/*.exe");
SignFiles(files, "Demo application of MahApps.Metro, a toolkit for creating Metro / Modern UI styled WPF applications.");
});
Task("SignNuGet")
.WithCriteria<BuildData>((context, data) => !data.IsPullRequest)
.WithCriteria<BuildData>((context, data) => DirectoryExists(Directory(publishDir)))
.ContinueOnError()
.Does(() =>
{
var vurl = EnvironmentVariable("azure-key-vault-url");
if(string.IsNullOrWhiteSpace(vurl)) {
Error("Could not resolve signing url.");
return;
}
var vcid = EnvironmentVariable("azure-key-vault-client-id");
if(string.IsNullOrWhiteSpace(vcid)) {
Error("Could not resolve signing client id.");
return;
}
var vctid = EnvironmentVariable("azure-key-vault-tenant-id");
if(string.IsNullOrWhiteSpace(vctid)) {
Error("Could not resolve signing client tenant id.");
return;
}
var vcs = EnvironmentVariable("azure-key-vault-client-secret");
if(string.IsNullOrWhiteSpace(vcs)) {
Error("Could not resolve signing client secret.");
return;
}
var vc = EnvironmentVariable("azure-key-vault-certificate");
if(string.IsNullOrWhiteSpace(vc)) {
Error("Could not resolve signing certificate.");
return;
}
var nugetFiles = GetFiles(publishDir + "/*.nupkg");
var signTool = Context.Tools.Resolve("NuGetKeyVaultSignTool.exe");
foreach(var file in nugetFiles)
{
Information($"Sign file: {file}");
ExecuteProcess(signTool,
new ProcessArgumentBuilder()
.Append("sign")
.Append(MakeAbsolute(file).FullPath)
.Append("--force")
.AppendSwitchQuoted("--file-digest", "sha256")
.AppendSwitchQuoted("--timestamp-rfc3161", "http://timestamp.digicert.com")
.AppendSwitchQuoted("--timestamp-digest", "sha256")
.AppendSwitchQuoted("--azure-key-vault-url", vurl)
.AppendSwitchQuotedSecret("--azure-key-vault-client-id", vcid)
.AppendSwitchQuotedSecret("--azure-key-vault-tenant-id", vctid)
.AppendSwitchQuotedSecret("--azure-key-vault-client-secret", vcs)
.AppendSwitchQuotedSecret("--azure-key-vault-certificate", vc)
);
}
});
Task("Zip")
.Does<BuildData>(data =>
{
EnsureDirectoryExists(Directory(publishDir));
Zip("./src/MahApps.Metro.Samples/MahApps.Metro.Demo/bin/" + data.Configuration, publishDir + "/MahApps.Metro.Demo-v" + data.GitVersion.NuGetVersion + ".zip");
Zip("./src/MahApps.Metro.Samples/MahApps.Metro.Caliburn.Demo/bin/" + data.Configuration, publishDir + "/MahApps.Metro.Caliburn.Demo-v" + data.GitVersion.NuGetVersion + ".zip");
});
Task("Tests")
.ContinueOnError()
.Does<BuildData>(data =>
{
CleanDirectory(testResultsDir);
var settings = new DotNetTestSettings
{
Configuration = data.Configuration,
NoBuild = true,
NoRestore = true,
ResultsDirectory = testResultsDir,
Verbosity = data.DotNetVerbosity
};
DotNetTest("./src/Mahapps.Metro.Tests/Mahapps.Metro.Tests.csproj", settings);
});
Task("StyleXaml")
.Description("Ensures XAML Formatting is Clean")
.Does(() =>
{
Func<IFileSystemInfo, bool> exclude_Dir =
fileSystemInfo => !fileSystemInfo.Path.Segments.Contains("obj") && !fileSystemInfo.Path.ToString().Contains("Styles/Themes");
var files = GetFiles(srcDir + "/**/*.xaml", new GlobberSettings { Predicate = exclude_Dir });
Information("\nChecking " + files.Count() + " file(s) for XAML Structure");
ExecuteProcess(styler, "-f \"" + string.Join(",", files.Select(f => f.ToString())) + "\" -c \"" + stylerFile + "\"");
});
Task("CreateRelease")
.WithCriteria<BuildData>((context, data) => !data.IsPullRequest)
.Does<BuildData>(data =>
{
var token = EnvironmentVariable("GITHUB_TOKEN");
if (string.IsNullOrEmpty(token))
{
throw new Exception("The GITHUB_TOKEN environment variable is not defined.");
}
GitReleaseManagerCreate(token, "MahApps", repoName, new GitReleaseManagerCreateSettings {
Milestone = data.GitVersion.MajorMinorPatch,
Name = data.GitVersion.AssemblySemFileVer,
Prerelease = data.IsDevelopBranch,
TargetCommitish = data.GitVersion.BranchName,
WorkingDirectory = "."
});
});
///////////////////////////////////////////////////////////////////////////////
// HELPER
///////////////////////////////////////////////////////////////////////////////
void SignFiles(IEnumerable<FilePath> files, string description)
{
var vurl = EnvironmentVariable("azure-key-vault-url");
if(string.IsNullOrWhiteSpace(vurl)) {
Error("Could not resolve signing url.");
return;
}
var vcid = EnvironmentVariable("azure-key-vault-client-id");
if(string.IsNullOrWhiteSpace(vcid)) {
Error("Could not resolve signing client id.");
return;
}
var vctid = EnvironmentVariable("azure-key-vault-tenant-id");
if(string.IsNullOrWhiteSpace(vctid)) {
Error("Could not resolve signing client tenant id.");
return;
}
var vcs = EnvironmentVariable("azure-key-vault-client-secret");
if(string.IsNullOrWhiteSpace(vcs)) {
Error("Could not resolve signing client secret.");
return;
}
var vc = EnvironmentVariable("azure-key-vault-certificate");
if(string.IsNullOrWhiteSpace(vc)) {
Error("Could not resolve signing certificate.");
return;
}
var filesToSign = string.Join(" ", files.Select(f => MakeAbsolute(f).FullPath));
var azureSignTool = Context.Tools.Resolve("azuresigntool.exe");
ExecuteProcess(azureSignTool,
new ProcessArgumentBuilder()
.Append("sign")
.Append(filesToSign)
.AppendSwitchQuoted("--file-digest", "sha256")
.AppendSwitchQuoted("--description", description)
.AppendSwitchQuoted("--description-url", "https://github.com/MahApps/MahApps.Metro")
.Append("--no-page-hashing")
.AppendSwitchQuoted("--timestamp-rfc3161", "http://timestamp.digicert.com")
.AppendSwitchQuoted("--timestamp-digest", "sha256")
.AppendSwitchQuoted("--azure-key-vault-url", vurl)
.AppendSwitchQuotedSecret("--azure-key-vault-client-id", vcid)
.AppendSwitchQuotedSecret("--azure-key-vault-tenant-id", vctid)
.AppendSwitchQuotedSecret("--azure-key-vault-client-secret", vcs)
.AppendSwitchQuotedSecret("--azure-key-vault-certificate", vc)
);
}
void ExecuteProcess(FilePath fileName, ProcessArgumentBuilder arguments, string workingDirectory = null)
{
if (!FileExists(fileName))
{
throw new Exception($"File not found: {fileName}");
}
var processSettings = new ProcessSettings
{
RedirectStandardOutput = true,
RedirectStandardError = true,
Arguments = arguments
};
if (!string.IsNullOrEmpty(workingDirectory))
{
processSettings.WorkingDirectory = workingDirectory;
}
Information($"Arguments: {arguments.RenderSafe()}");
using(var process = StartAndReturnProcess(fileName, processSettings))
{
process.WaitForExit();
if (process.GetStandardOutput().Any())
{
Information($"Output:{Environment.NewLine} {string.Join(Environment.NewLine, process.GetStandardOutput())}");
}
if (process.GetStandardError().Any())
{
// Information($"Errors occurred:{Environment.NewLine} {string.Join(Environment.NewLine, process.GetStandardError())}");
throw new Exception($"Errors occurred:{Environment.NewLine} {string.Join(Environment.NewLine, process.GetStandardError())}");
}
// This should output 0 as valid arguments supplied
var exitCode = process.GetExitCode();
Information($"Exit code: {exitCode}");
if (exitCode > 0)
{
throw new Exception($"Exit code: {exitCode}");
}
}
}
///////////////////////////////////////////////////////////////////////////////
// TASK TARGETS
///////////////////////////////////////////////////////////////////////////////
Task("Default")
.IsDependentOn("Clean")
.IsDependentOn("Restore")
.IsDependentOn("StyleXaml")
.IsDependentOn("Build")
.IsDependentOn("Tests")
;
Task("ci")
.IsDependentOn("Default")
.IsDependentOn("Sign")
.IsDependentOn("Pack")
.IsDependentOn("SignNuGet")
.IsDependentOn("Zip")
;
///////////////////////////////////////////////////////////////////////////////
// EXECUTION
///////////////////////////////////////////////////////////////////////////////
RunTarget(target);