<Project>

  <PropertyGroup>
    <StaticWebAssetsSdkRoot>$(RepoRoot)\src\StaticWebAssetsSdk\</StaticWebAssetsSdkRoot>
    <PackageId>Microsoft.NET.Sdk.StaticWebAssets</PackageId>
    <OutDirName>$(Configuration)\Sdks\$(PackageId)\tasks</OutDirName>
  </PropertyGroup>

  <Import Project="Sdk.props" Sdk="Microsoft.NET.Sdk" />

  <PropertyGroup>
    <TargetFrameworks>$(SdkTargetFramework);net472</TargetFrameworks>
    <OutputType>Library</OutputType>
    <Description>SDK for building and publishing applications containing static web assets.</Description>
    <RootNamespace>Microsoft.NET.Sdk.StaticWebAssets</RootNamespace>

    <GenerateDependencyFile>false</GenerateDependencyFile>
    <IncludeBuildOutput>false</IncludeBuildOutput>

    <NoPackageAnalysis>true</NoPackageAnalysis>
    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>

    <CopyLocalLockFileAssemblies Condition="'$(TargetFramework)'!='net472'">false</CopyLocalLockFileAssemblies>
    <!-- MSBuild Task DLLs need to be versioned with every build -->
    <AutoGenerateAssemblyVersion>true</AutoGenerateAssemblyVersion>

    <OutputPath>$(BaseOutputPath)</OutputPath>
    <IsPackable>true</IsPackable>
    <PackageLayoutOutputPath>$(ArtifactsBinDir)$(Configuration)\Sdks\$(PackageId)\</PackageLayoutOutputPath>
    <NoDefaultExcludes>true</NoDefaultExcludes>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Build.Framework" ExcludeAssets="Runtime" />
    <PackageReference Include="Microsoft.Build.Utilities.Core" ExcludeAssets="Runtime" />
    <PackageReference Include="Microsoft.Extensions.FileSystemGlobbing" />
    <PackageReference Include="Microsoft.Css.Parser" />
  </ItemGroup>

  <ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework'">
    <PackageReference Include="System.Reflection.Metadata" VersionOverride="$(SystemReflectionMetadataVersion)" />
    <PackageReference Include="System.Text.Json" />
  </ItemGroup>

  <!-- Only include these files in the outer build to avoid double writes. -->
  <ItemGroup Condition="'$(IsCrossTargetingBuild)' == 'true'">
    <AdditionalContent Include="$(StaticWebAssetsSdkRoot)Targets\**\*.*"
                       Pack="true"
                       PackagePath="targets" />
    <AdditionalContent Include="$(StaticWebAssetsSdkRoot)Sdk\**\*.*"
                       Pack="true"
                       PackagePath="Sdk" />
  </ItemGroup>

  <ItemDefinitionGroup>
    <PackageReference>
      <PrivateAssets>All</PrivateAssets>
      <Publish>true</Publish>
    </PackageReference>
  </ItemDefinitionGroup>

  <Target Name="CopyFileSystemGlobbing"
          BeforeTargets="PrepareAdditionalFilesToLayout"
          AfterTargets="ResolveReferences"
          Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp'">
    <ItemGroup>
      <_FileSystemGlobbing Include="@(ReferencePath)" Condition="'%(ReferencePath.NuGetPackageId)' == 'Microsoft.Extensions.FileSystemGlobbing'" />
      <_FileSystemGlobbingContent Include="@(_FileSystemGlobbing)" TargetPath="tasks\$(TargetFramework)\%(_FileSystemGlobbing.Filename)%(_FileSystemGlobbing.Extension)" />
      <AdditionalContent Include="@(_FileSystemGlobbing)" PackagePath="tasks\$(TargetFramework)" />
    </ItemGroup>

    <Copy SourceFiles="@(_FileSystemGlobbingContent)"
          DestinationFiles="@(_FileSystemGlobbingContent->'$(PackageLayoutOutputPath)%(TargetPath)')"
          SkipUnchangedFiles="true">
      <Output TaskParameter="DestinationFiles" ItemName="FileWrites" />
    </Copy>
  </Target>

  <Target Name="CopyCssParser"
          BeforeTargets="PrepareAdditionalFilesToLayout"
          AfterTargets="ResolveReferences"
          Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp'">
    <ItemGroup>
      <_CssParser Include="@(ReferencePath->WithMetadataValue('NuGetPackageId', 'Microsoft.Css.Parser'))" />
      <_CssParserContent Include="@(_CssParser)" TargetPath="tasks\$(TargetFramework)\%(_CssParser.Filename)%(_CssParser.Extension)" />
      <AdditionalContent Include="@(_CssParser)" PackagePath="tasks\$(TargetFramework)" />
    </ItemGroup>

    <Copy SourceFiles="@(_CssParserContent)"
          DestinationFiles="@(_CssParserContent->'$(PackageLayoutOutputPath)%(TargetPath)')"
          SkipUnchangedFiles="true">
      <Output TaskParameter="DestinationFiles" ItemName="FileWrites" />
    </Copy>
  </Target>

  <Target Name="PrepareAdditionalFilesToLayout" BeforeTargets="AssignTargetPaths">
    <ItemGroup>
      <LayoutFile Include="@(AdditionalContent)"
                  Condition="'%(AdditionalContent.PackagePath)' != '' and '%(AdditionalContent.PackagePath)' != 'Icon.png'"
                  TargetPath="%(AdditionalContent.PackagePath)\%(AdditionalContent.RecursiveDir)%(AdditionalContent.Filename)%(AdditionalContent.Extension)" />
    </ItemGroup>
  </Target>

  <Target Name="CopyAdditionalFilesToLayout"
          DependsOnTargets="PrepareAdditionalFilesToLayout"
          AfterTargets="Build"
          Inputs="@(LayoutFile)"
          Outputs="@(LayoutFile->'$(PackageLayoutOutputPath)%(TargetPath)')">
    <Copy SourceFiles="@(LayoutFile)" DestinationFiles="@(LayoutFile->'$(PackageLayoutOutputPath)%(TargetPath)')">
      <Output TaskParameter="DestinationFiles" ItemName="FileWrites" />
    </Copy>
  </Target>

  <Target Name="PackLayout"
          DependsOnTargets="CopyAdditionalFilesToLayout"
          BeforeTargets="$(GenerateNuspecDependsOn)">
    <Message Importance="high" Text="Packing assets at $(PackageLayoutOutputPath)" />

    <ItemGroup>
      <Content Include="$(PackageLayoutOutputPath)**\*" PackagePath="\" />
    </ItemGroup>
  </Target>

  <!-- Global usings -->
  <!-- See: https://learn.microsoft.com/dotnet/core/project-sdk/msbuild-props#using -->
  <ItemGroup>
    <!-- This causes conflicts with System.Threading.Tasks.Task. Giving it an alias to avoid the conflict. -->
    <Using Include="Microsoft.Build.Utilities.Task" Alias="Task" />
  </ItemGroup>

  <Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" />

</Project>
