<Project>

  <Target Name="GenerateMSBuildExtensions"
          DependsOnTargets="GenerateBundledVersionsProps;GenerateBundledCliToolsProps;RestoreMSBuildExtensionsPackages">

    <ItemGroup>

      <!-- The MSBuildExtensionsContent item is for the files that will be laid out in the CLI install -->
      <MSBuildExtensionsContent Include="$(GeneratedMSBuildExtensionsDirectory)/**/*" />

      <!-- We want to include the tasks and targets from the Microsoft.NET.Build.Extensions package, but we don't want to include the DLLs
           from that package under the net461, net462, etc folders.  That is because they come from the NETStandard.Library.NETFramework
           package, and we want to insert them directly into the CLI from CoreFx instead of having to do a two-hop insertion (CoreFX -> SDK -> CLI)
           if we need to update them.

           https://github.com/dotnet/sdk/issues/1324 has been filed to exclude these from the Microsoft.NET.Build.Extensions package when
           we generate it.
           -->
      <MSBuildExtensionsContent Include="$(MSBuildExtensionsNuPkgPath)\msbuildExtensions\**\*.*"
                                Exclude="$(MSBuildExtensionsNuPkgPath)\msbuildExtensions\Microsoft\Microsoft.NET.Build.Extensions\net*\**" />

      <!-- Don't include .NET Framework MS.NET.Build.Extensions tasks in CLI layout -->
      <MSBuildExtensionsContent Remove="$(MSBuildExtensionsNuPkgPath)\msbuildExtensions\Microsoft\Microsoft.NET.Build.Extensions\tools\net46\**" />

      <MSBuildExtensionsContent Include="$(MSBuildExtensionsNuPkgPath)\msbuildExtensions-ver\**\*.*"
                                DeploymentSubpath="$(MSBuildExtensionsVersionSubfolder)/" />

      <MSBuildExtensionsContent Include="$(NETStandardLibraryNETFrameworkNuPkgPath)\build\**\*.*"
                                Exclude="$(NETStandardLibraryNETFrameworkNuPkgPath)\build\**\*.props;$(NETStandardLibraryNETFrameworkNuPkgPath)\build\**\*.targets"
                                DeploymentSubpath="Microsoft/Microsoft.NET.Build.Extensions/" />


      <!-- The VSMSBuildExtensionsContent item is for the files that will be included in the VS.Redist.Common.Net.Core.SDK.MSBuildExtensions
           package and inserted into Visual Studio -->
      <VSMSBuildExtensionsContent Include="$(MSBuildExtensionsNuPkgPath)\msbuildExtensions\**\*.*"
                                  Exclude="$(MSBuildExtensionsNuPkgPath)\msbuildExtensions\Microsoft\Microsoft.NET.Build.Extensions\net*\**"
                                  DeploymentSubpath="msbuildExtensions/"/>

      <!-- Don't include .NET Core MS.NET.Build.Extensions tasks in Full Framework MSBuild layout -->
      <VSMSBuildExtensionsContent Remove="$(MSBuildExtensionsNuPkgPath)\msbuildExtensions\Microsoft\Microsoft.NET.Build.Extensions\tools\netcoreapp*\**" />

      <VSMSBuildExtensionsContent Include="$(MSBuildExtensionsNuPkgPath)\msbuildExtensions-ver\**\*.*"
                                  DeploymentSubpath="msbuildExtensions-ver/"/>
      <VSMSBuildExtensionsContent Include="$(NETStandardLibraryNETFrameworkNuPkgPath)\build\**\*.*"
                                  Exclude="$(NETStandardLibraryNETFrameworkNuPkgPath)\build\**\*.props;$(NETStandardLibraryNETFrameworkNuPkgPath)\build\**\*.targets"
                                  DeploymentSubpath="msbuildExtensions/Microsoft/Microsoft.NET.Build.Extensions/" />

      <VSMSBuildExtensionsContent Update="@(VSMSBuildExtensionsContent)">
        <DestinationPath >$(MSBuildExtensionsLayoutDirectory)/%(VSMSBuildExtensionsContent.DeploymentSubpath)%(RecursiveDir)%(Filename)%(Extension)</DestinationPath>
      </VSMSBuildExtensionsContent>
    </ItemGroup>

    <Copy SourceFiles="@(VSMSBuildExtensionsContent)" DestinationFiles="%(VSMSBuildExtensionsContent.DestinationPath)" />
  </Target>

  <Target Name="PrepareMSBuildExtensionsProps">
    <PropertyGroup>
      <MSBuildExtensionsPackageName>Microsoft.NET.Build.Extensions</MSBuildExtensionsPackageName>
      <MSBuildExtensionsVersionSubfolder>15.0</MSBuildExtensionsVersionSubfolder>
      <MSBuildExtensionsNuPkgPath>$(NuGetPackagesDir)/$(MSBuildExtensionsPackageName.ToLower())/$(MicrosoftNETBuildExtensionsPackageVersion.ToLower())</MSBuildExtensionsNuPkgPath>

      <NETStandardLibraryNETFrameworkPackageName>NETStandard.Library.NETFramework</NETStandardLibraryNETFrameworkPackageName>
      <NETStandardLibraryNETFrameworkNuPkgPath>$(NuGetPackagesDir)/$(NETStandardLibraryNETFrameworkPackageName.ToLower())/$(CLI_NETStandardLibraryNETFrameworkVersion.ToLower())</NETStandardLibraryNETFrameworkNuPkgPath>
    </PropertyGroup>
  </Target>

  <Target Name="RestoreMSBuildExtensionsPackages"
          DependsOnTargets="PrepareMSBuildExtensionsProps">
    <ItemGroup>
      <ExtensionPackageToRestore Include="$(MSBuildExtensionsPackageName)" Version="$(MicrosoftNETBuildExtensionsPackageVersion)"/>
      <ExtensionPackageToRestore Include="$(NETStandardLibraryNETFrameworkPackageName)" Version="$(CLI_NETStandardLibraryNETFrameworkVersion)"/>

      <ExtensionRestore Include="$(RepoRoot)/build/RestoreDependency.proj">
        <Properties>
          CLIBuildDll=$(CLIBuildDll);
          NuGetPackagesDir=$(NuGetPackagesDir);
          DependencyPackageName=%(ExtensionPackageToRestore.Identity);
          DependencyPackageVersion=%(ExtensionPackageToRestore.Version);
          PreviousStageDirectory=$(PreviousStageDirectory)
        </Properties>
      </ExtensionRestore>
    </ItemGroup>

    <MSBuild
      BuildInParallel="False"
      Projects="@(ExtensionRestore)">
    </MSBuild>
  </Target>

  <Target Name="GenerateBundledVersionsProps" DependsOnTargets="RunResolvePackageDependencies">
    <PropertyGroup>
      <BundledVersionsPropsFileName>Microsoft.NETCoreSdk.BundledVersions.props</BundledVersionsPropsFileName>
    </PropertyGroup>

    <!--
      The bundled version of Microsoft.NETCore.App is determined statically from DependencyVersions.props whereas the bundled version
      of NETStandard.Library is defined by what Microsoft.NETCore.App pulls in. This digs in to the package resolution items of
      of our build against Microsoft.NETCore.App to find the correct NETStandard.Library version
    -->
    <ItemGroup>
      <_NETStandardLibraryPackageVersions Include="@(PackageDefinitions->'%(Version)')"
                                   Condition="%(PackageDefinitions.Name) == 'NetStandard.Library'" />
      <_NETCorePlatformsPackageVersions Include="@(PackageDefinitions->'%(Version)')"
                                   Condition="%(PackageDefinitions.Name) == 'Microsoft.NETCore.Platforms'" />
    </ItemGroup>

    <Error Condition="@(_NETStandardLibraryPackageVersions->Distinct()->Count()) != 1"
           Text="Failed to determine the NETStandard.Library version pulled in Microsoft.NETCore.App" />
    <Error Condition="@(_NETCorePlatformsPackageVersions->Distinct()->Count()) != 1"
           Text="Failed to determine the Microsoft.NETCore.Platforms version pulled in Microsoft.NETCore.App" />

    <PropertyGroup>
      <_NETCoreAppPackageVersion>$(MicrosoftNETCoreAppPackageVersion)</_NETCoreAppPackageVersion>
      <_NETStandardLibraryPackageVersion>@(_NETStandardLibraryPackageVersions->Distinct())</_NETStandardLibraryPackageVersion>
      <_NETCorePlatformsPackageVersion>@(_NETCorePlatformsPackageVersions->Distinct())</_NETCorePlatformsPackageVersion>
      <_AspNetCoreAllPackageVersion>$(MicrosoftAspNetCoreAllPackageVersion)</_AspNetCoreAllPackageVersion>
      <_AspNetCoreAppPackageVersion>$(MicrosoftAspNetCoreAppPackageVersion)</_AspNetCoreAppPackageVersion>

      <!-- Default aspnetcore patch versions, specified as a stabilized version -->
      <_DefaultPatchVersionForAspNetCoreAll2_1>2.1.1</_DefaultPatchVersionForAspNetCoreAll2_1>
      <_DefaultPatchVersionForAspNetCoreApp2_1>$(_DefaultPatchVersionForAspNetCoreAll2_1)</_DefaultPatchVersionForAspNetCoreApp2_1>
      <!-- If we are currently building a prerelease of the version that we intend to lock to, set the default version to the ingested version instead of the stabilized version. -->
      <_DefaultPatchVersionForAspNetCoreAll2_1 Condition="$(_AspNetCoreAllPackageVersion.StartsWith('$(_DefaultPatchVersionForAspNetCoreAll2_1)-'))">$(_AspNetCoreAllPackageVersion)</_DefaultPatchVersionForAspNetCoreAll2_1>
      <_DefaultPatchVersionForAspNetCoreApp2_1 Condition="$(_AspNetCoreAppPackageVersion.StartsWith('$(_DefaultPatchVersionForAspNetCoreApp2_1)-'))">$(_AspNetCoreAppPackageVersion)</_DefaultPatchVersionForAspNetCoreApp2_1>

      <!-- Use only major and minor in target framework version -->
      <_NETCoreAppTargetFrameworkVersion>$(_NETCoreAppPackageVersion.Split('.')[0]).$(_NETCoreAppPackageVersion.Split('.')[1])</_NETCoreAppTargetFrameworkVersion>
      <_NETStandardTargetFrameworkVersion>$(_NETStandardLibraryPackageVersion.Split('.')[0]).$(_NETStandardLibraryPackageVersion.Split('.')[1])</_NETStandardTargetFrameworkVersion>
      <_AspNetCoreAllTargetFrameworkVersion>$(_NETCoreAppTargetFrameworkVersion)</_AspNetCoreAllTargetFrameworkVersion>
      <_AspNetCoreAppTargetFrameworkVersion>$(_AspNetCoreAllTargetFrameworkVersion)</_AspNetCoreAppTargetFrameworkVersion>

      <_NETCoreSdkIsPreview Condition=" '$(DropSuffix)' != 'true' ">true</_NETCoreSdkIsPreview>
    </PropertyGroup>

    <!--
        Setting the property to true if patch == 0 and preview == true. SDK will set DefaultNetCorePatchVersion according to this flag.
        So that we don't need to manually update the version selection logic between when we ship a final release and when we ship the first patch
      -->
    <GetUseBundledNETCoreAppPackageVersionAsDefaultNetCorePatchVersion
      BundledNETCoreAppPackageVersion="$(_NETCoreAppPackageVersion)">
      <Output TaskParameter="UseBundledNETCoreAppPackageVersionAsDefaultNetCorePatchVersion"
              PropertyName="_UseBundledNETCoreAppPackageVersionAsDefaultNetCorePatchVersion" />
    </GetUseBundledNETCoreAppPackageVersionAsDefaultNetCorePatchVersion>

    <ItemGroup>
      <ImplicitPackageVariable Include="Microsoft.NETCore.App"
                               TargetFrameworkVersion="1.0"
                               DefaultVersion="1.0.5"
                               LatestVersion="1.0.12" />
      <ImplicitPackageVariable Include="Microsoft.NETCore.App"
                               TargetFrameworkVersion="1.1"
                               DefaultVersion="1.1.2"
                               LatestVersion="1.1.9" />
      <ImplicitPackageVariable Include="Microsoft.NETCore.App"
                               TargetFrameworkVersion="2.0"
                               DefaultVersion="2.0.0"
                               LatestVersion="2.0.9" />
      <ImplicitPackageVariable Include="Microsoft.NETCore.App"
                               TargetFrameworkVersion="2.1"
                               DefaultVersion="2.1.0"
                               LatestVersion="2.1.5" />
      <ImplicitPackageVariable Include="Microsoft.NETCore.App"
                               TargetFrameworkVersion="2.2"
                               DefaultVersion="$(_NETCoreAppPackageVersion)"
                               LatestVersion="$(_NETCoreAppPackageVersion)" />
      
      <ImplicitPackageVariable Include="Microsoft.AspNetCore.App"
                               TargetFrameworkVersion="2.1"
                               DefaultVersion="2.1.1"
                               LatestVersion="2.1.5"/>
      <ImplicitPackageVariable Include="Microsoft.AspNetCore.All"
                               TargetFrameworkVersion="2.1"
                               DefaultVersion="2.1.1"
                               LatestVersion="2.1.5"/>
      <ImplicitPackageVariable Include="Microsoft.AspNetCore.App"
                               TargetFrameworkVersion="2.2"
                               DefaultVersion="$(_AspNetCoreAppPackageVersion)"
                               LatestVersion="$(_AspNetCoreAppPackageVersion)"/>
      <ImplicitPackageVariable Include="Microsoft.AspNetCore.All"
                               TargetFrameworkVersion="2.2"
                               DefaultVersion="$(_AspNetCoreAllPackageVersion)"
                               LatestVersion="$(_AspNetCoreAllPackageVersion)"/>
    </ItemGroup>
    
    <ItemGroup>
      <BundledVersionsVariable Include="BundledAspNetCoreAllTargetFrameworkVersion" Value="$(_AspNetCoreAllTargetFrameworkVersion)" />
      <BundledVersionsVariable Include="BundledAspNetCoreAllPackageVersion" Value="$(_AspNetCoreAllPackageVersion)" />
      <BundledVersionsVariable Include="BundledAspNetCoreAppTargetFrameworkVersion" Value="$(_AspNetCoreAppTargetFrameworkVersion)" />
      <BundledVersionsVariable Include="BundledAspNetCoreAppPackageVersion" Value="$(_AspNetCoreAppPackageVersion)" />
    </ItemGroup>

    <ItemGroup Condition=" '$(IncludeAspNetCoreRuntime)' == 'false' ">
      <!--
        These properties indicate that the ASP.NET Core shared runtime is not bundled on this platform, so the SDK should not
        treat these packages as the 'platform' base.

        The bundled aspnet packages versions should still be set, however, so the default, version-less PackageReference
        still works.
        See also https://github.com/aspnet/Universe/pull/1130.
      -->
      <BundledVersionsVariable Include="_AspNetCoreAppSharedFxIsEnabled" Value="false" />
      <BundledVersionsVariable Include="_AspNetCoreAllSharedFxIsEnabled" Value="false" />
    </ItemGroup>

    <PropertyGroup>
      <BundledVersionsPropsContent>
