Skip to content

Commit 1be9a7e

Browse files
StartAutomatingStartAutomating
authored andcommitted
feat: Invoke-Interpreter Call Operator / Alias Support ( Fixes #920 )
1 parent 8a375f3 commit 1be9a7e

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

Commands/Interpreters/Invoke-Interpreter.ps1

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,40 @@ function Invoke-Interpreter {
1212
This will happen automatically as you attempt to use commands that have an associated interpreter
1313
#>
1414
param()
15+
16+
# Store myInvocation (we'll need it often)
1517
$myInv = $MyInvocation
18+
19+
# Determine the invocation name
1620
$invocationName = $MyInvocation.InvocationName
21+
22+
# If we do not have the correct invocation name, that's because the call operator is being used
23+
# Unfortunately, this makes this more complicated, as we have to go looking
24+
if ($invocationName -in '.', '&') {
25+
# Starting off by picking the words after the invocation
26+
$myLine = $myInv.Line.Substring($myInv.OffsetInLine) -replace '^\s{0,}'
27+
$MyWords = @($myLine -split '\s{1,}')
28+
29+
# If the first word is a variable
30+
if ($MyWords[0] -match '^\$(?<v>\w+)') {
31+
$firstWordVariableValue = $ExecutionContext.SessionState.PSVariable.Get($matches.0 -replace '^\s\$').Value
32+
# and it has a value
33+
if ($firstWordVariableValue) {
34+
# use that as the InvocationName.
35+
$invocationName = $firstWordVariableValue
36+
}
37+
}
38+
# If the first word is not a variable,
39+
elseif ($MyWords[0] -match '^(?<w>\w+)') {
40+
# see if it's an alias
41+
$firstWordAlias = $ExecutionContext.SessionState.InvokeCommand.GetCommand($mywords[0], 'Alias')
42+
if ($firstWordAlias.ReferencedCommand -ne $myInv.MyCommand.Name) {
43+
# and use the referenced command as the invocation name.
44+
$invocationName = $firstWordAlias
45+
}
46+
}
47+
}
48+
1749
# Return if we were called by our real name.
1850
if ($invocationName -eq $myInv.MyCommand.Name) { return }
1951
# If there are no interpreters, obviously return.
@@ -22,6 +54,13 @@ function Invoke-Interpreter {
2254
if (-not $PSInterpreters.ForFile) { return }
2355
$interpreterForFiles = $PSInterpreters.ForFile($invocationName)
2456
# or don't find a mapping, return.
57+
if (-not $interpreterForFiles -and $invocationName -notmatch '[\&\.]') {
58+
$nameIsAlias = Get-Alias -Name $InvocationName
59+
if ($nameIsAlias.ReferencedCommand.Name -ne $myInv.MyCommand.Name) {
60+
$invocationName = $nameIsAlias.ReferencedCommand.Name
61+
$interpreterForFiles = $PSInterpreters.ForFile($nameIsAlias.ReferencedCommand)
62+
}
63+
}
2564
if (-not $interpreterForFiles) { return }
2665

2766
# There can be more than one potential interpreter for each file

0 commit comments

Comments
 (0)