From 11ee21b3493ca0a52aeda89b7e8a49fd32b6bc29 Mon Sep 17 00:00:00 2001
From: Jacques Eloff <joeloff@users.noreply.github.com>
Date: Wed, 24 Jul 2024 11:32:01 -0700
Subject: [PATCH 1/2] Add RegistrySearch for Windows Installer

---
 .../build/wix/bundle/bundle.wxs               |  5 +++
 .../build/wix/bundle/upgradePolicies.wxs      | 40 +++++++++++++++++++
 .../build/wix/wix.targets                     |  3 ++
 3 files changed, 48 insertions(+)
 create mode 100644 src/Microsoft.DotNet.Build.Tasks.Installers/build/wix/bundle/upgradePolicies.wxs

diff --git a/src/Microsoft.DotNet.Build.Tasks.Installers/build/wix/bundle/bundle.wxs b/src/Microsoft.DotNet.Build.Tasks.Installers/build/wix/bundle/bundle.wxs
index 978324e7598..65f35e08aea 100644
--- a/src/Microsoft.DotNet.Build.Tasks.Installers/build/wix/bundle/bundle.wxs
+++ b/src/Microsoft.DotNet.Build.Tasks.Installers/build/wix/bundle/bundle.wxs
@@ -24,6 +24,11 @@
       </bal:Condition>
     <?endif?>
 
+    <!-- Search references for upgrade policy keys -->
+    <util:RegistrySearchRef Id="RemovePreviousVersionRegistryKeySearch"/>
+    <util:RegistrySearchRef Id="RemoveSpecificPreviousVersionRegistryKeyExistsSearch"/>
+    <util:RegistrySearchRef Id="RemoveSpecificPreviousVersionRegistryKeySearch"/>
+
     <!--
       List of bundles that this bundle is an upgrade for. Used to support upgrade from bundles
       that were produced before UpdateCode was standardized per major-minor channel.
diff --git a/src/Microsoft.DotNet.Build.Tasks.Installers/build/wix/bundle/upgradePolicies.wxs b/src/Microsoft.DotNet.Build.Tasks.Installers/build/wix/bundle/upgradePolicies.wxs
new file mode 100644
index 00000000000..6ac98e0cabd
--- /dev/null
+++ b/src/Microsoft.DotNet.Build.Tasks.Installers/build/wix/bundle/upgradePolicies.wxs
@@ -0,0 +1,40 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi" xmlns:util="http://schemas.microsoft.com/wix/UtilExtension">
+  <?include "..\variables.wxi" ?>
+
+  <Fragment>
+    <!-- Bundle variables become unset if a search fails. The global key is retrieved first. If this fails,
+         RemoveUpgradeRelatedBundle becomes unset, allowing the version specific search to potentially set 
+         or override the bundle variable. -->
+    <util:RegistrySearch Id="RemovePreviousVersionRegistryKeySearch"
+                         Root="HKLM"
+                         Key="SOFTWARE\Microsoft\.NET"
+                         Value="RemovePreviousVersion"
+                         Result="value"
+                         Variable="RemoveUpgradeRelatedBundle"
+                         Win64="yes" />
+
+    <!-- The version specific key matching the major/minor of the .NET bundle takes precedence. The first search
+         checks whether the registry value exists and creates a variable that can be used as a condition
+         to executes the second part of the search to retrieve it. If the value doesn't exist, RemoveUpgradeRelatedBundle
+         retains its original value, or if it wasn't set, will be assigned a proper default through the BA (wixstdba). -->
+    <util:RegistrySearch Id="RemoveSpecificPreviousVersionRegistryKeyExistsSearch"
+                         After="RemovePreviousVersionRegistryKeySearch"
+                         Root="HKLM"
+                         Key="SOFTWARE\Microsoft\.NET\$(var.MajorVersion).$(var.MinorVersion)"
+                         Value="RemovePreviousVersion"
+                         Result="exists"
+                         Variable="RemoveSpecificPreviousVersionRegistryKeyExists"
+                         Win64="yes" />
+    <util:RegistrySearch Id="RemoveSpecificPreviousVersionRegistryKeySearch"
+                         After="RemoveSpecificPreviousVersionRegistryKeyExistsSearch"
+                         Condition="RemoveSpecificPreviousVersionRegistryKeyExists=1"
+                         Root="HKLM"
+                         Key="SOFTWARE\Microsoft\.NET\$(var.MajorVersion).$(var.MinorVersion)"
+                         Value="RemovePreviousVersion"
+                         Result="value"
+                         Variable="RemoveUpgradeRelatedBundle"
+                         Win64="yes" />
+  </Fragment>
+
+</Wix>
diff --git a/src/Microsoft.DotNet.Build.Tasks.Installers/build/wix/wix.targets b/src/Microsoft.DotNet.Build.Tasks.Installers/build/wix/wix.targets
index 16410e4d890..4cef8b759ac 100644
--- a/src/Microsoft.DotNet.Build.Tasks.Installers/build/wix/wix.targets
+++ b/src/Microsoft.DotNet.Build.Tasks.Installers/build/wix/wix.targets
@@ -125,6 +125,7 @@
       <WixExtensions Include="WixUtilExtension.dll" />
 
       <WixSrcFile Include="$(MSBuildThisFileDirectory)bundle/bundle.wxs" />
