Powershell ISE的抽象语法树编程示例

所属分类: 脚本专栏 / PowerShell 阅读数: 2014
收藏 0 赞 0 分享

有一个让我非常喜欢Windows PowerShell ISE的理由,就是它将它的基础脚本对象模型暴露给用户,这样就允许用户按照自己的方式和需要去自定义脚本体验。
自定义ISE的核心是$psISE对象。$psISE对象允许用户去控制ISE许多方面的功能。你可以从这里获取关于$psISE的分层对象模型的介绍,和与这些对象相关联的功能。

这篇文章会讨论你怎样利用PowerShell公开提供的解释器接口,来结合ISE对象模型魅力,去创建脚本分析和快速定位的工具。

想象一下,你不得不分析一个相对庞大的PowerShell脚本。那这个脚本可能是别人写的,也有可能是你自己几个月前写的,扔了好久了。PowerShell ISE已经做了件非常棒的工作了,它提供了脚本环境。你可以通过添加Add-On(附加工具)来扩充它的功能,让你的脚本体验更好,更高效。从PowerShell 3.0开始,脚本的抽象语法树(AST)就可以使用语法解释器接口非常方便的获取了。下面的脚本行会获取当前打开的ISE中的脚本的AST:

复制代码 代码如下:

$AbstractSyntaxTree = [System.Management.Automation.Language.Parser]::
ParseInput($psISE.CurrentFile.Editor.Text, [ref]$null, [ref]$null)

接下来让我们查询脚本中所有的函数:

复制代码 代码如下:

$functionsInFile = $AbstractSyntaxTree.FindAll({$args[0] -is
 [System.Management.Automation.Language.FunctionDefinitionAst]}, $true)

撇开函数定位的定义,如果我们能回到光标之前出现的位置,那将太漂亮了。实现这个也非常简单。我们所要做的只是存储这些行号,然后按照反转顺序反转他们。(是否有人已经知道了,“堆栈”)

下面的脚本块展示了展示了Go-To Definition的实现。

复制代码 代码如下:

#Define some useful global variables
 
$global:__ISEGoToAddOncurrLine=1
 
$global:__ISEGoToAddOncurrcol=1
 
$global:__ISEGoToAddOnlineToGoTo=1
 
$global:__ISEGoToAddOncolToGoTo=1
 
#We need two stacks - one each for line and column
 
$global:__ISEGoToAddOnstackOfLine = New-Object System.Collections.Stack
 
$global:__ISEGoToAddOnstackOfCol = New-Object System.Collections.Stack
 
#This script block has the logic for the implementation of the Go-To definition functionality
 
$global:__ISEGoToAddOnscriptBlockGoTo =
 
{
 
$AbstractSyntaxTree =[System.Management.Automation.Language.Parser]::ParseInput($psISE.CurrentFile.Editor.Text,[ref]$null, [ref]$null)
 
$functionsInFile = $AbstractSyntaxTree.FindAll(
 
{$args[0] -is[System.Management.Automation.Language.FunctionDefinitionAst]}, $true)
 
#Get the text of the line where we have the cursor
 
$str = $psISE.CurrentFile.Editor.CaretLineText
 
#Store them on the stack for later use
 
$global:__ISEGoToAddOnstackOfLine.Push($psISE.CurrentFile.Editor.CaretLine)
 
$global:__ISEGoToAddOnstackOfCol.Push($psISE.CurrentFile.Editor.CaretColumn)
 
$global:__ISEGoToAddOncurrLine = $global:__ISEGoToAddOnstackOfLine.Peek()
 
$global:__ISEGoToAddOncurrcol = $global:__ISEGoToAddOnstackOfCol.Peek()
 
#Get the selected text so that it can be used for searching existing functions
 
$selectedFunction = $psISE.CurrentFile.Editor.SelectedText
 
#Ensure that the cursor is somewhere between the word boundaries of the function
 
$functionsInFile | %{if(($str.Contains($_.name)) `
 
–and ($global:__ISEGoToAddOncurrcol -ge
 
$str.IndexOf($_.name)) `
 
-and ($global:__ISEGoToAddOncurrcol -le
 
($str.IndexOf($_.name)+$_.name.length))
 
)
 
{$selectedFunction = $_.name}
 
}
 
if($selectedFunction -ne "")
 
{
 
#See if the selected function exists in the current open file
 
$functionToGoTo = $functionsInFile | ?{$_.name -eq "$selectedFunction"}
 
$global:__ISEGoToAddOnlineToGoTo = $functionToGoTo.Extent.StartLineNumber
 
$global:__ISEGoToAddOncolToGoTo = $functionToGoTo.Extent.StartColumnNumber
 
}
 
if($functionToGoTo -eq $null)
 
{
 
try
 
{
 
$comm = Get-Command -Name "$selectedFunction" -ErrorAction SilentlyContinue
 
$comm.Definition | Out-GridView
 
}
 
catch [System.Exception]
 
{
 
}
 
}
 
else
 
{
 
#Select the function definition, assuming the function name immediately follows the keyword 'function'
 
try
 
{
 
$psise.CurrentFile.Editor.Select($global:__ISEGoToAddOnlineToGoTo,
 
($global:__ISEGoToAddOncolToGoTo+9),
 
$global:__ISEGoToAddOnlineToGoTo,
 
($global:__ISEGoToAddOncolToGoTo+8+$selectedFunction.length+1))
 
}
 
catch [System.Exception]
 
{
 
}
 
}
 
}

