-
-
Notifications
You must be signed in to change notification settings - Fork 222
/
build.cake
388 lines (321 loc) · 13.8 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
///////////////////////////////////////////////////////////////////////////////
// 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.17.0
#tool dotnet:?package=XamlStyler.Console&version=3.2404.2
#tool vswhere&version=2.8.4
#tool nuget:?package=GitVersion.CommandLine&version=5.12.0
///////////////////////////////////////////////////////////////////////////////
// ARGUMENTS
///////////////////////////////////////////////////////////////////////////////
var target = Argument("target", "Default");
var configuration = Argument("configuration", "Release");
var verbosity = Argument("verbosity", Verbosity.Minimal);
var PROJECT_DIR = Context.Environment.WorkingDirectory.FullPath + "/";
var PACKAGE_DIR = Directory(Argument("artifact-dir", PROJECT_DIR + "Publish") + "/");
///////////////////////////////////////////////////////////////////////////////
// PREPARATION
///////////////////////////////////////////////////////////////////////////////
var repoName = "MahApps.Metro.IconPacks";
var baseDir = MakeAbsolute(Directory(".")).ToString();
var srcDir = baseDir + "/src";
var solution = srcDir + "/MahApps.Metro.IconPacks.sln";
var styler = Context.Tools.Resolve("xstyler.exe");
var stylerFile = baseDir + "/Settings.XAMLStyler";
var isLocal = BuildSystem.IsLocalBuild;
var isAppVeyorBuild = AppVeyor.IsRunningOnAppVeyor;
var isGitHubActionsBuild = GitHubActions.IsRunningOnGitHubActions;
// Set build version
if (isLocal == false || verbosity == Verbosity.Verbose)
{
GitVersion(new GitVersionSettings { OutputType = GitVersionOutput.BuildServer });
}
GitVersion gitVersion = GitVersion(new GitVersionSettings { OutputType = GitVersionOutput.Json });
var isPrerelease = gitVersion.NuGetVersion.Contains("-");
var branchName = gitVersion.BranchName;
var isPullRequest = (isAppVeyorBuild && AppVeyor.Environment.PullRequest.IsPullRequest) || (isGitHubActionsBuild && GitHubActions.Environment.PullRequest.IsPullRequest);
var latestInstallationPath = VSWhereLatest(new VSWhereLatestSettings { IncludePrerelease = false });
var msBuildPath = latestInstallationPath.Combine("./MSBuild/Current/Bin");
var msBuildPathExe = msBuildPath.CombineWithFilePath("./MSBuild.exe");
if (FileExists(msBuildPathExe) == false)
{
throw new NotImplementedException("You need at least Visual Studio 2019 to build this project.");
}
// Define global marcos.
Action Abort = () => { throw new Exception("a non-recoverable fatal error occurred."); };
///////////////////////////////////////////////////////////////////////////////
// SETUP / TEARDOWN
///////////////////////////////////////////////////////////////////////////////
Setup(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.");
}
Spectre.Console.AnsiConsole.Write(new Spectre.Console.FigletText("MahApps.Metro"));
Spectre.Console.AnsiConsole.Write(new Spectre.Console.FigletText("IconPacks"));
Information("Informational Version: {0}", gitVersion.InformationalVersion);
Information("SemVer Version: {0}", gitVersion.SemVer);
Information("AssemblySemVer Version: {0}", gitVersion.AssemblySemVer);
Information("MajorMinorPatch Version: {0}", gitVersion.MajorMinorPatch);
Information("NuGet Version: {0}", gitVersion.NuGetVersion);
Information("IsLocalBuild : {0}", isLocal);
Information("IsPrerelease : {0}", isPrerelease);
Information("Branch : {0}", branchName);
Information("Configuration : {0}", configuration);
Information("MSBuildPath : {0}", msBuildPath);
Information("Publish to : {0}", PACKAGE_DIR);
});
Teardown(ctx =>
{
});
///////////////////////////////////////////////////////////////////////////////
// TASKS
///////////////////////////////////////////////////////////////////////////////
Task("Clean")
.ContinueOnError()
.Does(() =>
{
var directoriesToDelete = GetDirectories("./**/obj")
.Concat(GetDirectories("./**/bin"))
.Concat(GetDirectories("./**/Publish"))
.Concat(GetDirectories("./**/output"));
DeleteDirectories(directoriesToDelete, new DeleteDirectorySettings { Recursive = true, Force = true });
});
Task("Restore")
.Does(() =>
{
NuGetRestore(solution, new NuGetRestoreSettings { MSBuildPath = msBuildPath.ToString() });
//DotNetCoreRestore(solution);
});
Task("Build")
.Does(() =>
{
EnsureDirectoryExists(PACKAGE_DIR);
var msBuildSettings = new MSBuildSettings {
Verbosity = verbosity
, ToolPath = msBuildPathExe
, Configuration = configuration
, ArgumentCustomization = args => args.Append("/m").Append("/nr:false") // The /nr switch tells msbuild to quite once it�s done
, BinaryLogger = new MSBuildBinaryLogSettings() { Enabled = isLocal }
};
MSBuild(solution, msBuildSettings
.SetMaxCpuCount(0)
.WithProperty("GeneratePackageOnBuild", "true")
.WithProperty("PackageOutputPath", MakeAbsolute(PACKAGE_DIR).ToString())
.WithProperty("RepositoryBranch", branchName)
.WithProperty("RepositoryCommit", gitVersion.Sha)
.WithProperty("Version", gitVersion.NuGetVersion)
.WithProperty("AssemblyVersion", gitVersion.AssemblySemVer)
.WithProperty("FileVersion", gitVersion.AssemblySemFileVer)
.WithProperty("InformationalVersion", gitVersion.InformationalVersion)
.WithProperty("ContinuousIntegrationBuild", isLocal ? "false": "true")
);
});
void SignFiles(IEnumerable<FilePath> files, string description, string repoUrl)
{
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 processSettings = new ProcessSettings {
RedirectStandardOutput = true,
RedirectStandardError = true,
Arguments = new ProcessArgumentBuilder()
.Append("sign")
.Append(filesToSign)
.AppendSwitchQuoted("--file-digest", "sha256")
.AppendSwitchQuoted("--description", description)
.AppendSwitchQuoted("--description-url", repoUrl)
.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)
};
using(var process = StartAndReturnProcess("tools/AzureSignTool", 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())}");
}
// This should output 0 as valid arguments supplied
Information("Exit code: {0}", process.GetExitCode());
}
}
void SignNuGet(string publishDir)
{
if (!DirectoryExists(Directory(publishDir)))
{
return;
}
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("Sign")
.WithCriteria(() => !isPullRequest)
.ContinueOnError()
.Does(() =>
{
SignNuGet(MakeAbsolute(PACKAGE_DIR).ToString());
});
Task("StyleXaml")
.Description("Ensures XAML Formatting is Clean")
.Does(() =>
{
Func<IFileSystemInfo, bool> exclude_Dir =
fileSystemInfo => !fileSystemInfo.Path.Segments.Contains("obj");
var files = GetFiles(srcDir + "/**/*.xaml", new GlobberSettings { Predicate = exclude_Dir });
Information("\nChecking " + files.Count() + " file(s) for XAML Structure");
StartProcess(styler, "-f \"" + string.Join(",", files.Select(f => f.ToString())) + "\" -c \"" + stylerFile + "\"");
});
Task("CreateRelease")
.WithCriteria(() => !isPullRequest)
.Does(() =>
{
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 = gitVersion.MajorMinorPatch,
Name = gitVersion.AssemblySemFileVer,
Prerelease = isPrerelease,
TargetCommitish = branchName,
WorkingDirectory = "."
});
});
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")
;
Task("ci")
.IsDependentOn("Default")
.IsDependentOn("Sign")
;
Task("azure")
.IsDependentOn("Default")
;
///////////////////////////////////////////////////////////////////////////////
// EXECUTION
///////////////////////////////////////////////////////////////////////////////
RunTarget(target);