Bra*_*lin 5 powershell parsing abstract-syntax-tree
我正在尝试解析Pester脚本并从-Tag参数中提取值.任何人都知道如何使用[System.Management.Automation.PSParser]?我在想我必须遍历从中返回的令牌,[System.Management.Automation.PSParser]::Tokenize()但这看起来非常糟糕,并且考虑到-Tag可以以多种不同的格式给出值,这不是很实用.
在一天结束时,我希望返回一个包含Describe块名称的集合,以及该块的标记列表(如果有的话).
Name Tags
---- ----
Section1 {tag1, tag2}
Section2 {foo, bar}
Section3 {asdf}
Section4 {}
Run Code Online (Sandbox Code Playgroud)
以下是我正在使用的Pester测试示例.
describe 'Section1' -Tag @('tag1', 'tag2') {
it 'blah1' {
$true | should be $true
}
}
describe 'Section2' -Tag 'foo', 'bar' {
it 'blah2' {
$true | should be $true
}
}
describe 'Section3' -Tag 'asdf'{
it 'blah3' {
$true | should be $true
}
}
describe 'Section4' {
it 'blah4' {
$true | should be $true
}
}
Run Code Online (Sandbox Code Playgroud)
任何人对如何解决这个问题都有任何想法?是[System.Management.Automation.PSParser]正确的方式还是有更好的方法?
干杯
使用PS3.0 + 语言命名空间 AST解析器:
$text = Get-Content 'pester-script.ps1' -Raw # text is a multiline string, not an array!
$tokens = $null
$errors = $null
[Management.Automation.Language.Parser]::ParseInput($text, [ref]$tokens, [ref]$errors).
FindAll([Func[Management.Automation.Language.Ast,bool]]{
param ($ast)
$ast.CommandElements -and
$ast.CommandElements[0].Value -eq 'describe'
}, $true) |
ForEach {
$CE = $_.CommandElements
$secondString = ($CE | Where { $_.StaticType.name -eq 'string' })[1]
$tagIdx = $CE.IndexOf(($CE | Where ParameterName -eq 'Tag')) + 1
$tags = if ($tagIdx -and $tagIdx -lt $CE.Count) {
$CE[$tagIdx].Extent
}
New-Object PSCustomObject -Property @{
Name = $secondString
Tags = $tags
}
}
Run Code Online (Sandbox Code Playgroud)
Run Code Online (Sandbox Code Playgroud)Name Tags ---- ---- 'Section1' @('tag1', 'tag2') 'Section2' 'foo', 'bar' 'Section3' 'asdf' 'Section4'
代码不会将标记解释为字符串列表,而只是使用原始文本extent.
使用PowerShell ISE/Visual Studio/VSCode中的调试器来检查各种数据类型的情况.