-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
MergeStaticWebAssets.cs
169 lines (144 loc) · 7.21 KB
/
MergeStaticWebAssets.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
// 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.AspNetCore.StaticWebAssets.Tasks;
public class MergeStaticWebAssets : Task
{
[Required]
public ITaskItem[] CandidateAssets { get; set; }
[Required]
public ITaskItem[] CandidateDiscoveryPatterns { get; set; }
public string MergeTarget { get; set; } = "";
[Output]
public ITaskItem[] MergedAssets { get; set; }
[Output]
public ITaskItem[] MergedDiscoveryPatterns { get; set; }
public override bool Execute()
{
var assets = CandidateAssets.OrderBy(a => a.GetMetadata("FullPath")).Select(StaticWebAsset.FromTaskItem);
var assetsByTargetPath = assets
.GroupBy(a => a.ComputeTargetPath("", '/'), StringComparer.OrdinalIgnoreCase)
.ToDictionary(g => g.Key, g => g.ToList());
foreach (var kvp in assetsByTargetPath)
{
var group = kvp.Value;
if (group.Count > 1)
{
Log.LogMessage(MessageImportance.Normal, $"Merging '{group.Count}' assets for {kvp.Key}.");
ApplyMergeRules(group, MergeTarget);
}
}
MergedAssets = assetsByTargetPath.Values.SelectMany(g => g).Select(a => a.ToTaskItem()).ToArray();
// We always want to merge the discovery patterns, we just need to remove duplicates if any.
var candidates = CandidateDiscoveryPatterns.Select(StaticWebAssetsDiscoveryPattern.FromTaskItem).ToList();
for (var i = candidates.Count - 1; i > 0; i--)
{
var candidate = candidates[i];
for (var j = i - 1; j >= 0; j--)
{
var other = candidates[j];
if (candidate.Equals(other))
{
Log.LogMessage(MessageImportance.Normal, $"Removing '{candidate.ContentRoot}' because it is a duplicate of '{other.ContentRoot}'.");
candidates.RemoveAt(i);
break;
}
}
}
MergedDiscoveryPatterns = candidates.Select(a => a.ToTaskItem()).ToArray();
return !Log.HasLoggedErrors;
}
internal void ApplyMergeRules(List<StaticWebAsset> group, string source)
{
// All the assets in the group target the same relative path. The normal outcome for that is a conflict,
// except when we are merging assets across targets, in which case there are rules to determine what happens.
StaticWebAsset prototypeItem = null;
StaticWebAsset build = null;
StaticWebAsset publish = null;
StaticWebAsset all = null;
var assetsToRemove = new List<StaticWebAsset>();
foreach (var item in group)
{
prototypeItem ??= item;
if (!ReferenceEquals(prototypeItem, item) && string.Equals(prototypeItem.Identity, item.Identity, OSPath.PathComparison))
{
var assetToRemove = SelectAssetToRemove(prototypeItem, item, source);
if (assetToRemove != null)
{
Log.LogMessage(MessageImportance.Normal, $"Removing '{assetToRemove.Identity}' because merge behavior is {assetToRemove.AssetMergeBehavior}.");
assetsToRemove.Add(assetToRemove);
continue;
}
}
if (!prototypeItem.HasSourceId(item.SourceId))
{
var assetToRemove = SelectAssetToRemove(prototypeItem, item, source);
if (assetToRemove != null)
{
Log.LogMessage(MessageImportance.Normal, $"Removing '{assetToRemove.Identity}' because merge behavior is {assetToRemove.AssetMergeBehavior}.");
assetsToRemove.Add(assetToRemove);
continue;
}
}
build ??= item.IsBuildOnly() ? item : build;
if (build != null && item.IsBuildOnly() && !ReferenceEquals(build, item))
{
var assetToRemove = SelectAssetToRemove(prototypeItem, item, source);
if (assetToRemove != null)
{
Log.LogMessage(MessageImportance.Normal, $"Removing '{assetToRemove.Identity}' because merge behavior is {assetToRemove.AssetMergeBehavior}.");
assetsToRemove.Add(assetToRemove);
continue;
}
}
publish ??= item.IsPublishOnly() ? item : publish;
if (publish != null && item.IsPublishOnly() && !ReferenceEquals(publish, item))
{
var assetToRemove = SelectAssetToRemove(prototypeItem, item, source);
if (assetToRemove != null)
{
Log.LogMessage(MessageImportance.Normal, $"Removing '{assetToRemove.Identity}' because merge behavior is {assetToRemove.AssetMergeBehavior}.");
assetsToRemove.Add(assetToRemove);
continue;
}
}
all ??= item.IsBuildAndPublish() ? item : all;
if (all != null && item.IsBuildAndPublish() && !ReferenceEquals(all, item))
{
var assetToRemove = SelectAssetToRemove(prototypeItem, item, source);
if (assetToRemove != null)
{
Log.LogMessage(MessageImportance.Normal, $"Removing '{assetToRemove.Identity}' because merge behavior is {assetToRemove.AssetMergeBehavior}.");
assetsToRemove.Add(assetToRemove);
continue;
}
}
}
foreach (var asset in assetsToRemove)
{
group.Remove(asset);
}
StaticWebAsset SelectAssetToRemove(StaticWebAsset left, StaticWebAsset right, string mergeTarget)
{
var leftMergeSource = left.AssetMergeSource;
var rightMergeSource = right.AssetMergeSource;
if (string.Equals(leftMergeSource, rightMergeSource, StringComparison.Ordinal))
{
Log.LogMessage(MessageImportance.Normal, $"Skipping '{right.Identity}' because it is a duplicate of '{left.Identity}'.");
return null;
}
var (targetAsset, sourceAsset) = string.Equals(leftMergeSource, mergeTarget) ? (left, right) : (right, left);
if (!string.Equals(targetAsset.AssetMergeBehavior, sourceAsset.AssetMergeBehavior, StringComparison.Ordinal))
{
Log.LogMessage(MessageImportance.Normal, $"Skipping '{sourceAsset.Identity}' because merge behavior '{sourceAsset.AssetMergeBehavior}' is different from '{targetAsset.AssetMergeBehavior}'.");
return null;
}
var behavior = targetAsset.AssetMergeBehavior;
// PreferTarget: The target asset wins.
// PreferSource: The source asset wins.
// Exclude: The assets are not merged, and a failure happens later on during validation.
return string.Equals(behavior, "PreferTarget", StringComparison.Ordinal) ? targetAsset :
(string.Equals(behavior, "PreferSource", StringComparison.Ordinal) ? sourceAsset : null);
}
}
}