forked from dotnet/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProduceContentAssets.cs
298 lines (255 loc) · 11.7 KB
/
ProduceContentAssets.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
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace Microsoft.NET.Build.Tasks
{
/// <summary>
/// Read raised lock file items for content assets and process them to handle
/// preprocessing tokens, identify items that should be copied to output, and
/// other filtering on content assets, including whether they match the active
/// project language.
/// </summary>
public sealed class ProduceContentAssets : TaskBase
{
private const string PPOutputPathKey = "ppOutputPath";
private readonly Dictionary<string, string> _resolvedPaths = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
private readonly List<ITaskItem> _contentItems = new List<ITaskItem>();
private readonly List<ITaskItem> _fileWrites = new List<ITaskItem>();
private readonly List<ITaskItem> _copyLocalItems = new List<ITaskItem>();
private IContentAssetPreprocessor _assetPreprocessor;
#region Output Items
/// <summary>
/// Content items that are marked copy to output with resolved path
/// </summary>
[Output]
public ITaskItem[] CopyLocalItems => _copyLocalItems.ToArray();
/// <summary>
/// All content items produced with content item metadata.
/// </summary>
[Output]
public ITaskItem[] ProcessedContentItems => _contentItems.ToArray();
/// <summary>
/// Files written to during the generation process.
/// </summary>
[Output]
public ITaskItem[] FileWrites => _fileWrites.ToArray();
#endregion
#region Inputs
/// <summary>
/// File Definitions, including metadata for the resolved path.
/// </summary>
[Required]
public ITaskItem[] ContentFileDefinitions { get; set; }
/// <summary>
/// Subset of File Dependencies that are content files, including metadata
/// such as buildAction, ppOutputPath etc.
/// </summary>
[Required]
public ITaskItem[] ContentFileDependencies { get; set; }
/// <summary>
/// Items specifying the tokens that can be substituted into preprocessed
/// content files. The ItemSpec of each item is the name of the token,
/// without the surrounding $$, and the Value metadata should specify the
/// replacement value.
/// </summary>
public ITaskItem[] ContentPreprocessorValues
{
get; set;
}
/// <summary>
/// The base output directory where the temporary, preprocessed files should be written to.
/// </summary>
public string ContentPreprocessorOutputDirectory
{
get; set;
}
/// <summary>
/// Optional the Project Language (E.g. C#, VB)
/// </summary>
public string ProjectLanguage
{
get; set;
}
/// <summary>
/// Optionally filter the operation of this task to just preprocessor files
/// </summary>
public bool ProduceOnlyPreprocessorFiles
{
get; set;
}
#endregion
public ProduceContentAssets()
{
}
#region Test Support
internal ProduceContentAssets(IContentAssetPreprocessor assetPreprocessor)
: this()
{
_assetPreprocessor = assetPreprocessor;
}
#endregion
/// <summary>
/// Resource for reading, processing and writing content assets
/// </summary>
internal IContentAssetPreprocessor AssetPreprocessor
{
get
{
if (_assetPreprocessor == null)
{
_assetPreprocessor = new NugetContentAssetPreprocessor();
}
return _assetPreprocessor;
}
}
protected override void ExecuteCore()
{
MapContentFileDefinitions();
ProcessContentFileInputs();
}
private void MapContentFileDefinitions()
{
foreach (var item in ContentFileDefinitions ?? Enumerable.Empty<ITaskItem>())
{
_resolvedPaths.Add(item.ItemSpec, item.GetMetadata(MetadataKeys.ResolvedPath));
}
}
private void ProcessContentFileInputs()
{
var preprocessorValues = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
// If a preprocessor directory isn't set, then we won't have a place to generate.
if (!string.IsNullOrEmpty(ContentPreprocessorOutputDirectory))
{
// Assemble the preprocessor values up-front
var duplicatedPreprocessorKeys = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
foreach (var preprocessorValueItem in ContentPreprocessorValues ?? Enumerable.Empty<ITaskItem>())
{
if (preprocessorValues.ContainsKey(preprocessorValueItem.ItemSpec))
{
duplicatedPreprocessorKeys.Add(preprocessorValueItem.ItemSpec);
}
preprocessorValues[preprocessorValueItem.ItemSpec] = preprocessorValueItem.GetMetadata("Value");
}
foreach (var duplicatedPreprocessorKey in duplicatedPreprocessorKeys)
{
Log.LogWarning(Strings.DuplicatePreprocessorToken, duplicatedPreprocessorKey, preprocessorValues[duplicatedPreprocessorKey]);
}
AssetPreprocessor.ConfigurePreprocessor(ContentPreprocessorOutputDirectory, preprocessorValues);
}
var contentFileDeps = ContentFileDependencies ?? Enumerable.Empty<ITaskItem>();
var contentFileGroups = contentFileDeps
.Where(f => !ProduceOnlyPreprocessorFiles || IsPreprocessorFile(f))
.GroupBy(t => t.GetMetadata(MetadataKeys.ParentPackage));
foreach (var grouping in contentFileGroups)
{
// Is there an asset with our exact language? If so, we use that. Otherwise we'll simply collect "any" assets.
string codeLanguageToSelect;
if (string.IsNullOrEmpty(ProjectLanguage))
{
codeLanguageToSelect = "any";
}
else
{
string projectLanguage = NuGetUtils.GetLockFileLanguageName(ProjectLanguage);
if (grouping.Any(t => t.GetMetadata("codeLanguage") == projectLanguage))
{
codeLanguageToSelect = projectLanguage;
}
else
{
codeLanguageToSelect = "any";
}
}
foreach (var contentFile in grouping)
{
// Ignore magic _._ placeholder files. We couldn't ignore them during the project language
// selection, since you could imagine somebody might have a package that puts assets under
// "any" but then uses _._ to opt some languages out of it
if (NuGetUtils.IsPlaceholderFile(contentFile.ItemSpec))
{
continue;
}
if (contentFile.GetMetadata("codeLanguage") == codeLanguageToSelect)
{
ProduceContentAsset(contentFile);
}
}
}
}
private bool IsPreprocessorFile(ITaskItem contentFile) =>
!string.IsNullOrEmpty(contentFile.GetMetadata(PPOutputPathKey));
private void ProduceContentAsset(ITaskItem contentFile)
{
string resolvedPath;
if (!_resolvedPaths.TryGetValue(contentFile.ItemSpec, out resolvedPath))
{
Log.LogWarning(Strings.UnableToFindResolvedPath, contentFile.ItemSpec);
return;
}
string pathToFinalAsset = resolvedPath;
string ppOutputPath = contentFile.GetMetadata(PPOutputPathKey);
string parentPackage = contentFile.GetMetadata(MetadataKeys.ParentPackage);
string[] parts = parentPackage?.Split('/');
if (parts == null || parts.Length != 2)
{
throw new BuildErrorException(Strings.ContentFileDoesNotContainExpectedParentPackageInformation, contentFile.ItemSpec);
}
string packageName = parts[0];
string packageVersion = parts[1];
if (!string.IsNullOrEmpty(ppOutputPath))
{
if (string.IsNullOrEmpty(ContentPreprocessorOutputDirectory))
{
throw new BuildErrorException(Strings.ContentPreproccessorParameterRequired, nameof(ProduceContentAssets), nameof(ContentPreprocessorOutputDirectory));
}
// We need the preprocessed output, so let's run the preprocessor here
string relativeOutputPath = Path.Combine(packageName, packageVersion, ppOutputPath);
if (AssetPreprocessor.Process(resolvedPath, relativeOutputPath, out pathToFinalAsset))
{
_fileWrites.Add(new TaskItem(pathToFinalAsset));
}
}
if (contentFile.GetBooleanMetadata("copyToOutput") == true)
{
string outputPath = contentFile.GetMetadata("outputPath");
outputPath = string.IsNullOrEmpty(outputPath) ? ppOutputPath : outputPath;
if (!string.IsNullOrEmpty(outputPath))
{
var item = new TaskItem(pathToFinalAsset);
item.SetMetadata("TargetPath", outputPath);
item.SetMetadata(MetadataKeys.ParentPackage, parentPackage);
item.SetMetadata(MetadataKeys.PackageName, packageName);
item.SetMetadata(MetadataKeys.PackageVersion, packageVersion);
_copyLocalItems.Add(item);
}
else
{
Log.LogWarning(Strings.ContentItemDoesNotProvideOutputPath, pathToFinalAsset, "copyToOutput", "outputPath", PPOutputPathKey);
}
}
// TODO if build action is none do we even need to write the processed file above?
string buildAction = contentFile.GetMetadata("buildAction");
if (!string.Equals(buildAction, "none", StringComparison.OrdinalIgnoreCase))
{
var item = new TaskItem(pathToFinalAsset);
item.SetMetadata(MetadataKeys.ParentPackage, parentPackage);
item.SetMetadata(MetadataKeys.PackageName, packageName);
item.SetMetadata(MetadataKeys.PackageVersion, packageVersion);
// We'll put additional metadata on the item so we can convert it back to the real item group in our targets
item.SetMetadata("ProcessedItemType", buildAction);
// TODO is this needed for .NETCore?
// If this is XAML, the build targets expect Link metadata to construct the relative path
if (string.Equals(buildAction, "Page", StringComparison.OrdinalIgnoreCase))
{
item.SetMetadata("Link", Path.Combine("NuGet", parentPackage, Path.GetFileName(resolvedPath)));
}
_contentItems.Add(item);
}
}
}
}