Skip to content

Commit 2e1b0e4

Browse files
author
James Brundage
committed
feat: Invoke-PipeScript Interpreter Simplification ( Fixes #941 )
Internally aliasing to Invoke-Interpreter
1 parent 6d0c52b commit 2e1b0e4

File tree

1 file changed

+6
-47
lines changed

1 file changed

+6
-47
lines changed

Commands/PipeScript/Invoke-PipeScript.ps1

Lines changed: 6 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -250,54 +250,13 @@
250250

251251
# If we have an interpreter for that language
252252
if ($matchingPipeScriptLanguage.Interpreter -or $matchingPipeScriptLanguage.Runner) {
253+
# we'll want to run it.
253254

254-
# Figure out what command or script block we will run instead
255-
$interpreterCommand = $null
256-
$InterpreterOrRunner = if ($matchingPipeScriptLanguage.Interpreter) {
257-
$matchingPipeScriptLanguage.Interpreter
258-
} else {
259-
$matchingPipeScriptLanguage.Runner
260-
}
261-
$interpreterArguments = @(
262-
# and what other arguments we pass positionally
263-
switch ($InterpreterOrRunner) {
264-
{ $_ -is [Management.Automation.CommandInfo] -and -not $interpreterCommand} { $interpreterCommand = $_ }
265-
{ $_ -is [scriptblock] -and -not $interpreterCommand} { $interpreterCommand = $_ }
266-
default {
267-
# (any other results in a language's Interpreter will be counted as positional arguments)
268-
if (-not $interpreterCommand -and $_ -is [string]) {
269-
$interpreterCommand = $_
270-
} else {
271-
$_
272-
}
273-
}
274-
}
275-
)
276-
277-
# If we found an interpreter
278-
if ($interpreterCommand) {
279-
# rearrange the arguments
280-
$ArgumentList =
281-
@(
282-
if ($matchingPipeScriptLanguage.Interpreter -isnot [ScriptBlock]) {
283-
$ArgumentList |
284-
. { process {
285-
if ($_ -isnot [string]) {
286-
ConvertTo-Json -InputObject $_ -Depth 100
287-
} else {
288-
$_
289-
}
290-
} }
291-
} else {
292-
$ArgumentList | . { process { $_ } }
293-
})
294-
$ArgumentList = @($interpreterArguments) + $(
295-
if ($matchingPipeScriptLanguage.Interpreter) {
296-
$command.Source
297-
}) + $ArgumentList
298-
# and change the command we're calling.
299-
$command = $interpreterCommand
300-
}
255+
# Rather than duplicate a lot of code, the easiest way to do this is simply to alias the full path
256+
Set-Alias $command.Source Invoke-Interpreter
257+
# and reset the value of $Command
258+
$command = $ExecutionContext.SessionState.InvokeCommand.GetCommand($command.Source, 'Alias')
259+
# (the alias will only exist locally, and not exist when Invoke-PipeScript returns)
301260
}
302261

303262
$CommandStart = [DateTime]::now

0 commit comments

Comments
 (0)