-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathLazyItemEvaluator.cs
715 lines (611 loc) · 33 KB
/
LazyItemEvaluator.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
714
715
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using Microsoft.Build.BackEnd.Logging;
using Microsoft.Build.Collections;
using Microsoft.Build.Construction;
using Microsoft.Build.Evaluation.Context;
using Microsoft.Build.Eventing;
using Microsoft.Build.Framework;
using Microsoft.Build.Shared;
using Microsoft.Build.Shared.FileSystem;
using Microsoft.Build.Utilities;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using System.Threading;
#nullable disable
namespace Microsoft.Build.Evaluation
{
internal partial class LazyItemEvaluator<P, I, M, D>
where P : class, IProperty, IEquatable<P>, IValued
where I : class, IItem<M>, IMetadataTable
where M : class, IMetadatum
where D : class, IItemDefinition<M>
{
private readonly IEvaluatorData<P, I, M, D> _outerEvaluatorData;
private readonly Expander<P, I> _outerExpander;
private readonly IEvaluatorData<P, I, M, D> _evaluatorData;
private readonly Expander<P, I> _expander;
private readonly IItemFactory<I, I> _itemFactory;
private readonly LoggingContext _loggingContext;
private readonly EvaluationProfiler _evaluationProfiler;
private int _nextElementOrder = 0;
private Dictionary<string, LazyItemList> _itemLists = Traits.Instance.EscapeHatches.UseCaseSensitiveItemNames ?
new Dictionary<string, LazyItemList>() :
new Dictionary<string, LazyItemList>(StringComparer.OrdinalIgnoreCase);
protected EvaluationContext EvaluationContext { get; }
protected IFileSystem FileSystem => EvaluationContext.FileSystem;
protected FileMatcher FileMatcher => EvaluationContext.FileMatcher;
public LazyItemEvaluator(IEvaluatorData<P, I, M, D> data, IItemFactory<I, I> itemFactory, LoggingContext loggingContext, EvaluationProfiler evaluationProfiler, EvaluationContext evaluationContext)
{
_outerEvaluatorData = data;
_outerExpander = new Expander<P, I>(_outerEvaluatorData, _outerEvaluatorData, evaluationContext);
_evaluatorData = new EvaluatorData(_outerEvaluatorData, itemType => GetItems(itemType));
_expander = new Expander<P, I>(_evaluatorData, _evaluatorData, evaluationContext);
_itemFactory = itemFactory;
_loggingContext = loggingContext;
_evaluationProfiler = evaluationProfiler;
EvaluationContext = evaluationContext;
}
private ImmutableList<I> GetItems(string itemType)
{
return _itemLists.TryGetValue(itemType, out LazyItemList itemList) ?
itemList.GetMatchedItems(ImmutableHashSet<string>.Empty) :
ImmutableList<I>.Empty;
}
public bool EvaluateConditionWithCurrentState(ProjectElement element, ExpanderOptions expanderOptions, ParserOptions parserOptions)
{
return EvaluateCondition(element.Condition, element, expanderOptions, parserOptions, _expander, this);
}
private static bool EvaluateCondition(
string condition,
ProjectElement element,
ExpanderOptions expanderOptions,
ParserOptions parserOptions,
Expander<P, I> expander,
LazyItemEvaluator<P, I, M, D> lazyEvaluator
)
{
if (condition?.Length == 0)
{
return true;
}
MSBuildEventSource.Log.EvaluateConditionStart(condition);
using (lazyEvaluator._evaluationProfiler.TrackCondition(element.ConditionLocation, condition))
{
bool result = ConditionEvaluator.EvaluateCondition
(
condition,
parserOptions,
expander,
expanderOptions,
GetCurrentDirectoryForConditionEvaluation(element, lazyEvaluator),
element.ConditionLocation,
lazyEvaluator._loggingContext.LoggingService,
lazyEvaluator._loggingContext.BuildEventContext,
lazyEvaluator.FileSystem,
loggingContext: lazyEvaluator._loggingContext
);
MSBuildEventSource.Log.EvaluateConditionStop(condition, result);
return result;
}
}
/// <summary>
/// COMPAT: Whidbey used the "current project file/targets" directory for evaluating Import and PropertyGroup conditions
/// Orcas broke this by using the current root project file for all conditions
/// For Dev10+, we'll fix this, and use the current project file/targets directory for Import, ImportGroup and PropertyGroup
/// but the root project file for the rest. Inside of targets will use the root project file as always.
/// </summary>
private static string GetCurrentDirectoryForConditionEvaluation(ProjectElement element, LazyItemEvaluator<P, I, M, D> lazyEvaluator)
{
if (element is ProjectPropertyGroupElement || element is ProjectImportElement || element is ProjectImportGroupElement)
{
return element.ContainingProject.DirectoryPath;
}
else
{
return lazyEvaluator._outerEvaluatorData.Directory;
}
}
public struct ItemData
{
public ItemData(I item, ProjectItemElement originatingItemElement, int elementOrder, bool conditionResult, string normalizedItemValue = null)
{
Item = item;
OriginatingItemElement = originatingItemElement;
ElementOrder = elementOrder;
ConditionResult = conditionResult;
_normalizedItemValue = normalizedItemValue;
}
public ItemData Clone(IItemFactory<I, I> itemFactory, ProjectItemElement initialItemElementForFactory)
{
// setting the factory's item element to the original item element that produced the item
// otherwise you get weird things like items that appear to have been produced by update elements
itemFactory.ItemElement = OriginatingItemElement;
var clonedItem = itemFactory.CreateItem(Item, OriginatingItemElement.ContainingProject.FullPath);
itemFactory.ItemElement = initialItemElementForFactory;
return new ItemData(clonedItem, OriginatingItemElement, ElementOrder, ConditionResult, _normalizedItemValue);
}
public I Item { get; }
public ProjectItemElement OriginatingItemElement { get; }
public int ElementOrder { get; }
public bool ConditionResult { get; }
/// <summary>
/// Lazily created normalized item value.
/// </summary>
private string _normalizedItemValue;
public string NormalizedItemValue
{
get
{
var normalizedItemValue = Volatile.Read(ref _normalizedItemValue);
if (normalizedItemValue == null)
{
normalizedItemValue = FileUtilities.NormalizePathForComparisonNoThrow(Item.EvaluatedInclude, Item.ProjectDirectory);
Volatile.Write(ref _normalizedItemValue, normalizedItemValue);
}
return normalizedItemValue;
}
}
}
private class MemoizedOperation : IItemOperation
{
public LazyItemOperation Operation { get; }
private Dictionary<ISet<string>, OrderedItemDataCollection> _cache;
private bool _isReferenced;
#if DEBUG
private int _applyCalls;
#endif
public MemoizedOperation(LazyItemOperation operation)
{
Operation = operation;
}
public void Apply(OrderedItemDataCollection.Builder listBuilder, ImmutableHashSet<string> globsToIgnore)
{
#if DEBUG
CheckInvariant();
#endif
Operation.Apply(listBuilder, globsToIgnore);
// cache results if somebody is referencing this operation
if (_isReferenced)
{
AddItemsToCache(globsToIgnore, listBuilder.ToImmutable());
}
#if DEBUG
_applyCalls++;
CheckInvariant();
#endif
}
#if DEBUG
private void CheckInvariant()
{
if (_isReferenced)
{
var cacheCount = _cache?.Count ?? 0;
Debug.Assert(_applyCalls == cacheCount, "Apply should only be called once per globsToIgnore. Otherwise caching is not working");
}
else
{
// non referenced operations should not be cached
// non referenced operations should have as many apply calls as the number of cache keys of the immediate dominator with _isReferenced == true
Debug.Assert(_cache == null);
}
}
#endif
public bool TryGetFromCache(ISet<string> globsToIgnore, out OrderedItemDataCollection items)
{
if (_cache != null)
{
return _cache.TryGetValue(globsToIgnore, out items);
}
items = null;
return false;
}
/// <summary>
/// Somebody is referencing this operation
/// </summary>
public void MarkAsReferenced()
{
_isReferenced = true;
}
private void AddItemsToCache(ImmutableHashSet<string> globsToIgnore, OrderedItemDataCollection items)
{
if (_cache == null)
{
_cache = new Dictionary<ISet<string>, OrderedItemDataCollection>();
}
_cache[globsToIgnore] = items;
}
}
private class LazyItemList
{
private readonly LazyItemList _previous;
private readonly MemoizedOperation _memoizedOperation;
public LazyItemList(LazyItemList previous, LazyItemOperation operation)
{
_previous = previous;
_memoizedOperation = new MemoizedOperation(operation);
}
public ImmutableList<I> GetMatchedItems(ImmutableHashSet<string> globsToIgnore)
{
ImmutableList<I>.Builder items = ImmutableList.CreateBuilder<I>();
foreach (ItemData data in GetItemData(globsToIgnore))
{
if (data.ConditionResult)
items.Add(data.Item);
}
return items.ToImmutable();
}
public OrderedItemDataCollection.Builder GetItemData(ImmutableHashSet<string> globsToIgnore)
{
// Cache results only on the LazyItemOperations whose results are required by an external caller (via GetItems). This means:
// - Callers of GetItems who have announced ahead of time that they would reference an operation (via MarkAsReferenced())
// This includes: item references (Include="@(foo)") and metadata conditions (Condition="@(foo->Count()) == 0")
// Without ahead of time notifications more computation is done than needed when the results of a future operation are requested
// The future operation is part of another item list referencing this one (making this operation part of the tail).
// The future operation will compute this list but since no ahead of time notifications have been made by callers, it won't cache the
// intermediary operations that would be requested by those callers.
// - Callers of GetItems that cannot announce ahead of time. This includes item referencing conditions on
// Item Groups and Item Elements. However, those conditions are performed eagerly outside of the LazyItemEvaluator, so they will run before
// any item referencing operations from inside the LazyItemEvaluator. This
//
// If the head of this LazyItemList is uncached, then the tail may contain cached and un-cached nodes.
// In this case we have to compute the head plus the part of the tail up to the first cached operation.
//
// The cache is based on a couple of properties:
// - uses immutable lists for structural sharing between multiple cached nodes (multiple include operations won't have duplicated memory for the common items)
// - if an operation is cached for a certain set of globsToIgnore, then the entire operation tail can be reused. This is because (i) the structure of LazyItemLists
// does not mutate: one can add operations on top, but the base never changes, and (ii) the globsToIgnore passed to the tail is the concatenation between
// the globsToIgnore received as an arg, and the globsToIgnore produced by the head (if the head is a Remove operation)
OrderedItemDataCollection items;
if (_memoizedOperation.TryGetFromCache(globsToIgnore, out items))
{
return items.ToBuilder();
}
else
{
// tell the cache that this operation's result is needed by an external caller
// this is required for callers that cannot tell the item list ahead of time that
// they would be using an operation
MarkAsReferenced();
return ComputeItems(this, globsToIgnore);
}
}
/// <summary>
/// Applies uncached item operations (include, remove, update) in order. Since Remove effectively overwrites Include or Update,
/// Remove operations are preprocessed (adding to globsToIgnore) to create a longer list of globs we don't need to process
/// properly because we know they will be removed. Update operations are batched as much as possible, meaning rather
/// than being applied immediately, they are combined into a dictionary of UpdateOperations that need to be applied. This
/// is to optimize the case in which as series of UpdateOperations, each of which affects a single ItemSpec, are applied to all
/// items in the list, leading to a quadratic-time operation.
/// </summary>
private static OrderedItemDataCollection.Builder ComputeItems(LazyItemList lazyItemList, ImmutableHashSet<string> globsToIgnore)
{
// Stack of operations up to the first one that's cached (exclusive)
Stack<LazyItemList> itemListStack = new Stack<LazyItemList>();
OrderedItemDataCollection.Builder items = null;
// Keep a separate stack of lists of globs to ignore that only gets modified for Remove operations
Stack<ImmutableHashSet<string>> globsToIgnoreStack = null;
for (var currentList = lazyItemList; currentList != null; currentList = currentList._previous)
{
var globsToIgnoreFromFutureOperations = globsToIgnoreStack?.Peek() ?? globsToIgnore;
OrderedItemDataCollection itemsFromCache;
if (currentList._memoizedOperation.TryGetFromCache(globsToIgnoreFromFutureOperations, out itemsFromCache))
{
// the base items on top of which to apply the uncached operations are the items of the first operation that is cached
items = itemsFromCache.ToBuilder();
break;
}
// If this is a remove operation, then add any globs that will be removed
// to a list of globs to ignore in previous operations
if (currentList._memoizedOperation.Operation is RemoveOperation removeOperation)
{
globsToIgnoreStack ??= new Stack<ImmutableHashSet<string>>();
var globsToIgnoreForPreviousOperations = removeOperation.GetRemovedGlobs();
foreach (var globToRemove in globsToIgnoreFromFutureOperations)
{
globsToIgnoreForPreviousOperations.Add(globToRemove);
}
globsToIgnoreStack.Push(globsToIgnoreForPreviousOperations.ToImmutable());
}
itemListStack.Push(currentList);
}
if (items == null)
{
items = OrderedItemDataCollection.CreateBuilder();
}
ImmutableHashSet<string> currentGlobsToIgnore = globsToIgnoreStack == null ? globsToIgnore : globsToIgnoreStack.Peek();
Dictionary<string, UpdateOperation> itemsWithNoWildcards = new Dictionary<string, UpdateOperation>(StringComparer.OrdinalIgnoreCase);
bool addedToBatch = false;
// Walk back down the stack of item lists applying operations
while (itemListStack.Count > 0)
{
var currentList = itemListStack.Pop();
if (currentList._memoizedOperation.Operation is UpdateOperation op)
{
bool addToBatch = true;
int i;
// The TextFragments are things like abc.def or x*y.*z.
for (i = 0; i < op.Spec.Fragments.Count; i++)
{
ItemSpecFragment frag = op.Spec.Fragments[i];
if (MSBuildConstants.CharactersForExpansion.Any(frag.TextFragment.Contains))
{
// Fragment contains wild cards, items, or properties. Cannot batch over it using a dictionary.
addToBatch = false;
break;
}
string fullPath = FileUtilities.GetFullPath(frag.TextFragment, frag.ProjectDirectory);
if (itemsWithNoWildcards.ContainsKey(fullPath))
{
// Another update will already happen on this path. Make that happen before evaluating this one.
addToBatch = false;
break;
}
else
{
itemsWithNoWildcards.Add(fullPath, op);
}
}
if (!addToBatch)
{
// We found a wildcard. Remove any fragments associated with the current operation and process them later.
for (int j = 0; j < i; j++)
{
itemsWithNoWildcards.Remove(currentList._memoizedOperation.Operation.Spec.Fragments[j].TextFragment);
}
}
else
{
addedToBatch = true;
continue;
}
}
if (addedToBatch)
{
addedToBatch = false;
ProcessNonWildCardItemUpdates(itemsWithNoWildcards, items);
}
// If this is a remove operation, then it could modify the globs to ignore, so pop the potentially
// modified entry off the stack of globs to ignore
if (currentList._memoizedOperation.Operation is RemoveOperation)
{
globsToIgnoreStack.Pop();
currentGlobsToIgnore = globsToIgnoreStack.Count == 0 ? globsToIgnore : globsToIgnoreStack.Peek();
}
currentList._memoizedOperation.Apply(items, currentGlobsToIgnore);
}
// We finished looping through the operations. Now process the final batch if necessary.
ProcessNonWildCardItemUpdates(itemsWithNoWildcards, items);
return items;
}
private static void ProcessNonWildCardItemUpdates(Dictionary<string, UpdateOperation> itemsWithNoWildcards, OrderedItemDataCollection.Builder items)
{
#if DEBUG
ErrorUtilities.VerifyThrow(itemsWithNoWildcards.All(fragment => !MSBuildConstants.CharactersForExpansion.Any(fragment.Key.Contains)), $"{nameof(itemsWithNoWildcards)} should not contain any text fragments with wildcards.");
#endif
if (itemsWithNoWildcards.Count > 0)
{
for (int i = 0; i < items.Count; i++)
{
string fullPath = FileUtilities.GetFullPath(items[i].Item.EvaluatedIncludeEscaped, items[i].Item.ProjectDirectory);
if (itemsWithNoWildcards.TryGetValue(fullPath, out UpdateOperation op))
{
items[i] = op.UpdateItem(items[i]);
}
}
itemsWithNoWildcards.Clear();
}
}
public void MarkAsReferenced()
{
_memoizedOperation.MarkAsReferenced();
}
}
private class OperationBuilder
{
// WORKAROUND: Unnecessary boxed allocation: https://github.com/dotnet/corefx/issues/24563
private static readonly ImmutableDictionary<string, LazyItemList> s_emptyIgnoreCase = ImmutableDictionary.Create<string, LazyItemList>(StringComparer.OrdinalIgnoreCase);
public ProjectItemElement ItemElement { get; set; }
public string ItemType { get; set; }
public ItemSpec<P,I> ItemSpec { get; set; }
public ImmutableDictionary<string, LazyItemList>.Builder ReferencedItemLists { get; } = Traits.Instance.EscapeHatches.UseCaseSensitiveItemNames ?
ImmutableDictionary.CreateBuilder<string, LazyItemList>() :
s_emptyIgnoreCase.ToBuilder();
public bool ConditionResult { get; set; }
public OperationBuilder(ProjectItemElement itemElement, bool conditionResult)
{
ItemElement = itemElement;
ItemType = itemElement.ItemType;
ConditionResult = conditionResult;
}
}
private class OperationBuilderWithMetadata : OperationBuilder
{
public ImmutableList<ProjectMetadataElement>.Builder Metadata = ImmutableList.CreateBuilder<ProjectMetadataElement>();
public OperationBuilderWithMetadata(ProjectItemElement itemElement, bool conditionResult) : base(itemElement, conditionResult)
{
}
}
private void AddReferencedItemList(string itemType, IDictionary<string, LazyItemList> referencedItemLists)
{
if (_itemLists.TryGetValue(itemType, out LazyItemList itemList))
{
itemList.MarkAsReferenced();
referencedItemLists[itemType] = itemList;
}
}
public IEnumerable<ItemData> GetAllItemsDeferred()
{
return _itemLists.Values.SelectMany(itemList => itemList.GetItemData(ImmutableHashSet<string>.Empty))
.OrderBy(itemData => itemData.ElementOrder);
}
public void ProcessItemElement(string rootDirectory, ProjectItemElement itemElement, bool conditionResult)
{
LazyItemOperation operation = null;
if (itemElement.IncludeLocation != null)
{
operation = BuildIncludeOperation(rootDirectory, itemElement, conditionResult);
}
else if (itemElement.RemoveLocation != null)
{
operation = BuildRemoveOperation(rootDirectory, itemElement, conditionResult);
}
else if (itemElement.UpdateLocation != null)
{
operation = BuildUpdateOperation(rootDirectory, itemElement, conditionResult);
}
else
{
ErrorUtilities.ThrowInternalErrorUnreachable();
}
_itemLists.TryGetValue(itemElement.ItemType, out LazyItemList previousItemList);
LazyItemList newList = new LazyItemList(previousItemList, operation);
_itemLists[itemElement.ItemType] = newList;
}
private UpdateOperation BuildUpdateOperation(string rootDirectory, ProjectItemElement itemElement, bool conditionResult)
{
OperationBuilderWithMetadata operationBuilder = new OperationBuilderWithMetadata(itemElement, conditionResult);
// Proces Update attribute
ProcessItemSpec(rootDirectory, itemElement.Update, itemElement.UpdateLocation, operationBuilder);
ProcessMetadataElements(itemElement, operationBuilder);
return new UpdateOperation(operationBuilder, this);
}
private IncludeOperation BuildIncludeOperation(string rootDirectory, ProjectItemElement itemElement, bool conditionResult)
{
IncludeOperationBuilder operationBuilder = new IncludeOperationBuilder(itemElement, conditionResult);
operationBuilder.ElementOrder = _nextElementOrder++;
operationBuilder.RootDirectory = rootDirectory;
operationBuilder.ConditionResult = conditionResult;
// Process include
ProcessItemSpec(rootDirectory, itemElement.Include, itemElement.IncludeLocation, operationBuilder);
// Code corresponds to Evaluator.EvaluateItemElement
// Process exclude (STEP 4: Evaluate, split, expand and subtract any Exclude)
if (itemElement.Exclude.Length > 0)
{
// Expand properties here, because a property may have a value which is an item reference (ie "@(Bar)"), and
// if so we need to add the right item reference
string evaluatedExclude = _expander.ExpandIntoStringLeaveEscaped(itemElement.Exclude, ExpanderOptions.ExpandProperties, itemElement.ExcludeLocation);
if (evaluatedExclude.Length > 0)
{
var excludeSplits = ExpressionShredder.SplitSemiColonSeparatedList(evaluatedExclude);
foreach (var excludeSplit in excludeSplits)
{
operationBuilder.Excludes.Add(excludeSplit);
AddItemReferences(excludeSplit, operationBuilder, itemElement.ExcludeLocation);
}
}
}
// Process Metadata (STEP 5: Evaluate each metadata XML and apply them to each item we have so far)
ProcessMetadataElements(itemElement, operationBuilder);
return new IncludeOperation(operationBuilder, this);
}
private RemoveOperation BuildRemoveOperation(string rootDirectory, ProjectItemElement itemElement, bool conditionResult)
{
RemoveOperationBuilder operationBuilder = new RemoveOperationBuilder(itemElement, conditionResult);
ProcessItemSpec(rootDirectory, itemElement.Remove, itemElement.RemoveLocation, operationBuilder);
// Process MatchOnMetadata
if (itemElement.MatchOnMetadata.Length > 0)
{
string evaluatedmatchOnMetadata = _expander.ExpandIntoStringLeaveEscaped(itemElement.MatchOnMetadata, ExpanderOptions.ExpandProperties, itemElement.MatchOnMetadataLocation);
if (evaluatedmatchOnMetadata.Length > 0)
{
var matchOnMetadataSplits = ExpressionShredder.SplitSemiColonSeparatedList(evaluatedmatchOnMetadata);
foreach (var matchOnMetadataSplit in matchOnMetadataSplits)
{
AddItemReferences(matchOnMetadataSplit, operationBuilder, itemElement.MatchOnMetadataLocation);
string metadataExpanded = _expander.ExpandIntoStringLeaveEscaped(matchOnMetadataSplit, ExpanderOptions.ExpandPropertiesAndItems, itemElement.MatchOnMetadataLocation);
var metadataSplits = ExpressionShredder.SplitSemiColonSeparatedList(metadataExpanded);
operationBuilder.MatchOnMetadata.AddRange(metadataSplits);
}
}
}
operationBuilder.MatchOnMetadataOptions = MatchOnMetadataOptions.CaseSensitive;
if (Enum.TryParse(itemElement.MatchOnMetadataOptions, out MatchOnMetadataOptions options))
{
operationBuilder.MatchOnMetadataOptions = options;
}
return new RemoveOperation(operationBuilder, this);
}
private void ProcessItemSpec(string rootDirectory, string itemSpec, IElementLocation itemSpecLocation, OperationBuilder builder)
{
builder.ItemSpec = new ItemSpec<P, I>(itemSpec, _outerExpander, itemSpecLocation, rootDirectory, loggingContext: _loggingContext);
foreach (ItemSpecFragment fragment in builder.ItemSpec.Fragments)
{
if (fragment is ItemSpec<P, I>.ItemExpressionFragment itemExpression)
{
AddReferencedItemLists(builder, itemExpression.Capture);
}
}
}
private static IEnumerable<string> GetExpandedMetadataValuesAndConditions(ICollection<ProjectMetadataElement> metadata, Expander<P, I> expander, LoggingContext loggingContext = null)
{
// Since we're just attempting to expand properties in order to find referenced items and not expanding metadata,
// unexpected errors may occur when evaluating property functions on unexpanded metadata. Just ignore them if that happens.
// See: https://github.com/dotnet/msbuild/issues/3460
const ExpanderOptions expanderOptions = ExpanderOptions.ExpandProperties | ExpanderOptions.LeavePropertiesUnexpandedOnError;
// Expand properties here, because a property may have a value which is an item reference (ie "@(Bar)"), and
// if so we need to add the right item reference.
foreach (var metadatumElement in metadata)
{
yield return expander.ExpandIntoStringLeaveEscaped(
metadatumElement.Value,
expanderOptions,
metadatumElement.Location,
loggingContext);
yield return expander.ExpandIntoStringLeaveEscaped(
metadatumElement.Condition,
expanderOptions,
metadatumElement.ConditionLocation);
}
}
private void ProcessMetadataElements(ProjectItemElement itemElement, OperationBuilderWithMetadata operationBuilder)
{
if (itemElement.HasMetadata)
{
operationBuilder.Metadata.AddRange(itemElement.Metadata);
var itemsAndMetadataFound = ExpressionShredder.GetReferencedItemNamesAndMetadata(GetExpandedMetadataValuesAndConditions(itemElement.Metadata, _expander, _loggingContext));
if (itemsAndMetadataFound.Items != null)
{
foreach (var itemType in itemsAndMetadataFound.Items)
{
AddReferencedItemList(itemType, operationBuilder.ReferencedItemLists);
}
}
}
}
private void AddItemReferences(string expression, OperationBuilder operationBuilder, IElementLocation elementLocation)
{
if (expression.Length == 0)
{
return;
}
else
{
ExpressionShredder.ItemExpressionCapture match = Expander<P, I>.ExpandSingleItemVectorExpressionIntoExpressionCapture(
expression, ExpanderOptions.ExpandItems, elementLocation);
if (match == null)
{
return;
}
AddReferencedItemLists(operationBuilder, match);
}
}
private void AddReferencedItemLists(OperationBuilder operationBuilder, ExpressionShredder.ItemExpressionCapture match)
{
if (match.ItemType != null)
{
AddReferencedItemList(match.ItemType, operationBuilder.ReferencedItemLists);
}
if (match.Captures != null)
{
foreach (var subMatch in match.Captures)
{
AddReferencedItemLists(operationBuilder, subMatch);
}
}
}
}
}