-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathProjectItemInstance.cs
2349 lines (2052 loc) · 97.4 KB
/
ProjectItemInstance.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
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
#if FEATURE_APPDOMAIN
using System.Runtime.Remoting;
#endif
using Microsoft.Build.BackEnd;
using Microsoft.Build.Collections;
using Microsoft.Build.Construction;
using Microsoft.Build.Evaluation;
using Microsoft.Build.Framework;
using Microsoft.Build.Shared;
using Microsoft.Build.Shared.FileSystem;
#nullable disable
namespace Microsoft.Build.Execution
{
/// <summary>
/// Wraps an evaluated item for build purposes
/// </summary>
/// <remarks>
/// Does not store XML location information. That is not needed by the build process as all correctness checks
/// and evaluation has already been performed, so it is unnecessary bulk.
/// </remarks>
[DebuggerDisplay("{ItemType}={EvaluatedInclude} #DirectMetadata={DirectMetadataCount})")]
public class ProjectItemInstance :
IItem<ProjectMetadataInstance>,
ITaskItem2,
IMetadataTable,
ITranslatable,
IMetadataContainer,
IItemTypeDefinition,
IItemData
{
/// <summary>
/// The project instance to which this item belongs.
/// Never null.
/// </summary>
private ProjectInstance _project;
/// <summary>
/// Item type, for example "Compile"
/// Never null.
/// </summary>
private string _itemType;
/// <summary>
/// Backing task item holding the other data.
/// Never null.
/// </summary>
private TaskItem _taskItem;
/// <summary>
/// Constructor for items with no metadata.
/// Include may be empty.
/// Called before the build when virtual items are added,
/// and during the build when tasks emit items.
/// Mutability follows the project.
/// </summary>
internal ProjectItemInstance(ProjectInstance project, string itemType, string includeEscaped, string definingFileEscaped)
: this(project, itemType, includeEscaped, includeEscaped, definingFileEscaped)
{
}
/// <summary>
/// Constructor for items with no metadata.
/// Include may be empty.
/// Called before the build when virtual items are added,
/// and during the build when tasks emit items.
/// Mutability follows the project.
/// </summary>
internal ProjectItemInstance(ProjectInstance project, string itemType, string includeEscaped, string includeBeforeWildcardExpansionEscaped, string definingFileEscaped)
: this(project, itemType, includeEscaped, includeBeforeWildcardExpansionEscaped, null /* no direct metadata */, null /* need to add item definition metadata */, definingFileEscaped, useItemDefinitionsWithoutModification: false)
{
}
/// <summary>
/// Constructor for items with metadata.
/// Called before the build when virtual items are added,
/// and during the build when tasks emit items.
/// Include may be empty.
/// Direct metadata may be null, indicating no metadata. It will be cloned.
/// Builtin metadata may be null, indicating it has not been populated. It will be cloned.
/// Inherited item definition metadata may be null. It is assumed to ALREADY HAVE BEEN CLONED.
/// Mutability follows the project.
/// </summary>
/// <remarks>
/// Not public since the only creation scenario is setting on a project.
/// </remarks>
internal ProjectItemInstance(
ProjectInstance project,
string itemType,
string includeEscaped,
string includeBeforeWildcardExpansionEscaped,
ICopyOnWritePropertyDictionary<ProjectMetadataInstance> directMetadata,
IList<ProjectItemDefinitionInstance> itemDefinitions,
string definingFileEscaped,
bool useItemDefinitionsWithoutModification)
{
CommonConstructor(project, itemType, includeEscaped, includeBeforeWildcardExpansionEscaped, directMetadata, itemDefinitions, definingFileEscaped, useItemDefinitionsWithoutModification);
}
/// <summary>
/// Constructor for items with metadata.
/// Called when a ProjectInstance is created.
/// Include may be empty.
/// Direct metadata may be null, indicating no metadata. It will be cloned.
/// Metadata collection provided is cloned.
/// Mutability follows the project.
/// </summary>
/// <remarks>
/// Not public since the only creation scenario is setting on a project.
/// </remarks>
internal ProjectItemInstance(ProjectInstance project, string itemType, string includeEscaped, IEnumerable<KeyValuePair<string, string>> directMetadata, string definingFileEscaped)
{
CopyOnWritePropertyDictionary<ProjectMetadataInstance> metadata = null;
if (directMetadata?.GetEnumerator().MoveNext() == true)
{
metadata = new CopyOnWritePropertyDictionary<ProjectMetadataInstance>();
IEnumerable<ProjectMetadataInstance> directMetadataInstances = directMetadata.Select(metadatum => new ProjectMetadataInstance(metadatum.Key, metadatum.Value));
metadata.ImportProperties(directMetadataInstances);
}
CommonConstructor(project, itemType, includeEscaped, includeEscaped, metadata, null /* need to add item definition metadata */, definingFileEscaped, useItemDefinitionsWithoutModification: false);
}
/// <summary>
/// Cloning constructor, retaining same parentage.
/// </summary>
private ProjectItemInstance(ProjectItemInstance that)
: this(that, that._project)
{
}
/// <summary>
/// Cloning constructor.
/// </summary>
private ProjectItemInstance(ProjectItemInstance that, ProjectInstance newProject)
{
_project = newProject;
_itemType = that._itemType;
_taskItem = that._taskItem.DeepClone(newProject.IsImmutable);
}
/// <summary>
/// Constructor for serialization
/// </summary>
private ProjectItemInstance(ProjectInstance projectInstance)
{
_project = projectInstance;
// Deserialization continues
}
/// <summary>
/// Owning project
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public ProjectInstance Project
{
get { return _project; }
}
/// <summary>
/// Item type, for example "Compile"
/// </summary>
/// <remarks>
/// This cannot be set, as it is used as the key into
/// the project's items table.
/// </remarks>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public string ItemType
{
[DebuggerStepThrough]
get
{ return _itemType; }
}
/// <inheritdoc cref="IItemData.EvaluatedInclude"/>
/// <remarks>
/// Evaluated include value.
/// May be empty string.
/// </remarks>
public string EvaluatedInclude
{
[DebuggerStepThrough]
get
{
return _taskItem.ItemSpec;
}
[DebuggerStepThrough]
set
{
ErrorUtilities.VerifyThrowArgumentLength(value, "EvaluatedInclude");
_project.VerifyThrowNotImmutable();
_taskItem.ItemSpec = value;
}
}
/// <summary>
/// Evaluated include value, escaped as necessary.
/// May be empty string.
/// </summary>
string IItem.EvaluatedIncludeEscaped
{
[DebuggerStepThrough]
get
{ return _taskItem.IncludeEscaped; }
}
/// <summary>
/// Evaluated include value, escaped as necessary.
/// May be empty string.
/// </summary>
string ITaskItem2.EvaluatedIncludeEscaped
{
[DebuggerStepThrough]
get
{
return _taskItem.IncludeEscaped;
}
set
{
_project.VerifyThrowNotImmutable();
_taskItem.IncludeEscaped = value;
}
}
/// <summary>
/// Unordered collection of evaluated metadata on the item.
/// If there is no metadata, returns an empty collection.
/// Does not include built-in metadata.
/// Includes any from item definitions.
/// This is a read-only collection. To modify the metadata, use <see cref="SetMetadata(string, string)"/>.
/// </summary>
/// <comment>
/// Computed, not necessarily fast.
/// </comment>
[SuppressMessage("Microsoft.Naming", "CA1721:PropertyNamesShouldNotMatchGetMethods", Justification = "This is a reasonable choice. API review approved")]
public IEnumerable<ProjectMetadataInstance> Metadata
{
get { return _taskItem.MetadataCollection; }
}
/// <summary>
/// Number of pieces of metadata on this item
/// </summary>
public int DirectMetadataCount
{
get { return _taskItem.DirectMetadataCount; }
}
/// <summary>
/// Implementation of IKeyed exposing the item type
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
string IKeyed.Key
{
get { return ItemType; }
}
/// <summary>
/// Returns all the metadata names on this item.
/// Includes names from any applicable item definitions.
/// Includes names of built-in metadata.
/// </summary>
/// <comment>
/// Computed, not necessarily fast.
/// </comment>
public ICollection<string> MetadataNames
{
get { return new ReadOnlyCollection<string>(_taskItem.MetadataNames.Cast<string>()); }
}
/// <summary>
/// ITaskItem implementation
/// </summary>
string ITaskItem.ItemSpec
{
get
{
return EvaluatedInclude;
}
set
{
_project.VerifyThrowNotImmutable();
EvaluatedInclude = value;
}
}
/// <inheritdoc cref="IItemData.EnumerateMetadata"/>
IEnumerable<KeyValuePair<string, string>> IItemData.EnumerateMetadata() => ((IMetadataContainer)this).EnumerateMetadata();
/// <summary>
/// ITaskItem implementation
/// </summary>
/// <comment>
/// Computed, not necessarily fast.
/// </comment>
ICollection ITaskItem.MetadataNames
{
get { return new List<string>(MetadataNames); }
}
/// <summary>
/// Returns the number of metadata entries.
/// Includes any from applicable item definitions.
/// Includes both custom and built-in metadata.
/// </summary>
/// <comment>
/// Computed, not necessarily fast.
/// </comment>
public int MetadataCount
{
get { return _taskItem.MetadataCount; }
}
/// <summary>
/// The directory of the project being built
/// Never null: If there is no project filename yet, it will use the current directory
/// </summary>
string IItem.ProjectDirectory
{
get { return _project.Directory; }
}
/// <summary>
/// Retrieves the comparer used for determining equality between ProjectItemInstances.
/// </summary>
internal static IEqualityComparer<ProjectItemInstance> EqualityComparer
{
get { return ProjectItemInstanceEqualityComparer.Default; }
}
/// <summary>
/// The full path to the project file being built
/// Can be null: if the project hasn't been saved yet it will be null
/// </summary>
internal string ProjectFullPath
{
get { return _project.FullPath; }
}
/// <summary>
/// Get any metadata in the item that has the specified name,
/// otherwise returns null.
/// Includes any metadata inherited from item definitions.
/// Includes any built-in metadata.
/// </summary>
public ProjectMetadataInstance GetMetadata(string name)
{
return _taskItem.GetMetadataObject(name);
}
/// <summary>
/// Get the value of a metadata on this item, or
/// String.Empty if it does not exist or has no value.
/// Includes any metadata inherited from item definitions and any built-in metadata.
/// To determine whether a piece of metadata is actually present
/// but with an empty value, use <see cref="HasMetadata(string)">HasMetadata</see>.
/// </summary>
public string GetMetadataValue(string name)
{
return _taskItem.GetMetadata(name);
}
/// <summary>
/// Returns true if a particular piece of metadata is defined on this item (even if
/// its value is empty string) otherwise false.
/// This includes built-in metadata and metadata from item definitions.
/// </summary>
/// <remarks>
/// It has to include all of these because it's used for batching, which doesn't
/// care where the metadata originated.
/// </remarks>
public bool HasMetadata(string name)
{
return _taskItem.HasMetadata(name);
}
/// <summary>
/// Add a metadata with the specified name and value.
/// Overwrites any metadata with the same name already in the collection.
/// </summary>
public ProjectMetadataInstance SetMetadata(string name, string evaluatedValue)
{
_project.VerifyThrowNotImmutable();
return _taskItem.SetMetadataObject(name, evaluatedValue, false /* built-in metadata not allowed */);
}
/// <summary>
/// Add a metadata with the specified names and values.
/// Overwrites any metadata with the same name already in the collection.
/// </summary>
public void SetMetadata(IEnumerable<KeyValuePair<string, string>> metadataDictionary)
{
_project.VerifyThrowNotImmutable();
_taskItem.SetMetadata(metadataDictionary);
}
/// <summary>
/// Removes a metadatum with the specified name.
/// Used by TaskItem
/// </summary>
public void RemoveMetadata(string metadataName)
{
_project.VerifyThrowNotImmutable();
_taskItem.RemoveMetadata(metadataName);
}
/// <summary>
/// Produce a string representation.
/// </summary>
public override string ToString()
{
return _taskItem.ToString();
}
/// <summary>
/// Get the value of a metadata on this item, or
/// String.Empty if it does not exist or has no value.
/// Includes any metadata inherited from item definitions and any built-in metadata.
/// To determine whether a piece of metadata is actually present
/// but with an empty value, use <see cref="HasMetadata(string)">HasMetadata</see>.
/// </summary>
string IItem.GetMetadataValueEscaped(string name)
{
return _taskItem.GetMetadataEscaped(name);
}
/// <summary>
/// Sets the specified metadata. Discards the xml part except for the name.
/// Discards the location of the original element. This is not interesting in the Execution world
/// as it should never be needed for any messages, and is just extra bulk.
/// Predecessor is discarded as it is only needed for design time.
/// </summary>
ProjectMetadataInstance IItem<ProjectMetadataInstance>.SetMetadata(ProjectMetadataElement metadataElement, string evaluatedInclude)
{
_project.VerifyThrowNotImmutable();
return SetMetadata(metadataElement.Name, evaluatedInclude);
}
/// <summary>
/// ITaskItem implementation.
/// </summary>
/// <remarks>
/// ITaskItem should not return null if metadata is not present.
/// </remarks>
string ITaskItem.GetMetadata(string metadataName)
{
return GetMetadataValue(metadataName);
}
/// <summary>
/// ITaskItem2 implementation.
/// </summary>
/// <remarks>
/// ITaskItem2 should not return null if metadata is not present.
/// </remarks>
string ITaskItem2.GetMetadataValueEscaped(string name)
{
return _taskItem.GetMetadataEscaped(name);
}
/// <summary>
/// ITaskItem implementation
/// </summary>
/// <comments>
/// MetadataValue is assumed to be in its escaped form.
/// </comments>
void ITaskItem.SetMetadata(string metadataName, string metadataValue)
{
SetMetadata(metadataName, metadataValue);
}
/// <summary>
/// ITaskItem2 implementation
/// </summary>
/// <comments>
/// Assumes metadataValue is unescaped.
/// </comments>
void ITaskItem2.SetMetadataValueLiteral(string metadataName, string metadataValue)
{
_project.VerifyThrowNotImmutable();
((ITaskItem2)_taskItem).SetMetadataValueLiteral(metadataName, metadataValue);
}
/// <summary>
/// ITaskItem implementation
/// </summary>
void ITaskItem.CopyMetadataTo(ITaskItem destinationItem)
{
_taskItem.CopyMetadataTo(destinationItem);
}
/// <summary>
/// ITaskItem implementation
/// </summary>
/// <comments>
/// Returns a dictionary of the UNESCAPED values of the metadata
/// </comments>
IDictionary ITaskItem.CloneCustomMetadata()
{
return _taskItem.CloneCustomMetadata();
}
/// <summary>
/// ITaskItem2 implementation
/// </summary>
/// <comments>
/// Returns a dictionary of the ESCAPED values of the metadata
/// </comments>
IDictionary ITaskItem2.CloneCustomMetadataEscaped()
{
return ((ITaskItem2)_taskItem).CloneCustomMetadataEscaped();
}
IEnumerable<KeyValuePair<string, string>> IMetadataContainer.EnumerateMetadata() => _taskItem.EnumerateMetadata();
void IMetadataContainer.ImportMetadata(IEnumerable<KeyValuePair<string, string>> metadata) => _taskItem.ImportMetadata(metadata);
#region IMetadataTable Members
/// <summary>
/// Retrieves any value we have in our metadata table for the metadata name specified.
/// If no value is available, returns empty string.
/// </summary>
string IMetadataTable.GetEscapedValue(string name)
{
return _taskItem.GetMetadataEscaped(name);
}
/// <summary>
/// Retrieves any value we have in our metadata table for the metadata name and item type specified.
/// If no value is available, returns empty string.
/// If item type is null, it is ignored, otherwise it must match.
/// </summary>
string IMetadataTable.GetEscapedValue(string itemType, string name)
{
string value = ((IMetadataTable)this).GetEscapedValueIfPresent(itemType, name);
return value ?? String.Empty;
}
/// <summary>
/// Returns the value if it exists.
/// If no value is available, returns null.
/// If item type is null, it is ignored, otherwise it must match.
/// </summary>
string IMetadataTable.GetEscapedValueIfPresent(string itemType, string name)
{
if (itemType == null || String.Equals(itemType, _itemType, StringComparison.OrdinalIgnoreCase))
{
string value = _taskItem.GetMetadataEscaped(name);
if (value.Length > 0 || HasMetadata(name))
{
return value;
}
}
return null;
}
#endregion
#region INodePacketTranslatable Members
/// <summary>
/// Translation method.
/// </summary>
void ITranslatable.Translate(ITranslator translator)
{
translator.Translate(ref _itemType);
translator.Translate(ref _taskItem, TaskItem.FactoryForDeserialization);
}
#endregion
/// <summary>
/// Set all the supplied metadata on all the supplied items.
/// </summary>
internal static void SetMetadata(IEnumerable<KeyValuePair<string, string>> metadataList, IEnumerable<ProjectItemInstance> items)
{
// Set up a single dictionary that can be applied to all the items
CopyOnWritePropertyDictionary<ProjectMetadataInstance> metadata = new();
IEnumerable<ProjectMetadataInstance> projectMetadataInstances = metadataList.Select(metadatum => new ProjectMetadataInstance(metadatum.Key, metadatum.Value));
metadata.ImportProperties(projectMetadataInstances);
foreach (ProjectItemInstance item in items)
{
item._taskItem.SetMetadata(metadata); // Potential copy on write
}
}
/// <summary>
/// Factory for deserialization.
/// </summary>
internal static ProjectItemInstance FactoryForDeserialization(ITranslator translator, ProjectInstance projectInstance)
{
ProjectItemInstance newItem = new ProjectItemInstance(projectInstance);
((ITranslatable)newItem).Translate(translator);
return newItem;
}
/// <summary>
/// Add a metadata with the specified names and values.
/// Overwrites any metadata with the same name already in the collection.
/// </summary>
internal void SetMetadata(ICopyOnWritePropertyDictionary<ProjectMetadataInstance> metadataDictionary)
{
_project.VerifyThrowNotImmutable();
_taskItem.SetMetadata(metadataDictionary);
}
/// <summary>
/// Sets metadata where one built-in metadata is allowed to be set: RecursiveDir.
/// This is not normally legal to set outside of evaluation. However, the CreateItem
/// needs to be able to set it as a task output, because it supports wildcards. So as a special exception we allow
/// tasks to set this particular metadata as a task output.
/// Other built in metadata names are ignored. That's because often task outputs are items that were passed in,
/// which legally have built-in metadata. If necessary we can calculate it on the new items we're making if requested.
/// We don't copy them too because tasks shouldn't set them (they might become inconsistent)
/// </summary>
internal void SetMetadataOnTaskOutput(IEnumerable<KeyValuePair<string, string>> items)
{
_project.VerifyThrowNotImmutable();
_taskItem.SetMetadataOnTaskOutput(items);
}
/// <summary>
/// Deep clone the item.
/// Any metadata inherited from item definitions are also copied.
/// </summary>
internal ProjectItemInstance DeepClone()
{
return new ProjectItemInstance(this);
}
/// <summary>
/// Deep clone the item.
/// Any metadata inherited from item definitions are also copied.
/// </summary>
internal ProjectItemInstance DeepClone(ProjectInstance newProject)
{
return new ProjectItemInstance(this, newProject);
}
/// <summary>
/// Generates a ProjectItemElement representing this instance.
/// </summary>
/// <param name="parent">The root element to which the element will belong.</param>
/// <returns>The new element.</returns>
internal ProjectItemElement ToProjectItemElement(ProjectElementContainer parent)
{
ProjectItemElement item = parent.ContainingProject.CreateItemElement(ItemType);
item.Include = EvaluatedInclude;
parent.AppendChild(item);
foreach (ProjectMetadataInstance metadataInstance in Metadata)
{
item.AddMetadata(metadataInstance.Name, metadataInstance.EvaluatedValue);
}
return item;
}
/// <summary>
/// Common constructor code.
/// Direct metadata may be null, indicating no metadata. It will be cloned.
/// Builtin metadata may be null, indicating it has not been populated. It will be cloned.
/// Inherited item definition metadata may be null. It is assumed to ALREADY HAVE BEEN CLONED.
/// Mutability follows the project.
/// </summary>
private void CommonConstructor(
ProjectInstance projectToUse,
string itemTypeToUse,
string includeEscaped,
string includeBeforeWildcardExpansionEscaped,
ICopyOnWritePropertyDictionary<ProjectMetadataInstance> directMetadata,
IList<ProjectItemDefinitionInstance> itemDefinitions,
string definingFileEscaped,
bool useItemDefinitionsWithoutModification)
{
ErrorUtilities.VerifyThrowArgumentNull(projectToUse, "project");
ErrorUtilities.VerifyThrowArgumentLength(itemTypeToUse, "itemType");
XmlUtilities.VerifyThrowArgumentValidElementName(itemTypeToUse);
ErrorUtilities.VerifyThrowArgument(!XMakeElements.ReservedItemNames.Contains(itemTypeToUse), "OM_ReservedName", itemTypeToUse);
IList<ProjectItemDefinitionInstance> inheritedItemDefinitions;
if (itemDefinitions == null || !useItemDefinitionsWithoutModification)
{
// TaskItems don't have an item type. So for their benefit, we have to lookup and add the regular item definition.
inheritedItemDefinitions = (itemDefinitions == null) ? null : new List<ProjectItemDefinitionInstance>(itemDefinitions);
ProjectItemDefinitionInstance itemDefinition;
if (projectToUse.ItemDefinitions.TryGetValue(itemTypeToUse, out itemDefinition))
{
inheritedItemDefinitions ??= new List<ProjectItemDefinitionInstance>();
inheritedItemDefinitions.Add(itemDefinition);
}
}
else
{
// In this case the caller specifying useItemDefinitionsWithoutModification is guaranteeing that
// the itemDefinitions collection contains all necessary definitions (including the definition
// associated with itemTypeToUse) and, for performance reasons, the provided (immutable) collection
// should be used as is.
inheritedItemDefinitions = itemDefinitions;
}
_project = projectToUse;
_itemType = itemTypeToUse;
_taskItem = new TaskItem(
includeEscaped,
includeBeforeWildcardExpansionEscaped,
directMetadata?.DeepClone(), // copy on write!
inheritedItemDefinitions,
_project.Directory,
_project.IsImmutable,
definingFileEscaped);
}
/// <summary>
/// An item without an item type. Cast to an ITaskItem, this is
/// what is given to tasks. It is also used for target outputs.
/// </summary>
internal sealed class TaskItem :
#if FEATURE_APPDOMAIN
MarshalByRefObject,
#endif
ITaskItem2,
IItem<ProjectMetadataInstance>,
ITranslatable,
IEquatable<TaskItem>,
IMetadataContainer
{
/// <summary>
/// The source file that defined this item.
/// </summary>
private string _definingFileEscaped;
/// <summary>
/// Evaluated include, escaped as necessary.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private string _includeEscaped;
/// <summary>
/// The evaluated (escaped) include prior to wildcard expansion. Used to determine the
/// RecursiveDir build-in metadata value.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private string _includeBeforeWildcardExpansionEscaped;
/// <summary>
/// Evaluated metadata.
/// May be null.
/// </summary>
/// <remarks>
/// Lazily created, as there are huge numbers of items generated in
/// a build that have no metadata at all.
/// </remarks>
private ICopyOnWritePropertyDictionary<ProjectMetadataInstance> _directMetadata;
/// <summary>
/// Cached value of the fullpath metadata. All other metadata are computed on demand.
/// </summary>
private string _fullPath;
/// <summary>
/// All the item definitions that apply to this item, in order of
/// decreasing precedence. At the bottom will be an item definition
/// that directly applies to the item type that produced this item. The others will
/// be item definitions inherited from items that were
/// used to create this item.
/// </summary>
private IList<ProjectItemDefinitionInstance> _itemDefinitions;
/// <summary>
/// Directory of the associated project. If this is available,
/// it is used to calculate built-in metadata. Otherwise,
/// the current directory is used.
/// </summary>
private string _projectDirectory;
/// <summary>
/// Whether the task item is immutable.
/// </summary>
private bool _isImmutable;
/// <summary>
/// Creates an instance of this class given the item-spec.
/// </summary>
internal TaskItem(string includeEscaped, string definingFileEscaped)
: this(includeEscaped, includeEscaped, null, null, null, immutable: false, definingFileEscaped)
{
}
/// <summary>
/// Creates an instance of this class given the item-spec and a built-in metadata collection.
/// Parameters are assumed to be ALREADY CLONED.
/// </summary>
internal TaskItem(
string includeEscaped,
string includeBeforeWildcardExpansionEscaped,
ICopyOnWritePropertyDictionary<ProjectMetadataInstance> directMetadata,
IList<ProjectItemDefinitionInstance> itemDefinitions,
string projectDirectory,
bool immutable,
string definingFileEscaped) // the actual project file (or import) that defines this item.
{
ErrorUtilities.VerifyThrowArgumentLength(includeEscaped);
ErrorUtilities.VerifyThrowArgumentLength(includeBeforeWildcardExpansionEscaped);
_includeEscaped = FileUtilities.FixFilePath(includeEscaped);
_includeBeforeWildcardExpansionEscaped = FileUtilities.FixFilePath(includeBeforeWildcardExpansionEscaped);
_directMetadata = (directMetadata == null || directMetadata.Count == 0) ? null : directMetadata; // If the metadata was all removed, toss the dictionary
_itemDefinitions = itemDefinitions;
_projectDirectory = projectDirectory;
_isImmutable = immutable;
_definingFileEscaped = definingFileEscaped;
}
/// <summary>
/// Creates a task item by copying the information from a <see cref="ProjectItemInstance"/>.
/// Parameters are cloned.
/// </summary>
internal TaskItem(ProjectItemInstance item)
: this(item._taskItem, false /* no original itemspec */)
{
}
/// <summary>
/// Creates an instance of this class given the backing item.
/// Does not copy immutability, since there is no connection with the original.
/// </summary>
private TaskItem(TaskItem source, bool addOriginalItemSpec)
{
_includeEscaped = source._includeEscaped;
_includeBeforeWildcardExpansionEscaped = source._includeBeforeWildcardExpansionEscaped;
source.CopyMetadataTo(this, addOriginalItemSpec);
_fullPath = source._fullPath;
_definingFileEscaped = source._definingFileEscaped;
}
/// <summary>
/// Private constructor used for serialization.
/// </summary>
private TaskItem(ITranslator translator)
{
((ITranslatable)this).Translate(translator);
}
/// <summary>
/// Private constructor used for serialization.
/// </summary>
private TaskItem(ITranslator translator, LookasideStringInterner interner)
{
this.TranslateWithInterning(translator, interner);
}
/// <summary>
/// Gets or sets the unescaped include, or "name", for the item.
/// </summary>
/// <comments>
/// This one is a bit tricky. Orcas assumed that the value being set was escaped, but
/// that the value being returned was unescaped. Maintain that behaviour here. To get
/// the escaped value, use ITaskItem2.EvaluatedIncludeEscaped.
/// </comments>
public string ItemSpec
{
get
{
return EscapingUtilities.UnescapeAll(_includeEscaped);
}
set
{
ProjectInstance.VerifyThrowNotImmutable(_isImmutable);
// Historically empty string was allowed
ErrorUtilities.VerifyThrowArgumentNull(value, "ItemSpec");
_includeEscaped = value;
_fullPath = null; // Clear cached value
}
}
/// <summary>
/// Gets or sets the escaped include, or "name", for the item.
/// </summary>
/// <remarks>
/// Taking the opportunity to fix the property name, although this doesn't
/// make it obvious it's an improvement on ItemSpec.
/// </remarks>
string ITaskItem2.EvaluatedIncludeEscaped
{
get
{
return _includeEscaped;
}
set
{
ProjectInstance.VerifyThrowNotImmutable(_isImmutable);
// setter on ItemSpec already expects an escaped value.
ItemSpec = value;
}
}
/// <summary>
/// Gets the names of metadata on the item.
/// Includes all built-in metadata.
/// Computed, not necessarily fast.
/// </summary>
public ICollection MetadataNames
{
get
{
ICopyOnWritePropertyDictionary<ProjectMetadataInstance> metadataCollection = MetadataCollection;
List<string> names = new List<string>(capacity: metadataCollection.Count + FileUtilities.ItemSpecModifiers.All.Length);
foreach (ProjectMetadataInstance metadatum in (IEnumerable<ProjectMetadataInstance>)metadataCollection)
{
names.Add(metadatum.Name);
}
names.AddRange(FileUtilities.ItemSpecModifiers.All);
return names;
}
}
/// <summary>
/// Gets the number of metadata set on the item.
/// Computed, not necessarily fast.
/// </summary>
public int MetadataCount
{
get { return MetadataNames.Count; }
}
/// <summary>
/// Gets the evaluated include for this item, unescaped.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
string IItem.EvaluatedInclude
{
get { return EscapingUtilities.UnescapeAll(_includeEscaped); }
}
/// <summary>
/// Gets the evaluated include for this item, escaped.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
string IItem.EvaluatedIncludeEscaped
{
get { return _includeEscaped; }
}
/// <summary>
/// The directory of the project owning this TaskItem.
/// May be null if this is not well defined.
/// </summary>
string IItem.ProjectDirectory
{
get { return _projectDirectory; }
}
#region IKeyed Members
/// <summary>
/// Returns some value useful for a key in a dictionary
/// </summary>