<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="15.0">
  <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
  <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.tasks" />
  <Import Project="$(RepoRoot)/build/compile/LzmaArchive.targets" />
  <Import Project="$(RepoRoot)/build/MSBuildExtensions.targets" />

  <PropertyGroup>
    <VersionPrefix>$(CliVersionPrefix)</VersionPrefix>
    <TargetFramework>netcoreapp2.0</TargetFramework>
    <RuntimeFrameworkVersion>$(CLI_SharedFrameworkVersion)</RuntimeFrameworkVersion>
    <GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
    <CopyBuildOutputToPublishDirectory>false</CopyBuildOutputToPublishDirectory>
    <PackageTargetFallback>$(PackageTargetFallback);dotnet5.4</PackageTargetFallback>
    <PublishDir>$(SdkOutputDirectory)</PublishDir>
    <VersionSuffix>$(CommitCount)</VersionSuffix>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.NETCore.App" Version="$(CLI_SharedFrameworkVersion)" />
    <PackageReference Include="Microsoft.Build.Runtime" Version="$(CLI_MSBuild_Version)" />
    <PackageReference Include="NuGet.Build.Tasks" Version="$(CLI_NuGet_Version)" />
    <PackageReference Include="Microsoft.TestPlatform.CLI" Version="$(CLI_TestPlatform_Version)" />
    <PackageReference Include="Microsoft.TestPlatform.Build" Version="$(CLI_TestPlatform_Version)" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\dotnet\dotnet.csproj" />
    <ProjectReference Include="..\Microsoft.DotNet.TestFramework\Microsoft.DotNet.TestFramework.csproj" />
    <ProjectReference Include="..\tool_msbuild\tool_msbuild.csproj" />
    <ProjectReference Include="..\tool_nuget\tool_nuget.csproj" />
  </ItemGroup>

  <Target Name="PublishSharedFramework"
          BeforeTargets="Publish">
    <ItemGroup>
      <SharedFramework Remove="*" />
      <SharedFramework Include="$(SharedFrameworkPublishDirectory)/**/*" />
    </ItemGroup>

    <Copy SourceFiles="@(SharedFramework)"
          DestinationFiles="@(SharedFramework->'$(OutputDirectory)/%(RecursiveDir)%(Filename)%(Extension)')" />
  </Target>

  <Target Name="PublishVersionFile"
          BeforeTargets="Publish">

    <WriteLinesToFile File="$(PublishDir)/.version"
                      Lines="$(CommitHash);$(SdkVersion);$(Rid)"
                      Overwrite="true" />
  </Target>

  <Target Name="PublishRoslyn"
          BeforeTargets="Publish">
          
    <DotNetPublish ToolPath="$(Stage0Directory)"
                   Configuration="$(Configuration)"
                   ProjectPath="$(SrcDirectory)/tool_roslyn/tool_roslyn.csproj" />
  </Target>

  <Target Name="GenerateCliRuntimeConfigurationFiles"
          AfterTargets="Publish">
    <RemoveAssetFromDepsPackages DepsFile="$(PublishDir)/$(TargetName).deps.json"
                                 SectionName="runtime"
                                 AssetPath="%(BundledToolProjects.Identity).dll" />

    <Copy SourceFiles="$(PublishDir)/$(TargetName).deps.json"
          DestinationFiles="$(PublishDir)/%(BundledTools.Identity).deps.json" />

    <Copy SourceFiles="$(PublishDir)/$(TargetName).runtimeconfig.json"
          DestinationFiles="$(PublishDir)/%(BundledTools.Identity).runtimeconfig.json" />

    <Delete Files="$(PublishDir)/$(TargetName).deps.json;
                   $(PublishDir)/$(TargetName).runtimeconfig.json;
                   $(PublishDir)/%(BundledToolProjects.Identity).dll" />

    <ChangeEntryPointLibraryName
          DepsFile="$(PublishDir)/%(BundledTools.Identity).deps.json"
          NewName="%(BundledTools.Identity).deps.json" />
  </Target>

  <Target Name="PublishMSBuildExtensions"
          DependsOnTargets="GenerateMSBuildExtensions"
          AfterTargets="Publish"
          BeforeTargets="GenerateCliRuntimeConfigurationFiles">
    
      <Copy SourceFiles="@(MSBuildExtensionsContent)"
            DestinationFolder="$(PublishDir)/%(RecursiveDir)" />

      <PropertyGroup>
        <MSBuildTargetsDirectory>$(PublishDir)/runtimes/any/native</MSBuildTargetsDirectory>
      </PropertyGroup>

      <ItemGroup>
        <NuPkgContentForMSBuildExtensions Include="$(MSBuildTargetsDirectory)/**/*.targets;
                                                   $(MSBuildTargetsDirectory)/**/*.props;
                                                   $(MSBuildTargetsDirectory)/**/*.overridetasks;
                                                   $(MSBuildTargetsDirectory)/**/*.tasks" />

        <NuPkgContentForMSBuildExtensionsRelativePaths Include="@(NuPkgContentForMSBuildExtensions->'runtimes/any/native/%(RecursiveDir)%(Filename)%(Extension)')" />
      </ItemGroup>

      <Move SourceFiles="@(NuPkgContentForMSBuildExtensions)"
            DestinationFiles="@(NuPkgContentForMSBuildExtensions->'$(PublishDir)/%(RecursiveDir)%(Filename)%(Extension)')" />

      <RemoveAssetFromDepsPackages DepsFile="$(PublishDir)/$(TargetName).deps.json"
                                   SectionName="runtimeTargets"
                                   AssetPath="%(NuPkgContentForMSBuildExtensionsRelativePaths.Identity)" />
  </Target>

  <Target Name="PublishSdks"
          AfterTargets="Publish">
    <ItemGroup>
      <SdksToBundle Include="$(RepoRoot)/build/BundledSdks.proj">
        <Properties>
          CLIBuildDll=$(CLIBuildDll);
          NuGetPackagesDir=$(NuGetPackagesDir);
          SdkLayoutDirectory=$(SdkOutputDirectory)/Sdks/%(BundledSdk.Identity);
          SdkPackageName=%(BundledSdk.Identity);
          SdkPackageVersion=%(BundledSdk.Version);
          Stage0Directory=$(Stage0Directory)
        </Properties>
      </SdksToBundle>
    </ItemGroup>

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

  <Target Name="PublishTemplates"
          AfterTargets="Publish">
    <ItemGroup>
      <TemplatesToBundle Include="$(RepoRoot)/build/BundledTemplates.proj">
        <Properties>
          TemplateLayoutDirectory=$(SdkOutputDirectory)/Templates;
          TemplatePackageName=%(BundledTemplate.Identity);
          TemplatePackageVersion=%(BundledTemplate.Version);
          Stage0Directory=$(Stage0Directory)
        </Properties>
      </TemplatesToBundle>
    </ItemGroup>

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

  <Target Name="PublishLzmaArchive"
          DependsOnTargets="GetNuGetPackagesArchive"
          AfterTargets="Publish">
  </Target>

  <Target Name="CrossgenPublishDir"
          Condition=" '$(DISABLE_CROSSGEN)' == '' "
          AfterTargets="PublishMSBuildExtensions">
    <ItemGroup>
      <!-- Removing Full CLR built TestHost assemblies from getting Crossgen as it is throwing error -->
      <SdkFiles Include="$(PublishDir)/**/*" Exclude="$(PublishDir)/TestHost*/**/*;$(PublishDir)/Sdks/**/*" />
    </ItemGroup>

    <AddMetadataIsPE Items="@(SdkFiles)">
      <Output TaskParameter="ResultItems" ItemName="SdkFilesWithPEMarker" />
    </AddMetadataIsPE>

    <ItemGroup>
      <CrossgenTargets Include="%(SdkFilesWithPEMarker.FullPath)" Condition=" '%(SdkFilesWithPEMarker.IsPE)' == 'True' " />
    </ItemGroup>

    <RemoveDuplicates Inputs="@(SdkFiles->'%(RootDir)%(Directory)')">
      <Output TaskParameter="Filtered" ItemName="PublishDirSubDirectories" />
    </RemoveDuplicates>

    <!-- Ensure crossgen tool is executable.  See https://github.com/NuGet/Home/issues/4424 -->
    <Chmod Condition=" '$(OSName)' != 'win' "
           Glob="$(CrossgenPath)"
           Mode="u+x" />

    <Crossgen SourceAssembly="%(CrossgenTargets.FullPath)"
              DestinationPath="%(CrossgenTargets.FullPath)"
              JITPath="$(LibCLRJitPath)"
              CrossgenPath="$(CrossgenPath)"
              ReadyToRun="True"
              PlatformAssemblyPaths="@(PlatformAssemblies);
                                     @(PublishDirSubDirectories);
                                     $(SharedFrameworkNameVersionPath)" />
  </Target>

  <Target Name="RemoveVbc"
          AfterTargets="CrossgenPublishDir">
    <ItemGroup>
      <_VbcPath Include="$(PublishDir)/**/vbc.exe" />
    </ItemGroup>

    <Delete Files="@(_VbcPath)" />
  </Target>

  <Target Name="ChmodPublishDir"
          AfterTargets="RemoveVbc"
          Condition=" '$(OSName)' != 'win' ">
    
    <Exec Command="find $(SdkOutputDirectory) -type d -exec chmod 755 {} \;" />
    <Exec Command="find $(SdkOutputDirectory) -type f -exec chmod 644 {} \;" />
    <Chmod Mode="755" Glob="$(SdkOutputDirectory)/Roslyn/RunCsc.sh" />
  </Target>

  <Target Name="CreateSymbolsDirectory"
          AfterTargets="RemoveVbc">
    <ItemGroup>
      <_AllSdkFiles Include="$(PublishDir)/**/*" />
    </ItemGroup>

    <Copy SourceFiles="@(_AllSdkFiles)"
        DestinationFiles="@(_AllSdkFiles->'$(SymbolsDirectory)/sdk/%(RecursiveDir)%(Filename)%(Extension)')" />
  </Target>

  <Target Name="RemovePdbsFromPublishDir"
          AfterTargets="CreateSymbolsDirectory">
    <ItemGroup>
      <PdbsToClean Include="$(PublishDir)/**/*.pdb" />
    </ItemGroup>

    <Delete Files="@(PdbsToClean)" />
  </Target>

  <Target Name="PublishStage2WithBackwardsCompatibleRuntimes"
          AfterTargets="PublishLzmaArchive;">
    <ItemGroup>
      <BackwardsCompatibleSharedFrameworks Remove="*" />
      <BackwardsCompatibleSharedFrameworks Include="$(BackwardsCompatibleSharedFrameworksPublishDirectory)/**/*" />

      <Stage2Cli Remove="*" />
      <Stage2Cli Include="$(OutputDirectory)/**/*" />
    </ItemGroup>

    <Copy SourceFiles="@(BackwardsCompatibleSharedFrameworks)"
          DestinationFiles="@(BackwardsCompatibleSharedFrameworks->'$(Stage2WithBackwardsCompatibleRuntimesOutputDirectory)/%(RecursiveDir)%(Filename)%(Extension)')" />

    <Copy SourceFiles="@(Stage2Cli)"
          DestinationFiles="@(Stage2Cli->'$(Stage2WithBackwardsCompatibleRuntimesOutputDirectory)/%(RecursiveDir)%(Filename)%(Extension)')" />
  </Target>
</Project>
