File: ResolveStaticWebAssetEndpointRoutes.cs
Web Access
Project: ..\..\..\src\StaticWebAssetsSdk\Tasks\Microsoft.NET.Sdk.StaticWebAssets.Tasks.csproj (Microsoft.NET.Sdk.StaticWebAssets.Tasks)
// 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 ResolveStaticWebAssetEndpointRoutes : Task
{
    [Required] public ITaskItem[] Endpoints { get; set; } = [];
 
    [Required] public ITaskItem[] Assets { get; set; } = [];
 
    [Output] public ITaskItem[] ResolvedEndpoints { get; set; } = [];
 
    public override bool Execute()
    {
        var endpoints = StaticWebAssetEndpoint.FromItemGroup(Endpoints);
        var assets = StaticWebAsset.ToAssetDictionary(Assets);
 
        foreach (var endpoint in endpoints)
        {
            if (!assets.TryGetValue(endpoint.AssetFile, out var asset))
            {
                Log.LogError($"The asset file '{endpoint.AssetFile}' for endpoint '{endpoint.Route}' was not found.");
                return false;
            }
            var route = asset.ReplaceTokens(endpoint.Route, StaticWebAssetTokenResolver.Instance);
            endpoint.Route = route;
        }
 
        ResolvedEndpoints = endpoints.Select(e => e.ToTaskItem()).ToArray();
 
        return !Log.HasLoggedErrors;
    }
}