<?xml version="1.0" encoding="utf-8"?>
<Project InitialTargets="SetSigningProperties" DefaultTargets="SignFiles" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <PropertyGroup>
    <!-- The signing infrastructure runs using MSBuild 14, which doesn't support some of the new syntax we're using.  So set the BuildingSingingProject
         property here to avoid importing files we don't need for signing which would cause errors if imported when using MSBuild 14. -->
    <BuildingSigningProject>true</BuildingSigningProject>
    <ExternalCertificateId Condition="'$(ExternalCertificateId)' == ''">135020001</ExternalCertificateId>
    <InternalCertificateId Condition="'$(InternalCertificateId)' == ''">Microsoft402</InternalCertificateId>
    <NugetCertificateId Condition="'$(NugetCertificateId)' == ''">NuGet</NugetCertificateId>
  </PropertyGroup>
  
  <Import Project="..\dir.props" />
  <Import Project="MicroBuild.props" />
  <Import Project="$(MicroBuildPropsAndTargetsPath)MicroBuild.Core.props" />

  <!-- This will be overridden if we're building with MicroBuild. -->
  <Target Name="SignFiles">
    <Message Text="Fake sign target.  Would sign: @(FilesToSign)" />
  </Target>

  <Target Name="SetSigningProperties">
    <Error Condition="'$(Rid)' == ''" Text="Missing required property 'Rid'." />
    <PropertyGroup>
      <SdkResolverOutputDirectory>$(IntermediateDirectory)/MSBuildExtensionsLayout/MSBuildSdkResolver</SdkResolverOutputDirectory>

      <!-- The OutDir and IntermediateOutputPath properties are required by MicroBuild. MicroBuild only
           signs files that are under these paths. -->
      <OutDir Condition="'$(OutDir)' == ''">$(BaseOutputDirectory)</OutDir>
      <IntermediateOutputPath Condition="'$(IntermediateOutputPath)' == ''">$(IntermediateDirectory)</IntermediateOutputPath>
    </PropertyGroup>
  </Target>

  <Target Name="PostCompileSign" DependsOnTargets="GetPostCompileSignFiles;GetSignNuPkgContentsFiles;GetSignSdkResolverContentsFiles;SignFiles" />

  <Target Name="GetPostCompileSignFiles">
    <ItemGroup>
      <!-- External files -->
      <FilesToSign Include="$(OutputDirectory)/sdk/**/Newtonsoft.Json.dll;
                            $(OutputDirectory)/shared/**/libuv.dll">
        <Authenticode>$(ExternalCertificateId)</Authenticode>
      </FilesToSign>
      <!-- Built binaries -->
      <FilesToSign Include="$(OutputDirectory)/sdk/**/csc.exe;
                            $(OutputDirectory)/sdk/**/csc.dll;
                            $(OutputDirectory)/sdk/**/VBCSCompiler.dll;
                            $(OutputDirectory)/sdk/**/vbc.exe;
                            $(OutputDirectory)/sdk/**/vbc.dll;
                            $(OutputDirectory)/sdk/**/fsc.exe;
                            $(OutputDirectory)/sdk/**/fsi.exe;
                            $(OutputDirectory)/sdk/**/FSharp.*.dll;
                            $(OutputDirectory)/sdk/**/dotnet.dll;
                            $(OutputDirectory)/sdk/**/dotnet.resources.dll;
                            $(OutputDirectory)/sdk/**/System.*.dll;
                            $(OutputDirectory)/sdk/**/Microsoft.*.dll;
                            $(OutputDirectory)/sdk/**/NuGet*.dll;
                            $(OutputDirectory)/sdk/**/datacollector.dll;
                            $(OutputDirectory)/sdk/**/datacollector.exe;
                            $(OutputDirectory)/sdk/**/MSBuild.dll;
                            $(OutputDirectory)/sdk/**/MSBuild.resources.dll;
                            $(OutputDirectory)/sdk/**/testhost.dll;
                            $(OutputDirectory)/sdk/**/testhost.exe;
                            $(OutputDirectory)/sdk/**/testhost.x86.exe;
                            $(OutputDirectory)/sdk/**/vstest.console.dll;
                            $(OutputDirectory)/sdk/**/vstest.console.resources.dll">
        <Authenticode>$(InternalCertificateId)</Authenticode>
      </FilesToSign>
      <!-- Built files for the packages -->
      <FilesToSign Include="$(CompilationDirectory)/forPackaging/**/*dotnet*.dll;
                            $(CompilationDirectory)/forPackaging/**/Microsoft.Extensions.DependencyModel.dll;
                            $(CompilationDirectory)/forPackaging/**/Microsoft.Extensions.Testing.Abstractions.dll">
        <Authenticode>$(InternalCertificateId)</Authenticode>
      </FilesToSign>
    </ItemGroup>
  </Target>

  <!-- Keeping this target around so as not to break existing build definitions. -->
  <Target Name="SignNuPkgContents" />

  <Target Name="GetSignNuPkgContentsFiles">
    <ItemGroup>
      <!-- NuPkg contents -->
      <FilesToSign Include="$(BaseOutputDirectory)/bin/Microsoft.DotNet.Cli.Utils/**/Microsoft.DotNet.Cli.Utils.dll;
                            $(BaseOutputDirectory)/bin/Microsoft.DotNet.Cli.Utils/**/Microsoft.DotNet.Cli.Utils.resources.dll;
                            $(BaseOutputDirectory)/bin/Microsoft.DotNet.MSBuildSdkResolver/**/Microsoft.DotNet.MSBuildSdkResolver.dll">
        <Authenticode>$(InternalCertificateId)</Authenticode>
      </FilesToSign>
    </ItemGroup>
  </Target>

  <!-- Keeping this target around so as not to break existing build definitions. -->
  <Target Name="SignSdkResolverContents" />

  <Target Name="GetSignSdkResolverContentsFiles">
    <ItemGroup>
      <FilesToSign Include="$(SdkResolverOutputDirectory)/Microsoft.DotNet.MSBuildSdkResolver.dll">
        <Authenticode>$(InternalCertificateId)</Authenticode>
      </FilesToSign>
    </ItemGroup>
  </Target>

  <Target Name="SignMsiAndCab" DependsOnTargets="GetSignMsiAndCabFiles;SignFiles" />

  <Target Name="GetSignMsiAndCabFiles">
    <ItemGroup>
      <FilesToSign Include="$(PackagesDirectory)/**/*.msi">
        <Authenticode>$(InternalCertificateId)</Authenticode>
      </FilesToSign>
      <FilesToSign Include="$(PackagesDirectory)/**/*.cab">
        <Authenticode>$(InternalCertificateId)</Authenticode>
      </FilesToSign>
    </ItemGroup>
  </Target>

  <Target Name="SignEngine" DependsOnTargets="GetSignEngineFiles;SignFiles" />

  <Target Name="GetSignEngineFiles">
    <ItemGroup>
      <FilesToSign Include="$(PackagesDirectory)/**/*engine.exe">
        <Authenticode>$(InternalCertificateId)</Authenticode>
      </FilesToSign>
    </ItemGroup>
  </Target>

  <Target Name="SignCliBundle" DependsOnTargets="GetSignCliBundleFiles;SignFiles" />

  <Target Name="GetSignCliBundleFiles">
    <ItemGroup>
      <FilesToSign Include="$(PackagesDirectory)/**/*.exe">
        <Authenticode>$(InternalCertificateId)</Authenticode>
      </FilesToSign>
    </ItemGroup>
  </Target>

  <Import Project="$(MicroBuildPropsAndTargetsPath)MicroBuild.Core.targets" />
</Project>
