From 22aa3ba435dc2378099a773907c07a1664883b78 Mon Sep 17 00:00:00 2001 From: Gal Szkolnik Date: Tue, 27 Apr 2021 16:04:12 +0000 Subject: [PATCH] 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 --- base.linux/profile.d/UnixCompleters.ps1 | 2 +- base/Edit-MyScript.ps1 | 4 ++-- base/Edit-TextFile.ps1 | 2 +- base/Reload-MyScripts.ps1 | 2 +- .../{Join-String.ps1 => EnsureJoinString.ps1} | 2 +- base/profile.d/FromPowerShellCookbook.ps1 | 8 ++++---- base/profile.d/FunctionName.class.ps1 | 4 ++-- base/profile.d/Style.ps1 | 6 +++--- base/src/Packagesz.class.inc.ps1 | 4 ++-- scoop/_.package.json | Bin 758 -> 296 bytes 10 files changed, 17 insertions(+), 17 deletions(-) rename base/profile.d/{Join-String.ps1 => EnsureJoinString.ps1} (86%) diff --git a/base.linux/profile.d/UnixCompleters.ps1 b/base.linux/profile.d/UnixCompleters.ps1 index 061b232..add4b6d 100644 --- a/base.linux/profile.d/UnixCompleters.ps1 +++ b/base.linux/profile.d/UnixCompleters.ps1 @@ -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 } diff --git a/base/Edit-MyScript.ps1 b/base/Edit-MyScript.ps1 index 1cace96..c61f2f4 100644 --- a/base/Edit-MyScript.ps1 +++ b/base/Edit-MyScript.ps1 @@ -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 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 } diff --git a/base/Edit-TextFile.ps1 b/base/Edit-TextFile.ps1 index b9a8c0c..5d8b17c 100644 --- a/base/Edit-TextFile.ps1 +++ b/base/Edit-TextFile.ps1 @@ -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; } diff --git a/base/Reload-MyScripts.ps1 b/base/Reload-MyScripts.ps1 index 9c6b868..583ca6c 100644 --- a/base/Reload-MyScripts.ps1 +++ b/base/Reload-MyScripts.ps1 @@ -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 { diff --git a/base/profile.d/Join-String.ps1 b/base/profile.d/EnsureJoinString.ps1 similarity index 86% rename from base/profile.d/Join-String.ps1 rename to base/profile.d/EnsureJoinString.ps1 index 2716a0f..7719e4b 100644 --- a/base/profile.d/Join-String.ps1 +++ b/base/profile.d/EnsureJoinString.ps1 @@ -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" diff --git a/base/profile.d/FromPowerShellCookbook.ps1 b/base/profile.d/FromPowerShellCookbook.ps1 index 75018ca..ddd29df 100644 --- a/base/profile.d/FromPowerShellCookbook.ps1 +++ b/base/profile.d/FromPowerShellCookbook.ps1 @@ -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 + } diff --git a/base/profile.d/FunctionName.class.ps1 b/base/profile.d/FunctionName.class.ps1 index 44fbdd1..2ae60be 100644 --- a/base/profile.d/FunctionName.class.ps1 +++ b/base/profile.d/FunctionName.class.ps1 @@ -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 } diff --git a/base/profile.d/Style.ps1 b/base/profile.d/Style.ps1 index 71d4127..618a135 100644 --- a/base/profile.d/Style.ps1 +++ b/base/profile.d/Style.ps1 @@ -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 diff --git a/base/src/Packagesz.class.inc.ps1 b/base/src/Packagesz.class.inc.ps1 index 26a9f3e..e78e555 100644 --- a/base/src/Packagesz.class.inc.ps1 +++ b/base/src/Packagesz.class.inc.ps1 @@ -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 ) { diff --git a/scoop/_.package.json b/scoop/_.package.json index 8390926a968531855ed6148604a5dc307ce3a0b9..7572a7e86c7925d5a9ad24707c088bdb9e597fb9 100644 GIT binary patch delta 117 zcmeyyx`Jtf+GHU{p@~bRCLRz2vZQpC7#tZ2fY6npf*}=5W-=5rlmM|3gB61UkSu0M uX2=J^0tTgt2Sq1WF$uB9GUzbyGH^}wlodpg2MJCtWD@481+jp-6&L_3r&QYai9ZTVt7&9-z20J7? z*}eFe-HNGTY_jb-ZDRVxBqxw^4hcI5za2M|tas$NBDVgh$=VS#{=CmtOp z*4XprF;l=IV@kxMM-a_8621bymaMdbh5>Aqn{D+9hkG?JQ8ird#3!y%N4U^$@h>K2 Vjv*yuke$!@(HW!j7gJs@*Kf0COxyqf