-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathComputeEndpointsForReferenceStaticWebAssets.cs
More file actions
70 lines (55 loc) · 2.82 KB
/
ComputeEndpointsForReferenceStaticWebAssets.cs
File metadata and controls
70 lines (55 loc) · 2.82 KB
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#nullable disable
using Microsoft.Build.Framework;
namespace Microsoft.AspNetCore.StaticWebAssets.Tasks;
public class ComputeEndpointsForReferenceStaticWebAssets : Task
{
[Required]
public ITaskItem[] Assets { get; set; }
[Required]
public ITaskItem[] CandidateEndpoints { get; set; }
[Output]
public ITaskItem[] Endpoints { get; set; }
public override bool Execute()
{
var assets = StaticWebAsset.ToAssetDictionary(Assets);
var candidateEndpoints = StaticWebAssetEndpoint.FromItemGroup(CandidateEndpoints);
var endpoints = new List<StaticWebAssetEndpoint>();
foreach (var candidateEndpoint in candidateEndpoints)
{
if (assets.TryGetValue(candidateEndpoint.AssetFile, out var asset))
{
// We need to adjust the path to include the base path for the asset, since this is going to be used
// as a reference.
// Note that the caller is responsible for ensuring that only assets meant for the current project and
// destined to be used as a reference by other project are passed to this task.
var oldRoute = candidateEndpoint.Route;
if (oldRoute.StartsWith(asset.BasePath))
{
Log.LogMessage(MessageImportance.Low, "Skipping endpoint '{0}' because route '{1}' is already updated.", asset.Identity, oldRoute);
}
else
{
candidateEndpoint.Route = StaticWebAsset.CombineNormalizedPaths("", asset.BasePath, candidateEndpoint.Route, '/');
for (var i = 0; i < candidateEndpoint.EndpointProperties.Length; i++)
{
ref var property = ref candidateEndpoint.EndpointProperties[i];
if (string.Equals(property.Name, "label", StringComparison.OrdinalIgnoreCase))
{
property.Value = StaticWebAsset.CombineNormalizedPaths("", asset.BasePath, property.Value, '/');
}
}
Log.LogMessage(MessageImportance.Low, "Adding endpoint {0} for asset {1} with updated route {2}.", candidateEndpoint.Route, candidateEndpoint.AssetFile, candidateEndpoint.Route);
endpoints.Add(candidateEndpoint);
}
}
else
{
Log.LogMessage(MessageImportance.Low, "Skipping endpoint {0} because the asset {1} was not found.", candidateEndpoint.Route, candidateEndpoint.AssetFile);
}
}
Endpoints = StaticWebAssetEndpoint.ToTaskItems(endpoints);
return true;
}
}