forked from dotnet/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CheckForDuplicateFrameworkReferences.cs
65 lines (53 loc) · 2.14 KB
/
CheckForDuplicateFrameworkReferences.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.Build.Framework;
namespace Microsoft.NET.Build.Tasks
{
public class CheckForDuplicateFrameworkReferences : TaskBase
{
[Required]
public ITaskItem[] FrameworkReferences { get; set; }
[Required]
public string MoreInformationLink { get; set; }
[Output]
public ITaskItem[] ItemsToAdd { get; set; }
[Output]
public ITaskItem[] ItemsToRemove { get; set; }
protected override void ExecuteCore()
{
if (FrameworkReferences == null || FrameworkReferences.Length == 0)
{
return;
}
var duplicateItems = FrameworkReferences.GroupBy(i => i.ItemSpec, StringComparer.OrdinalIgnoreCase).Where(g => g.Count() > 1);
if (duplicateItems.Any())
{
List<ITaskItem> itemsToAdd = new();
List<ITaskItem> itemsToRemove = new();
foreach (var duplicateItemGroup in duplicateItems)
{
int remainingItems = 0;
foreach (var item in duplicateItemGroup)
{
if (item.GetMetadata(MetadataKeys.IsImplicitlyDefined).Equals("true", StringComparison.OrdinalIgnoreCase))
{
itemsToRemove.Add(item);
Log.LogWarning(Strings.FrameworkReferenceOverrideWarning, item.ItemSpec, MoreInformationLink);
}
else
{
remainingItems++;
itemsToAdd.Add(item);
}
}
if (remainingItems > 1)
{
Log.LogError(Strings.FrameworkReferenceDuplicateError, duplicateItemGroup.Key);
}
}
ItemsToAdd = itemsToAdd.ToArray();
ItemsToRemove = itemsToRemove.ToArray();
}
}
}
}