补充一下,Go-To Definition 功能,如果当前Powershell会话中存在的话,以上脚本会显示选中文本的定义。(另外,上面的脚本只是一个简单的例子,假如你的“function”关键字和函数名出现在脚本的同一行。这在PowerShell中并不是必须的,所以如果你的脚本风格不同,你可能需要微调一下逻辑。)

接下来应当是在Add-on(附加工具)菜单上添加这些脚本,并把它作为选中脚本的一个命令。下面两行就可以做这件事。

复制代码 代码如下:

$global:__ISEGoToAddOnsb1 =
{& $global:__ISEGoToAddOnscriptBlockGoTo | Out-Null}
$null=$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add(
"Go do definition", $global:__ISEGoToAddOnsb1, "F12")

现在来看看我们怎样实现Go-Back 功能,使用我们定义的全局堆栈,几行代码即可:

复制代码 代码如下:

$global:__ISEGoToAddOnscriptBlockGoBack =
 
{
 
try
 
{
 
#Pop the line and column numbers from the stack to do a reverse traversal
 
$global:__ISEGoToAddOncurrLine =
 
$global:__ISEGoToAddOnstackOfLine.Pop()
 
$global:__ISEGoToAddOncurrcol =
 
$global:__ISEGoToAddOnstackOfCol.Pop()
 
$psISE.CurrentFile.Editor.SetCaretPosition(
 
$global:__ISEGoToAddOncurrLine, $global:__ISEGoToAddOncurrcol)
 
$psISE.CurrentFile.Editor.SelectCaretLine();
 
}
 
catch [System.Exception]
 
{
 
}
 
}
 
$global:__ISEGoToAddOnsb2 = {& $global:__ISEGoToAddOnscriptBlockGoBack | Out-Null}
 
$null=$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add("Go Back",$global:__ISEGoToAddOnsb2, "Shift+F12")

就到这里了,只用了一些PowerShell代码就实现了Visual Studio中的Go-To Definition (转向定义)和Go-Back(返回)功能。

你还可以继续扩展这个脚本,让它包含这些任务:诸如显示脚本中所有函数,点击函数转到函数定义。作为大家进一步扩展功能的鼓励,我给你看下我的 ISE附加工具现在的样子。

扩展PowerShell ISE 中的 “附加工具”菜单

更多精彩内容其他人还在看

PowerShell隐藏不显示窗口的多种方法

这篇文章主要介绍了PowerShell隐藏不显示窗口的多种方法,本文讲解了启动PowerShell时隐藏自己的窗口、在PowerShell启动其它进程时隐藏窗口、使用PowerShell隐藏其它进程的窗口三种方法,需要的朋友可以参考下
收藏 0 赞 0 分享

PowerShell启用winrm失败:拒绝访问 0x80070005 -2147024891

这篇文章主要介绍了PowerShell启用winrm失败:拒绝访问 0x80070005 -2147024891,本文给出了详细的排查步骤和解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Powershell目录文件夹管理权限的继承和指定方法

这篇文章主要介绍了Powershell目录文件夹管理权限的继承和指定方法,本文给出了创建文件夹、获取当前权限、添加新的权限、添加管理员权限等,需要的朋友可以参考下
收藏 0 赞 0 分享

PowerShell查看本机文件关联程序和默认打开程序的方法

这篇文章主要介绍了PowerShell查看本机文件关联程序和默认打开程序的方法,本文给出了查看方法,同时给出了一份读取结果,需要的朋友可以参考下
收藏 0 赞 0 分享

PowerShell脚本反引号用法实例:随时随地给代码换行

这篇文章主要介绍了PowerShell脚本反引号用法实例:随时随地给代码换行,在遇到一些超长代码行时非常有用,一般编程代码一行的字符数不超过80个哦,需要的朋友可以参考下
收藏 0 赞 0 分享

PowerShell 数组的多种录入方法

这篇文章主要介绍了PowerShell 数组的多种录入方法,需要的朋友可以参考下
收藏 0 赞 0 分享

PowerShell String对象方法小结

这篇文章主要介绍了PowerShell String对象方法,需要的朋友可以参考下
收藏 0 赞 0 分享

使用PowerShell获取当前主机内存使用量和总量的方法

这篇文章主要介绍了使用PowerShell获取当前主机内存使用量和总量的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

PowerShell批量修改AD用户密码属性的代码

这篇文章主要介绍了PowerShell批量修改AD用户密码属性的代码,需要的朋友可以参考下
收藏 0 赞 0 分享

PowerShell 自动备份oracle并上传到ftp

我这里有这样一个需求:有一个数据库,每天使用SQL Server Agent自动生成备份文件。然后,这个数据库非常重要,需要把每天的备份上传一个远程的FTP服务器上去。下面我们来看看如何使用Powershell来实现吧
收藏 0 赞 0 分享
查看更多