forked from dotnet/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CheckForDuplicateItems.cs
59 lines (44 loc) · 1.92 KB
/
CheckForDuplicateItems.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Globalization;
using Microsoft.Build.Framework;
namespace Microsoft.NET.Build.Tasks
{
public class CheckForDuplicateItems : TaskBase
{
[Required]
public ITaskItem[] Items { get; set; }
[Required]
public string ItemName { get; set; }
public bool DefaultItemsEnabled { get; set; }
public bool DefaultItemsOfThisTypeEnabled { get; set; }
[Required]
public string PropertyNameToDisableDefaultItems { get; set; }
public string PropertyValueToDisableDefaultItems { get; set; } = "false";
[Required]
public string MoreInformationLink { get; set; }
[Output]
public ITaskItem[] DeduplicatedItems { get; set; }
protected override void ExecuteCore()
{
DeduplicatedItems = Array.Empty<ITaskItem>();
if (DefaultItemsEnabled && DefaultItemsOfThisTypeEnabled)
{
var itemGroups = Items.GroupBy(i => i.ItemSpec, StringComparer.OrdinalIgnoreCase);
var duplicateItems = itemGroups.Where(g => g.Count() > 1).ToList();
if (duplicateItems.Any())
{
string duplicateItemsFormatted = string.Join("; ", duplicateItems.Select(d => $"'{d.Key}'"));
string message = string.Format(CultureInfo.CurrentCulture, Strings.DuplicateItemsError,
ItemName,
PropertyNameToDisableDefaultItems,
PropertyValueToDisableDefaultItems,
duplicateItemsFormatted,
MoreInformationLink);
Log.LogError(message);
DeduplicatedItems = itemGroups.Select(g => g.First()).ToArray();
}
}
}
}
}