4
4
using System ;
5
5
using System . Collections . Generic ;
6
6
using System . Linq ;
7
+ using System . Runtime . CompilerServices ;
7
8
using System . Threading ;
8
9
using Microsoft . Build . Collections ;
9
10
using Microsoft . Build . Evaluation ;
@@ -636,7 +637,7 @@ internal void SetProperty(ProjectPropertyInstance property)
636
637
/// <summary>
637
638
/// Implements a true add, an item that has been created in a batch.
638
639
/// </summary>
639
- internal void AddNewItemsOfItemType ( string itemType , ICollection < ProjectItemInstance > group , bool doNotAddDuplicates = false )
640
+ internal void AddNewItemsOfItemType ( string itemType , ICollection < ProjectItemInstance > group , bool doNotAddDuplicates = false , Action < List < ProjectItemInstance > > logFunction = null )
640
641
{
641
642
// Adding to outer scope could be easily implemented, but our code does not do it at present
642
643
MustNotBeOuterScope ( ) ;
@@ -663,12 +664,32 @@ internal void AddNewItemsOfItemType(string itemType, ICollection<ProjectItemInst
663
664
664
665
// Ensure we don't also add any that already exist.
665
666
var existingItems = GetItems ( itemType ) ;
667
+
668
+ var existingItemsHashSet = existingItems . ToHashSet ( ProjectItemInstance . EqualityComparer ) ;
669
+
666
670
if ( existingItems . Count > 0 )
667
671
{
668
- itemsToAdd = itemsToAdd . Where ( item => ! existingItems . Contains ( item , ProjectItemInstance . EqualityComparer ) ) ;
672
+
673
+ var deduplicatedItemsToAdd = new List < ProjectItemInstance > ( ) ;
674
+ foreach ( var item in itemsToAdd ) {
675
+ if ( ! existingItemsHashSet . Contains ( item ) ) {
676
+ deduplicatedItemsToAdd . Add ( item ) ;
677
+ }
678
+ }
679
+ itemsToAdd = deduplicatedItemsToAdd ;
669
680
}
670
681
}
671
682
683
+ if ( doNotAddDuplicates )
684
+ {
685
+ logFunction ? . Invoke ( itemsToAdd . ToList ( ) ) ;
686
+ }
687
+ else {
688
+ var groupAsList = group as List < ProjectItemInstance > ;
689
+ logFunction ? . Invoke ( groupAsList ?? group . ToList ( ) ) ;
690
+ }
691
+
692
+
672
693
PrimaryAddTable . ImportItemsOfType ( itemType , itemsToAdd ) ;
673
694
}
674
695
0 commit comments