Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix path search #135

Merged
merged 9 commits into from
Feb 13, 2025
95 changes: 66 additions & 29 deletions resources/Microsoft.VSCode.Dsc/Microsoft.VSCode.Dsc.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,81 @@ $ErrorActionPreference = 'Stop'
Set-StrictMode -Version Latest

#region Functions
function Get-VSCodeCLIPath {
function Search-UninstallRegistry {
[CmdletBinding(DefaultParameterSetName = 'User')]
param (
[switch]$Insiders
)

# Product Codes for VSCode and VSCode Insider:
$vsCodeUserProductCode = '{771FD6B0-FA20-440A-A002-3B3BAC16DC50}_is1'
$vsCodeMachineProductCode = '{EA457B21-F73E-494C-ACAB-524FDE069978}_is1'
[Parameter(ParameterSetName = 'User', Mandatory = $true)]
[switch] $User,

$vsCodeInsidersUserProductCode = '{217B4C08-948D-4276-BFBB-BEE930AE5A2C}_is1'
$vsCodeInsidersMachineProductCode = '{1287CAD5-7C8D-410D-88B9-0D1EE4A83FF2}_is1'
[Parameter(ParameterSetName = 'Machine', Mandatory = $true)]
[switch] $Machine,
Gijsreyn marked this conversation as resolved.
Show resolved Hide resolved

# Note: WOW6432 registry not checked as no uninstall entry was found.
$userUninstallRegistry = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall'
$machineUninstallRegistry = 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall'
$installLocationProperty = 'InstallLocation'
[Parameter(Mandatory = $true)]
[string] $DisplayName
)

if ($Insiders) {
$cmdPath = 'bin\code-insiders.cmd'
$insidersUserInstallLocation = TryGetRegistryValue -Key "$($userUninstallRegistry)\$($vsCodeInsidersUserProductCode)" -Property $installLocationProperty
if ($insidersUserInstallLocation) {
return $insidersUserInstallLocation + $cmdPath
switch ($PSCmdlet.ParameterSetName) {
'User' {
$Path = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall'
}
'Machine' {
$Path = 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall'
}
}

$insidersMachineInstallLocation = TryGetRegistryValue -Key "$($machineUninstallRegistry)\$($vsCodeInsidersMachineProductCode)" -Property $installLocationProperty
if ($insidersMachineInstallLocation) {
return $insidersMachineInstallLocation + $cmdPath
$UninstallKeys = Get-ChildItem -Path $Path
foreach ($key in $UninstallKeys) {
$value = Get-ItemProperty -Path $key.PSPath

if ($value.DisplayName -eq $DisplayName) {
return $value
}
Gijsreyn marked this conversation as resolved.
Show resolved Hide resolved
} else {
$cmdPath = 'bin\code.cmd'
$codeUserInstallLocation = TryGetRegistryValue -Key "$($userUninstallRegistry)\$($vsCodeUserProductCode)" -Property $installLocationProperty
if ($codeUserInstallLocation) {
return $codeUserInstallLocation + $cmdPath
}
}

function Get-VSCodeCLIPath {
param (
[switch]$Insiders
)

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) {
return $InstallLocation

Gijsreyn marked this conversation as resolved.
Show resolved Hide resolved
}
Gijsreyn marked this conversation as resolved.
Show resolved Hide resolved
} else {
$InstallLocation = Join-Path ($env:PATH.Split([System.IO.Path]::PathSeparator) -match 'Microsoft VS Code') 'code'
if (Test-Path $InstallLocation) {
Gijsreyn marked this conversation as resolved.
Show resolved Hide resolved
return $InstallLocation
}
}
}

$codeMachineInstallLocation = TryGetRegistryValue -Key "$($machineUninstallRegistry)\$($vsCodeMachineProductCode)" -Property $installLocationProperty
if ($codeMachineInstallLocation) {
return $codeMachineInstallLocation + $cmdPath
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
}
}
}

Expand Down
Loading