<![CDATA[
<!--
***********************************************************************************************
$(BundledVersionsPropsFileName)

WARNING:  DO NOT MODIFY this file unless you are knowledgeable about MSBuild and have
          created a backup copy.  Incorrect changes to this file will make it
          impossible to load or build your projects from the command-line or the IDE.

Copyright (c) .NET Foundation. All rights reserved.
***********************************************************************************************
-->
<Project>
  <PropertyGroup>
    <BundledNETCoreAppTargetFrameworkVersion>$(_NETCoreAppTargetFrameworkVersion)</BundledNETCoreAppTargetFrameworkVersion>
    <BundledNETCoreAppPackageVersion>$(_NETCoreAppPackageVersion)</BundledNETCoreAppPackageVersion>
    <UseBundledNETCoreAppPackageVersionAsDefaultNetCorePatchVersion>$(_UseBundledNETCoreAppPackageVersionAsDefaultNetCorePatchVersion)</UseBundledNETCoreAppPackageVersionAsDefaultNetCorePatchVersion>
    <BundledNETStandardTargetFrameworkVersion>$(_NETStandardTargetFrameworkVersion)</BundledNETStandardTargetFrameworkVersion>
    <BundledNETStandardPackageVersion>$(_NETStandardLibraryPackageVersion)</BundledNETStandardPackageVersion>
    <BundledNETCorePlatformsPackageVersion>$(_NETCorePlatformsPackageVersion)</BundledNETCorePlatformsPackageVersion>
    @(BundledVersionsVariable->'<%(Identity)>%(Value)</%(Identity)>', '%0A    ')
    <NETCoreSdkVersion>$(SdkVersion)</NETCoreSdkVersion>
    <_NETCoreSdkIsPreview>$(_NETCoreSdkIsPreview)</_NETCoreSdkIsPreview>

    <!-- Default patch versions for each minor version of ASP.NET Core -->
    <DefaultPatchVersionForAspNetCoreAll2_1>$(_DefaultPatchVersionForAspNetCoreAll2_1)</DefaultPatchVersionForAspNetCoreAll2_1>
    <DefaultPatchVersionForAspNetCoreApp2_1>$(_DefaultPatchVersionForAspNetCoreApp2_1)</DefaultPatchVersionForAspNetCoreApp2_1>

    <!-- Latest patch versions for each minor version of .NET Core -->
    <LatestPatchVersionForNetCore1_0 Condition="'%24(LatestPatchVersionForNetCore1_0)' == ''">1.0.12</LatestPatchVersionForNetCore1_0>
    <LatestPatchVersionForNetCore1_1 Condition="'%24(LatestPatchVersionForNetCore1_1)' == ''">1.1.9</LatestPatchVersionForNetCore1_1>
    <LatestPatchVersionForNetCore2_0 Condition="'%24(LatestPatchVersionForNetCore2_0)' == ''">2.0.9</LatestPatchVersionForNetCore2_0>
    <LatestPatchVersionForNetCore2_1 Condition="'%24(LatestPatchVersionForNetCore2_1)' == ''">2.1.2</LatestPatchVersionForNetCore2_1>
  </PropertyGroup>
  <ItemGroup>
    @(ImplicitPackageVariable->'<ImplicitPackageReferenceVersion Include="%(Identity)" TargetFrameworkVersion="%(TargetFrameworkVersion)" DefaultVersion="%(DefaultVersion)" LatestVersion="%(LatestVersion)"/>', '%0A    ')
  </ItemGroup>
</Project>
]]>
    </BundledVersionsPropsContent>
  </PropertyGroup>

    <MakeDir Directories="$(GeneratedMSBuildExtensionsDirectory)" />
    <WriteLinesToFile File="$(GeneratedMSBuildExtensionsDirectory)/$(BundledVersionsPropsFileName)"
                      Lines="$(BundledVersionsPropsContent)"
                      Overwrite="true" />
  </Target>

  <Target Name="GenerateBundledCliToolsProps">
    <PropertyGroup>
      <BundledBundledCliToolsPropsFileName>Microsoft.NETCoreSdk.BundledCliTools.props</BundledBundledCliToolsPropsFileName>
    </PropertyGroup>

    <PropertyGroup>
      <BundledBundledCliToolsPropsContent>
<![CDATA[
<!--
***********************************************************************************************
$(BundledBundledCliToolsPropsFileName)

WARNING:  DO NOT MODIFY this file unless you are knowledgeable about MSBuild and have
          created a backup copy.  Incorrect changes to this file will make it
          impossible to load or build your projects from the command-line or the IDE.

Copyright (c) .NET Foundation. All rights reserved.
***********************************************************************************************
-->
<Project>
  <ItemGroup>
@(BundledDotnetTools->HasMetadata('ObsoletesCliTool')->'    %3CBundledDotNetCliToolReference Include="%(ObsoletesCliTool)" /%3E','%0A')
  </ItemGroup>
</Project>
]]>
    </BundledBundledCliToolsPropsContent>
  </PropertyGroup>

    <MakeDir Directories="$(GeneratedMSBuildExtensionsDirectory)" />
    <WriteLinesToFile File="$(GeneratedMSBuildExtensionsDirectory)/$(BundledBundledCliToolsPropsFileName)"
                      Lines="$(BundledBundledCliToolsPropsContent)"
                      Overwrite="true" />
  </Target>
</Project>
