Skip to content

Commit

Permalink
Remove $global usage
Browse files Browse the repository at this point in the history
  • Loading branch information
codyduong committed Apr 12, 2024
1 parent c525e52 commit 99c0b30
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 18 deletions.
5 changes: 3 additions & 2 deletions alias-tips/Private/Find-RegexThreadJob.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function Find-RegexThreadJob {
if ($null -ne $global:AliasTipsProxyFunctionRegex -and $null -ne $global:AliasTipsProxyFunctionRegexNoArgs) {
if ($null -ne $script:AliasTipsProxyFunctionRegex -and $null -ne $script:AliasTipsProxyFunctionRegexNoArgs) {
return
}

Expand All @@ -14,5 +14,6 @@ function Find-RegexThreadJob {
}
$result = Receive-Job -Job $existingJob -Wait -AutoRemoveJob

$global:AliasTipsProxyFunctionRegex, $global:AliasTipsProxyFunctionRegexNoArgs = $result
# this is a regex to find all commands, not just aliases/functions
$script:AliasTipsProxyFunctionRegex, $script:AliasTipsProxyFunctionRegexNoArgs = $result
}
5 changes: 2 additions & 3 deletions alias-tips/Private/Get-Aliases.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ function Get-Aliases {
$ProxyName = $f | Select-Object -ExpandProperty 'Name'
$ProxyDef = $f | Select-Object -ExpandProperty 'Definition'
# validate there is a command
if ($ProxyDef -match $AliasTipsProxyFunctionRegex) {
if ($ProxyDef -match $script:AliasTipsProxyFunctionRegex) {
$CleanedCommand = ("$($matches['cmd'].TrimStart()) $($matches['params'])") | Format-Command

if ($ProxyDef -match '\$args') {
# Use the shorter of two if we already have hashed this command
if ($Hash.ContainsKey($CleanedCommand + ' $args')) {
Expand All @@ -23,7 +23,6 @@ function Get-Aliases {
else {
$Hash[$CleanedCommand + ' $args'] = $ProxyName
}

}

# quick alias
Expand Down
2 changes: 1 addition & 1 deletion alias-tips/Private/Get-CommandRegex.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function Get-CommandRegex {

process {
# The parse is a bit naive...
if ($Command -match $global:AliasTipsProxyFunctionRegexNoArgs) {
if ($Command -match $script:AliasTipsProxyFunctionRegexNoArgs) {
# Clean up the command by removing extra delimiting whitespace and backtick preceding newlines
$CommandString = ("$($matches['cmd'].TrimStart())")

Expand Down
1 change: 0 additions & 1 deletion alias-tips/Private/Start-RegexThreadJob.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ function Start-RegexThreadJob {
}
}


Get-CommandsRegex | Get-ProxyFunctionRegexes
}
}
Expand Down
8 changes: 4 additions & 4 deletions alias-tips/Public/PSConsoleHostReadLine.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Store the original PSConsoleHostReadLine function when importing the module
$global:AliasTipsOriginalPSConsoleHostReadLine = Get-Item Function:\PSConsoleHostReadLine -ErrorAction SilentlyContinue
$script:AliasTipsOriginalPSConsoleHostReadLine = Get-Item Function:\PSConsoleHostReadLine -ErrorAction SilentlyContinue

function PSConsoleHostReadLine {
## Get the execution status of the last accepted user input.
Expand Down Expand Up @@ -40,8 +40,8 @@ $DEFAULT_PSConsoleHostReadLine = {
}

$MyInvocation.MyCommand.ScriptBlock.Module.OnRemove = {
if ($null -eq $global:AliasTipsOriginalPSConsoleHostReadLine) {
$global:AliasTipsOriginalPSConsoleHostReadLine = $DEFAULT_PSConsoleHostReadLine
if ($null -eq $script:AliasTipsOriginalPSConsoleHostReadLine) {
$script:AliasTipsOriginalPSConsoleHostReadLine = $DEFAULT_PSConsoleHostReadLine
}
$toFixStr = "Set-Item Function:\PSConsoleHostReadLine -Value `$AliasTipsOriginalPSConsoleHostReadLine"
@"
Expand All @@ -51,5 +51,5 @@ $toFixStr
"@ | Out-Host
Set-Clipboard -Value $toFixStr
# TODO is there a way to restore this automagically??
Set-Item Function:\PSConsoleHostReadLine -Value $AliasTipsOriginalPSConsoleHostReadLine -Force
Set-Item Function:\PSConsoleHostReadLine -Value $script:AliasTipsOriginalPSConsoleHostReadLine -Force
}
1 change: 1 addition & 0 deletions alias-tips/alias-tips.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ Initialize-EnvVariable "ALIASTIPS_DISABLE" $false

$AliasTipsHash = @{}
$AliasTipsHashEvaluated = @{}
$script:AliasTipsProxyFunctionRegex, $script:AliasTipsProxyFunctionRegexNoArgs = $null, $null
11 changes: 4 additions & 7 deletions tests/unit/Find-Alias.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ InModuleScope 'alias-tips' {
Out-Null $args
}

$global:AliasTipsProxyFunctionRegex, $global:AliasTipsProxyFunctionRegexNoArgs = $null, $null
$global:AliasTipsHash = Find-AliasTips
Find-AliasTips
}
Describe 'Find-Alias' {
it 'simple alias' {
Expand Down Expand Up @@ -67,12 +66,10 @@ InModuleScope 'alias-tips' {

Remove-Item Env:\PESTER -ErrorAction SilentlyContinue
Remove-Item alias:as_s -ErrorAction SilentlyContinue
Remove-Item alias:as_f -ErrorAction SilentlyContinue
Remove-Item alias:as_f_long -ErrorAction SilentlyContinue
Remove-Item function:as_f -ErrorAction SilentlyContinue
Remove-Item function:as_f_long -ErrorAction SilentlyContinue

Remove-Variable -Scope global -Name "AliasTipsHash" -ErrorAction SilentlyContinue
Remove-Variable -Scope global -Name "AliasTipsProxyFunctionRegex" -ErrorAction SilentlyContinue
Remove-Variable -Scope global -Name "AliasTipsProxyFunctionRegexNoArgs" -ErrorAction SilentlyContinue
Find-AliasTips

Get-Job -Name "FindAliasTipsJob" -ErrorAction SilentlyContinue | Stop-Job -PassThru | Remove-Job

Expand Down

0 comments on commit 99c0b30

Please sign in to comment.