-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtools-tasks.cake
433 lines (334 loc) · 17.9 KB
/
tools-tasks.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
#l "tools-variables.cake"
using System.Xml.Linq;
//-------------------------------------------------------------
public class ToolsProcessor : ProcessorBase
{
public ToolsProcessor(BuildContext buildContext)
: base(buildContext)
{
}
private void EnsureChocolateyLicenseFile(string projectName)
{
// Required for Chocolatey
var projectDirectory = GetProjectDirectory(projectName);
var outputDirectory = GetProjectOutputDirectory(BuildContext, projectName);
var legalDirectory = System.IO.Path.Combine(outputDirectory, "legal");
System.IO.Directory.CreateDirectory(legalDirectory);
// Check if it already exists
var fileName = System.IO.Path.Combine(legalDirectory, "LICENSE.txt");
if (!CakeContext.FileExists(fileName))
{
CakeContext.Information("Creating Chocolatey license file for '{0}'", projectName);
// Option 1: Copy from root
var sourceFile = System.IO.Path.Combine(".", "LICENSE");
if (CakeContext.FileExists(sourceFile))
{
CakeContext.Information("Using license file from repository");
CakeContext.CopyFile(sourceFile, fileName);
return;
}
// Option 2: use expression (PackageLicenseExpression)
throw new Exception("Cannot find ./LICENSE, which is required for Chocolatey");
}
}
private void EnsureChocolateyVerificationFile(string projectName)
{
// Required for Chocolatey
var projectDirectory = GetProjectDirectory(projectName);
var outputDirectory = GetProjectOutputDirectory(BuildContext, projectName);
var legalDirectory = System.IO.Path.Combine(outputDirectory, "legal");
System.IO.Directory.CreateDirectory(legalDirectory);
// Check if it already exists
var fileName = System.IO.Path.Combine(legalDirectory, "VERIFICATION.txt");
if (!CakeContext.FileExists(fileName))
{
CakeContext.Information("Creating Chocolatey verification file for '{0}'", projectName);
var verificationBuilder = new StringBuilder(@"VERIFICATION
Verification is intended to assist the Chocolatey moderators and community
in verifying that this package's contents are trustworthy.
This package is submitted by the software vendor - checksum verification is optional but still included.
SHA512 CHECKSUMS GENERATED BY BUILD TOOL:
");
verificationBuilder.AppendLine();
var files = new List<FilePath>();
var exePattern = $"{outputDirectory}/**/*.exe";
files.AddRange(CakeContext.GetFiles(exePattern));
var dllPattern = $"{outputDirectory}/**/*.dll";
files.AddRange(CakeContext.GetFiles(dllPattern));
var outputDirectoryPath = new DirectoryPath(outputDirectory);
foreach (var packageFile in files/*.OrderBy(x => x.FullPath)*/)
{
var relativeFileName = outputDirectoryPath.GetRelativePath(packageFile);
// Using 'outputDirectory' results in a file like '[ProductName]/netcoreapp3.1/nl/Catel.MVVM.resources.dll',
// so trying to fix the directory to be relative to the output including the target framework by faking
// that the file is 2 directories up
var fixedRelativePathSegments = relativeFileName.Segments.Skip(1).ToArray();
var fixedRelativePath = new FilePath(System.IO.Path.Combine(fixedRelativePathSegments));
var fileHash = CakeContext.CalculateFileHash(packageFile, HashAlgorithm.SHA512);
verificationBuilder.AppendLine($"* tools/{fixedRelativePath.FullPath} | {fileHash.ToHex()}");
}
System.IO.File.WriteAllText(fileName, verificationBuilder.ToString());
}
}
private string GetToolsNuGetRepositoryUrls(string projectName)
{
// Allow per project overrides via "NuGetRepositoryUrlFor[ProjectName]"
return GetProjectSpecificConfigurationValue(BuildContext, projectName, "ToolsNuGetRepositoryUrlsFor", BuildContext.Tools.NuGetRepositoryUrls);
}
private string GetToolsNuGetRepositoryApiKeys(string projectName)
{
// Allow per project overrides via "NuGetRepositoryApiKeyFor[ProjectName]"
return GetProjectSpecificConfigurationValue(BuildContext, projectName, "ToolsNuGetRepositoryApiKeysFor", BuildContext.Tools.NuGetRepositoryApiKeys);
}
public override bool HasItems()
{
return BuildContext.Tools.Items.Count > 0;
}
public override async Task PrepareAsync()
{
if (!HasItems())
{
return;
}
// Check whether projects should be processed, `.ToList()`
// is required to prevent issues with foreach
foreach (var tool in BuildContext.Tools.Items.ToList())
{
if (!ShouldProcessProject(BuildContext, tool))
{
BuildContext.Tools.Items.Remove(tool);
}
}
if (BuildContext.General.IsLocalBuild && BuildContext.General.Target.ToLower().Contains("packagelocal"))
{
foreach (var tool in BuildContext.Tools.Items)
{
var expandableCacheDirectory = System.IO.Path.Combine("%userprofile%", ".nuget", "packages", tool, BuildContext.General.Version.NuGet);
var cacheDirectory = Environment.ExpandEnvironmentVariables(expandableCacheDirectory);
CakeContext.Information("Checking for existing local NuGet cached version at '{0}'", cacheDirectory);
var retryCount = 3;
while (retryCount > 0)
{
if (!CakeContext.DirectoryExists(cacheDirectory))
{
break;
}
CakeContext.Information("Deleting already existing NuGet cached version from '{0}'", cacheDirectory);
CakeContext.DeleteDirectory(cacheDirectory, new DeleteDirectorySettings()
{
Force = true,
Recursive = true
});
await System.Threading.Tasks.Task.Delay(1000);
retryCount--;
}
}
}
}
public override async Task UpdateInfoAsync()
{
if (!HasItems())
{
return;
}
foreach (var tool in BuildContext.Tools.Items)
{
CakeContext.Information("Updating version for tool '{0}'", tool);
var projectFileName = GetProjectFileName(BuildContext, tool);
CakeContext.TransformConfig(projectFileName, new TransformationCollection
{
{ "Project/PropertyGroup/PackageVersion", BuildContext.General.Version.NuGet }
});
}
}
public override async Task BuildAsync()
{
if (!HasItems())
{
return;
}
foreach (var tool in BuildContext.Tools.Items)
{
BuildContext.CakeContext.LogSeparator("Building tool '{0}'", tool);
var projectFileName = GetProjectFileName(BuildContext, tool);
var msBuildSettings = new MSBuildSettings {
Verbosity = Verbosity.Quiet,
//Verbosity = Verbosity.Diagnostic,
ToolVersion = MSBuildToolVersion.Default,
Configuration = BuildContext.General.Solution.ConfigurationName,
MSBuildPlatform = MSBuildPlatform.x86, // Always require x86, see platform for actual target platform
PlatformTarget = PlatformTarget.MSIL
};
ConfigureMsBuild(BuildContext, msBuildSettings, tool, "build");
// SourceLink specific stuff
var repositoryUrl = BuildContext.General.Repository.Url;
var repositoryCommitId = BuildContext.General.Repository.CommitId;
if (!BuildContext.General.SourceLink.IsDisabled &&
!BuildContext.General.IsLocalBuild &&
!string.IsNullOrWhiteSpace(repositoryUrl))
{
CakeContext.Information("Repository url is specified, enabling SourceLink to commit '{0}/commit/{1}'",
repositoryUrl, repositoryCommitId);
// TODO: For now we are assuming everything is git, we might need to change that in the future
// See why we set the values at https://github.com/dotnet/sourcelink/issues/159#issuecomment-427639278
msBuildSettings.WithProperty("EnableSourceLink", "true");
msBuildSettings.WithProperty("EnableSourceControlManagerQueries", "false");
msBuildSettings.WithProperty("PublishRepositoryUrl", "true");
msBuildSettings.WithProperty("RepositoryType", "git");
msBuildSettings.WithProperty("RepositoryUrl", repositoryUrl);
msBuildSettings.WithProperty("RevisionId", repositoryCommitId);
InjectSourceLinkInProjectFile(BuildContext, tool, projectFileName);
}
RunMsBuild(BuildContext, tool, projectFileName, msBuildSettings, "build");
}
}
public override async Task PackageAsync()
{
if (!HasItems())
{
return;
}
var configurationName = BuildContext.General.Solution.ConfigurationName;
var version = BuildContext.General.Version.NuGet;
foreach (var tool in BuildContext.Tools.Items)
{
if (!ShouldPackageProject(BuildContext, tool))
{
CakeContext.Information("Tool '{0}' should not be packaged", tool);
continue;
}
BuildContext.CakeContext.LogSeparator("Packaging tool '{0}'", tool);
var projectDirectory = System.IO.Path.Combine(".", "src", tool);
var projectFileName = System.IO.Path.Combine(projectDirectory, $"{tool}.csproj");
var outputDirectory = GetProjectOutputDirectory(BuildContext, tool);
CakeContext.Information("Output directory: '{0}'", outputDirectory);
// Step 1: remove intermediate files to ensure we have the same results on the build server, somehow NuGet
// targets tries to find the resource assemblies in [ProjectName]\obj\Release\net46\de\[ProjectName].resources.dll',
// we won't run a clean on the project since it will clean out the actual output (which we still need for packaging)
CakeContext.Information("Cleaning intermediate files for tool '{0}'", tool);
var binFolderPattern = string.Format("{0}/bin/{1}/**.dll", projectDirectory, configurationName);
CakeContext.Information("Deleting 'bin' directory contents using '{0}'", binFolderPattern);
var binFiles = CakeContext.GetFiles(binFolderPattern);
CakeContext.DeleteFiles(binFiles);
var objFolderPattern = string.Format("{0}/obj/{1}/**.dll", projectDirectory, configurationName);
CakeContext.Information("Deleting 'bin' directory contents using '{0}'", objFolderPattern);
var objFiles = CakeContext.GetFiles(objFolderPattern);
CakeContext.DeleteFiles(objFiles);
// We know we *highly likely* need to sign, so try doing this upfront
if (BuildContext.General.CodeSign.IsAvailable ||
BuildContext.General.AzureCodeSign.IsAvailable)
{
SignFilesInDirectory(BuildContext, outputDirectory, string.Empty);
}
else
{
BuildContext.CakeContext.Warning("No signing certificate subject name provided, not signing any files");
}
CakeContext.Information(string.Empty);
// Step 2: Ensure chocolatey stuff
EnsureChocolateyLicenseFile(tool);
EnsureChocolateyVerificationFile(tool);
// Step 3: Go packaging!
CakeContext.Information("Using 'msbuild' to package '{0}'", tool);
var msBuildSettings = new MSBuildSettings
{
Verbosity = Verbosity.Quiet,
//Verbosity = Verbosity.Diagnostic,
ToolVersion = MSBuildToolVersion.Default,
Configuration = configurationName,
MSBuildPlatform = MSBuildPlatform.x86, // Always require x86, see platform for actual target platform
PlatformTarget = PlatformTarget.MSIL
};
ConfigureMsBuild(BuildContext, msBuildSettings, tool, "pack");
msBuildSettings.WithProperty("ConfigurationName", configurationName);
msBuildSettings.WithProperty("PackageVersion", version);
// SourceLink specific stuff
var repositoryUrl = BuildContext.General.Repository.Url;
var repositoryCommitId = BuildContext.General.Repository.CommitId;
if (!BuildContext.General.SourceLink.IsDisabled &&
!BuildContext.General.IsLocalBuild &&
!string.IsNullOrWhiteSpace(repositoryUrl))
{
CakeContext.Information("Repository url is specified, adding commit specific data to package");
// TODO: For now we are assuming everything is git, we might need to change that in the future
// See why we set the values at https://github.com/dotnet/sourcelink/issues/159#issuecomment-427639278
msBuildSettings.WithProperty("PublishRepositoryUrl", "true");
msBuildSettings.WithProperty("RepositoryType", "git");
msBuildSettings.WithProperty("RepositoryUrl", repositoryUrl);
msBuildSettings.WithProperty("RevisionId", repositoryCommitId);
}
// Fix for .NET Core 3.0, see https://github.com/dotnet/core-sdk/issues/192, it
// uses obj/release instead of [outputdirectory]
msBuildSettings.WithProperty("DotNetPackIntermediateOutputPath", outputDirectory);
// No dependencies for tools
msBuildSettings.WithProperty("SuppressDependenciesWhenPacking", "true");
// As described in the this issue: https://github.com/NuGet/Home/issues/4360
// we should not use IsTool, but set BuildOutputTargetFolder instead
msBuildSettings.WithProperty("CopyLocalLockFileAssemblies", "true");
msBuildSettings.WithProperty("IncludeBuildOutput", "true");
msBuildSettings.WithProperty("BuildOutputTargetFolder", "tools");
msBuildSettings.WithProperty("NoDefaultExcludes", "true");
// Ensures that files are written to "tools", not "tools\\netcoreapp3.1"
msBuildSettings.WithProperty("IsTool", "false");
msBuildSettings.WithProperty("NoBuild", "true");
msBuildSettings.Targets.Add("Pack");
RunMsBuild(BuildContext, tool, projectFileName, msBuildSettings, "pack");
BuildContext.CakeContext.LogSeparator();
}
await SignNuGetPackageAsync();
}
public override async Task DeployAsync()
{
if (!HasItems())
{
return;
}
var version = BuildContext.General.Version.NuGet;
foreach (var tool in BuildContext.Tools.Items)
{
if (!ShouldDeployProject(BuildContext, tool))
{
CakeContext.Information("Tool '{0}' should not be deployed", tool);
continue;
}
BuildContext.CakeContext.LogSeparator("Deploying tool '{0}'", tool);
var packageToPush = System.IO.Path.Combine(BuildContext.General.OutputRootDirectory, $"{tool}.{version}.nupkg");
var nuGetRepositoryUrls = GetToolsNuGetRepositoryUrls(tool);
var nuGetRepositoryApiKeys = GetToolsNuGetRepositoryApiKeys(tool);
var nuGetServers = GetNuGetServers(nuGetRepositoryUrls, nuGetRepositoryApiKeys);
if (nuGetServers.Count == 0)
{
throw new Exception("No NuGet repositories specified, as a protection mechanism this must *always* be specified to make sure packages aren't accidentally deployed to the default public NuGet feed");
}
CakeContext.Information("Found '{0}' target NuGet servers to push tool '{1}'", nuGetServers.Count, tool);
foreach (var nuGetServer in nuGetServers)
{
CakeContext.Information("Pushing to '{0}'", nuGetServer);
CakeContext.NuGetPush(packageToPush, new NuGetPushSettings
{
Source = nuGetServer.Url,
ApiKey = nuGetServer.ApiKey
});
}
await BuildContext.Notifications.NotifyAsync(tool, string.Format("Deployed to NuGet store(s)"), TargetType.Tool);
}
}
public override async Task FinalizeAsync()
{
}
private async Task SignNuGetPackageAsync()
{
if (BuildContext.General.IsCiBuild ||
BuildContext.General.IsLocalBuild)
{
return;
}
// For details, see https://docs.microsoft.com/en-us/nuget/create-packages/sign-a-package
// nuget sign MyPackage.nupkg -CertificateSubjectName <MyCertSubjectName> -Timestamper <TimestampServiceURL>
var filesToSign = CakeContext.GetFiles($"{BuildContext.General.OutputRootDirectory}/*.nupkg");
foreach (var fileToSign in filesToSign)
{
SignNuGetPackage(BuildContext, fileToSign.FullPath);
}
}
}