-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcomponents-tasks.cake
379 lines (301 loc) · 16.3 KB
/
components-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
#l "components-variables.cake"
using System.Xml.Linq;
//-------------------------------------------------------------
public class ComponentsProcessor : ProcessorBase
{
public ComponentsProcessor(BuildContext buildContext)
: base(buildContext)
{
}
public override bool HasItems()
{
return BuildContext.Components.Items.Count > 0;
}
private string GetComponentNuGetRepositoryUrl(string projectName)
{
// Allow per project overrides via "NuGetRepositoryUrlFor[ProjectName]"
return GetProjectSpecificConfigurationValue(BuildContext, projectName, "NuGetRepositoryUrlFor", BuildContext.Components.NuGetRepositoryUrl);
}
private string GetComponentNuGetRepositoryApiKey(string projectName)
{
// Allow per project overrides via "NuGetRepositoryApiKeyFor[ProjectName]"
return GetProjectSpecificConfigurationValue(BuildContext, projectName, "NuGetRepositoryApiKeyFor", BuildContext.Components.NuGetRepositoryApiKey);
}
public override async Task PrepareAsync()
{
if (!HasItems())
{
return;
}
// Check whether projects should be processed, `.ToList()`
// is required to prevent issues with foreach
foreach (var component in BuildContext.Components.Items.ToList())
{
if (!ShouldProcessProject(BuildContext, component))
{
BuildContext.Components.Items.Remove(component);
}
}
if (BuildContext.General.IsLocalBuild && BuildContext.General.Target.ToLower().Contains("packagelocal"))
{
foreach (var component in BuildContext.Components.Items)
{
var expandableCacheDirectory = System.IO.Path.Combine("%userprofile%", ".nuget", "packages", component, 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 component in BuildContext.Components.Items)
{
CakeContext.Information("Updating version for component '{0}'", component);
var projectFileName = GetProjectFileName(BuildContext, component);
CakeContext.TransformConfig(projectFileName, new TransformationCollection
{
{ "Project/PropertyGroup/PackageVersion", BuildContext.General.Version.NuGet }
});
}
}
public override async Task BuildAsync()
{
if (!HasItems())
{
return;
}
foreach (var component in BuildContext.Components.Items)
{
BuildContext.CakeContext.LogSeparator("Building component '{0}'", component);
var projectFileName = GetProjectFileName(BuildContext, component);
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, component, "build");
// Note: we need to set OverridableOutputPath because we need to be able to respect
// AppendTargetFrameworkToOutputPath which isn't possible for global properties (which
// are properties passed in using the command line)
var outputDirectory = GetProjectOutputDirectory(BuildContext, component);
CakeContext.Information("Output directory: '{0}'", outputDirectory);
msBuildSettings.WithProperty("OverridableOutputPath", outputDirectory);
// SourceLink specific stuff
if (IsSourceLinkSupported(BuildContext, component, projectFileName))
{
var repositoryUrl = BuildContext.General.Repository.Url;
var repositoryCommitId = BuildContext.General.Repository.CommitId;
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, component, projectFileName);
}
RunMsBuild(BuildContext, component, projectFileName, msBuildSettings, "build");
// Specific code signing, requires the following MSBuild properties:
// * CodeSignEnabled
// * CodeSignCommand
//
// This feature is built to allow projects that have post-build copy
// steps (e.g. for assets) to be signed correctly before being embedded
if (ShouldSignImmediately(BuildContext, component))
{
SignProjectFiles(BuildContext, component);
}
}
}
public override async Task PackageAsync()
{
if (!HasItems())
{
return;
}
var configurationName = BuildContext.General.Solution.ConfigurationName;
foreach (var component in BuildContext.Components.Items)
{
// Note: some projects, such as Catel.Fody, require packaging
// of non-deployable projects
if (BuildContext.General.SkipComponentsThatAreNotDeployable &&
!ShouldPackageProject(BuildContext, component))
{
CakeContext.Information("Component '{0}' should not be packaged", component);
continue;
}
// Special exception for Blazor projects
var isBlazorProject = IsBlazorProject(BuildContext, component);
var isPackageContainerProject = IsPackageContainerProject(BuildContext, component);
BuildContext.CakeContext.LogSeparator("Packaging component '{0}'", component);
CakeContext.Information("IsPackageContainerProject = '{0}'", isPackageContainerProject);
var projectDirectory = GetProjectDirectory(component);
var projectFileName = GetProjectFileName(BuildContext, component);
var outputDirectory = GetProjectOutputDirectory(BuildContext, component);
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 component '{0}'", component);
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);
if (!isBlazorProject)
{
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);
}
CakeContext.Information(string.Empty);
// Step 2: Go packaging!
CakeContext.Information("Using 'msbuild' to package '{0}'", component);
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, component, "pack");
// Note: we need to set OverridableOutputPath because we need to be able to respect
// AppendTargetFrameworkToOutputPath which isn't possible for global properties (which
// are properties passed in using the command line)
msBuildSettings.WithProperty("OverridableOutputPath", outputDirectory);
msBuildSettings.WithProperty("ConfigurationName", configurationName);
msBuildSettings.WithProperty("PackageVersion", BuildContext.General.Version.NuGet);
// 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);
}
// Disable Multilingual App Toolkit (MAT) during packaging
msBuildSettings.WithProperty("DisableMAT", "true");
// 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);
var noBuild = true;
if (isBlazorProject)
{
CakeContext.Information("Allowing build and package restore during package phase since this is a Blazor project which requires the 'obj' directory");
// Don't use WithProperty since that will concatenate, and we need to overwrite the
// value here
//msBuildSettings.WithProperty("ResolveNuGetPackages", "true");
msBuildSettings.Properties["ResolveNuGetPackages"] = new List<string>
{
"true"
};
msBuildSettings.Restore = true;
noBuild = false;
}
if (isPackageContainerProject)
{
// In debug / local builds, automatic building of reference projects
// is enabled for convenience. If that is the case, noBuild must be
// set to false, but *only* in debug mode
if (BuildContext.General.IsLocalBuild)
{
noBuild = false;
}
}
// 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("NoDefaultExcludes", "true");
msBuildSettings.WithProperty("NoBuild", noBuild.ToString());
msBuildSettings.Targets.Add("Pack");
RunMsBuild(BuildContext, component, projectFileName, msBuildSettings, "pack");
BuildContext.CakeContext.LogSeparator();
}
await SignNuGetPackageAsync();
}
public override async Task DeployAsync()
{
if (!HasItems())
{
return;
}
foreach (var component in BuildContext.Components.Items)
{
if (!ShouldDeployProject(BuildContext, component))
{
CakeContext.Information("Component '{0}' should not be deployed", component);
continue;
}
BuildContext.CakeContext.LogSeparator("Deploying component '{0}'", component);
var packageToPush = System.IO.Path.Combine(BuildContext.General.OutputRootDirectory, $"{component}.{BuildContext.General.Version.NuGet}.nupkg");
var nuGetRepositoryUrl = GetComponentNuGetRepositoryUrl(component);
var nuGetRepositoryApiKey = GetComponentNuGetRepositoryApiKey(component);
if (string.IsNullOrWhiteSpace(nuGetRepositoryUrl))
{
throw new Exception("NuGet repository is empty, as a protection mechanism this must *always* be specified to make sure packages aren't accidentally deployed to the default public NuGet feed");
}
CakeContext.NuGetPush(packageToPush, new NuGetPushSettings
{
Source = nuGetRepositoryUrl,
ApiKey = nuGetRepositoryApiKey,
ArgumentCustomization = args => args.Append("-SkipDuplicate")
});
await BuildContext.Notifications.NotifyAsync(component, string.Format("Deployed to NuGet store"), TargetType.Component);
}
}
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);
}
}
}