Skip to content
This repository was archived by the owner on Jun 13, 2024. It is now read-only.

Commit 3e08d6d

Browse files
authored
Merge pull request #351 from PowerShell/edyoung/fast_clobber_check
Check for clobbered commands faster
2 parents 712d6d3 + 82e4042 commit 3e08d6d

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

src/PowerShellGet/private/functions/Validate-ModuleCommandAlreadyAvailable.ps1

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,16 @@ function Validate-ModuleCommandAlreadyAvailable
4646
if(-not $InstalledModuleInfo -or -not $InstalledModuleInfo.ModuleBase.StartsWith($InstallLocation, [System.StringComparison]::OrdinalIgnoreCase))
4747
{
4848
# Throw an error if there is a command with the same name from a different source.
49-
# Get-Command loads the module if a command is already available.
50-
# To avoid that, appending '*' at the end for each name then comparing the results.
5149
$CommandNames = $CurrentModuleInfo.ExportedCommands.Values.Name
52-
$CommandNamesWithWildcards = $CommandNames | Microsoft.PowerShell.Core\Foreach-Object { "$_*" }
5350

54-
$AvailableCommands = Microsoft.PowerShell.Core\Get-Command -Name $CommandNamesWithWildcards `
51+
# construct a hash with all of the commands in this module.
52+
$CommandNameHash = @{}
53+
$CommandNames | % { $CommandNameHash[$_] = 1 }
54+
55+
$AvailableCommands = Microsoft.PowerShell.Core\Get-Command `
5556
-ErrorAction Ignore `
5657
-WarningAction SilentlyContinue |
57-
Microsoft.PowerShell.Core\Where-Object { ($CommandNames -contains $_.Name) -and
58+
Microsoft.PowerShell.Core\Where-Object { ($CommandNameHash.ContainsKey($_.Name)) -and
5859
($_.ModuleName -ne $script:PSModuleProviderName) -and
5960
($_.ModuleName -ne 'PSModule') -and
6061
($_.ModuleName -ne $CurrentModuleInfo.Name) }

0 commit comments

Comments
 (0)