Skip to content

Commit

Permalink
(chocolatey#310) Fixup v2 compatibility
Browse files Browse the repository at this point in the history
Previous changes for this issue had issues loading in powershell v2,
this fixes those to the extent it's possible to do so.
  • Loading branch information
vexx32 authored and gep13 committed May 30, 2024
1 parent 1245511 commit 71df5cc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
2 changes: 1 addition & 1 deletion nuspec/chocolatey/chocolatey/tools/chocolateysetup.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,7 @@ function Invoke-Chocolatey-Initial {
try {
$chocoInstallationFolder = Get-ChocolateyInstallFolder
$chocoExe = Join-Path -Path $chocoInstallationFolder -ChildPath "choco.exe"
$runResult = & $chocoExe -v *>&1
$runResult = & $chocoExe -v 2>&1
if ($LASTEXITCODE -eq 0) {
Write-Debug "Chocolatey CLI execution completed successfully."
}
Expand Down
39 changes: 22 additions & 17 deletions src/chocolatey.resources/helpers/chocolateyInstaller.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -153,26 +153,31 @@ if (Test-Path $extensionsPath) {
# In effect we ensure that any command calls that match the name of one of our commands
# will resolve to _our_ commands (preferring licensed cmdlets in the case of a name collision),
# preventing packages from overriding them with their own commands and potentially breaking things.
$ExecutionContext.InvokeCommand.PreCommandLookupAction = {
param($command, $eventArgs)
#
# This functionality is only available in v3 and later, so using this in v2 will not
# work; check for the property before trying to set it.
if ($ExecutionContext.InvokeCommand.PreCommandLookupAction) {
$ExecutionContext.InvokeCommand.PreCommandLookupAction = {
param($command, $eventArgs)

# Don't run this handler for stuff PowerShell is looking up internally
if ($eventArgs.CommandOrigin -eq 'Runspace') {
$resolvedCommand = if ($command -in $chocolateyCmdlets.Licensed) {
Get-Command "$command*" -Module 'chocolatey.licensed' -CommandType Cmdlet -ErrorAction Ignore |
Where-Object { $_.Name -match "^$command(Cmdlet)?$" } |
Select-Object -First 1
}
elseif ($command -in $chocolateyCmdlets.Default) {
Get-Command $command -Module "Chocolatey.PowerShell" -CommandType Cmdlet -ErrorAction Ignore
}
# Don't run this handler for stuff PowerShell is looking up internally
if ($eventArgs.CommandOrigin -eq 'Runspace') {
$resolvedCommand = if ($chocolateyCmdlets.Licensed -contains $command) {
Get-Command "$command*" -Module 'chocolatey.licensed' -CommandType Cmdlet -ErrorAction SilentlyContinue |
Where-Object { @($command, "$($command)Cmdlet") -contains $_.Name } |
Select-Object -First 1
}
elseif ($chocolateyCmdlets.Default -contains $command) {
Get-Command $command -Module "Chocolatey.PowerShell" -CommandType Cmdlet -ErrorAction SilentlyContinue
}

if ($resolvedCommand) {
$eventArgs.Command = $resolvedCommand
$eventArgs.StopSearch = $true
if ($resolvedCommand) {
$eventArgs.Command = $resolvedCommand
$eventArgs.StopSearch = $true
}
}
}
}.GetNewClosure()
}.GetNewClosure()
}

# todo: explore removing this for a future version
Export-ModuleMember -Function * -Alias * -Cmdlet *
Expand Down

0 comments on commit 71df5cc

Please sign in to comment.