-
-
Notifications
You must be signed in to change notification settings - Fork 348
/
build.cake
524 lines (466 loc) · 18.4 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
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
#addin "nuget:?package=Cake.SemVer&version=4.0.0"
#addin "nuget:?package=semver&version=2.3.0"
#addin nuget:?package=Cake.Git&version=3.0.0
#tool "nuget:?package=ILRepack&version=2.0.27"
#tool "nuget:?package=NUnit.ConsoleRunner&version=3.16.3"
using System.Text.RegularExpressions;
using Semver;
var buildNetFramework = "net48";
var target = Argument<string>("target", "Default");
var configuration = Argument<string>("configuration", "Debug");
var solution = Argument<string>("solution", "CKAN.sln");
var rootDirectory = Context.Environment.WorkingDirectory;
var coreProj = rootDirectory.Combine("Core")
.CombineWithFilePath("CKAN-core.csproj")
.FullPath;
var buildDirectory = rootDirectory.Combine("_build");
var nugetDirectory = buildDirectory.Combine("lib")
.Combine("nuget");
var outDirectory = buildDirectory.Combine("out");
var nupkgFile = outDirectory.Combine("CKAN")
.Combine(configuration)
.Combine("bin")
.CombineWithFilePath($"CKAN.{GetVersion(false)}.nupkg");
var repackDirectory = buildDirectory.Combine("repack");
var ckanFile = repackDirectory.Combine(configuration)
.CombineWithFilePath("ckan.exe");
var updaterFile = repackDirectory.Combine(configuration)
.CombineWithFilePath("AutoUpdater.exe");
var netkanFile = repackDirectory.Combine(configuration)
.CombineWithFilePath("netkan.exe");
Task("Default")
.Description("Build ckan.exe and netkan.exe")
.IsDependentOn("Ckan")
.IsDependentOn("Netkan");
Task("Debug")
.Description("Build ckan.exe and netkan.exe in Debug configuration")
.IsDependentOn("Default");
Task("Release")
.Description("Build ckan.exe and netkan.exe in Release configuration")
.IsDependentOn("Default");
Task("Ckan")
.Description("Build only ckan.exe")
.IsDependentOn("Repack-Ckan");
Task("Netkan")
.Description("Build only netkan.exe")
.IsDependentOn("Repack-Netkan");
Task("osx")
.Description("Build the macOS(OSX) dmg package.")
.IsDependentOn("Ckan")
.Does(() => MakeIn("macosx"));
Task("osx-clean")
.Description("Clean the output directory of the macOS(OSX) package.")
.Does(() => MakeIn("macosx", "clean"));
Task("deb")
.Description("Build the deb package for Debian-based distros.")
.IsDependentOn("Ckan")
.Does(() => MakeIn("debian"));
Task("deb-sign")
.Description("Build the deb package for Debian-based distros.")
.IsDependentOn("Ckan")
.Does(() => MakeIn("debian", "sign"));
Task("deb-test")
.Description("Test the deb packaging.")
.IsDependentOn("deb")
.Does(() => MakeIn("debian", "test"));
Task("deb-clean")
.Description("Clean the deb output directory.")
.Does(() => MakeIn("debian", "clean"));
Task("rpm")
.Description("Build the rpm package for RPM-based distros.")
.IsDependentOn("Ckan")
.Does(() => MakeIn("rpm"));
Task("rpm-repo")
.Description("Build the rpm repository for RPM-based distros.")
.IsDependentOn("Ckan")
.Does(() => MakeIn("rpm", "repo"));
Task("rpm-test")
.Description("Test the rpm packaging.")
.IsDependentOn("Ckan")
.Does(() => MakeIn("rpm", "test"));
Task("rpm-clean")
.Description("Clean the rpm package output directory.")
.Does(() => MakeIn("rpm", "clean"));
private void MakeIn(string dir, string args = null)
{
int exitCode = StartProcess("make", new ProcessSettings {
WorkingDirectory = dir,
Arguments = args,
EnvironmentVariables = new Dictionary<string, string> { { "CONFIGURATION", configuration } }
});
if (exitCode != 0)
{
throw new Exception("Make failed with exit code: " + exitCode);
}
}
Task("Restore")
.Description("Intermediate - Download dependencies")
.Does(() =>
{
DotNetRestore(solution, new DotNetRestoreSettings
{
PackagesDirectory = nugetDirectory,
EnvironmentVariables = new Dictionary<string, string> { { "Configuration", configuration } },
});
});
Task("Build")
.Description("Intermediate - Build everything")
.IsDependentOn("Restore")
.IsDependentOn("Generate-GlobalAssemblyVersionInfo")
.Does(() =>
{
// dotnet build won't let us compile WinForms on non-Windows,
// fall back to mono
if (IsRunningOnWindows())
{
DotNetBuild(solution, new DotNetBuildSettings
{
Configuration = configuration,
NoRestore = true,
});
}
else
{
// Use dotnet to build the Core DLL to get the nupkg
// (only created if all TargetFrameworks are built together)
DotNetBuild(coreProj, new DotNetBuildSettings
{
Configuration = configuration,
});
// Use Mono to build for net48 since dotnet can't use WinForms on Linux
MSBuild(solution,
settings => settings.SetConfiguration(configuration)
.SetMaxCpuCount(0)
.WithProperty("TargetFramework",
buildNetFramework));
// Use dotnet to build the stuff Mono can't build
DotNetBuild(solution, new DotNetBuildSettings
{
Configuration = "NoGUI",
Framework = "net8.0",
});
}
});
Task("Generate-GlobalAssemblyVersionInfo")
.Description("Intermediate - Calculate the version strings for the assembly.")
.Does(() =>
{
var metaDirectory = buildDirectory.Combine("meta");
CreateDirectory(metaDirectory);
var version = GetVersion();
CreateAssemblyInfo(metaDirectory.CombineWithFilePath("GlobalAssemblyVersionInfo.cs"), new AssemblyInfoSettings
{
Version = string.Format("{0}.{1}", version.Major, version.Minor),
FileVersion = string.IsNullOrEmpty(version.Metadata)
? string.Format("{0}.{1}.{2}",
version.Major, version.Minor, version.Patch)
: string.Format("{0}.{1}.{2}.{3}",
version.Major, version.Minor, version.Patch,
version.Metadata),
InformationalVersion = version.ToString()
});
});
Task("Repack-Ckan")
.Description("Intermediate - Merge all the separate DLLs and EXEs to a single executable.")
.IsDependentOn("Build")
.Does(() =>
{
CreateDirectory(repackDirectory.Combine(configuration));
var cmdLineBinDirectory = outDirectory.Combine("CKAN-CmdLine")
.Combine(configuration)
.Combine("bin")
.Combine(buildNetFramework);
var assemblyPaths = GetFiles(string.Format("{0}/*.dll", cmdLineBinDirectory));
assemblyPaths.Add(cmdLineBinDirectory.CombineWithFilePath("CKAN-GUI.exe"));
assemblyPaths.Add(cmdLineBinDirectory.CombineWithFilePath("CKAN-ConsoleUI.exe"));
assemblyPaths.Add(GetFiles(string.Format(
"{0}/*/*.resources.dll",
outDirectory.Combine("CKAN-CmdLine")
.Combine(configuration)
.Combine("bin")
.Combine(buildNetFramework))));
// Need facade to instantiate types from netstandard2.0 DLLs on Mono
assemblyPaths.Add(FacadesDirectory().CombineWithFilePath("netstandard.dll"));
var ckanLogFile = repackDirectory.Combine(configuration)
.CombineWithFilePath("ckan.log");
ReportRepacking(ckanFile, ckanLogFile);
ILRepack(
ckanFile,
cmdLineBinDirectory.CombineWithFilePath("CKAN-CmdLine.exe"),
assemblyPaths,
new ILRepackSettings
{
Libs = new List<DirectoryPath> { cmdLineBinDirectory },
TargetPlatform = TargetPlatformVersion.v4,
Parallel = true,
Verbose = false,
SetupProcessSettings = RepackSilently,
Log = ckanLogFile.FullPath,
});
var autoupdateBinDirectory = outDirectory.Combine("CKAN-AutoUpdateHelper")
.Combine(configuration)
.Combine("bin")
.Combine(buildNetFramework);
var updaterLogFile = repackDirectory.Combine(configuration)
.CombineWithFilePath("AutoUpdater.log");
ReportRepacking(updaterFile, updaterLogFile);
ILRepack(
updaterFile,
autoupdateBinDirectory.CombineWithFilePath("CKAN-AutoUpdateHelper.exe"),
GetFiles(string.Format("{0}/*/*.resources.dll",
autoupdateBinDirectory)),
new ILRepackSettings
{
Libs = new List<DirectoryPath> { autoupdateBinDirectory },
TargetPlatform = TargetPlatformVersion.v4,
Parallel = true,
Verbose = false,
SetupProcessSettings = RepackSilently,
Log = updaterLogFile.FullPath,
});
CopyFile(ckanFile, buildDirectory.CombineWithFilePath(ckanFile.GetFilename()));
});
Task("Repack-Netkan")
.Description("Intermediate - Merge all the separate DLLs and EXEs to a single executable.")
.IsDependentOn("Build")
.Does(() =>
{
CreateDirectory(repackDirectory.Combine(configuration));
var netkanBinDirectory = outDirectory.Combine("CKAN-NetKAN")
.Combine(configuration)
.Combine("bin")
.Combine(buildNetFramework);
var netkanLogFile = repackDirectory.Combine(configuration)
.CombineWithFilePath("netkan.log");
var assemblyPaths = GetFiles(string.Format("{0}/*.dll", netkanBinDirectory));
// Need facade to instantiate types from netstandard2.0 DLLs on Mono
assemblyPaths.Add(FacadesDirectory().CombineWithFilePath("netstandard.dll"));
ReportRepacking(netkanFile, netkanLogFile);
ILRepack(
netkanFile,
netkanBinDirectory.CombineWithFilePath("CKAN-NetKAN.exe"),
assemblyPaths,
new ILRepackSettings
{
Libs = new List<DirectoryPath> { netkanBinDirectory },
TargetPlatform = TargetPlatformVersion.v4,
Parallel = true,
Verbose = false,
SetupProcessSettings = RepackSilently,
Log = netkanLogFile.FullPath,
}
);
CopyFile(netkanFile, buildDirectory.CombineWithFilePath(netkanFile.GetFilename()));
});
Task("Prepare-SignPath")
.Description("Create a folder with all artifacts to be signed")
.IsDependentOn("Repack-Ckan")
.Does(() =>
{
var targetDir = buildDirectory.Combine("signpath")
.Combine(configuration);
CreateDirectory(targetDir);
CopyFile(ckanFile, targetDir.CombineWithFilePath(ckanFile.GetFilename()));
CopyFile(updaterFile, targetDir.CombineWithFilePath(updaterFile.GetFilename()));
CopyFile(nupkgFile, targetDir.CombineWithFilePath(nupkgFile.GetFilename()));
});
private void ReportRepacking(FilePath target, FilePath log)
{
// ILRepack is extremly noisy by default and has no options to
// make it quieter other than shutting it up completely.
//
using (NormalVerbosity())
{
Information("Repacking {0}, logging details to {1}...",
rootDirectory.GetRelativePath(target),
rootDirectory.GetRelativePath(log));
}
}
private void RepackSilently(ProcessSettings settings)
=> settings.SetRedirectStandardOutput(true)
.SetRedirectedStandardOutputHandler(s => "")
.SetRedirectStandardError(true)
.SetRedirectedStandardErrorHandler(s => "");
Task("Test")
.Description("Run tests after compilation.")
.IsDependentOn("Default")
.IsDependentOn("Test+Only");
Task("Test+Only")
.Description("Run tests without compiling.")
.IsDependentOn("Test-Executables+Only")
.IsDependentOn("Test-UnitTests+Only");
Task("Test-Executables+Only")
.Description("Intermediate - Test executables without compiling.")
.IsDependentOn("Test-CkanExecutable+Only")
.IsDependentOn("Test-NetkanExecutable+Only");
Task("Test-CkanExecutable+Only")
.Description("Intermediate - Test ckan.exe without compiling.")
.Does(() =>
{
var output = RunExecutable(ckanFile, "version").FirstOrDefault();
if (output != string.Format("v{0}", GetVersion()))
{
throw new Exception($"ckan.exe smoke test failed: {output}");
}
});
Task("Test-NetkanExecutable+Only")
.Description("Intermediate - Test netkan.exe without compiling.")
.Does(() =>
{
var output = RunExecutable(netkanFile, "--version").FirstOrDefault();
if (output != string.Format("v{0}", GetVersion()))
{
throw new Exception($"netkan.exe smoke test failed: {output}");
}
});
Task("Test-UnitTests+Only")
.Description("Intermediate - Run unit tests without compiling.")
.Does(() =>
{
var where = Argument<string>("where", null);
var nunitOutputDirectory = buildDirectory.Combine("test")
.Combine("nunit");
CreateDirectory(nunitOutputDirectory);
// Only Mono's msbuild can handle WinForms on Linux,
// but dotnet build can handle multi-targeting on Windows
if (IsRunningOnWindows())
{
DotNetTest(solution, new DotNetTestSettings
{
Configuration = configuration,
NoRestore = true,
NoBuild = true,
NoLogo = true,
Filter = where,
ResultsDirectory = nunitOutputDirectory,
Verbosity = DotNetVerbosity.Minimal,
});
}
else
{
DotNetTest(solution, new DotNetTestSettings
{
Configuration = "NoGUI",
Framework = "net8.0",
NoRestore = true,
NoBuild = true,
NoLogo = true,
Filter = where,
ResultsDirectory = nunitOutputDirectory,
Verbosity = DotNetVerbosity.Minimal,
});
var testFile = outDirectory.Combine("CKAN.Tests")
.Combine(configuration)
.Combine("bin")
.Combine(buildNetFramework)
.CombineWithFilePath("CKAN.Tests.dll");
NUnit3(testFile.FullPath, new NUnit3Settings
{
Configuration = configuration,
Where = where,
Work = nunitOutputDirectory,
NoHeader = true,
// Work around System.Runtime.Remoting.RemotingException : Tcp transport error.
Process = NUnit3ProcessOption.InProcess,
});
}
});
Task("Version")
.Description("Print the current CKAN version.")
.Does(() =>
{
using (NormalVerbosity())
{
Information(GetVersion().ToString());
}
});
Setup(context =>
{
var argConfiguration = Argument<string>("configuration", null);
if (string.Equals(target, "Release", StringComparison.OrdinalIgnoreCase))
{
if (argConfiguration != null)
Warning($"Ignoring configuration argument: '{argConfiguration}'");
configuration = "Release";
}
else if (string.Equals(target, "Debug", StringComparison.OrdinalIgnoreCase))
{
if (argConfiguration != null)
Warning($"Ignoring configuration argument: '{argConfiguration}'");
configuration = "Debug";
}
});
Teardown(context =>
{
var quote = GetQuote(rootDirectory.CombineWithFilePath("quotes.txt"));
if (quote != null)
{
using (NormalVerbosity())
{
Information(quote);
}
}
});
RunTarget(target);
private DirectoryPath FacadesDirectory()
=> IsRunningOnWindows()
? Context.Environment.GetSpecialPath(SpecialPath.ProgramFilesX86)
.Combine("Reference Assemblies")
.Combine("Microsoft")
.Combine("Framework")
.Combine(".NETFramework")
.Combine("v4.8")
.Combine("Facades")
: new DirectoryPath("/usr").Combine("lib")
.Combine("mono")
.Combine("4.8-api")
.Combine("Facades");
private Semver.SemVersion GetVersion(bool withBuild = true)
{
var pattern = new Regex(@"^\s*##\s+v(?<version>\S+)\s?.*$");
var rootDirectory = Context.Environment.WorkingDirectory;
var versionMatch = System.IO.File
.ReadAllLines(rootDirectory.CombineWithFilePath("CHANGELOG.md").FullPath)
.Select(i => pattern.Match(i))
.FirstOrDefault(i => i.Success);
var version = ParseSemVer(versionMatch.Groups["version"].Value);
if (withBuild && DirectoryExists(rootDirectory.Combine(".git")))
{
var commitDate = GitLogTip(rootDirectory).Committer.When;
version = CreateSemVer(version.Major,
version.Minor,
version.Patch,
version.Prerelease,
commitDate.ToString("yy") + commitDate.DayOfYear.ToString("000"));
}
return version;
}
private IEnumerable<string> RunExecutable(FilePath executable, string arguments)
{
IEnumerable<string> output;
var exitCode = StartProcess(
executable,
new ProcessSettings { Arguments = arguments, RedirectStandardOutput = true },
out output
);
if (exitCode == 0)
{
return output;
}
else
{
throw new Exception("Process failed with exit code: " + exitCode);
}
}
private string GetQuote(FilePath file)
{
if (FileExists(file))
{
var quotes = System.IO.File
.ReadAllText(file.FullPath)
.Split(new [] { '%' }, StringSplitOptions.RemoveEmptyEntries);
if (quotes.Length > 0)
return quotes[(new Random()).Next(0, quotes.Length)];
}
return null;
}