<!--
***********************************************************************************************
Microsoft.NET.Sdk.BlazorWebAssembly.ServiceWorkerAssetsManifest.targets

WARNING:  DO NOT MODIFY this file unless you are knowledgeable about MSBuild and have
          created a backup copy.  Incorrect changes to this file will make it
          impossible to load or build your projects from the command-line or the IDE.

Copyright (c) .NET Foundation. All rights reserved.
***********************************************************************************************
-->

<Project>
  <UsingTask TaskName="Microsoft.NET.Sdk.BlazorWebAssembly.GenerateServiceWorkerAssetsManifest" AssemblyFile="$(_BlazorWebAssemblySdkTasksAssembly)" />

  <Target Name="_ComputeServiceWorkerAssets" BeforeTargets="ResolveStaticWebAssetsInputs">

    <PropertyGroup>
      <_ServiceWorkerAssetsManifestIntermediateOutputPath Condition="'$([System.IO.Path]::IsPathRooted($(BaseIntermediateOutputPath)))' == 'true'">obj\$(Configuration)\$(TargetFramework)\$(ServiceWorkerAssetsManifest)</_ServiceWorkerAssetsManifestIntermediateOutputPath>
      <_ServiceWorkerAssetsManifestIntermediateOutputPath Condition="'$([System.IO.Path]::IsPathRooted($(BaseIntermediateOutputPath)))' != 'true'">$(IntermediateOutputPath)$(ServiceWorkerAssetsManifest)</_ServiceWorkerAssetsManifestIntermediateOutputPath>
      <_ServiceWorkerAssetsManifestFullPath>$([System.IO.Path]::GetFullPath('$(MSBuildProjectDirectory)/$(_ServiceWorkerAssetsManifestIntermediateOutputPath)'))</_ServiceWorkerAssetsManifestFullPath>
    </PropertyGroup>

    <ItemGroup>
      <_ManifestStaticWebAsset Include="$(_ServiceWorkerAssetsManifestFullPath)">
        <SourceType></SourceType>
        <SourceId>$(PackageId)</SourceId>
        <ContentRoot>$([MSBuild]::NormalizeDirectory('$(TargetDir)wwwroot\'))</ContentRoot>
        <BasePath>$(StaticWebAssetBasePath)</BasePath>
        <RelativePath>$(ServiceWorkerAssetsManifest)</RelativePath>
        <CopyToPublishDirectory>Never</CopyToPublishDirectory>
      </_ManifestStaticWebAsset>

      <!-- Figure out where we're getting the content for each @(ServiceWorker) entry, depending on whether there's a PublishedContent value -->
      <_ServiceWorkerIntermediateFile Include="@(ServiceWorker->'$(IntermediateOutputPath)serviceworkers\%(Identity)')">
        <ContentSourcePath Condition="'%(_ServiceWorker.PublishedContent)' != ''">%(ServiceWorker.PublishedContent)</ContentSourcePath>
        <ContentSourcePath Condition="'%(_ServiceWorker.PublishedContent)' == ''">%(ServiceWorker.Identity)</ContentSourcePath>
        <OriginalPath>%(ServiceWorker.Identity)</OriginalPath>
        <TargetOutputPath>%(ServiceWorker.Identity)</TargetOutputPath>
        <TargetOutputPath Condition="$([System.String]::Copy('%(ServiceWorker.Identity)').Replace('\','/').StartsWith('wwwroot/'))">$([System.String]::Copy('%(ServiceWorker.Identity)').Substring(8))</TargetOutputPath>
      </_ServiceWorkerIntermediateFile>

      <_ServiceWorkerStaticWebAsset Include="%(_ServiceWorkerIntermediateFile.FullPath)">
        <SourceType></SourceType>
        <SourceId>$(PackageId)</SourceId>
        <ContentRoot>$([MSBuild]::NormalizeDirectory('$(TargetDir)wwwroot\'))</ContentRoot>
        <BasePath>$(StaticWebAssetBasePath)</BasePath>
        <RelativePath>%(TargetOutputPath)</RelativePath>
        <CopyToPublishDirectory>Never</CopyToPublishDirectory>
      </_ServiceWorkerStaticWebAsset>

      <StaticWebAsset Include="
          @(_ManifestStaticWebAsset);
          @(_ServiceWorkerStaticWebAsset)" />
    </ItemGroup>

  </Target>

  <Target Name="_WriteServiceWorkerAssetsManifest"
    DependsOnTargets="_ComputeServiceWorkerAssets;ResolveStaticWebAssetsInputs">

    <ItemGroup>
      <_ServiceWorkItem Include="@(StaticWebAsset)" Exclude="$(_ServiceWorkerAssetsManifestFullPath);@(_ServiceWorkerStaticWebAsset)">
        <AssetUrl>$([System.String]::Copy('$([System.String]::Copy('%(StaticWebAsset.BasePath)').TrimEnd('/'))/%(StaticWebAsset.RelativePath)').Replace('\','/').TrimStart('/'))</AssetUrl>
      </_ServiceWorkItem>
    </ItemGroup>

    <GenerateServiceWorkerAssetsManifest
      Version="$(ServiceWorkerAssetsManifestVersion)"
      Assets="@(_ServiceWorkItem)"
      OutputPath="$(_ServiceWorkerAssetsManifestIntermediateOutputPath)">
      <Output TaskParameter="CalculatedVersion" PropertyName="_ServiceWorkerAssetsManifestVersion" />
    </GenerateServiceWorkerAssetsManifest>

    <Copy
      SourceFiles="%(_ServiceWorkerIntermediateFile.ContentSourcePath)"
      DestinationFiles="%(_ServiceWorkerIntermediateFile.Identity)" />

    <WriteLinesToFile
      File="%(_ServiceWorkerIntermediateFile.Identity)"
      Lines="/* Manifest version: $(_ServiceWorkerAssetsManifestVersion) */"
      Condition="'$(_ServiceWorkerAssetsManifestVersion)' != ''" />

    <ItemGroup>
      <FileWrites Include="@(_ServiceWorkerIntermediateFile)" />
      <FileWrites Include="$(_ServiceWorkerAssetsManifestIntermediateOutputPath)" />
    </ItemGroup>

  </Target>

  <Target Name="_BlazorStaticAssetsCopyFilesToOutputDirectory" AfterTargets="CopyFilesToOutputDirectory" DependsOnTargets="_WriteServiceWorkerAssetsManifest">
    <Copy
        SourceFiles="@(_ManifestStaticWebAsset);@(_ServiceWorkerStaticWebAsset)"
        DestinationFiles="$(OutDir)wwwroot\%(RelativePath)"
        SkipUnchangedFiles="$(SkipCopyUnchangedFiles)"
        OverwriteReadOnlyFiles="$(OverwriteReadOnlyFiles)"
        Retries="$(CopyRetryCount)"
        RetryDelayMilliseconds="$(CopyRetryDelayMilliseconds)"
        UseHardlinksIfPossible="$(CreateHardLinksForCopyFilesToOutputDirectoryIfPossible)"
        UseSymboliclinksIfPossible="$(CreateSymbolicLinksForCopyFilesToOutputDirectoryIfPossible)"
        ErrorIfLinkFails="$(ErrorIfLinkFailsForCopyFilesToOutputDirectory)"
        Condition="Exists('%(Identity)')">

      <Output TaskParameter="DestinationFiles" ItemName="FileWrites"/>
    </Copy>
  </Target>

  <Target Name="_OmitServiceWorkerContent"
    BeforeTargets="AssignTargetPaths;ResolveCurrentProjectStaticWebAssetsInputs">

    <ItemGroup>
      <!-- Don't emit the service worker source files to the output -->
      <Content Remove="@(ServiceWorker)" />
      <Content Remove="@(ServiceWorker->'%(PublishedContent)')" />
    </ItemGroup>
  </Target>

  <Target Name="_GenerateServiceWorkerFileForPublish"
    BeforeTargets="_BlazorCompressPublishFiles"
    AfterTargets="_ProcessPublishFilesForBlazor">

    <PropertyGroup>
      <_ServiceWorkerAssetsManifestPublishIntermediateOutputPath>$(IntermediateOutputPath)publish-$(ServiceWorkerAssetsManifest)</_ServiceWorkerAssetsManifestPublishIntermediateOutputPath>
    </PropertyGroup>

    <ItemGroup>
      <_ServiceWorkerIntermediatePublishFile Include="$(IntermediateOutputPath)serviceworkers\%(FileName).publish%(Extension)">
        <ContentSourcePath Condition="'%(ServiceWorker.PublishedContent)' != ''">%(ServiceWorker.PublishedContent)</ContentSourcePath>
        <ContentSourcePath Condition="'%(ServiceWorker.PublishedContent)' == ''">%(ServiceWorker.Identity)</ContentSourcePath>
        <RelativePath>%(ServiceWorker.Identity)</RelativePath>
        <RelativePath Condition="$([System.String]::Copy('%(ServiceWorker.Identity)').Replace('/','\').StartsWith('wwwroot\'))">$(_BlazorPublishOutputPath)$([System.String]::Copy('%(ServiceWorker.Identity)').Substring(8))</RelativePath>
      </_ServiceWorkerIntermediatePublishFile>

      <_ServiceWorkerPublishFile Include="@(ResolvedFileToPublish)" Condition="$([System.String]::Copy('%(ResolvedFileToPublish.RelativePath)').Replace('\','/').StartsWith('wwwroot/'))">
        <AssetUrl>$([System.String]::Copy('%(ResolvedFileToPublish.RelativePath)').Replace('\','/').TrimStart('/'))</AssetUrl>
        <AssetUrl>$([System.String]::Copy('%(RelativePath)').Replace('\','/').Substring(8))</AssetUrl>
      </_ServiceWorkerPublishFile>
    </ItemGroup>

    <GenerateServiceWorkerAssetsManifest
      Version="$(ServiceWorkerAssetsManifestVersion)"
      Assets="@(_ServiceWorkerPublishFile)"
      OutputPath="$(_ServiceWorkerAssetsManifestPublishIntermediateOutputPath)">

      <Output TaskParameter="CalculatedVersion" PropertyName="_ServiceWorkerPublishAssetsManifestVersion" />
    </GenerateServiceWorkerAssetsManifest>

    <Copy SourceFiles="%(_ServiceWorkerIntermediatePublishFile.ContentSourcePath)"
      DestinationFiles="%(_ServiceWorkerIntermediatePublishFile.Identity)" />

    <WriteLinesToFile
      File="%(_ServiceWorkerIntermediatePublishFile.Identity)"
      Lines="/* Manifest version: $(_ServiceWorkerPublishAssetsManifestVersion) */" />

    <ItemGroup>
      <ResolvedFileToPublish
        Include="@(_ServiceWorkerIntermediatePublishFile)"
        CopyToPublishDirectory="PreserveNewest"
        RelativePath="%(_ServiceWorkerIntermediatePublishFile.RelativePath)"
        ExcludeFromSingleFile="true" />

      <ResolvedFileToPublish
        Include="$(_ServiceWorkerAssetsManifestPublishIntermediateOutputPath)"
        CopyToPublishDirectory="PreserveNewest"
        RelativePath="$(_BlazorPublishOutputPath)$(ServiceWorkerAssetsManifest)"
        ExcludeFromSingleFile="true" />
    </ItemGroup>
  </Target>

</Project>
