Skip to content

Commit 8a375f3

Browse files
author
James Brundage
committed
feat: Invoke-Interpreter Call Operator / Alias Support ( Fixes #920 )
1 parent 1f911f9 commit 8a375f3

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

Commands/Interpreters/Invoke-Interpreter.ps.ps1

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

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

0 commit comments

Comments
 (0)