+      <WixSrcFile Include="$(MSBuildThisFileDirectory)bundle/upgradePolicies.wxs" />
     </ItemGroup>
   </Target>
 
@@ -181,6 +182,8 @@
       <LocFile Include="$(BundleThemeDirectory)\theme\**\bundle.wxl" />
       <LocDirName Include="$([System.String]::new('%(LocFile.RecursiveDir)').TrimEnd('\'))" />
 
+      <CandleVariables Include="MajorVersion" Value="$(MajorVersion)" />
+      <CandleVariables Include="MinorVersion" Value="$(MinorVersion)" />
       <CandleVariables Include="DisplayVersion" Value="$(MajorVersion).$(MinorVersion).$(PatchVersion).$(BuildNumberMajor)" />
       <CandleVariables Include="LcidList" Value="@(LocDirName)" />
       <CandleVariables Include="BundleThmDir" Value="$(BundleThemeDirectory)" />

From 81b4d2056bf7ced5a70a00a831d88d74b258d2b6 Mon Sep 17 00:00:00 2001
From: Jacques Eloff <joeloff@users.noreply.github.com>
Date: Thu, 25 Jul 2024 12:11:13 -0700
Subject: [PATCH 2/2] Remove unnecessary include (breaks SDK)

---
 .../build/wix/bundle/upgradePolicies.wxs                       | 3 ---
 1 file changed, 3 deletions(-)

diff --git a/src/Microsoft.DotNet.Build.Tasks.Installers/build/wix/bundle/upgradePolicies.wxs b/src/Microsoft.DotNet.Build.Tasks.Installers/build/wix/bundle/upgradePolicies.wxs
index 6ac98e0cabd..ffaa06038b7 100644
--- a/src/Microsoft.DotNet.Build.Tasks.Installers/build/wix/bundle/upgradePolicies.wxs
+++ b/src/Microsoft.DotNet.Build.Tasks.Installers/build/wix/bundle/upgradePolicies.wxs
@@ -1,7 +1,5 @@
 <?xml version="1.0" encoding="utf-8"?>
 <Wix xmlns="http://schemas.microsoft.com/wix/2006/wi" xmlns:util="http://schemas.microsoft.com/wix/UtilExtension">
-  <?include "..\variables.wxi" ?>
-
   <Fragment>
     <!-- Bundle variables become unset if a search fails. The global key is retrieved first. If this fails,
          RemoveUpgradeRelatedBundle becomes unset, allowing the version specific search to potentially set 
@@ -36,5 +34,4 @@
                          Variable="RemoveUpgradeRelatedBundle"
                          Win64="yes" />
   </Fragment>
-
 </Wix>
