Skip to content

Commit

Permalink
Fix path search
Browse files Browse the repository at this point in the history
  • Loading branch information
Gijsreyn committed Dec 6, 2024
1 parent b9c45c8 commit fd3f028
Showing 1 changed file with 66 additions and 29 deletions.
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,

# 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
}
} 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

}
} else {
$InstallLocation = Join-Path ($env:PATH.Split([System.IO.Path]::PathSeparator) -match 'Microsoft VS Code') 'code'
if (Test-Path $InstallLocation) {
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

0 comments on commit fd3f028

Please sign in to comment.