<Project>

  <PropertyGroup>
    <CopyBuildOutputToOutputDirectory>false</CopyBuildOutputToOutputDirectory>
    <CopyOutputSymbolsToOutputDirectory>false</CopyOutputSymbolsToOutputDirectory>
    <OutputType>Library</OutputType>
    <GenerateDependencyFile>false</GenerateDependencyFile>
    <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
    <ResolvePackageDependenciesForBuild>false</ResolvePackageDependenciesForBuild>
    <NonShipping>true</NonShipping>
    <NoStdLib>true</NoStdLib>
    <ProducesNoOutput>true</ProducesNoOutput>
  </PropertyGroup>

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

  <PropertyGroup>
    <LanguageTargets>$(MSBuildToolsPath)\Microsoft.CSharp.targets</LanguageTargets>
    <IsPackable>false</IsPackable>
    <DeterministicSourcePaths>false</DeterministicSourcePaths>
    <TargetFramework>$(CoreSdkTargetFramework)</TargetFramework>
  </PropertyGroup>

  <PropertyGroup>
    <RedistLayoutPath>$(ArtifactsBinDir)redist\$(Configuration)\dotnet\</RedistLayoutPath>
    <DotnetToTestPath>$(ArtifactsBinDir)redist\$(Configuration)\dotnet-with-previous-runtimes\</DotnetToTestPath>
    <TestExecutionDirectory>$([MSBuild]::NormalizePath($(ArtifactsTmpDir), 'dotnetSdkTests'))</TestExecutionDirectory>
    <DOTNET_CLI_HOME>$(ArtifactsTmpDir)DOTNET_CLI_HOME\</DOTNET_CLI_HOME>
  </PropertyGroup>

  <ItemGroup>
    <SdkTest Include="Build" />
    <SdkTest Include="Clean" />
    <SdkTest Include="Pack" />
    <SdkTest Include="Rebuild" />
    <SdkTest Include="Restore" />
    <SdkTest Include="ToolPack" />
  </ItemGroup>

  <!-- Disable Publish tests on Windows x86 until we figure out why they were timing out
        https://github.com/dotnet/core-sdk/issues/6285 -->
  <ItemGroup Condition="('$(OS)' != 'Windows_NT') Or ('$(Architecture)' != 'x86')">
    <SdkTest Include="Publish" />
  </ItemGroup>

  <Target Name="GetRuntimesToInstall">
    <ReadLinesFromFile File="/etc/os-release" Condition="'$(OS)' != 'Windows_NT'">
      <Output TaskParameter="Lines" PropertyName="OsReleaseContents" />
    </ReadLinesFromFile>

    <!-- Set OsReleaseContents to Windows to make later comparisons easier -->
    <PropertyGroup Condition="'$(OS)' == 'Windows_NT'">
      <OsReleaseContents>Windows</OsReleaseContents>
    </PropertyGroup>

    <ItemGroup>
      <RuntimeVersionToInstall Include="2.1.0" />
      <RuntimeVersionToInstall Include="2.2.5" />
      <RuntimeVersionToInstall Include="3.0.0" />
      <RuntimeVersionToInstall Include="3.1.0" />
      <RuntimeVersionToInstall Include="5.0.0" />
      <RuntimeVersionToInstall Include="6.0.0" />
      <RuntimeVersionToInstall Include="7.0.0" />
      <RuntimeVersionToInstall Include="8.0.0" />
    </ItemGroup>
  </Target>

  <Target Name="SetupLayoutWithPreviousRuntimes" DependsOnTargets="GetRuntimesToInstall">
    <ItemGroup>
      <LayoutFilesToCopyToDotnetToTest Include="$(RedistLayoutPath)**" />
      <LayoutFilesToCopyToDotnetToTest>
        <DestinationPath>$(DotnetToTestPath)%(RecursiveDir)%(Filename)%(Extension)</DestinationPath>
      </LayoutFilesToCopyToDotnetToTest>
    </ItemGroup>

    <Copy SourceFiles="@(LayoutFilesToCopyToDotnetToTest)"
          DestinationFiles="@(LayoutFilesToCopyToDotnetToTest->'%(DestinationPath)')"
          SkipUnchangedFiles="true" />

    <MSBuild Projects="$(MSBuildProjectFullPath)"
         Targets="InstallRuntimeToLayout"
         Properties="RuntimeVersionToInstall=%(RuntimeVersionToInstall.Identity)" />
  </Target>

  <Target Name="InstallRuntimeToLayout">
    <PropertyGroup>
      <RuntimeTargetDirectory>$(DotnetToTestPath)shared\Microsoft.NETCore.App\$(RuntimeVersionToInstall)</RuntimeTargetDirectory>
    </PropertyGroup>

    <PropertyGroup Condition="'$(OS)' == 'Windows_NT'">
      <!-- Set TLS1.2 explicitly before running this. If TLS1.2 isnt' enabled by default this command may fail in some
           scenarios.  Waiting on a fix to dotnet-install.ps1 to fix this in the right place. -->
      <InstallRuntimeCommand>powershell -NoLogo -NoProfile -ExecutionPolicy ByPass -Command "&amp; {</InstallRuntimeCommand>
      <InstallRuntimeCommand>$(InstallRuntimeCommand) [Net.ServicePointManager]::SecurityProtocol = 'Tls12, Tls13';</InstallRuntimeCommand>
      <InstallRuntimeCommand>$(InstallRuntimeCommand) &amp; '$(DotNetRoot)dotnet-install.ps1'</InstallRuntimeCommand>
      <InstallRuntimeCommand>$(InstallRuntimeCommand) -Version $(RuntimeVersionToInstall)</InstallRuntimeCommand>
      <InstallRuntimeCommand>$(InstallRuntimeCommand) -InstallDir $(DotnetToTestPath)</InstallRuntimeCommand>
      <InstallRuntimeCommand>$(InstallRuntimeCommand) -Runtime 'dotnet'</InstallRuntimeCommand>
      <InstallRuntimeCommand Condition="'$(Architecture)' != ''">$(InstallRuntimeCommand) -Architecture '$(Architecture)'</InstallRuntimeCommand>
      <InstallRuntimeCommand>$(InstallRuntimeCommand) }"</InstallRuntimeCommand>
    </PropertyGroup>

    <PropertyGroup Condition="'$(OS)' != 'Windows_NT'">
      <InstallRuntimeCommand>/bin/bash</InstallRuntimeCommand>
      <InstallRuntimeCommand>$(InstallRuntimeCommand) "$(DotNetRoot)dotnet-install.sh"</InstallRuntimeCommand>
      <InstallRuntimeCommand>$(InstallRuntimeCommand) --version $(RuntimeVersionToInstall)</InstallRuntimeCommand>
      <InstallRuntimeCommand>$(InstallRuntimeCommand) --install-dir "$(DotnetToTestPath)"</InstallRuntimeCommand>
      <InstallRuntimeCommand>$(InstallRuntimeCommand) --runtime "dotnet"</InstallRuntimeCommand>
      <InstallRuntimeCommand Condition="'$(Architecture)' != ''">$(InstallRuntimeCommand) --architecture "$(Architecture)"</InstallRuntimeCommand>
    </PropertyGroup>

    <Exec Command="$(InstallRuntimeCommand)"
          Condition="!Exists($(RuntimeTargetDirectory))"
          IgnoreStandardErrorWarningFormat="true" />
  </Target>

  <Target Name="RunSdkTests" DependsOnTargets="SetupLayoutWithPreviousRuntimes">

    <!-- Isolate test working directory from higher-level Directory.Build files -->
    <Copy SourceFiles="$(RepoRoot)TestAssets\Directory.Build.props;$(RepoRoot)TestAssets\Directory.Build.targets"
          DestinationFolder="$(TestExecutionDirectory)"
          SkipUnchangedFiles="true" />

    <!-- Set up NuGet feeds -->
    <PropertyGroup>
      <GeneratedNuGetConfig>$(TestExecutionDirectory)\NuGet.config</GeneratedNuGetConfig>
      <NugetConfigContents>
        <![CDATA[
<configuration>
  <packageSources>
  </packageSources>
</configuration>
        ]]>
      </NugetConfigContents>
    </PropertyGroup>

    <WriteLinesToFile File="$(GeneratedNuGetConfig)" Lines="$(NugetConfigContents)" Overwrite="true" />

    <!-- Allow test list to be overridden with ToolTestName property -->
    <ItemGroup Condition="'$(ToolTestName)' != ''">
      <SdkTest Remove="@(SdkTest)" />
      <SdkTest Include="$(ToolTestName)" />
    </ItemGroup>

    <ItemGroup>
      <TestProjectToRun Include="$(MSBuildProjectFullPath)">
        <AdditionalProperties>ToolTestName=%(SdkTest.Identity)</AdditionalProperties>
      </TestProjectToRun>
    </ItemGroup>

    <MSBuild Projects="@(TestProjectToRun)" Targets="RunTestsAsTool" />
  </Target>

  <Target Name="RunTestsAsTool">
    <PropertyGroup>
      <ToolCommandName>testSdk$(ToolTestName)</ToolCommandName>
      <TestProjectName>Microsoft.NET.$(ToolTestName).Tests</TestProjectName>
      <TestLocalToolFolder>$(ArtifactsTmpDir)$(ToolCommandName)\</TestLocalToolFolder>
    </PropertyGroup>

    <RemoveDir Directories="$(TestLocalToolFolder)" />
    <MakeDir Directories="$(TestLocalToolFolder)" />
    <MakeDir Directories="$(ArtifactsTestResultsDir)" />

    <PropertyGroup Condition="'$(OS)' == 'Windows_NT'">
      <ToolRunPrefix>$(ToolRunPrefix)set MSBuildSDKsPath=&amp;&amp; </ToolRunPrefix>
      <ToolRunPrefix>$(ToolRunPrefix)set DOTNET_HOST_PATH=&amp;&amp; </ToolRunPrefix>
      <ToolRunPrefix>$(ToolRunPrefix)set DOTNET_INSTALLDIR=&amp;&amp; </ToolRunPrefix>
    </PropertyGroup>
    <PropertyGroup Condition="'$(OS)' != 'Windows_NT'">
      <ToolRunPrefix>$(ToolRunPrefix)unset MSBuildSDKsPath DOTNET_HOST_PATH DOTNET_INSTALLDIR &amp;&amp; </ToolRunPrefix>
    </PropertyGroup>

    <Exec Command="$(ToolRunPrefix)$(RedistLayoutPath)dotnet new tool-manifest"
          WorkingDirectory="$(TestLocalToolFolder)"
          EnvironmentVariables="DOTNET_CLI_HOME=$(DOTNET_CLI_HOME)"
          IgnoreStandardErrorWarningFormat="true" />

    <PropertyGroup>
      <SdkTestPackageVersion>$(MicrosoftNETSdkPackageVersion)</SdkTestPackageVersion>
      <InstallToolCommand>$(RedistLayoutPath)dotnet tool install --local $(ToolCommandName)</InstallToolCommand>
      <InstallToolCommand>$(InstallToolCommand) --version $(SdkTestPackageVersion)</InstallToolCommand>
    </PropertyGroup>

    <Exec Command="$(ToolRunPrefix)$(InstallToolCommand)"
      WorkingDirectory="$(TestLocalToolFolder)"
      EnvironmentVariables="DOTNET_CLI_HOME=$(DOTNET_CLI_HOME)" />

    <PropertyGroup>
      <ResultsXmlPath>$(ArtifactsTestResultsDir)$(TestProjectName).xml</ResultsXmlPath>
      <ResultsHtmlPath>$(ArtifactsTestResultsDir)$(TestProjectName).html</ResultsHtmlPath>
      <ResultsStdOutPath>$(ArtifactsLogDir)$(TestProjectName).log</ResultsStdOutPath>

      <TestArgs>-noautoreporters -noRepoInference</TestArgs>
      <TestArgs>$(TestArgs) -dotnetPath $(DotnetToTestPath)dotnet</TestArgs>
      <TestArgs>$(TestArgs) -testExecutionDirectory $(TestExecutionDirectory)</TestArgs>
      <TestArgs>$(TestArgs) -testConfigFile "$(MSBuildThisFileDirectory)TestConfig.xml"</TestArgs>
      <TestArgs>$(TestArgs) -xml "$(ResultsXmlPath)"</TestArgs>
      <TestArgs>$(TestArgs) -html "$(ResultsHtmlPath)" $(TestRunnerAdditionalArguments)</TestArgs>
      <TestArgs>$(TestArgs) &gt; $(ResultsStdOutPath) 2&gt;&amp;1</TestArgs>
      <TestArgs>$(TestArgs) -testList SdkIntegrationTests</TestArgs>
    </PropertyGroup>

    <PropertyGroup Condition="$(StabilizePackageVersion) == 'true'">
      <!-- Add list of tests to skip in stable SDKS -->
      <TestArgs>$(TestArgs) -testConfigFile "$(MSBuildThisFileDirectory)TestsToSkipStableSDK.xml"</TestArgs>
    </PropertyGroup>

    <PropertyGroup Condition="('$(OS)' == 'Windows_NT') And ('$(Architecture)' == 'x86')">
      <!-- Add list of tests to skip on x86 -->
      <TestArgs>$(TestArgs) -testConfigFile "$(MSBuildThisFileDirectory)TestsToSkipx86.xml"</TestArgs>
    </PropertyGroup>

    <PropertyGroup Condition="'$(IsLinuxPortable)' == 'true'">
      <!-- Add list of tests to skip on Linux Portable -->
      <TestArgs>$(TestArgs) -testConfigFile "$(MSBuildThisFileDirectory)TestsToSkipPortableLinux.xml"</TestArgs>
    </PropertyGroup>

    <PropertyGroup Condition="$([MSBuild]::IsOSPlatform('LINUX'))">
      <!-- Add list of tests to skip on Linux Portable -->
      <TestArgs>$(TestArgs) -testConfigFile "$(MSBuildThisFileDirectory)TestsToSkipLinux.xml"</TestArgs>
    </PropertyGroup>

    <PropertyGroup Condition="$([MSBuild]::IsOSPlatform('OSX'))">
      <!-- Add list of tests to skip on Linux Portable -->
      <TestArgs>$(TestArgs) -testConfigFile "$(MSBuildThisFileDirectory)TestsToSkipOSX.xml"</TestArgs>
    </PropertyGroup>

    <Exec Command="$(ToolRunPrefix)$(RedistLayoutPath)dotnet tool run $(ToolCommandName) -- $(TestArgs)"
          WorkingDirectory="$(TestLocalToolFolder)"
          EnvironmentVariables="DOTNET_CLI_HOME=$(DOTNET_CLI_HOME)" />
  </Target>

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

  <!-- Remove items which light up build logic which could generate warnings / errors -->
  <ItemGroup>
    <EmbeddedResource Remove="@(EmbeddedResource)" />
    <Reference Remove="@(Reference)" />
    <PackageReference Remove="@(PackageReference)" />
    <ProjectReference Remove="@(ProjectReference)" />
    <SwrFile Remove="@(SwrFile)" />
  </ItemGroup>

  <Target Name="CoreCompile" />
  <!-- Prevent Csc from being called -->
  <Target Name="GenerateTargetFrameworkMonikerAttribute" />
  <!-- Don't generate TFM attribute -->
  <Target Name="RuntimeImplementationProjectOutputGroup" />
  <!-- Group always attempts resolve runtime, regardless of <CopyNuGetImplementations>-->
  <Target Name="GetReferenceAssemblyPaths" />
  <!-- Don't go looking for framework reference assemblies-->
  <Target Name="GetFrameworkPaths" />
  <Target Name="GetBuildVersion" />
  <!-- For Packages projects, which use GetNuGetPackageVersionEx which depends on GetBuildVersion -->
  <Target Name="Pack" />
  <Target Name="Test" DependsOnTargets="RunSdkTests" />

</Project>
