forked from dotnet/msbuild
/
ItemGroupIntrinsicTask.cs
713 lines (629 loc) · 33.2 KB
/
ItemGroupIntrinsicTask.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
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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using Microsoft.Build.Collections;
using ElementLocation = Microsoft.Build.Construction.ElementLocation;
using Microsoft.Build.Execution;
using Microsoft.Build.Framework;
using Microsoft.Build.Shared;
using Microsoft.Build.Evaluation;
using Microsoft.Build.Shared.FileSystem;
using ProjectItemInstanceFactory = Microsoft.Build.Execution.ProjectItemInstance.TaskItem.ProjectItemInstanceFactory;
using EngineFileUtilities = Microsoft.Build.Internal.EngineFileUtilities;
using TargetLoggingContext = Microsoft.Build.BackEnd.Logging.TargetLoggingContext;
namespace Microsoft.Build.BackEnd
{
/// <summary>
/// Implementation of the ItemGroup intrinsic task
/// </summary>
internal class ItemGroupIntrinsicTask : IntrinsicTask
{
/// <summary>
/// The task instance data
/// </summary>
private ProjectItemGroupTaskInstance _taskInstance;
private EngineFileUtilities _engineFileUtilities;
/// <summary>
/// Instantiates an ItemGroup task
/// </summary>
/// <param name="taskInstance">The original task instance data</param>
/// <param name="loggingContext">The logging context</param>
/// <param name="projectInstance">The project instance</param>
/// <param name="logTaskInputs">Flag to determine whether or not to log task inputs.</param>
public ItemGroupIntrinsicTask(ProjectItemGroupTaskInstance taskInstance, TargetLoggingContext loggingContext, ProjectInstance projectInstance, bool logTaskInputs)
: base(loggingContext, projectInstance, logTaskInputs)
{
_taskInstance = taskInstance;
_engineFileUtilities = EngineFileUtilities.Default;
}
/// <summary>
/// Execute an ItemGroup element, including each child item expression
/// </summary>
/// <param name="lookup">The lookup used for evaluation and as a destination for these items.</param>
internal override void ExecuteTask(Lookup lookup)
{
foreach (ProjectItemGroupTaskItemInstance child in _taskInstance.Items)
{
List<ItemBucket> buckets = null;
try
{
List<string> parameterValues = new List<string>();
GetBatchableValuesFromBuildItemGroupChild(parameterValues, child);
buckets = BatchingEngine.PrepareBatchingBuckets(parameterValues, lookup, child.ItemType, _taskInstance.Location);
// "Execute" each bucket
foreach (ItemBucket bucket in buckets)
{
bool condition = ConditionEvaluator.EvaluateCondition
(
child.Condition,
ParserOptions.AllowAll,
bucket.Expander,
ExpanderOptions.ExpandAll,
Project.Directory,
child.ConditionLocation,
LoggingContext.LoggingService,
LoggingContext.BuildEventContext,
FileSystems.Default);
if (condition)
{
HashSet<string> keepMetadata = null;
HashSet<string> removeMetadata = null;
HashSet<string> matchOnMetadata = null;
MatchOnMetadataOptions matchOnMetadataOptions = MatchOnMetadataConstants.MatchOnMetadataOptionsDefaultValue;
if (!String.IsNullOrEmpty(child.KeepMetadata))
{
var keepMetadataEvaluated = bucket.Expander.ExpandIntoStringListLeaveEscaped(child.KeepMetadata, ExpanderOptions.ExpandAll, child.KeepMetadataLocation).ToList();
if (keepMetadataEvaluated.Count > 0)
{
keepMetadata = new HashSet<string>(keepMetadataEvaluated);
}
}
if (!String.IsNullOrEmpty(child.RemoveMetadata))
{
var removeMetadataEvaluated = bucket.Expander.ExpandIntoStringListLeaveEscaped(child.RemoveMetadata, ExpanderOptions.ExpandAll, child.RemoveMetadataLocation).ToList();
if (removeMetadataEvaluated.Count > 0)
{
removeMetadata = new HashSet<string>(removeMetadataEvaluated);
}
}
if (!String.IsNullOrEmpty(child.MatchOnMetadata))
{
var matchOnMetadataEvaluated = bucket.Expander.ExpandIntoStringListLeaveEscaped(child.MatchOnMetadata, ExpanderOptions.ExpandAll, child.MatchOnMetadataLocation).ToList();
if (matchOnMetadataEvaluated.Count > 0)
{
matchOnMetadata = new HashSet<string>(matchOnMetadataEvaluated);
}
Enum.TryParse(child.MatchOnMetadataOptions, out matchOnMetadataOptions);
}
if ((child.Include.Length != 0) ||
(child.Exclude.Length != 0))
{
// It's an item -- we're "adding" items to the world
ExecuteAdd(child, bucket, keepMetadata, removeMetadata);
}
else if (child.Remove.Length != 0)
{
// It's a remove -- we're "removing" items from the world
ExecuteRemove(child, bucket, matchOnMetadata, matchOnMetadataOptions);
}
else
{
// It's a modify -- changing existing items
ExecuteModify(child, bucket, keepMetadata, removeMetadata);
}
}
}
}
finally
{
if (buckets != null)
{
// Propagate the item changes to the bucket above
foreach (ItemBucket bucket in buckets)
{
bucket.LeaveScope();
}
}
}
}
}
/// <summary>
/// Add items to the world. This is the in-target equivalent of an item include expression outside of a target.
/// </summary>
/// <param name="child">The item specification to evaluate and add.</param>
/// <param name="bucket">The batching bucket.</param>
/// <param name="keepMetadata">An <see cref="ISet{String}"/> of metadata names to keep.</param>
/// <param name="removeMetadata">An <see cref="ISet{String}"/> of metadata names to remove.</param>
private void ExecuteAdd(ProjectItemGroupTaskItemInstance child, ItemBucket bucket, ISet<string> keepMetadata, ISet<string> removeMetadata)
{
// First, collect up the appropriate metadata collections. We need the one from the item definition, if any, and
// the one we are using for this batching bucket.
ProjectItemDefinitionInstance itemDefinition;
Project.ItemDefinitions.TryGetValue(child.ItemType, out itemDefinition);
// The NestedMetadataTable will handle the aggregation of the different metadata collections
NestedMetadataTable metadataTable = new NestedMetadataTable(child.ItemType, bucket.Expander.Metadata, itemDefinition);
IMetadataTable originalMetadataTable = bucket.Expander.Metadata;
bucket.Expander.Metadata = metadataTable;
// Second, expand the item include and exclude, and filter existing metadata as appropriate.
List<ProjectItemInstance> itemsToAdd = ExpandItemIntoItems(child, bucket.Expander, keepMetadata, removeMetadata);
// Third, expand the metadata.
foreach (ProjectItemGroupTaskMetadataInstance metadataInstance in child.Metadata)
{
bool condition = ConditionEvaluator.EvaluateCondition
(
metadataInstance.Condition,
ParserOptions.AllowAll,
bucket.Expander,
ExpanderOptions.ExpandAll,
Project.Directory,
metadataInstance.Location,
LoggingContext.LoggingService,
LoggingContext.BuildEventContext,
FileSystems.Default);
if (condition)
{
string evaluatedValue = bucket.Expander.ExpandIntoStringLeaveEscaped(metadataInstance.Value, ExpanderOptions.ExpandAll, metadataInstance.Location);
// This both stores the metadata so we can add it to all the items we just created later, and
// exposes this metadata to further metadata evaluations in subsequent loop iterations.
metadataTable.SetValue(metadataInstance.Name, evaluatedValue);
}
}
// Finally, copy the added metadata onto the new items. The set call is additive.
ProjectItemInstance.SetMetadata(metadataTable.AddedMetadata, itemsToAdd); // Add in one operation for potential copy-on-write
// Restore the original metadata table.
bucket.Expander.Metadata = originalMetadataTable;
// Determine if we should NOT add duplicate entries
bool keepDuplicates = ConditionEvaluator.EvaluateCondition
(
child.KeepDuplicates,
ParserOptions.AllowAll,
bucket.Expander,
ExpanderOptions.ExpandAll,
Project.Directory,
child.KeepDuplicatesLocation,
LoggingContext.LoggingService,
LoggingContext.BuildEventContext,
FileSystems.Default);
if (LogTaskInputs && !LoggingContext.LoggingService.OnlyLogCriticalEvents && itemsToAdd?.Count > 0)
{
var itemGroupText = ItemGroupLoggingHelper.GetParameterText(
ItemGroupLoggingHelper.ItemGroupIncludeLogMessagePrefix,
child.ItemType,
itemsToAdd,
logItemMetadata: true);
LoggingContext.LogCommentFromText(MessageImportance.Low, itemGroupText);
}
// Now add the items we created to the lookup.
bucket.Lookup.AddNewItemsOfItemType(child.ItemType, itemsToAdd, !keepDuplicates); // Add in one operation for potential copy-on-write
}
/// <summary>
/// Remove items from the world. Removes to items that are part of the project manifest are backed up, so
/// they can be reverted when the project is reset after the end of the build.
/// </summary>
/// <param name="child">The item specification to evaluate and remove.</param>
/// <param name="bucket">The batching bucket.</param>
/// <param name="matchOnMetadata">Metadata matching.</param>
/// <param name="matchingOptions">Options matching.</param>
private void ExecuteRemove(ProjectItemGroupTaskItemInstance child, ItemBucket bucket, HashSet<string> matchOnMetadata, MatchOnMetadataOptions matchingOptions)
{
ICollection<ProjectItemInstance> group = bucket.Lookup.GetItems(child.ItemType);
if (group == null)
{
// No items of this type to remove
return;
}
List<ProjectItemInstance> itemsToRemove;
if (matchOnMetadata == null)
{
itemsToRemove = FindItemsMatchingSpecification(group, child.Remove, child.RemoveLocation, bucket.Expander);
}
else
{
itemsToRemove = FindItemsMatchingMetadataSpecification(group, child, bucket.Expander, matchOnMetadata, matchingOptions);
}
if (itemsToRemove != null)
{
if (LogTaskInputs && !LoggingContext.LoggingService.OnlyLogCriticalEvents && itemsToRemove.Count > 0)
{
var itemGroupText = ItemGroupLoggingHelper.GetParameterText(
ItemGroupLoggingHelper.ItemGroupRemoveLogMessage,
child.ItemType,
itemsToRemove,
logItemMetadata: true);
LoggingContext.LogCommentFromText(MessageImportance.Low, itemGroupText);
}
bucket.Lookup.RemoveItems(itemsToRemove);
}
}
/// <summary>
/// Modifies items in the world - specifically, changes their metadata. Changes to items that are part of the project manifest are backed up, so
/// they can be reverted when the project is reset after the end of the build.
/// </summary>
/// <param name="child">The item specification to evaluate and modify.</param>
/// <param name="bucket">The batching bucket.</param>
/// <param name="keepMetadata">An <see cref="ISet{String}"/> of metadata names to keep.</param>
/// <param name="removeMetadata">An <see cref="ISet{String}"/> of metadata names to remove.</param>
private void ExecuteModify(ProjectItemGroupTaskItemInstance child, ItemBucket bucket, ISet<string> keepMetadata, ISet<string> removeMetadata)
{
ICollection<ProjectItemInstance> group = bucket.Lookup.GetItems(child.ItemType);
if (group == null || group.Count == 0)
{
// No items of this type to modify
return;
}
// Figure out what metadata names and values we need to set
var metadataToSet = new Lookup.MetadataModifications(keepMetadata != null);
// Filter the metadata as appropriate
if (keepMetadata != null)
{
foreach (var metadataName in keepMetadata)
{
metadataToSet[metadataName] = Lookup.MetadataModification.CreateFromNoChange();
}
}
else if (removeMetadata != null)
{
foreach (var metadataName in removeMetadata)
{
metadataToSet[metadataName] = Lookup.MetadataModification.CreateFromRemove();
}
}
foreach (ProjectItemGroupTaskMetadataInstance metadataInstance in child.Metadata)
{
bool condition = ConditionEvaluator.EvaluateCondition
(
metadataInstance.Condition,
ParserOptions.AllowAll,
bucket.Expander,
ExpanderOptions.ExpandAll,
Project.Directory,
metadataInstance.ConditionLocation,
LoggingContext.LoggingService,
LoggingContext.BuildEventContext,
FileSystems.Default);
if (condition)
{
string evaluatedValue = bucket.Expander.ExpandIntoStringLeaveEscaped(metadataInstance.Value, ExpanderOptions.ExpandAll, metadataInstance.Location);
metadataToSet[metadataInstance.Name] = Lookup.MetadataModification.CreateFromNewValue(evaluatedValue);
}
}
// Now apply the changes. This must be done after filtering, since explicitly set metadata overrides filters.
bucket.Lookup.ModifyItems(child.ItemType, group, metadataToSet);
}
/// <summary>
/// Adds batchable parameters from an item element into the list. If the item element was a task, these
/// would be its raw parameter values.
/// </summary>
/// <param name="parameterValues">The list of batchable values</param>
/// <param name="child">The item from which to find batchable values</param>
private void GetBatchableValuesFromBuildItemGroupChild(List<string> parameterValues, ProjectItemGroupTaskItemInstance child)
{
AddIfNotEmptyString(parameterValues, child.Include);
AddIfNotEmptyString(parameterValues, child.Exclude);
AddIfNotEmptyString(parameterValues, child.Remove);
AddIfNotEmptyString(parameterValues, child.Condition);
foreach (ProjectItemGroupTaskMetadataInstance metadataElement in child.Metadata)
{
AddIfNotEmptyString(parameterValues, metadataElement.Value);
AddIfNotEmptyString(parameterValues, metadataElement.Condition);
}
}
/// <summary>
/// Takes an item specification, evaluates it and expands it into a list of items
/// </summary>
/// <param name="originalItem">The original item data</param>
/// <param name="expander">The expander to use.</param>
/// <param name="keepMetadata">An <see cref="ISet{String}"/> of metadata names to keep.</param>
/// <param name="removeMetadata">An <see cref="ISet{String}"/> of metadata names to remove.</param>
/// <remarks>
/// This code is very close to that which exists in the Evaluator.EvaluateItemXml method. However, because
/// it invokes type constructors, and those constructors take arguments of fundamentally different types, it has not
/// been refactored.
/// </remarks>
/// <returns>A list of items.</returns>
private List<ProjectItemInstance> ExpandItemIntoItems
(
ProjectItemGroupTaskItemInstance originalItem,
Expander<ProjectPropertyInstance, ProjectItemInstance> expander,
ISet<string> keepMetadata,
ISet<string> removeMetadata
)
{
//todo this is duplicated logic with the item computation logic from evaluation (in LazyIncludeOperation.SelectItems)
ProjectErrorUtilities.VerifyThrowInvalidProject(!(keepMetadata != null && removeMetadata != null), originalItem.KeepMetadataLocation, "KeepAndRemoveMetadataMutuallyExclusive");
List<ProjectItemInstance> items = new List<ProjectItemInstance>();
// Expand properties and metadata in Include
string evaluatedInclude = expander.ExpandIntoStringLeaveEscaped(originalItem.Include, ExpanderOptions.ExpandPropertiesAndMetadata, originalItem.IncludeLocation);
if (evaluatedInclude.Length == 0)
{
return items;
}
// Compute exclude fragments, without expanding wildcards
var excludes = ImmutableList<string>.Empty.ToBuilder();
if (originalItem.Exclude.Length > 0)
{
string evaluatedExclude = expander.ExpandIntoStringLeaveEscaped(originalItem.Exclude, ExpanderOptions.ExpandAll, originalItem.ExcludeLocation);
if (evaluatedExclude.Length > 0)
{
var excludeSplits = ExpressionShredder.SplitSemiColonSeparatedList(evaluatedExclude);
foreach (string excludeSplit in excludeSplits)
{
excludes.Add(excludeSplit);
}
}
}
// Split Include on any semicolons, and take each split in turn
var includeSplits = ExpressionShredder.SplitSemiColonSeparatedList(evaluatedInclude);
ProjectItemInstanceFactory itemFactory = new ProjectItemInstanceFactory(this.Project, originalItem.ItemType);
foreach (string includeSplit in includeSplits)
{
// If expression is "@(x)" copy specified list with its metadata, otherwise just treat as string
bool throwaway;
IList<ProjectItemInstance> itemsFromSplit = expander.ExpandSingleItemVectorExpressionIntoItems(includeSplit,
itemFactory,
ExpanderOptions.ExpandItems,
false /* do not include null expansion results */,
out throwaway,
originalItem.IncludeLocation);
if (itemsFromSplit != null)
{
// Expression is in form "@(X)", so add these items directly.
foreach (ProjectItemInstance item in itemsFromSplit)
{
items.Add(item);
}
}
else
{
// The expression is not of the form "@(X)". Treat as string
// Pass the non wildcard expanded excludes here to fix https://github.com/Microsoft/msbuild/issues/2621
string[] includeSplitFiles = _engineFileUtilities.GetFileListEscaped(
Project.Directory,
includeSplit,
excludes);
foreach (string includeSplitFile in includeSplitFiles)
{
items.Add(new ProjectItemInstance(
Project,
originalItem.ItemType,
includeSplitFile,
includeSplit /* before wildcard expansion */,
null,
null,
originalItem.Location.File));
}
}
}
// Evaluate, split, expand and subtract any Exclude
HashSet<string> excludesUnescapedForComparison = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
foreach (string excludeSplit in excludes)
{
string[] excludeSplitFiles = _engineFileUtilities.GetFileListUnescaped(Project.Directory, excludeSplit);
foreach (string excludeSplitFile in excludeSplitFiles)
{
excludesUnescapedForComparison.Add(excludeSplitFile);
}
}
List<ProjectItemInstance> remainingItems = new List<ProjectItemInstance>();
for (int i = 0; i < items.Count; i++)
{
if (!excludesUnescapedForComparison.Contains(((IItem)items[i]).EvaluatedInclude))
{
remainingItems.Add(items[i]);
}
}
items = remainingItems;
// Filter the metadata as appropriate
if (keepMetadata != null)
{
foreach (var item in items)
{
var metadataToRemove = item.MetadataNames.Where(name => !keepMetadata.Contains(name));
foreach (var metadataName in metadataToRemove)
{
item.RemoveMetadata(metadataName);
}
}
}
else if (removeMetadata != null)
{
foreach (var item in items)
{
var metadataToRemove = item.MetadataNames.Where(name => removeMetadata.Contains(name));
foreach (var metadataName in metadataToRemove)
{
item.RemoveMetadata(metadataName);
}
}
}
return items;
}
/// <summary>
/// Returns a list of all items in the provided item group whose itemspecs match the specification, after it is split and any wildcards are expanded.
/// If no items match, returns null.
/// </summary>
/// <param name="items">The items to match</param>
/// <param name="specification">The specification to match against the items.</param>
/// <param name="specificationLocation">The specification to match against the provided items</param>
/// <param name="expander">The expander to use</param>
/// <returns>A list of matching items</returns>
private List<ProjectItemInstance> FindItemsMatchingSpecification
(
ICollection<ProjectItemInstance> items,
string specification,
ElementLocation specificationLocation,
Expander<ProjectPropertyInstance, ProjectItemInstance> expander
)
{
if (items.Count == 0 || specification.Length == 0)
{
return null;
}
// This is a hashtable whose key is the filename for the individual items
// in the Exclude list, after wildcard expansion.
HashSet<string> specificationsToFind = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
// Split by semicolons
var specificationPieces = expander.ExpandIntoStringListLeaveEscaped(specification, ExpanderOptions.ExpandAll, specificationLocation);
foreach (string piece in specificationPieces)
{
// Take each individual path or file expression, and expand any
// wildcards. Then loop through each file returned, and add it
// to our hashtable.
// Don't unescape wildcards just yet - if there were any escaped, the caller wants to treat them
// as literals. Everything else is safe to unescape at this point, since we're only matching
// against the file system.
string[] fileList = _engineFileUtilities.GetFileListEscaped(Project.Directory, piece);
foreach (string file in fileList)
{
// Now unescape everything, because this is the end of the road for this filename.
// We're just going to compare it to the unescaped include path to filter out the
// file excludes.
specificationsToFind.Add(EscapingUtilities.UnescapeAll(file));
}
}
if (specificationsToFind.Count == 0)
{
return null;
}
// Now loop through our list and filter out any that match a
// filename in the remove list.
List<ProjectItemInstance> itemsRemoved = new List<ProjectItemInstance>();
foreach (ProjectItemInstance item in items)
{
// Even if the case for the excluded files is different, they
// will still get excluded, as expected. However, if the excluded path
// references the same file in a different way, such as by relative
// path instead of absolute path, we will not realize that they refer
// to the same file, and thus we will not exclude it.
if (specificationsToFind.Contains(item.EvaluatedInclude))
{
itemsRemoved.Add(item);
}
}
return itemsRemoved;
}
private List<ProjectItemInstance> FindItemsMatchingMetadataSpecification(
ICollection<ProjectItemInstance> group,
ProjectItemGroupTaskItemInstance child,
Expander<ProjectPropertyInstance, ProjectItemInstance> expander,
HashSet<string> matchOnMetadata,
MatchOnMetadataOptions matchingOptions)
{
ItemSpec<ProjectPropertyInstance, ProjectItemInstance> itemSpec = new ItemSpec<ProjectPropertyInstance, ProjectItemInstance>(child.Remove, expander, child.RemoveLocation, Project.Directory, true);
ProjectFileErrorUtilities.VerifyThrowInvalidProjectFile(
itemSpec.Fragments.All(f => f is ItemSpec<ProjectPropertyInstance, ProjectItemInstance>.ItemExpressionFragment),
new BuildEventFileInfo(string.Empty),
"OM_MatchOnMetadataIsRestrictedToReferencedItems",
child.RemoveLocation,
child.Remove);
MetadataTrie<ProjectPropertyInstance, ProjectItemInstance> metadataSet = new MetadataTrie<ProjectPropertyInstance, ProjectItemInstance>(matchingOptions, matchOnMetadata, itemSpec);
return group.Where(item => metadataSet.Contains(matchOnMetadata.Select(m => item.GetMetadataValue(m)))).ToList();
}
/// <summary>
/// This class is used during ItemGroup intrinsic tasks to resolve metadata references. It consists of three tables:
/// 1. The metadata added during evaluation.
/// 1. The metadata table created for the bucket, may be null.
/// 2. The metadata table derived from the item definition group, may be null.
/// </summary>
private class NestedMetadataTable : IMetadataTable
{
/// <summary>
/// The table for all metadata added during expansion
/// </summary>
private Dictionary<string, string> _addTable;
/// <summary>
/// The table for metadata which was generated for this batch bucket.
/// May be null.
/// </summary>
private IMetadataTable _bucketTable;
/// <summary>
/// The table for metadata from the item definition
/// May be null.
/// </summary>
private IMetadataTable _itemDefinitionTable;
/// <summary>
/// The item type to which this metadata applies.
/// </summary>
private string _itemType;
/// <summary>
/// Creates a new metadata table aggregating the bucket and item definition tables.
/// </summary>
/// <param name="itemType">The type of item for which we are doing evaluation.</param>
/// <param name="bucketTable">The metadata table created for this batch bucket. May be null.</param>
/// <param name="itemDefinitionTable">The metadata table for the item definition representing this item. May be null.</param>
internal NestedMetadataTable(string itemType, IMetadataTable bucketTable, IMetadataTable itemDefinitionTable)
{
_itemType = itemType;
_addTable = new Dictionary<string, string>(MSBuildNameIgnoreCaseComparer.Default);
_bucketTable = bucketTable;
_itemDefinitionTable = itemDefinitionTable;
}
/// <summary>
/// Retrieves the metadata table used to collect additions.
/// </summary>
internal Dictionary<string, string> AddedMetadata
{
get { return _addTable; }
}
#region IMetadataTable Members
// NOTE: Leaving these methods public so as to avoid having to explicitly define them
// through the IMetadataTable interface and then cast everywhere they're used. This class
// is private, so it ultimately doesn't matter.
/// <summary>
/// Gets the specified metadata value. Returns an empty string if none is set.
/// </summary>
public string GetEscapedValue(string name)
{
return GetEscapedValue(null, name);
}
/// <summary>
/// Gets the specified metadata value for the qualified item type. Returns an empty string if none is set.
/// </summary>
public string GetEscapedValue(string specifiedItemType, string name)
{
return GetEscapedValueIfPresent(specifiedItemType, name) ?? String.Empty;
}
/// <summary>
/// Gets the specified metadata value for the qualified item type. Returns null if none is set.
/// </summary>
public string GetEscapedValueIfPresent(string specifiedItemType, string name)
{
string value = null;
if (specifiedItemType == null || specifiedItemType == _itemType)
{
// Look in the addTable
if (_addTable.TryGetValue(name, out value))
{
return value;
}
}
// Look in the bucket table
if (_bucketTable != null)
{
value = _bucketTable.GetEscapedValueIfPresent(specifiedItemType, name);
if (value != null)
{
return value;
}
}
// Look in the item definition table
if (_itemDefinitionTable != null)
{
value = _itemDefinitionTable.GetEscapedValueIfPresent(specifiedItemType, name);
}
return value;
}
#endregion
/// <summary>
/// Sets the metadata value.
/// </summary>
internal void SetValue(string name, string value)
{
_addTable[name] = value;
}
}
}
}