-
Notifications
You must be signed in to change notification settings - Fork 1k
/
ResolveStaticWebAssetsEmbeddedProjectConfiguration.cs
259 lines (223 loc) · 13.6 KB
/
ResolveStaticWebAssetsEmbeddedProjectConfiguration.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// 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;
using Microsoft.Build.Utilities;
namespace Microsoft.AspNetCore.StaticWebAssets.Tasks
{
// Handles the logic for resolving configurations and embedding rules during
// single targeting builds and multi-targeting builds.
// In single targeting builds, passing the TargetFramework is needed. We determine
// which configurations are applicable to a given target framework based on the rules.
// In multi-targeting builds, passing the TargetFrameworks is needed. We determine
// which rules are applicable, and for those configurations we determine are applicable
// we remove them from the list of FilteredCrossTargetingBuildConfigurations.
// We filter the build configurations in cross targeting to reflect the dependencies between
// TFMs, and we avoid building them in the multi-targeted build.
public class ResolveStaticWebAssetsEmbeddedProjectConfiguration : Task
{
[Required]
public ITaskItem[] StaticWebAssetProjectConfiguration { get; set; }
[Required]
public ITaskItem[] EmbeddingConfiguration { get; set; }
public string TargetFramework { get; set; }
public ITaskItem[] TargetFrameworks { get; set; } = Array.Empty<ITaskItem>();
public ITaskItem[] CrossTargetingBuildConfigurations { get; set; } = Array.Empty<ITaskItem>();
[Output]
public ITaskItem[] EmbeddedProjectAssetConfigurations { get; set; }
[Output]
public ITaskItem[] FilteredCrossTargetingBuildConfigurations { get; set; }
public override bool Execute()
{
var embeddedProjectAssetConfigurations = new EmbeddedStaticWebAssetProjectConfiguration[StaticWebAssetProjectConfiguration.Length];
for (var i = 0; i < StaticWebAssetProjectConfiguration.Length; i++)
{
embeddedProjectAssetConfigurations[i] = EmbeddedStaticWebAssetProjectConfiguration.FromTaskItem(StaticWebAssetProjectConfiguration[i]);
}
var embeddingRules = new StaticWebAssetEmbeddingConfiguration[EmbeddingConfiguration.Length];
for (var i = 0; i < EmbeddingConfiguration.Length; i++)
{
embeddingRules[i] = StaticWebAssetEmbeddingConfiguration.FromTaskItem(EmbeddingConfiguration[i]);
}
var targetFrameworks = TargetFrameworks.Any() ? TargetFrameworks.Select(t => t.ItemSpec).ToArray() : new[] { TargetFramework };
var matchingConfigurations = new List<EmbeddedStaticWebAssetProjectConfiguration>();
foreach (var targetFramework in targetFrameworks)
{
Log.LogMessage("Evaluating rules for target framework: '{0}'", targetFramework);
foreach (var rule in embeddingRules)
{
Log.LogMessage("Evaluate rule: '{0}'", rule.Id);
if (string.Equals(rule.Id, targetFramework, StringComparison.Ordinal))
{
Log.LogMessage("Rule matches target framework: '{0}'", rule.Id);
foreach (var configuration in embeddedProjectAssetConfigurations)
{
if (Matches(configuration, rule))
{
matchingConfigurations.Add(configuration);
}
}
}
}
}
if (CrossTargetingBuildConfigurations.Any())
{
var filteredConfigurations = new List<ITaskItem>();
foreach (var configurationsToRemove in CrossTargetingBuildConfigurations)
{
foreach (var embeddedConfiguration in matchingConfigurations)
{
if (configurationsToRemove.GetMetadata("AdditionalProperties")?
.Contains($"TargetFramework={embeddedConfiguration.TargetFramework}") == true)
{
Log.LogMessage($"Removing configuration '{configurationsToRemove.GetMetadata("AdditonalProperties")}' because it is embedded.");
// We remove the configuration because there is a rule to embed it.
filteredConfigurations.Add(configurationsToRemove);
}
}
}
FilteredCrossTargetingBuildConfigurations = CrossTargetingBuildConfigurations.Except(filteredConfigurations).ToArray();
}
EmbeddedProjectAssetConfigurations = matchingConfigurations.Select(c => c.ToTaskItem()).ToArray();
return !Log.HasLoggedErrors;
}
private bool Matches(EmbeddedStaticWebAssetProjectConfiguration configuration, StaticWebAssetEmbeddingConfiguration rule)
{
// If the rule specifies a concrete target framework, it must match.
if (!string.IsNullOrWhiteSpace(rule.TargetFramework) &&
!string.Equals(rule.TargetFramework, configuration.TargetFramework, StringComparison.Ordinal))
{
Log.LogMessage("Project configuration not applicable due to framework mismatch: '{0}' != '{1}'", rule.TargetFramework, configuration.TargetFramework);
return false;
}
// If the rule specifies a concrete target framework identifier, it must match.
if (!string.IsNullOrWhiteSpace(rule.TargetFrameworkIdentifier) &&
!string.Equals(rule.TargetFrameworkIdentifier, configuration.TargetFrameworkIdentifier, StringComparison.Ordinal))
{
Log.LogMessage("Project configuration not applicable due to framework identifier mismatch: '{0}' != '{1}'", rule.TargetFrameworkIdentifier, configuration.TargetFrameworkIdentifier);
return false;
}
// If the rule specifies a concrete target framework version, it must match.
if (!string.IsNullOrWhiteSpace(rule.TargetFrameworkVersion) &&
!string.Equals(rule.TargetFrameworkVersion, configuration.TargetFrameworkVersion, StringComparison.Ordinal))
{
Log.LogMessage("Project configuration not applicable due to framework version mismatch: '{0}' != '{1}'", rule.TargetFrameworkVersion, configuration.TargetFrameworkVersion);
return false;
}
// If the rule specifies a concrete platform, it must match.
if (!string.IsNullOrWhiteSpace(rule.Platform) &&
!string.Equals(rule.Platform, configuration.Platform, StringComparison.Ordinal))
{
Log.LogMessage("Project configuration not applicable due to platform mismatch: '{0}' != '{1}'", rule.Platform, configuration.Platform);
return false;
}
// If the rule specifies a concrete platform version, it must match.
if (!string.IsNullOrWhiteSpace(rule.PlatformVersion) &&
!string.Equals(rule.PlatformVersion, configuration.PlatformVersion, StringComparison.Ordinal))
{
Log.LogMessage("Project configuration not applicable due to platform version mismatch: '{0}' != '{1}'", rule.PlatformVersion, configuration.PlatformVersion);
return false;
}
Log.LogMessage("Project configuration applicable: '{0}'", configuration.TargetFramework);
return true;
}
}
public class EmbeddedStaticWebAssetProjectConfiguration
{
public string Id { get; set; }
public string Version { get; set; }
public string Source { get; set; }
public string GetEmbeddedBuildAssetsTargets { get; set; }
public string AdditionalEmbeddedBuildProperties { get; set; }
public string AdditionalEmbeddedBuildPropertiesToRemove { get; set; }
public string GetEmbeddedPublishAssetsTargets { get; set; }
public string AdditionalEmbeddedPublishProperties { get; set; }
public string AdditionalEmbeddedPublishPropertiesToRemove { get; set; }
public string TargetFramework { get; set; }
public string TargetFrameworkIdentifier { get; set; }
public string TargetFrameworkVersion { get; set; }
public string Platform { get; set; }
public string PlatformVersion { get; set; }
public ITaskItem2 ToTaskItem()
{
var result = new TaskItem(Id);
result.SetMetadata(nameof(Version), Version);
result.SetMetadata(nameof(Source), Source);
result.SetMetadata(nameof(GetEmbeddedBuildAssetsTargets), GetEmbeddedBuildAssetsTargets);
result.SetMetadata(nameof(AdditionalEmbeddedBuildProperties), AdditionalEmbeddedBuildProperties);
result.SetMetadata(nameof(AdditionalEmbeddedBuildPropertiesToRemove), AdditionalEmbeddedBuildPropertiesToRemove);
result.SetMetadata(nameof(GetEmbeddedPublishAssetsTargets), GetEmbeddedPublishAssetsTargets);
result.SetMetadata(nameof(AdditionalEmbeddedPublishProperties), AdditionalEmbeddedPublishProperties);
result.SetMetadata(nameof(AdditionalEmbeddedPublishPropertiesToRemove), AdditionalEmbeddedPublishPropertiesToRemove);
result.SetMetadata(nameof(TargetFramework), TargetFramework);
return result;
}
public static EmbeddedStaticWebAssetProjectConfiguration FromTaskItem(ITaskItem source)
{
return new EmbeddedStaticWebAssetProjectConfiguration
{
Id = source.ItemSpec,
Version = source.GetMetadata(nameof(Version)),
Source = source.GetMetadata(nameof(Source)),
GetEmbeddedBuildAssetsTargets = source.GetMetadata(nameof(GetEmbeddedBuildAssetsTargets)),
AdditionalEmbeddedBuildProperties = source.GetMetadata(nameof(AdditionalEmbeddedBuildProperties)),
AdditionalEmbeddedBuildPropertiesToRemove = source.GetMetadata(nameof(AdditionalEmbeddedBuildPropertiesToRemove)),
GetEmbeddedPublishAssetsTargets = source.GetMetadata(nameof(GetEmbeddedPublishAssetsTargets)),
AdditionalEmbeddedPublishProperties = source.GetMetadata(nameof(AdditionalEmbeddedPublishProperties)),
AdditionalEmbeddedPublishPropertiesToRemove = source.GetMetadata(nameof(AdditionalEmbeddedPublishPropertiesToRemove)),
TargetFramework = source.GetMetadata(nameof(TargetFramework)),
TargetFrameworkIdentifier = source.GetMetadata(nameof(TargetFrameworkIdentifier)),
TargetFrameworkVersion = source.GetMetadata(nameof(TargetFrameworkVersion)),
Platform = source.GetMetadata(nameof(Platform)),
PlatformVersion = source.GetMetadata(nameof(PlatformVersion))
};
}
}
// Defines the rules for which other TFM's assets should be embedded into the current TFM.
// The rules are defined as an embeddedConfiguration group as follows
// <StaticWebAssetsEmbeddingConfiguration Include="<<target-tfm>>">
// <Platform>browser</Platform>
// <StaticWebAssetsEmbeddingConfiguration>
// For example, to embed wasm asssets within a non wasm project, you would have the following
// <StaticWebAssetsEmbeddingConfiguration Include="$(TargetFramework)"
// Condition="'$([MSBuild]::GetTargetPlatformIdentifier($(TargetFramework)))' == ''">
// <Platform>browser</Platform>
// <StaticWebAssetsEmbeddingConfiguration>
// This essentially enables the rule when we are targeting for example, net8.0, but does not
// require the rule to be defined for net8.0 specifically.
// It also means that if in the future you are targeting net9.0, you don't need to update the
// browser to net9.0-browser.
// During the evaluation, we match the rule ID (in the Include attribute) with the current TFM
// and if it matches, we evaluate all the other metadata against the available project configurations
// and determine all the ones that match, which we return.
public class StaticWebAssetEmbeddingConfiguration
{
public string Id { get; set; }
public string TargetFramework { get; set; }
public string Platform { get; set; }
public string PlatformVersion { get; set; }
public string TargetFrameworkIdentifier { get; set; }
public string TargetFrameworkVersion { get; set; }
public static StaticWebAssetEmbeddingConfiguration FromTaskItem(ITaskItem item)
{
return new StaticWebAssetEmbeddingConfiguration
{
Id = item.ItemSpec,
TargetFramework = item.GetMetadata("TargetFramework"),
Platform = item.GetMetadata("Platform"),
PlatformVersion = item.GetMetadata("PlatformVersion"),
TargetFrameworkIdentifier = item.GetMetadata("TargetFrameworkIdentifier"),
TargetFrameworkVersion = item.GetMetadata("TargetFrameworkVersion"),
};
}
public static ITaskItem2 ToTaskItem(StaticWebAssetEmbeddingConfiguration configuration)
{
var item = new TaskItem(configuration.Id);
item.SetMetadata("TargetFramework", configuration.TargetFramework);
item.SetMetadata("Platform", configuration.Platform);
item.SetMetadata("PlatformVersion", configuration.PlatformVersion);
item.SetMetadata("TargetFrameworkIdentifier", configuration.TargetFrameworkIdentifier);
item.SetMetadata("TargetFrameworkVersion", configuration.TargetFrameworkVersion);
return item;
}
}
}