-
-
Notifications
You must be signed in to change notification settings - Fork 345
/
Copy pathProgram.cs
383 lines (355 loc) · 15.4 KB
/
Program.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using Cake.Common;
using Cake.Common.Diagnostics;
using Cake.Common.IO;
using Cake.Common.Solution.Project.Properties;
using Cake.Common.Tools.DotNet;
using Cake.Common.Tools.DotNet.Build;
using Cake.Common.Tools.DotNet.Restore;
using Cake.Common.Tools.DotNet.Test;
using Cake.Common.Tools.ILMerge;
using Cake.Common.Tools.ILRepack;
using Cake.Common.Tools.MSBuild;
using Cake.Common.Tools.NUnit;
using Cake.Core.IO;
using Cake.Frosting;
namespace Build;
public static class Program
{
public static int Main(string[] args)
{
return new CakeHost()
.ConfigureServices(services =>
{
services.UseToolPath(new DirectoryPath(Environment.CurrentDirectory)
.GetParent()
.Combine("_build")
.Combine("tools"));
})
.InstallTool(new Uri("nuget:?package=ILRepack&version=2.0.27"))
.InstallTool(new Uri("nuget:?package=NUnit.ConsoleRunner&version=3.16.3"))
.UseContext<BuildContext>()
.UseLifetime<BuildLifetime>()
.Run(args);
}
}
[TaskName("Default")]
[TaskDescription("Build ckan.exe and netkan.exe")]
[IsDependentOn(typeof(CkanTask))]
[IsDependentOn(typeof(NetkanTask))]
public sealed class DefaultTask : FrostingTask<BuildContext>;
[TaskName("Debug")]
[TaskDescription("Build ckan.exe and netkan.exe in Debug configuration")]
[IsDependentOn(typeof(DefaultTask))]
public sealed class DebugTask : FrostingTask<BuildContext>;
[TaskName("Release")]
[TaskDescription("Build ckan.exe and netkan.exe in Release configuration")]
[IsDependentOn(typeof(DefaultTask))]
public sealed class ReleaseTask : FrostingTask<BuildContext>;
[TaskName("Netkan")]
[TaskDescription("Build only netkan.exe")]
[IsDependentOn(typeof(RepackNetkanTask))]
public sealed class NetkanTask : FrostingTask<BuildContext>;
[TaskName("Ckan")]
[TaskDescription("Build only ckan.exe")]
[IsDependentOn(typeof(RepackCkanTask))]
public sealed class CkanTask : FrostingTask<BuildContext>;
[TaskName("Restore")]
[TaskDescription("Intermediate - Download dependencies")]
public sealed class RestoreTask : FrostingTask<BuildContext>
{
public override void Run(BuildContext context)
{
context.DotNetRestore(new DotNetRestoreSettings
{
WorkingDirectory = context.Paths.RootDirectory,
PackagesDirectory = context.Paths.NugetDirectory,
EnvironmentVariables = new Dictionary<string, string?> { { "Configuration", context.BuildConfiguration } }
});
}
}
[TaskName("Generate-GlobalAssemblyVersionInfo")]
[TaskDescription("Intermediate - Calculate the version strings for the assembly.")]
public sealed class GenerateGlobalAssemblyVersionInfoTask : FrostingTask<BuildContext>
{
public override void Run(BuildContext context)
{
var metaDirectory = context.Paths.BuildDirectory.Combine("meta");
context.CreateDirectory(metaDirectory);
var version = context.GetVersion();
context.CreateAssemblyInfo(
metaDirectory.CombineWithFilePath("GlobalAssemblyVersionInfo.cs"), new AssemblyInfoSettings
{
Version = $"{version.Major}.{version.Minor}",
FileVersion = version.HasMeta
? $"{version.Major}.{version.Minor}.{version.Patch}.{version.Meta}"
: $"{version.Major}.{version.Minor}.{version.Patch}",
InformationalVersion = version.ToString()
});
}
}
[TaskName("Build")]
[TaskDescription("Intermediate - Build everything")]
[IsDependentOn(typeof(RestoreTask))]
[IsDependentOn(typeof(GenerateGlobalAssemblyVersionInfoTask))]
public sealed class BuildTask : FrostingTask<BuildContext>
{
public override void Run(BuildContext context)
{
// dotnet build won't let us compile WinForms on non-Windows,
// fall back to mono
if (context.IsRunningOnWindows())
{
context.DotNetBuild(context.Solution, new DotNetBuildSettings
{
Configuration = context.BuildConfiguration,
NoRestore = true,
});
}
else
{
// Use dotnet to build the Core DLL to get the nupkg
// (only created if all TargetFrameworks are built together)
context.DotNetBuild(context.Paths.CoreProject.FullPath, new DotNetBuildSettings
{
Configuration = context.BuildConfiguration,
});
// Use Mono to build for net48 since dotnet can't use WinForms on Linux
context.MSBuild(context.Solution, new MSBuildSettings
{
Configuration = context.BuildConfiguration,
MaxCpuCount = 0,
Properties = { { "TargetFramework", [context.BuildNetFramework] } },
});
// Use dotnet to build the stuff Mono can't build
context.DotNetBuild(context.Solution, new DotNetBuildSettings
{
Configuration = "NoGUI",
Framework = "net8.0",
});
}
}
}
[TaskName("Repack-Ckan")]
[TaskDescription("Intermediate - Merge all the separate DLLs and EXEs to a single executable.")]
[IsDependentOn(typeof(BuildTask))]
public sealed class RepackCkanTask : FrostingTask<BuildContext>
{
public override void Run(BuildContext context)
{
context.CreateDirectory(context.Paths.RepackDirectory.Combine(context.BuildConfiguration));
var cmdLineBinDirectory = context.Paths.OutDirectory.Combine("CKAN-CmdLine")
.Combine(context.BuildConfiguration)
.Combine("bin")
.Combine(context.BuildNetFramework);
var assemblyPaths = context.GetFiles($"{cmdLineBinDirectory}/*.dll");
assemblyPaths.Add(cmdLineBinDirectory.CombineWithFilePath("CKAN-GUI.exe"));
assemblyPaths.Add(cmdLineBinDirectory.CombineWithFilePath("CKAN-ConsoleUI.exe"));
var cmdlinePath = context.Paths.OutDirectory.Combine("CKAN-CmdLine")
.Combine(context.BuildConfiguration)
.Combine("bin")
.Combine(context.BuildNetFramework);
assemblyPaths.Add(context.GetFiles($"{cmdlinePath}/*/*.resources.dll"));
// Need facade to instantiate types from netstandard2.0 DLLs on Mono
assemblyPaths.Add(context.FacadesDirectory().CombineWithFilePath("netstandard.dll"));
var ckanLogFile = context.Paths.RepackDirectory.Combine(context.BuildConfiguration)
.CombineWithFilePath("ckan.log");
context.ReportRepacking(context.Paths.CkanFile, ckanLogFile);
context.ILRepack(
context.Paths.CkanFile,
cmdLineBinDirectory.CombineWithFilePath("CKAN-CmdLine.exe"),
assemblyPaths,
new ILRepackSettings
{
Libs = [cmdLineBinDirectory],
TargetPlatform = TargetPlatformVersion.v4,
Parallel = true,
Verbose = false,
SetupProcessSettings = BuildContext.RepackSilently,
Log = ckanLogFile.FullPath,
});
var autoupdateBinDirectory = context.Paths.OutDirectory.Combine("CKAN-AutoUpdateHelper")
.Combine(context.BuildConfiguration)
.Combine("bin")
.Combine(context.BuildNetFramework);
var updaterLogFile = context.Paths.RepackDirectory.Combine(context.BuildConfiguration)
.CombineWithFilePath("AutoUpdater.log");
context.ReportRepacking(context.Paths.UpdaterFile, updaterLogFile);
context.ILRepack(
context.Paths.UpdaterFile,
autoupdateBinDirectory.CombineWithFilePath("CKAN-AutoUpdateHelper.exe"),
context.GetFiles($"{autoupdateBinDirectory}/*/*.resources.dll"),
new ILRepackSettings
{
Libs = [autoupdateBinDirectory],
TargetPlatform = TargetPlatformVersion.v4,
Parallel = true,
Verbose = false,
SetupProcessSettings = BuildContext.RepackSilently,
Log = updaterLogFile.FullPath,
});
context.CopyFile(context.Paths.CkanFile,
context.Paths.BuildDirectory.CombineWithFilePath(context.Paths.CkanFile.GetFilename()));
}
}
[TaskName("Repack-Netkan")]
[TaskDescription("Intermediate - Merge all the separate DLLs and EXEs to a single executable.")]
[IsDependentOn(typeof(BuildTask))]
public sealed class RepackNetkanTask : FrostingTask<BuildContext>
{
public override void Run(BuildContext context)
{
context.CreateDirectory(context.Paths.RepackDirectory.Combine(context.BuildConfiguration));
var netkanBinDirectory = context.Paths.OutDirectory.Combine("CKAN-NetKAN")
.Combine(context.BuildConfiguration)
.Combine("bin")
.Combine(context.BuildNetFramework);
var netkanLogFile = context.Paths.RepackDirectory.Combine(context.BuildConfiguration)
.CombineWithFilePath("netkan.log");
var assemblyPaths = context.GetFiles($"{netkanBinDirectory}/*.dll");
// Need facade to instantiate types from netstandard2.0 DLLs on Mono
assemblyPaths.Add(context.FacadesDirectory().CombineWithFilePath("netstandard.dll"));
context.ReportRepacking(context.Paths.NetkanFile, netkanLogFile);
context.ILRepack(
context.Paths.NetkanFile,
netkanBinDirectory.CombineWithFilePath("CKAN-NetKAN.exe"),
assemblyPaths,
new ILRepackSettings
{
Libs = [netkanBinDirectory],
TargetPlatform = TargetPlatformVersion.v4,
Parallel = true,
Verbose = false,
SetupProcessSettings = BuildContext.RepackSilently,
Log = netkanLogFile.FullPath,
}
);
context.CopyFile(context.Paths.NetkanFile,
context.Paths.BuildDirectory.CombineWithFilePath(context.Paths.NetkanFile.GetFilename()));
}
}
[TaskName("Prepare-SignPath")]
[TaskDescription("Create a folder with all artifacts to be signed")]
[IsDependentOn(typeof(RepackCkanTask))]
public sealed class PrepareSignPathTask : FrostingTask<BuildContext>
{
public override void Run(BuildContext context)
{
var targetDir = context.Paths.BuildDirectory.Combine("signpath")
.Combine(context.BuildConfiguration);
context.CreateDirectory(targetDir);
context.CopyFile(context.Paths.CkanFile, targetDir.CombineWithFilePath(context.Paths.CkanFile.GetFilename()));
context.CopyFile(context.Paths.UpdaterFile, targetDir.CombineWithFilePath(context.Paths.UpdaterFile.GetFilename()));
context.CopyFile(context.Paths.NupkgFile, targetDir.CombineWithFilePath(context.Paths.NupkgFile.GetFilename()));
}
}
[TaskName("Test")]
[TaskDescription("Run tests after compilation.")]
[IsDependentOn(typeof(DefaultTask))]
[IsDependentOn(typeof(TestOnlyTask))]
public sealed class TestTask : FrostingTask<BuildContext>;
[TaskName("Test+Only")]
[TaskDescription("Run tests without compiling.")]
[IsDependentOn(typeof(TestExecutablesOnlyTask))]
[IsDependentOn(typeof(TestUnitTestsOnlyTask))]
public sealed class TestOnlyTask : FrostingTask<BuildContext>;
[TaskName("Test-Executables+Only")]
[TaskDescription("Intermediate - Test executables without compiling.")]
[IsDependentOn(typeof(TestCkanExecutableOnlyTask))]
[IsDependentOn(typeof(TestNetkanExecutableOnlyTask))]
public sealed class TestExecutablesOnlyTask : FrostingTask<BuildContext>;
[TaskName("Test-CkanExecutable+Only")]
[TaskDescription("Intermediate - Test ckan.exe without compiling.")]
public sealed class TestCkanExecutableOnlyTask : FrostingTask<BuildContext>
{
public override void Run(BuildContext context)
{
var output = context.RunExecutable(context.Paths.CkanFile, "version").FirstOrDefault();
if (output != $"v{context.GetVersion()}")
{
throw new Exception($"ckan.exe smoke test failed: {output}");
}
}
}
[TaskName("Test-NetkanExecutable+Only")]
[TaskDescription("Intermediate - Test netkan.exe without compiling.")]
public sealed class TestNetkanExecutableOnlyTask : FrostingTask<BuildContext>
{
public override void Run(BuildContext context)
{
var output = context.RunExecutable(context.Paths.NetkanFile, "--version").FirstOrDefault();
if (output != $"v{context.GetVersion()}")
{
throw new Exception($"netkan.exe smoke test failed: {output}");
}
}
}
[TaskName("Test-UnitTests+Only")]
[TaskDescription("Intermediate - Run unit tests without compiling.")]
public sealed class TestUnitTestsOnlyTask : FrostingTask<BuildContext>
{
public override void Run(BuildContext context)
{
var where = context.Argument<string?>("where", null);
var nunitOutputDirectory = context.Paths.BuildDirectory.Combine("test")
.Combine("nunit");
context.CreateDirectory(nunitOutputDirectory);
// Only Mono's msbuild can handle WinForms on Linux,
// but dotnet build can handle multi-targeting on Windows
if (context.IsRunningOnWindows())
{
context.DotNetTest(context.Solution, new DotNetTestSettings
{
Configuration = context.BuildConfiguration,
NoRestore = true,
NoBuild = true,
NoLogo = true,
Filter = where,
ResultsDirectory = nunitOutputDirectory,
Verbosity = DotNetVerbosity.Minimal,
});
}
else
{
context.DotNetTest(context.Solution, new DotNetTestSettings
{
Configuration = "NoGUI",
Framework = "net8.0",
NoRestore = true,
NoBuild = true,
NoLogo = true,
Filter = where,
ResultsDirectory = nunitOutputDirectory,
Verbosity = DotNetVerbosity.Minimal,
});
var testFile = context.Paths.OutDirectory.Combine("CKAN.Tests")
.Combine(context.BuildConfiguration)
.Combine("bin")
.Combine(context.BuildNetFramework)
.CombineWithFilePath("CKAN.Tests.dll");
context.NUnit3(testFile.FullPath, new NUnit3Settings
{
Configuration = context.BuildConfiguration,
Where = where,
Work = nunitOutputDirectory,
NoHeader = true,
// Work around System.Runtime.Remoting.RemotingException : Tcp transport error.
Process = NUnit3ProcessOption.InProcess,
});
}
}
}
[TaskName("Version")]
[TaskDescription("Print the current CKAN version.")]
public sealed class VersionTask : FrostingTask<BuildContext>
{
public override void Run(BuildContext context)
{
using (context.NormalVerbosity())
{
context.Information(context.GetVersion().ToString());
}
}
}