From 7cc1632ae0429fcfae44c77630fff4d29ead8891 Mon Sep 17 00:00:00 2001 From: "G.Reijn" Date: Wed, 11 Dec 2024 02:11:38 +0100 Subject: [PATCH] Rewrite logic to hardcode key --- .../Microsoft.VSCode.Dsc.psm1 | 82 +++++++++---------- 1 file changed, 39 insertions(+), 43 deletions(-) diff --git a/resources/Microsoft.VSCode.Dsc/Microsoft.VSCode.Dsc.psm1 b/resources/Microsoft.VSCode.Dsc/Microsoft.VSCode.Dsc.psm1 index 6ed13847..04561d23 100644 --- a/resources/Microsoft.VSCode.Dsc/Microsoft.VSCode.Dsc.psm1 +++ b/resources/Microsoft.VSCode.Dsc/Microsoft.VSCode.Dsc.psm1 @@ -5,35 +5,40 @@ $ErrorActionPreference = 'Stop' Set-StrictMode -Version Latest #region Functions -function Search-UninstallRegistry { - [CmdletBinding(DefaultParameterSetName = 'User')] +function TryGetRegistryValue { param ( - [Parameter(ParameterSetName = 'User', Mandatory = $true)] - [switch] $User, - - [Parameter(ParameterSetName = 'Machine', Mandatory = $true)] - [switch] $Machine, + [Parameter(Mandatory = $true)] + [string]$Key, [Parameter(Mandatory = $true)] - [string] $DisplayName + [string]$Property ) - switch ($PSCmdlet.ParameterSetName) { - 'User' { - $Path = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall' - } - 'Machine' { - $Path = 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall' + if (Test-Path -Path $Key) { + try { + return (Get-ItemProperty -Path $Key | Select-Object -ExpandProperty $Property) + } catch { + Write-Verbose "Property `"$($Property)`" could not be found." } + } else { + Write-Verbose 'Registry key does not exist.' } +} - $UninstallKeys = Get-ChildItem -Path $Path - foreach ($key in $UninstallKeys) { - $value = Get-ItemProperty -Path $key.PSPath +function Get-OSArchitectureRegistryKey { + [CmdletBinding()] + param ( + [switch] $Insiders + ) - if ($value.DisplayName -eq $DisplayName) { - return $value - } + $architecture = [System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture + + switch ($architecture) { + 'X64' { return $Insiders ? @('{217B4C08-948D-4276-BFBB-BEE930AE5A2C}_is1', '{1287CAD5-7C8D-410D-88B9-0D1EE4A83FF2}_is1') : @('{771FD6B0-FA20-440A-A002-3B3BAC16DC50}_is1', '{EA457B21-F73E-494C-ACAB-524FDE069978}_is1') } + 'X86' { return $Insiders ? @('{C26E74D1-022E-4238-8B9D-1E7564A36CC9}_is1', '{26F4A15E-E392-4887-8C09-7BC55712FD5B}_is1') : @('{D628A17A-9713-46BF-8D57-E671B46A741E}_is1', '{F8A2A208-72B3-4D61-95FC-8A65D340689B}_is1') } + 'Arm' { Throw 'Not supported.' } + 'Arm64' { return $Insiders ? @('{69BD8F7B-65EB-4C6F-A14E-44CFA83712C0}_is1', '{0AEDB616-9614-463B-97D7-119DD86CCA64}_is1') : @('{D9E514E7-1A56-452D-9337-2990C0DC4310}_is1', '{A5270FC5-65AD-483E-AC30-2C276B63D0AC}_is1') } + Default { Throw 'Could not determine architecture.' } } } @@ -42,43 +47,34 @@ function Get-VSCodeCLIPath { [switch]$Insiders ) + # Get the available keys + $registryKeys = Get-OSArchitectureRegistryKey -Insiders:$Insiders.IsPresent + $registryHive = @('HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall', 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall', 'HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall') + if ($IsLinux) { if ($Insiders) { $InstallLocation = Join-Path ($env:PATH.Split([System.IO.Path]::PathSeparator) -match 'Microsoft VS Code Insiders') 'code-insiders' - if (Test-Path $InstallLocation) { + if (Test-Path $InstallLocation -ErrorAction SilentlyContinue) { return $InstallLocation } } else { $InstallLocation = Join-Path ($env:PATH.Split([System.IO.Path]::PathSeparator) -match 'Microsoft VS Code') 'code' - if (Test-Path $InstallLocation) { + if (Test-Path $InstallLocation -ErrorAction SilentlyContinue) { return $InstallLocation } } } if ($IsWindows) { - if ($Insiders) { - $cmdPath = 'bin\code-insiders.cmd' - $insidersUserInstallLocation = Search-UninstallRegistry -User -DisplayName 'Microsoft Visual Studio Code Insiders (User)' - if ($insidersUserInstallLocation) { - return $insidersUserInstallLocation.InstallLocation + $cmdPath - } - - $insidersMachineInstallLocation = Search-UninstallRegistry -Machine -DisplayName 'Microsoft Visual Studio Code Insiders' - if ($insidersMachineInstallLocation) { - return $insidersMachineInstallLocation.InstallLocation + $cmdPath - } - } else { - $cmdPath = 'bin\code.cmd' - $codeUserInstallLocation = Search-UninstallRegistry -User -DisplayName 'Microsoft Visual Studio Code (User)' - if ($codeUserInstallLocation) { - return $codeUserInstallLocation.InstallLocation + $cmdPath - } - - $codeMachineInstallLocation = Search-UninstallRegistry -Machine -DisplayName 'Microsoft Visual Studio Code (User)' - if ($codeMachineInstallLocation) { - return $codeMachineInstallLocation + $cmdPath + foreach ($hive in $registryHive) { + foreach ($key in $registryKeys) { + Write-Verbose -Message ("Searching path '{0}' with key '{1}'" -f $hive, $key) + $installLocation = TryGetRegistryValue -Key "$hive\$key" -Property 'InstallLocation' + if ($installLocation) { + $cmdPath = $Insiders ? 'bin\code-insiders.cmd' : 'bin\code.cmd' + return Join-Path $installLocation $cmdPath + } } } }