forked from punker76/gong-wpf-dragdrop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.cake
422 lines (357 loc) · 15.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
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
///////////////////////////////////////////////////////////////////////////////
// TOOLS / ADDINS
///////////////////////////////////////////////////////////////////////////////
#tool dotnet:?package=NuGetKeyVaultSignTool&version=1.2.28
#tool dotnet:?package=AzureSignTool&version=3.0.0
#tool dotnet:?package=GitReleaseManager.Tool&version=0.12.1
#tool dotnet:?package=GitVersion.Tool&version=5.6.3
#tool dotnet:?package=XamlStyler.Console&version=3.2008.4
#tool vswhere&version=2.8.4
#addin nuget:?package=Cake.Figlet&version=2.0.1
///////////////////////////////////////////////////////////////////////////////
// ARGUMENTS
///////////////////////////////////////////////////////////////////////////////
var target = Argument("target", "Default");
///////////////////////////////////////////////////////////////////////////////
// PREPARATION
///////////////////////////////////////////////////////////////////////////////
var repoName = "gong-wpf-dragdrop";
var baseDir = MakeAbsolute(Directory(".")).ToString();
var srcDir = baseDir + "/src";
var solution = srcDir + "/GongSolutions.WPF.DragDrop.sln";
var publishDir = baseDir + "/Publish";
var styler = Context.Tools.Resolve("xstyler.exe");
var stylerFile = baseDir + "/Settings.XAMLStyler";
public class BuildData
{
public string Configuration { get; }
public Verbosity Verbosity { get; }
public DotNetCoreVerbosity DotNetCoreVerbosity { 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 DirectoryPath MSBuildPath { get; }
public FilePath MSBuildExe { get; }
public BuildData(
string configuration,
Verbosity verbosity,
DotNetCoreVerbosity dotNetCoreVerbosity,
DirectoryPath latestMSBuildInstallationPath
)
{
Configuration = configuration;
Verbosity = verbosity;
DotNetCoreVerbosity = dotNetCoreVerbosity;
MSBuildPath = latestMSBuildInstallationPath.Combine("./MSBuild/Current/Bin");
MSBuildExe = MSBuildPath.CombineWithFilePath("./MSBuild.exe");
}
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("dotnet-gitversion.exe");
Information("GitVersion : {0}", gitVersionPath);
var buildData = new BuildData(
configuration: Argument("configuration", "Release"),
verbosity: Argument("verbosity", Verbosity.Minimal),
dotNetCoreVerbosity: Argument("dotnetnetcoreverbosity", DotNetCoreVerbosity.Minimal),
latestMSBuildInstallationPath: VSWhereLatest(new VSWhereLatestSettings { IncludePrerelease = true })
)
{
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("MSBuild : {0}", buildData.MSBuildExe);
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);
if (FileExists(buildData.MSBuildExe) == false)
{
throw new NotImplementedException("You need at least Visual Studio 2019 to build this project.");
}
return buildData;
});
Teardown(ctx =>
{
});
///////////////////////////////////////////////////////////////////////////////
// TASKS
///////////////////////////////////////////////////////////////////////////////
Task("Clean")
.ContinueOnError()
.Does(() =>
{
var directoriesToDelete = GetDirectories("./**/obj")
.Concat(GetDirectories("./**/bin"))
.Concat(GetDirectories("./**/Publish"));
DeleteDirectories(directoriesToDelete, new DeleteDirectorySettings { Recursive = true, Force = true });
});
Task("Restore")
.Does<BuildData>(data =>
{
DotNetCoreRestore(solution);
});
Task("Build")
.Does<BuildData>(data =>
{
var buildSettings = new DotNetCoreBuildSettings {
Verbosity = data.DotNetCoreVerbosity
, Configuration = data.Configuration
, ArgumentCustomization = args => args.Append("/m").Append("/nr:false") // The /nr switch tells msbuild to quite once it�s done
, MSBuildSettings = new DotNetCoreMSBuildSettings()
.SetMaxCpuCount(0)
.SetConfiguration(data.Configuration)
.WithProperty("Version", data.IsReleaseBranch ? data.GitVersion.MajorMinorPatch : data.GitVersion.NuGetVersion)
.WithProperty("AssemblyVersion", data.GitVersion.AssemblySemVer)
.WithProperty("FileVersion", data.GitVersion.AssemblySemFileVer)
.WithProperty("InformationalVersion", data.GitVersion.InformationalVersion)
.WithProperty("ContinuousIntegrationBuild", data.IsReleaseBranch ? "true" : "false")
};
DotNetCoreBuild(solution, buildSettings);
});
Task("Pack")
.ContinueOnError()
.Does<BuildData>(data =>
{
EnsureDirectoryExists(Directory(publishDir));
var buildSettings = new DotNetCorePackSettings {
Verbosity = data.DotNetCoreVerbosity
, Configuration = data.Configuration
, NoRestore = true
, MSBuildSettings = new DotNetCoreMSBuildSettings()
.SetMaxCpuCount(0)
.SetConfiguration(data.Configuration)
.WithProperty("NoBuild", "true")
.WithProperty("IncludeBuildOutput", "true")
.WithProperty("PackageOutputPath", MakeAbsolute(Directory(publishDir)).FullPath)
.WithProperty("RepositoryBranch", data.GitVersion.BranchName)
.WithProperty("RepositoryCommit", data.GitVersion.Sha)
.WithProperty("Version", data.IsReleaseBranch ? data.GitVersion.MajorMinorPatch : data.GitVersion.NuGetVersion)
.WithProperty("AssemblyVersion", data.GitVersion.AssemblySemVer)
.WithProperty("FileVersion", data.GitVersion.AssemblySemFileVer)
.WithProperty("InformationalVersion", data.GitVersion.InformationalVersion)
};
var projects = GetFiles("./src/GongSolutions.WPF.DragDrop/*.csproj");
foreach(var project in projects)
{
Information("Packing {0}", project);
DeleteFiles(GetFiles("./src/**/*.nuspec"));
DotNetCorePack(project.ToString(), buildSettings);
}
});
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 processSettings = new ProcessSettings {
RedirectStandardOutput = true,
RedirectStandardError = true,
Arguments = new ProcessArgumentBuilder()
.Append("sign")
.Append(filesToSign)
.AppendSwitchQuoted("--file-digest", "sha256")
.AppendSwitchQuoted("--description", description)
.AppendSwitchQuoted("--description-url", "https://github.com/punker76/gong-wpf-dragdrop")
.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());
}
}
Task("Sign")
.WithCriteria<BuildData>((context, data) => !data.IsPullRequest)
.ContinueOnError()
.Does(() =>
{
var files = GetFiles("./src/GongSolutions.WPF.DragDrop/bin/**/*/GongSolutions.WPF.DragDrop.dll");
SignFiles(files, "GongSolutions.WPF.DragDrop, an easy to use drag'n'drop framework for WPF applications.");
files = GetFiles("./src/Showcase/bin/**/*/Showcase.WPF.DragDrop.exe");
SignFiles(files, "Demo application of GongSolutions.WPF.DragDrop, an easy to use drag'n'drop framework for WPF applications.");
});
Task("SignNuGet")
.WithCriteria<BuildData>((context, data) => !data.IsPullRequest)
.ContinueOnError()
.Does(() =>
{
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 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");
foreach(var file in nugetFiles)
{
Information($"Sign file: {file}");
var processSettings = new ProcessSettings {
RedirectStandardOutput = true,
RedirectStandardError = true,
Arguments = 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-client-secret", vcs)
.AppendSwitchQuotedSecret("--azure-key-vault-certificate", vc)
};
using(var process = StartAndReturnProcess("tools/NuGetKeyVaultSignTool", 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());
}
}
});
Task("Zip")
.Does<BuildData>(data =>
{
EnsureDirectoryExists(Directory(publishDir));
Zip($"./src/Showcase/bin/{data.Configuration}", $"{publishDir}/Showcase.DragDrop.{data.Configuration}-v" + data.GitVersion.NuGetVersion + ".zip");
});
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<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, "punker76", repoName, new GitReleaseManagerCreateSettings {
Milestone = data.GitVersion.MajorMinorPatch,
Name = data.GitVersion.AssemblySemFileVer,
Prerelease = data.IsDevelopBranch,
TargetCommitish = data.GitVersion.BranchName,
WorkingDirectory = "."
});
});
///////////////////////////////////////////////////////////////////////////////
// TASK TARGETS
///////////////////////////////////////////////////////////////////////////////
Task("Default")
.IsDependentOn("Clean")
.IsDependentOn("Restore")
.IsDependentOn("StyleXaml")
.IsDependentOn("Build")
;
Task("ci")
.IsDependentOn("Default")
.IsDependentOn("Sign")
.IsDependentOn("Pack")
.IsDependentOn("SignNuGet")
.IsDependentOn("Zip")
;
///////////////////////////////////////////////////////////////////////////////
// EXECUTION
///////////////////////////////////////////////////////////////////////////////
RunTarget(target);