Improve performance of Package identification

Using -ListAvailable with Get-Command to ensure no module loading is
    attampeted while querying loaded commands.
    This improved performance when certain modules have not been
    loaded yet
This commit is contained in:
Gal Szkolnik 2021-04-27 16:04:12 +00:00
parent 499a86baa6
commit 22aa3ba435
10 changed files with 17 additions and 17 deletions

View File

@ -1,4 +1,4 @@
Get-Command Import-UnixCompleters -ErrorAction SilentlyContinue | ForEach-Object {
Get-Command -ListImported Import-UnixCompleters -ErrorAction SilentlyContinue | ForEach-Object {
Import-UnixCompleters
Set-UnixCompleter -ShellType Bash
}

View File

@ -54,7 +54,7 @@ if( $Package ) {
}
$local:ScriptPaths = @()
foreach( $local:p in $ImportFunction ) {
$local:f = Get-Command $p -Type Function
$local:f = Get-Command -ListImported $p -Type Function
if( $f ) {
$local:sp = Join-Path $EditRootPath "$p`.ps1"
if( $ForceImport -or -not (Test-Path $sp) ) {
@ -90,5 +90,5 @@ if( $sw.Elapsed.TotalSeconds -lt 1 ) {
$null = Read-Host "Waiting before refreshing. Press <Enter> when you're done editing ( don't forget to save ;) )..."
}
Get-Command Reload-MyScripts -ErrorAction SilentlyContinue |
Get-Command -ListImported Reload-MyScripts -ErrorAction SilentlyContinue |
ForEach-Object { . $_.Definition }

View File

@ -17,7 +17,7 @@ end {
$local:editors = $env:EDITOR,'nvim','code'
$local:editor = $null
foreach( $local:testEditor in $editors ) {
if( $(try{Get-Command -Type Application $testEditor -ErrorAction SilentlyContinue}catch{}) ) {
if( $(try{Get-Command -ListImported -Type Application $testEditor -ErrorAction SilentlyContinue}catch{}) ) {
$editor = $testEditor
break;
}

View File

@ -4,7 +4,7 @@ function RemoveAlias {
param([string]$Name)
$local:a = $Name | Where-Object { $_ } | Get-Alias -ErrorAction SilentlyContinue
if ( -not $a ) { return }
if ( Get-Command Remove-Alias -ErrorAction SilentlyContinue ) {
if ( Get-Command -ListImported Remove-Alias -ErrorAction SilentlyContinue ) {
$a | Remove-Alias -Scope $MyAliasScope -ErrorAction SilentlyContinue
}
else {

View File

@ -1,4 +1,4 @@
if( -not (Get-Command Join-String -ErrorAction SilentlyContinue) ) {
if( -not (Get-Command -ListImported Join-String -ErrorAction SilentlyContinue) ) {
if( -not (Get-Module -ListAvailable -Name "string") ) {
Write-Warning 'Join-String Command is missing, attempting to install stirng module'
$local:moduleScope = "CurrentUser"

View File

@ -15,7 +15,7 @@ if( Get-Module PowerShellCookbook ) {
Import-Module PowerShellCookbook -Cmdlet Add-ObjectCollector
}
#
. Get-Command Add-ObjectCollector -ErrorAction SilentlyContinue | ForEach-Object {
Add-ObjectCollector
Get-Item function:/Add-ObjectCollector | Remove-Item
}
. Get-Command -ListImported Add-ObjectCollector -ErrorAction SilentlyContinue | ForEach-Object {
Add-ObjectCollector
Get-Item function:/Add-ObjectCollector | Remove-Item
}

View File

@ -1,10 +1,10 @@
class FunctionName { #: System.Management.Automation.IValidateSetValuesGenerator {
static [string[]] _GetValidValues([string]$wordToComplete,[bool]$Strict) {
$local:possibleValues = Get-Command -Type Function | Select-Object -ExpandProperty Name
$local:possibleValues = Get-Command -ListImported -Type Function | Select-Object -ExpandProperty Name
return $(Get-PossibleArguments -WordToComplete $wordToComplete -FullValueSet $possibleValues -Strict:$Strict );
}
static [String[]] _GetValidValues([string]$wordToComplete,[switch]$Strict) {
return $(Get-PossibleArguments -WordToComplete $wordToComplete -FullValueSet $(Get-Command -Type Function | Select-Object -ExpandProperty Name) -Strict:$Strict)
return $(Get-PossibleArguments -WordToComplete $wordToComplete -FullValueSet $(Get-Command -ListImported -Type Function | Select-Object -ExpandProperty Name) -Strict:$Strict)
# $local:possibleValues =
# if( $wordToComplete ) {
# $possibleValues = $possibleValues | Where-Object { $_ -match $wordToComplete }

View File

@ -3,13 +3,13 @@ $global:IsOutputANSICompatible = (
) -or (
($env:ConEmuANSI -eq 'ON') -and ($Host.Name -eq 'ConsoleHost' )
)
Get-Command Set-PSReadLineKeyHandler -ErrorAction SilentlyContinue |
Get-Command -ListImported Set-PSReadLineKeyHandler -ErrorAction SilentlyContinue |
ForEach-Object { Set-PSReadLineKeyHandler -Key Tab -Function MenuComplete }
if( (Get-Command Set-PoshPrompt -ErrorAction SilentlyContinue) -and $IsOutputANSICompatible ) {
if( (Get-Command -ListImported Set-PoshPrompt -ErrorAction SilentlyContinue) -and $IsOutputANSICompatible ) {
$local:poshPrompt = Join-Path $HOME ".oh-my-posh.omp.json"
if( -not (Test-Path $poshPrompt) ) { $poshPrompt = "slim" }
Set-PoshPrompt -Theme $poshPrompt
} elseif( Get-Command Set-Theme -ErrorAction SilentlyContinue ) {
} elseif( Get-Command -ListImported Set-Theme -ErrorAction SilentlyContinue ) {
Set-Theme Paradox
} elseif( Get-Module -ListAvailable posh-git -ErrorAction SilentlyContinue ) {
Import-Module posh-git

View File

@ -23,7 +23,7 @@ class Packagesz { #: System.Management.Automation.IValidateSetValuesGenerator {
$local:valid = $Package.Condition[0].Logic -notin ([szLogic]::or, [szLogic]::ornot)
$local:currentSys = [SystemName]::_GetValidValues('',$true,$true);
$local:hostname = $(hostname)
$local:allCmdlets = Get-Command | Select-Object -ExpandProperty Name
$local:allCmdlets = Get-Command -ListImported | Select-Object -ExpandProperty Name
$local:username = $( if( $env:USER ) { $env:USER } else { $env:USERNAME } )
@ -45,7 +45,7 @@ class Packagesz { #: System.Management.Automation.IValidateSetValuesGenerator {
}
if( $v -and $c.AppExeExists ) {
$v = $v -and -not $($c.AppExeExists | Where-Object {
-not ( Get-Command $_ -Type Application -ErrorAction SilentlyContinue )
-not ( Get-Command -ListImported $_ -Type Application -ErrorAction SilentlyContinue )
})
}
if( $v -and $c.custom ) {

Binary file not shown.