forked from dotnet/msbuild
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ItemSpec.cs
500 lines (424 loc) · 18.4 KB
/
ItemSpec.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
// 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.Linq;
using Microsoft.Build.Globbing;
using Microsoft.Build.Internal;
using Microsoft.Build.Shared;
namespace Microsoft.Build.Evaluation
{
/// <summary>
/// Represents the elements of an item specification string (e.g. Include="*.cs;foo;@(i)") and
/// provides some operations over them (like matching items against a given ItemSpec)
/// </summary>
internal class ItemSpec<P, I>
where P : class, IProperty
where I : class, IItem, IMetadataTable
{
internal readonly struct ReferencedItem
{
public I Item { get; }
public ValueFragment ItemAsValueFragment { get; }
public ReferencedItem(I item, ValueFragment itemAsValueFragment)
{
Item = item;
ItemAsValueFragment = itemAsValueFragment;
}
}
internal class ItemExpressionFragment : ItemSpecFragment
{
private readonly ItemSpec<P, I> _containingItemSpec;
private Expander<P, I> _expander;
private IMSBuildGlob _msbuildGlob;
private List<ReferencedItem> _referencedItems;
public ExpressionShredder.ItemExpressionCapture Capture { get; }
public List<ReferencedItem> ReferencedItems
{
get
{
InitReferencedItemsIfNecessary();
return _referencedItems;
}
}
protected override IMSBuildGlob MsBuildGlob
{
get
{
if (InitReferencedItemsIfNecessary() || _msbuildGlob == null)
{
_msbuildGlob = CreateMsBuildGlob();
}
return _msbuildGlob;
}
}
public ItemExpressionFragment(
ExpressionShredder.ItemExpressionCapture capture,
string textFragment,
ItemSpec<P, I> containingItemSpec,
string projectDirectory)
: base(textFragment, projectDirectory)
{
Capture = capture;
_containingItemSpec = containingItemSpec;
_expander = _containingItemSpec.Expander;
}
public override int MatchCount(string itemToMatch)
{
return ReferencedItems.Count(v => v.ItemAsValueFragment.MatchCount(itemToMatch) > 0);
}
public override bool IsMatch(string itemToMatch)
{
return ReferencedItems.Any(v => v.ItemAsValueFragment.IsMatch(itemToMatch));
}
public override bool IsMatchOnMetadata(IItem item, IEnumerable<string> metadata, MatchOnMetadataOptions options)
{
return ReferencedItems.Any(referencedItem =>
metadata.All(m => !item.GetMetadataValue(m).Equals(string.Empty) && MetadataComparer(options, item.GetMetadataValue(m), referencedItem.Item.GetMetadataValue(m))));
}
private bool MetadataComparer(MatchOnMetadataOptions options, string itemMetadata, string referencedItemMetadata)
{
if (options.Equals(MatchOnMetadataOptions.PathLike))
{
return FileUtilities.ComparePathsNoThrow(itemMetadata, referencedItemMetadata, ProjectDirectory);
}
else
{
return String.Equals(itemMetadata, referencedItemMetadata, options.Equals(MatchOnMetadataOptions.CaseInsensitive) ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal);
}
}
public override IMSBuildGlob ToMSBuildGlob()
{
return MsBuildGlob;
}
protected override IMSBuildGlob CreateMsBuildGlob()
{
return new CompositeGlob(ReferencedItems.Select(i => i.ItemAsValueFragment.ToMSBuildGlob()));
}
private bool InitReferencedItemsIfNecessary()
{
// cache referenced items as long as the expander does not change
// reference equality works for now since the expander cannot mutate its item state (hopefully it stays that way)
if (_referencedItems == null || _expander != _containingItemSpec.Expander)
{
_expander = _containingItemSpec.Expander;
_expander.ExpandExpressionCapture(
Capture,
_containingItemSpec.ItemSpecLocation,
ExpanderOptions.ExpandItems,
includeNullEntries: false,
isTransformExpression: out _,
itemsFromCapture: out var itemsFromCapture);
_referencedItems =
itemsFromCapture.Select(i => new ReferencedItem(i.Value, new ValueFragment(i.Key, ProjectDirectory))).ToList();
return true;
}
return false;
}
}
public string ItemSpecString { get; }
/// <summary>
/// The fragments that compose an item spec string (values, globs, item references)
/// </summary>
public List<ItemSpecFragment> Fragments { get; }
/// <summary>
/// The expander needs to have a default item factory set.
/// </summary>
// todo Make this type immutable. Dealing with an Expander change is painful. See the ItemExpressionFragment
public Expander<P, I> Expander { get; set; }
/// <summary>
/// The xml attribute where this itemspec comes from
/// </summary>
public IElementLocation ItemSpecLocation { get; }
/// <param name="itemSpec">The string containing item syntax</param>
/// <param name="expander">Expects the expander to have a default item factory set</param>
/// <param name="itemSpecLocation">The xml location the itemspec comes from</param>
/// <param name="projectDirectory">The directory that the project is in.</param>
/// <param name="expandProperties">Expand properties before breaking down fragments. Defaults to true</param>
public ItemSpec(
string itemSpec,
Expander<P, I> expander,
IElementLocation itemSpecLocation,
string projectDirectory,
bool expandProperties = true)
{
ItemSpecString = itemSpec;
Expander = expander;
ItemSpecLocation = itemSpecLocation;
Fragments = BuildItemFragments(itemSpecLocation, projectDirectory, expandProperties);
}
private List<ItemSpecFragment> BuildItemFragments(IElementLocation itemSpecLocation, string projectDirectory, bool expandProperties)
{
// Code corresponds to Evaluator.CreateItemsFromInclude
var evaluatedItemspecEscaped = ItemSpecString;
if (string.IsNullOrEmpty(evaluatedItemspecEscaped))
{
return new List<ItemSpecFragment>();
}
// STEP 1: Expand properties in Include
if (expandProperties)
{
evaluatedItemspecEscaped = Expander.ExpandIntoStringLeaveEscaped(
ItemSpecString,
ExpanderOptions.ExpandProperties,
itemSpecLocation);
}
var semicolonCount = 0;
foreach (var c in evaluatedItemspecEscaped)
{
if (c == ';')
{
semicolonCount++;
}
}
// estimate the number of fragments with the number of semicolons. This is will overestimate in case of transforms with semicolons, but won't underestimate.
var fragments = new List<ItemSpecFragment>(semicolonCount + 1);
// STEP 2: Split Include on any semicolons, and take each split in turn
if (evaluatedItemspecEscaped.Length > 0)
{
var splitsEscaped = ExpressionShredder.SplitSemiColonSeparatedList(evaluatedItemspecEscaped);
foreach (var splitEscaped in splitsEscaped)
{
// STEP 3: If expression is "@(x)" copy specified list with its metadata, otherwise just treat as string
var itemReferenceFragment = ProcessItemExpression(
splitEscaped,
itemSpecLocation,
projectDirectory,
out var isItemListExpression);
if (isItemListExpression)
{
fragments.Add(itemReferenceFragment);
}
else
{
// The expression is not of the form "@(X)". Treat as string
// Code corresponds to EngineFileUtilities.GetFileList
var containsEscapedWildcards = EscapingUtilities.ContainsEscapedWildcards(splitEscaped);
var containsRealWildcards = FileMatcher.HasWildcards(splitEscaped);
// '*' is an illegal character to have in a filename.
// todo: file-system assumption on legal path characters: https://github.com/Microsoft/msbuild/issues/781
if (containsEscapedWildcards && containsRealWildcards)
{
// Just return the original string.
fragments.Add(new ValueFragment(splitEscaped, projectDirectory));
}
else if (!containsEscapedWildcards && containsRealWildcards)
{
// Unescape before handing it to the filesystem.
var filespecUnescaped = EscapingUtilities.UnescapeAll(splitEscaped);
fragments.Add(new GlobFragment(filespecUnescaped, projectDirectory));
}
else
{
// No real wildcards means we just return the original string. Don't even bother
// escaping ... it should already be escaped appropriately since it came directly
// from the project file
fragments.Add(new ValueFragment(splitEscaped, projectDirectory));
}
}
}
}
return fragments;
}
private ItemExpressionFragment ProcessItemExpression(
string expression,
IElementLocation elementLocation,
string projectDirectory,
out bool isItemListExpression)
{
isItemListExpression = false;
// Code corresponds to Expander.ExpandSingleItemVectorExpressionIntoItems
if (expression.Length == 0)
{
return null;
}
var capture = Expander<P, I>.ExpandSingleItemVectorExpressionIntoExpressionCapture(
expression,
ExpanderOptions.ExpandItems,
elementLocation);
if (capture == null)
{
return null;
}
isItemListExpression = true;
return new ItemExpressionFragment(capture, expression, this, projectDirectory);
}
/// <summary>
/// Return true if the given <paramref name="item" /> matches this itemspec
/// </summary>
/// <param name="item">The item to attempt to find a match for.</param>
public bool MatchesItem(I item)
{
// Avoid unnecessary LINQ/Func/Enumerator allocations on this path, this is called a lot
var evaluatedInclude = item.EvaluatedInclude;
foreach (var fragment in Fragments)
{
if (fragment.IsMatch(evaluatedInclude))
{
return true;
}
}
return false;
}
/// <summary>
/// Return true if any of the given <paramref name="metadata" /> matches the metadata on <paramref name="item" />
/// </summary>
/// <param name="item">The item to attempt to find a match for based on matching metadata</param>
/// <param name="metadata">Names of metadata to look for matches for</param>
/// <param name="options">metadata option matching</param>
/// <returns></returns>
public bool MatchesItemOnMetadata(IItem item, IEnumerable<string> metadata, MatchOnMetadataOptions options)
{
foreach (var fragment in Fragments)
{
if (fragment.IsMatchOnMetadata(item, metadata, options))
{
return true;
}
}
return false;
}
/// <summary>
/// Return the fragments that match against the given <paramref name="itemToMatch" />
/// </summary>
/// <param name="itemToMatch">The item to match.</param>
/// <param name="matches">
/// Total number of matches. Some fragments match more than once (item expression may contain multiple instances of
/// <paramref name="itemToMatch" />)
/// </param>
public IEnumerable<ItemSpecFragment> FragmentsMatchingItem(string itemToMatch, out int matches)
{
var result = new List<ItemSpecFragment>(Fragments.Count);
matches = 0;
foreach (var fragment in Fragments)
{
var itemMatches = fragment.MatchCount(itemToMatch);
if (itemMatches > 0)
{
result.Add(fragment);
matches += itemMatches;
}
}
return result;
}
/// <summary>
/// Return an MSBuildGlob that represents this ItemSpec.
/// </summary>
public IMSBuildGlob ToMSBuildGlob()
{
return new CompositeGlob(Fragments.Select(f => f.ToMSBuildGlob()));
}
/// <summary>
/// Returns all the fragment strings that represent it.
/// "1;*;2;@(foo)" gets returned as ["1", "2", "*", "a", "b"], given that @(foo)=["a", "b"]
/// Order is not preserved. Globs are not expanded. Item expressions get replaced with their referring item instances.
/// </summary>
public IEnumerable<string> FlattenFragmentsAsStrings()
{
foreach (var fragment in Fragments)
{
if (fragment is ValueFragment || fragment is GlobFragment)
{
yield return fragment.TextFragment;
}
else if (fragment is ItemExpressionFragment itemExpression)
{
foreach (var referencedItem in itemExpression.ReferencedItems)
{
yield return referencedItem.ItemAsValueFragment.TextFragment;
}
}
else
{
ErrorUtilities.ThrowInternalErrorUnreachable();
}
}
}
public override string ToString()
{
return ItemSpecString;
}
}
internal abstract class ItemSpecFragment
{
private FileSpecMatcherTester _fileMatcher;
private bool _fileMatcherInitialized;
private IMSBuildGlob _msbuildGlob;
/// <summary>
/// The substring from the original itemspec representing this fragment
/// </summary>
public string TextFragment { get; }
/// <summary>
/// Path of the project the itemspec is coming from
/// </summary>
protected string ProjectDirectory { get; }
// not a Lazy to reduce memory
private FileSpecMatcherTester FileMatcher
{
get
{
if (_fileMatcherInitialized)
{
return _fileMatcher;
}
_fileMatcher = CreateFileSpecMatcher();
_fileMatcherInitialized = true;
return _fileMatcher;
}
}
// not a Lazy to reduce memory
protected virtual IMSBuildGlob MsBuildGlob => _msbuildGlob ??= CreateMsBuildGlob();
protected ItemSpecFragment(string textFragment, string projectDirectory)
{
TextFragment = textFragment;
ProjectDirectory = projectDirectory;
}
/// <returns>The number of times the
/// <param name="itemToMatch"></param>
/// appears in this fragment
/// </returns>
public virtual int MatchCount(string itemToMatch)
{
return IsMatch(itemToMatch)
? 1
: 0;
}
public virtual bool IsMatch(string itemToMatch)
{
return FileMatcher.IsMatch(itemToMatch);
}
/// <summary>
/// Returns true if <paramref name="itemToMatch" /> matches any ReferencedItems based on <paramref name="metadata" /> and <paramref name="options" />.
/// </summary>
public virtual bool IsMatchOnMetadata(IItem itemToMatch, IEnumerable<string> metadata, MatchOnMetadataOptions options)
{
return false;
}
public virtual IMSBuildGlob ToMSBuildGlob()
{
return MsBuildGlob;
}
protected virtual IMSBuildGlob CreateMsBuildGlob()
{
return MSBuildGlob.Parse(ProjectDirectory, EscapingUtilities.UnescapeAll(TextFragment));
}
private FileSpecMatcherTester CreateFileSpecMatcher()
{
return FileSpecMatcherTester.Parse(ProjectDirectory, TextFragment);
}
}
internal class ValueFragment : ItemSpecFragment
{
public ValueFragment(string textFragment, string projectDirectory)
: base(textFragment, projectDirectory)
{
}
}
internal class GlobFragment : ItemSpecFragment
{
public GlobFragment(string textFragment, string projectDirectory)
: base(textFragment, projectDirectory)
{
}
}
}