-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
ResolvePackageFileConflicts.cs
181 lines (144 loc) · 7.4 KB
/
ResolvePackageFileConflicts.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
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Microsoft.NET.Build.Tasks.ConflictResolution
{
public class ResolvePackageFileConflicts : TaskBase
{
private HashSet<ITaskItem> referenceConflicts = new HashSet<ITaskItem>();
private HashSet<ITaskItem> copyLocalConflicts = new HashSet<ITaskItem>();
private HashSet<ConflictItem> allConflicts = new HashSet<ConflictItem>();
public ITaskItem[] References { get; set; }
public ITaskItem[] ReferenceCopyLocalPaths { get; set; }
public ITaskItem[] OtherRuntimeItems { get; set; }
public ITaskItem[] PlatformManifests { get; set; }
/// <summary>
/// NuGet3 and later only. In the case of a conflict with identical file version information a file from the most preferred package will be chosen.
/// </summary>
public string[] PreferredPackages { get; set; }
[Output]
public ITaskItem[] ReferencesWithoutConflicts { get; set; }
[Output]
public ITaskItem[] ReferenceCopyLocalPathsWithoutConflicts { get; set; }
[Output]
public ITaskItem[] Conflicts { get; set; }
protected override void ExecuteCore()
{
var log = new MSBuildLog(Log);
var packageRanks = new PackageRank(PreferredPackages);
// resolve conflicts at compile time
var referenceItems = GetConflictTaskItems(References, ConflictItemType.Reference).ToArray();
var compileConflictScope = new ConflictResolver<ConflictItem>(packageRanks, log);
compileConflictScope.ResolveConflicts(referenceItems,
ci => ItemUtilities.GetReferenceFileName(ci.OriginalItem),
HandleCompileConflict);
// resolve conflicts that class in output
var runtimeConflictScope = new ConflictResolver<ConflictItem>(packageRanks, log);
runtimeConflictScope.ResolveConflicts(referenceItems,
ci => ItemUtilities.GetReferenceTargetPath(ci.OriginalItem),
HandleRuntimeConflict);
var copyLocalItems = GetConflictTaskItems(ReferenceCopyLocalPaths, ConflictItemType.CopyLocal).ToArray();
runtimeConflictScope.ResolveConflicts(copyLocalItems,
ci => ItemUtilities.GetTargetPath(ci.OriginalItem),
HandleRuntimeConflict);
var otherRuntimeItems = GetConflictTaskItems(OtherRuntimeItems, ConflictItemType.Runtime).ToArray();
runtimeConflictScope.ResolveConflicts(otherRuntimeItems,
ci => ItemUtilities.GetTargetPath(ci.OriginalItem),
HandleRuntimeConflict);
// resolve conflicts with platform (eg: shared framework) items
// we only commit the platform items since its not a conflict if other items share the same filename.
var platformConflictScope = new ConflictResolver<ConflictItem>(packageRanks, log);
var platformItems = PlatformManifests?.SelectMany(pm => PlatformManifestReader.LoadConflictItems(pm.ItemSpec, log)) ?? Enumerable.Empty<ConflictItem>();
platformConflictScope.ResolveConflicts(platformItems, pi => pi.FileName, pi => { });
platformConflictScope.ResolveConflicts(referenceItems.Where(ri => !referenceConflicts.Contains(ri.OriginalItem)),
ri => ItemUtilities.GetReferenceTargetFileName(ri.OriginalItem),
HandleRuntimeConflict,
commitWinner:false);
platformConflictScope.ResolveConflicts(copyLocalItems.Where(ci => !copyLocalConflicts.Contains(ci.OriginalItem)),
ri => ri.FileName,
HandleRuntimeConflict,
commitWinner: false);
platformConflictScope.ResolveConflicts(otherRuntimeItems,
ri => ri.FileName,
HandleRuntimeConflict,
commitWinner: false);
ReferencesWithoutConflicts = RemoveConflicts(References, referenceConflicts);
ReferenceCopyLocalPathsWithoutConflicts = RemoveConflicts(ReferenceCopyLocalPaths, copyLocalConflicts);
Conflicts = CreateConflictTaskItems(allConflicts);
}
private ITaskItem[] CreateConflictTaskItems(ICollection<ConflictItem> conflicts)
{
var conflictItems = new ITaskItem[conflicts.Count];
int i = 0;
foreach(var conflict in conflicts)
{
conflictItems[i++] = CreateConflictTaskItem(conflict);
}
return conflictItems;
}
private ITaskItem CreateConflictTaskItem(ConflictItem conflict)
{
var item = new TaskItem(conflict.SourcePath);
if (conflict.PackageId != null)
{
item.SetMetadata(nameof(ConflictItemType), conflict.ItemType.ToString());
}
return item;
}
private IEnumerable<ConflictItem> GetConflictTaskItems(ITaskItem[] items, ConflictItemType itemType)
{
return (items != null) ? items.Select(i => new ConflictItem(i, itemType)) : Enumerable.Empty<ConflictItem>();
}
private void HandleCompileConflict(ConflictItem conflictItem)
{
if (conflictItem.ItemType == ConflictItemType.Reference)
{
referenceConflicts.Add(conflictItem.OriginalItem);
}
allConflicts.Add(conflictItem);
}
private void HandleRuntimeConflict(ConflictItem conflictItem)
{
if (conflictItem.ItemType == ConflictItemType.Reference)
{
conflictItem.OriginalItem.SetMetadata(MetadataNames.Private, "False");
}
else if (conflictItem.ItemType == ConflictItemType.CopyLocal)
{
copyLocalConflicts.Add(conflictItem.OriginalItem);
}
allConflicts.Add(conflictItem);
}
/// <summary>
/// Filters conflicts from original, maintaining order.
/// </summary>
/// <param name="original"></param>
/// <param name="conflicts"></param>
/// <returns></returns>
private ITaskItem[] RemoveConflicts(ITaskItem[] original, ICollection<ITaskItem> conflicts)
{
if (conflicts.Count == 0)
{
return original;
}
var result = new ITaskItem[original.Length - conflicts.Count];
int index = 0;
foreach(var originalItem in original)
{
if (!conflicts.Contains(originalItem))
{
if (index >= result.Length)
{
throw new ArgumentException($"Items from {nameof(conflicts)} were missing from {nameof(original)}");
}
result[index++] = originalItem;
}
}
return result;
}
}
}