<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net9.0</TargetFramework>
    <AssemblyOriginatorKeyFile>..\Jint\Jint.snk</AssemblyOriginatorKeyFile>
    <SignAssembly>true</SignAssembly>
    <IsPackable>false</IsPackable>
    <NoWarn>$(NoWarn);CS8002</NoWarn>
    <GeneratedTestSuiteDir>Generated</GeneratedTestSuiteDir>
    <EnableNUnitRunner>true</EnableNUnitRunner>
    <OutputType>Exe</OutputType>
  </PropertyGroup>

  <ItemGroup>
    <ProjectReference Include="..\Jint\Jint.csproj" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Acornima.Extras" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" />
    <PackageReference Include="Newtonsoft.Json" />
    <PackageReference Include="NUnit" />
    <PackageReference Include="NUnit3TestAdapter" />
    <PackageReference Include="Test262Harness" />
  </ItemGroup>

  <ItemGroup>
    <Using Include="NUnit.Framework" />
  </ItemGroup>

  <ItemGroup>
    <Content Include=".config\dotnet-tools.json" />
  </ItemGroup>

  <!-- Based on the idea presented at https://mhut.ch/journal/2015/06/30/build-time-code-generation-in-msbuild -->
  <Target Name="GenerateTestSuite" DependsOnTargets="_GenerateTestSuite" BeforeTargets="BeforeBuild" Condition="!Exists($([System.IO.Path]::Combine($(MSBuildThisFileDirectory), $(GeneratedTestSuiteDir))))">
    <ItemGroup>
      <Compile Include="$(GeneratedTestSuiteDir)\**\*.cs" />
    </ItemGroup>
  </Target>

  <Target Name="_GenerateTestSuite">
    <Exec Command="dotnet tool restore" />
    <Exec Command="dotnet test262 generate" />
  </Target>

  <Target Name="DeleteTestSuite" DependsOnTargets="_DeleteTestSuite" AfterTargets="AfterClean" Condition="Exists($([System.IO.Path]::Combine($(MSBuildThisFileDirectory), $(GeneratedTestSuiteDir))))">
    <RemoveDir Directories="$(GeneratedTestSuiteDir)" />
  </Target>

  <Target Name="_DeleteTestSuite">
    <ItemGroup>
      <Compile Remove="$(GeneratedTestSuiteDir)\**\*.cs" />
    </ItemGroup>
  </Target>
</Project>