<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="..\..\..\common.openconsole.props" Condition="'$(OpenConsoleDir)'==''" />

  <PropertyGroup Label="NuGet Dependencies">
    <TerminalMUX>true</TerminalMUX>
  </PropertyGroup>
  <Import Project="$(OpenConsoleDir)src\wap-common.build.pre.props" />
  <Import Project="$(OpenConsoleDir)src\common.nugetversions.props" />
  <PropertyGroup Label="Configuration">
    <!--
    These two properties are very important!
    Without them, msbuild will stomp MinVersion and MaxVersionTested in the
    Package.appxmanifest and replace them with whatever our values for
    TargetPlatformMinVersion and TargetPlatformVersion are.
     -->
    <AppxOSMinVersionReplaceManifestVersion>false</AppxOSMinVersionReplaceManifestVersion>
    <AppxOSMaxVersionTestedReplaceManifestVersion>false</AppxOSMaxVersionTestedReplaceManifestVersion>
    <OCExecutionAliasName Condition="'$(WindowsTerminalBranding)'=='' or '$(WindowsTerminalBranding)'=='Dev'">wtd</OCExecutionAliasName>
    <OCExecutionAliasName Condition="'$(OCExecutionAliasName)'==''">wt</OCExecutionAliasName>
  </PropertyGroup>
  <PropertyGroup>
    <ProjectGuid>CA5CAD1A-224A-4171-B13A-F16E576FDD12</ProjectGuid>
    <EntryPointProjectUniqueName>..\WindowsTerminal\WindowsTerminal.vcxproj</EntryPointProjectUniqueName>
    <DebuggerType>NativeOnly</DebuggerType>
  </PropertyGroup>
  <PropertyGroup Condition="!Exists('CascadiaPackage_TemporaryKey.pfx')">
    <AppxPackageSigningEnabled>false</AppxPackageSigningEnabled>
    <AppxBundle>Never</AppxBundle>
  </PropertyGroup>
  <PropertyGroup Condition="Exists('CascadiaPackage_TemporaryKey.pfx')">
    <AppxPackageSigningEnabled>true</AppxPackageSigningEnabled>
    <AppxAutoIncrementPackageRevision>False</AppxAutoIncrementPackageRevision>
    <PackageCertificateKeyFile>CascadiaPackage_TemporaryKey.pfx</PackageCertificateKeyFile>
  </PropertyGroup>
  <ItemGroup Condition="Exists('CascadiaPackage_TemporaryKey.pfx')">
    <None Include="CascadiaPackage_TemporaryKey.pfx" />
  </ItemGroup>
  <ItemGroup>
    <AppxManifest Include="Package.appxmanifest" Condition="'$(WindowsTerminalBranding)'=='Release'">
      <SubType>Designer</SubType>
    </AppxManifest>
    <AppxManifest Include="Package-Pre.appxmanifest" Condition="'$(WindowsTerminalBranding)'=='Preview'">
      <SubType>Designer</SubType>
    </AppxManifest>
    <AppxManifest Include="Package-Can.appxmanifest" Condition="'$(WindowsTerminalBranding)'=='Canary'">
      <SubType>Designer</SubType>
    </AppxManifest>
    <AppxManifest Include="Package-Dev.appxmanifest" Condition="'$(WindowsTerminalBranding)'=='' or '$(WindowsTerminalBranding)'=='Dev'">
      <SubType>Designer</SubType>
    </AppxManifest>
  </ItemGroup>
  <ItemGroup>
    <!-- Resources -->
    <!-- This resw only defines things that are used in this package's AppxManifest,
         so it's not in the common resource items. -->
    <PRIResource Include="Resources\en-US\Resources.resw" />
    <PRIResource Include="Resources\Resources.resw" />
    <OCResourceDirectory Include="Resources" />
  </ItemGroup>

  <!-- This is picked up by CascadiaResources.build.items. -->
  <PropertyGroup Condition="'$(WindowsTerminalBranding)'=='' or '$(WindowsTerminalBranding)'=='Dev'">
    <WindowsTerminalAssetSuffix>-Dev</WindowsTerminalAssetSuffix>
  </PropertyGroup>
  <PropertyGroup Condition="'$(WindowsTerminalBranding)'=='Preview'">
    <WindowsTerminalAssetSuffix>-Pre</WindowsTerminalAssetSuffix>
  </PropertyGroup>
  <PropertyGroup Condition="'$(WindowsTerminalBranding)'=='Canary'">
    <WindowsTerminalAssetSuffix>-Can</WindowsTerminalAssetSuffix>
  </PropertyGroup>

  <Import Project="$(MSBuildThisFileDirectory)..\CascadiaResources.build.items" />
  <Import Project="$(OpenConsoleDir)src\wap-common.build.post.props" />

  <ItemGroup>
    <ProjectReference Include="$(OpenConsoleDir)src\cascadia\WindowsTerminal\WindowsTerminal.vcxproj">
      <Project>{CA5CAD1A-1754-4A9D-93D7-857A9D17CB1B}</Project>
    </ProjectReference>
    <ProjectReference Include="$(OpenConsoleDir)src\cascadia\ShellExtension\WindowsTerminalShellExt.vcxproj">
      <Project>{f2ed628a-db22-446f-a081-4cc845b51a2b}</Project>
    </ProjectReference>
    <ProjectReference Include="$(OpenConsoleDir)src\cascadia\wt\wt.vcxproj">
      <Project>{506fd703-baa7-4f6e-9361-64f550ec8fca}</Project>
    </ProjectReference>
    <ProjectReference Include="$(OpenConsoleDir)src\host\exe\Host.EXE.vcxproj">
      <Project>{9CBD7DFA-1754-4A9D-93D7-857A9D17CB1B}</Project>
    </ProjectReference>
    <ProjectReference Include="$(OpenConsoleDir)src\cascadia\ElevateShim\elevate-shim.vcxproj" >
      <Project>{416fd703-baa7-4f6e-9361-64f550ec8fca}</Project>
    </ProjectReference>
    <ProjectReference Include="$(OpenConsoleDir)src\host\proxy\Host.Proxy.vcxproj">
      <Project>{71CC9D78-BA29-4D93-946F-BEF5D9A3A6EF}</Project>
    </ProjectReference>
  </ItemGroup>

  <Target Name="OpenConsoleStompSourceProjectForWapProject" BeforeTargets="_ConvertItems">
    <ItemGroup>
      <!-- Stomp all "SourceProject" values for all incoming dependencies to flatten the package. -->
      <_TemporaryFilteredWapProjOutput Include="@(_FilteredNonWapProjProjectOutput)" />
      <_FilteredNonWapProjProjectOutput Remove="@(_TemporaryFilteredWapProjOutput)" />
      <_FilteredNonWapProjProjectOutput Include="@(_TemporaryFilteredWapProjOutput)">
        <!-- Blank the SourceProject here to vend all files into the root of the package. -->
        <SourceProject>
        </SourceProject>
        <!-- Replace the filename for wt/wtd.exe with the one the manifest wants. -->
        <TargetPath Condition="'%(Filename)' == 'wt' and '%(Extension)' == '.exe'">$(OCExecutionAliasName).exe</TargetPath>
      </_FilteredNonWapProjProjectOutput>
    </ItemGroup>
  </Target>
  <!-- Move all the PRI files that would be packaged into the appx into _PriFile so that
       GenerateProjectPriFile catches them. This requires us to move payload collection
       up before GenerateProjectPriFile, when it is typically _after_ it (because the
       DesktopBridge project type is built to only prepare the payload during appx manifest
       generation.

       Since PRI file generation is _before_ manifest generation (for possibly obvious or
       important reasons), that doesn't work for us.
  -->
  <Target Name="OpenConsoleLiftDesktopBridgePriFiles" DependsOnTargets="_ConvertItems">
    <ItemGroup>
      <_PriFile Include="@(_NonWapProjProjectOutput)" Condition="'%(Extension)' == '.pri'" />
      <!-- Remove all other .pri files from the appx payload. -->
      <AppxPackagePayload Remove="@(AppxPackagePayload)" Condition="'%(Extension)' == '.pri'" />
    </ItemGroup>
  </Target>
  <!-- VS 16.3.0 added a rule to the WAP packaging project that removes all non-WAP payload, which we were relying on to
       roll up our subproject resources. We have to suppress that rule but keep part of its logic, because that rule is
       where the AppxPackagePayload items are created. -->
  <PropertyGroup>
    <WapProjBeforeGenerateAppxManifestDependsOn>
        $([MSBuild]::Unescape('$(WapProjBeforeGenerateAppxManifestDependsOn.Replace('_RemoveAllNonWapUWPItems', '_OpenConsoleRemoveAllNonWapUWPItems'))'))
    </WapProjBeforeGenerateAppxManifestDependsOn>
  </PropertyGroup>
  <Target Name="_OpenConsoleRemoveAllNonWapUWPItems">
    <ItemGroup>
      <AppxPackagePayload Include="@(WapProjPackageFile)" />
      <AppxUploadPackagePayload Include="@(UploadWapProjPackageFile)" />
      <!-- 16.3.0 - remove non-resources.pri PRI files since we just forced them back in. -->
      <AppxPackagePayload Remove="@(AppxPackagePayload)" Condition="'%(Extension)' == '.pri' and '%(Filename)' != 'resources'" />
      <AppxUploadPackagePayload Remove="@(AppxUploadPackagePayload)" Condition="'%(Extension)' == '.pri' and '%(Filename)' != 'resources'" />

      <!-- Remove all of the xaml files, because we are using embedded xbf payloads (saves about 500kb on disk!) -->
      <AppxPackagePayload Remove="@(AppxPackagePayload)" Condition="'%(Extension)' == '.xaml'" />
      <AppxUploadPackagePayload Remove="@(AppxUploadPackagePayload)" Condition="'%(Extension)' == '.xaml'" />
    </ItemGroup>
  </Target>

  <!--
    Some of our dependencies still require a CRT, so we're going to ship the forwarders in our package and
    depend on the desktop CRT. This lets us unify the Windows 10 and Windows 11 builds around a common CRT.
  -->
  <!-- This target removes the FrameworkSdkReferences from before the AppX package targets manifest generation happens.
       This is part of the generic machinery that applies to every AppX. -->
  <Target Name="_OpenConsoleStripAllDependenciesFromPackageFirstManifest" BeforeTargets="_GenerateCurrentProjectAppxManifest">
    <ItemGroup>
      <FrameworkSdkReference Remove="@(FrameworkSdkReference)" Condition="'%(FrameworkSdkReference.SimpleName)'=='Microsoft.VCLibs'" />
      <FrameworkSdkPackage Remove="@(FrameworkSdkPackage)" Condition="'%(FrameworkSdkPackage.Name)'=='Microsoft.VCLibs.140.00' or '%(FrameworkSdkPackage.Name)'=='Microsoft.VCLibs.140.00.Debug'" />
      <FrameworkSdkReference Remove="@(FrameworkSdkReference)" Condition="'%(FrameworkSdkReference.SimpleName)'=='Microsoft.VCLibs.Desktop'" />
      <FrameworkSdkPackage Remove="@(FrameworkSdkPackage)" Condition="'%(FrameworkSdkPackage.Name)'=='Microsoft.VCLibs.140.00.UWPDesktop' or '%(FrameworkSdkPackage.Name)'=='Microsoft.VCLibs.140.00.Debug.UWPDesktop'" />
    </ItemGroup>
  </Target>

  <!-- This target removes the FrameworkSdkPackages from before the *desktop bridge* manifest generation happens. -->
  <Target Name="_OpenConsoleStripAllDependenciesFromPackageSecondManifest" BeforeTargets="_GenerateDesktopBridgeAppxManifest" DependsOnTargets="_ResolveVCLibDependencies">
    <ItemGroup>
      <FrameworkSdkReference Remove="@(FrameworkSdkReference)" Condition="'%(FrameworkSdkReference.SimpleName)'=='Microsoft.VCLibs'" />
      <FrameworkSdkPackage Remove="@(FrameworkSdkPackage)" Condition="'%(FrameworkSdkPackage.Name)'=='Microsoft.VCLibs.140.00' or '%(FrameworkSdkPackage.Name)'=='Microsoft.VCLibs.140.00.Debug'" />
      <FrameworkSdkReference Remove="@(FrameworkSdkReference)" Condition="'%(FrameworkSdkReference.SimpleName)'=='Microsoft.VCLibs.Desktop'" />
      <FrameworkSdkPackage Remove="@(FrameworkSdkPackage)" Condition="'%(FrameworkSdkPackage.Name)'=='Microsoft.VCLibs.140.00.UWPDesktop' or '%(FrameworkSdkPackage.Name)'=='Microsoft.VCLibs.140.00.Debug.UWPDesktop'" />
    </ItemGroup>
  </Target>

  <!-- This is required to get the package dependency in the AppXManifest. -->
  <Import Project="$(OpenConsoleDir)src\common.nugetversions.targets" />

  <Import Project="$(SolutionDir)build\rules\CollectWildcardResources.targets" />
</Project>
