<Project>

  <PropertyGroup>
    <RazorSdkRoot>$(RepoRoot)\src\RazorSdk\</RazorSdkRoot>
    <PackageId>Microsoft.NET.Sdk.Razor</PackageId>
    <OutDirName>$(Configuration)\Sdks\$(PackageId)\tasks</OutDirName>
  </PropertyGroup>

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

  <PropertyGroup>
    <OutputType>Library</OutputType>

    <EnableDefaultItems>false</EnableDefaultItems>
    <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>

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

  <ItemGroup>
    <PackageReference Include="Microsoft.Build.Framework" ExcludeAssets="Runtime" />
    <PackageReference Include="Microsoft.Build.Utilities.Core" ExcludeAssets="Runtime" />
    <PackageReference Include="System.Reflection.Metadata" VersionOverride="$(SystemReflectionMetadataVersion)" Condition="'$(TargetFramework)'=='net472'" />
    <PackageReference Include="System.Text.Json" Condition="'$(TargetFramework)'=='net472'" />
    <PackageReference Include="Microsoft.Extensions.FileSystemGlobbing" />
  </ItemGroup>

  <ItemGroup>
    <AdditionalContent Include="$(RazorSdkRoot)Targets\**\*.*">
      <Pack>true</Pack>
      <PackagePath>targets</PackagePath>
    </AdditionalContent>
    <AdditionalContent Include="$(RazorSdkRoot)Sdk\**\*.*">
      <Pack>true</Pack>
      <PackagePath>Sdk</PackagePath>
    </AdditionalContent>
  </ItemGroup>

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

  <ItemGroup>
    <Compile Include="**\*.cs" />
    <Compile Include="..\Tool\CommandLine\ArgumentEscaper.cs">
      <Link>Shared\CommandLine\%(FileName)</Link>
    </Compile>
    <Compile Include="..\Tool\ServerProtocol\*.cs">
      <Link>Shared\ServerProtocol\%(FileName)</Link>
    </Compile>
    <Compile Include="..\Tool\PipeName.cs">
      <Link>Shared\PipeName.cs</Link>
    </Compile>
    <Compile Include="..\Tool\MutexName.cs">
      <Link>Shared\MutexName.cs</Link>
    </Compile>
    <Compile Include="..\Tool\Client.cs">
      <Link>Shared\Client.cs</Link>
    </Compile>
  </ItemGroup>

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

    <Copy SourceFiles="@(_FileSystemGlobbingContent)" DestinationFiles="@(_FileSystemGlobbingContent->'$(PackageLayoutOutputPath)%(TargetPath)')">
      <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)</TargetPath>
      </LayoutFile>
    </ItemGroup>
  </Target>

  <Target Name="CopyAdditionalFilesToLayout"
          Condition="'$(TargetFramework)' == ''"
          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)">
    <ItemGroup>
      <Content Include="$(PackageLayoutOutputPath)**\*" PackagePath="\" />
    </ItemGroup>
  </Target>

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

</Project>
