testing re-parsing for stubs when invoked with &

This commit is contained in:
Luke Sampson
2013-06-22 10:27:06 +10:00
parent 2a5932bbe7
commit ff7f29adf2
3 changed files with 19 additions and 8 deletions
+8 -2
View File
@@ -73,8 +73,14 @@ function stub($path) {
}
function exec_stub($path, $inv) {
$len = $inv.positionmessage.split("`n")[2] | sls '~+' | % { $_.matches[0].value.length }
$cmd = $inv.line.substring($inv.offsetinline-1,$len)
# re-parse the command line to work out which part of it to pass to the bin
# needs to handle brackets, pipes and `& 'cmd'`
$cmdName = $inv.InvocationName
$ast = [System.Management.Automation.Language.Parser]::ParseInput($inv.line, [ref]$null, [ref]$null)
$cmd = $ast.find({
($args[0].gettype().name -eq 'commandast') -and ($args.getcommandname() -eq $cmdName)
}, $true)
$rawargs = $cmd -replace "^\s*&?\s*(('?[^']*')|([^\s]*))\s*", ""
+10 -5
View File
@@ -1,13 +1,18 @@
$inv = $myinvocation
#$inv
$name = $inv.InvocationName
[System.Management.Automation.Language.Token[]]$tokens = $null
[System.Management.Automation.Language.ScriptBlockAst]$ast =
[System.Management.Automation.Language.Parser]::ParseInput($inv.line, [ref]$tokens, [ref]$null)
$tokens = $null
#$tokens | % { $_ }
$ast = [System.Management.Automation.Language.Parser]::ParseInput($inv.line, [ref]$tokens, [ref]$null)
$commands = $ast.FindAll({ ($args[0] -is [System.Management.Automation.Language.CommandAst]) -and ($args.getcommandname() -eq $name)}, $true)
$commands = $ast.FindAll({ ($args[0].gettype().name -eq 'commandast') -and ($args[0].invocationoperator -eq 'Ampersand')}, $true)
#$ast
#$tokens | where { $_.text -eq '&' } | % { $_.gettype() }
$commands | % { $_.extent.text }
+1 -1
View File
@@ -1 +1 @@
( .\stubtest -am "testing the params" 2>&1 ) | sort-object
( & '.\stubtest' -am "testing the params" 2>&1 ) | sort-object