<Project ToolsVersion="15.0" DefaultTargets="ExtractDotnetToolsToOutput">
  <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
  <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.tasks))\dir.tasks" />

  <Target Name="ExtractDotnetToolsToOutput"
          DependsOnTargets="EnsureDotnetToolsRestored">

    <Message Text="Restore DotnetTools $(TemplateFillInPackageName) from $(DotnetToolsNuPkgPath) to $(DotnetToolsLayoutDirectory)."
             Importance="High" />
  </Target>

  <Target Name="EnsureDotnetToolsRestored"
          Condition="!Exists('$(DotnetToolsNuPkgPath)/$(TemplateFillInPackageName.ToLower())')">

    <PropertyGroup>
      <DotnetToolsRestoreAdditionalParameters>--runtime any /p:RestoreProjectStyle=DotnetToolReference</DotnetToolsRestoreAdditionalParameters>
      <DotnetToolsRestoreAdditionalParameters>$(DotnetToolsRestoreAdditionalParameters) /p:TargetFramework=$(CliTargetFramework)</DotnetToolsRestoreAdditionalParameters>
      <DotnetToolsRestoreAdditionalParameters>$(DotnetToolsRestoreAdditionalParameters) /p:TemplateFillInPackageName=$(TemplateFillInPackageName)</DotnetToolsRestoreAdditionalParameters>
      <DotnetToolsRestoreAdditionalParameters>$(DotnetToolsRestoreAdditionalParameters) /p:TemplateFillInPackageVersion=$(TemplateFillInPackageVersion)</DotnetToolsRestoreAdditionalParameters>
      <DotnetToolsRestoreAdditionalParameters>$(DotnetToolsRestoreAdditionalParameters) /p:RestorePackagesPath=$(DotnetToolsLayoutDirectory)</DotnetToolsRestoreAdditionalParameters>
    </PropertyGroup>

    <DotNetRestore ToolPath="$(PreviousStageDirectory)"
                   ProjectPath="$(MSBuildThisFileDirectory)/templates/templates.csproj"
                   AdditionalParameters="$(DotnetToolsRestoreAdditionalParameters)" />
  </Target>
</Project>
