File: Utils\AssetToCompress.cs
Web Access
Project: src\src\sdk\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;
using Microsoft.Build.Utilities;

namespace Microsoft.AspNetCore.StaticWebAssets.Tasks.Utils;

internal static class AssetToCompress
{
    public static bool TryFindInputFilePath(ITaskItem assetToCompress, TaskLoggingHelper log, out string fullPath)
    {
        // Check RelatedAsset first (the asset's Identity path) as it's more reliable.
        // RelatedAssetOriginalItemSpec may point to a project file (e.g., .esproj) rather than the actual asset.
        var relatedAsset = assetToCompress.GetMetadata("RelatedAsset");
        if (File.Exists(relatedAsset))
        {
            log.LogMessage(MessageImportance.Low, "Asset '{0}' found at path '{1}'.",
                assetToCompress.ItemSpec,
                relatedAsset);
            fullPath = relatedAsset;
            return true;
        }

        var relatedAssetOriginalItemSpec = assetToCompress.GetMetadata("RelatedAssetOriginalItemSpec");
        if (File.Exists(relatedAssetOriginalItemSpec))
        {
            log.LogMessage(MessageImportance.Low, "Asset '{0}' found at original item spec '{1}'.",
                assetToCompress.ItemSpec,
                relatedAssetOriginalItemSpec);
            fullPath = relatedAssetOriginalItemSpec;
            return true;
        }

        log.LogError("The asset '{0}' can not be found at any of the searched locations '{1}' and '{2}'.",
            assetToCompress.ItemSpec,
            relatedAsset,
            relatedAssetOriginalItemSpec);
        fullPath = null;
        return false;
    }
}