-
Notifications
You must be signed in to change notification settings - Fork 1
/
ExportCompletersList.ps1
90 lines (83 loc) · 2.46 KB
/
ExportCompletersList.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using namespace System.Management.Automation.Language
param
(
[Parameter(Mandatory)]
[string]
$Path,
[Parameter(Mandatory)]
[string]
$ExportPath
)
function GetAstValue ([Ast]$ValueAst, [Ast]$BaseAst)
{
if ($ValueAst -is [CommandExpressionAst])
{
$ValueAst = $ValueAst.Expression
}
if ($ValueAst -is [StringConstantExpressionAst])
{
$ValueAst.Value
}
elseif ($ValueAst -is [ArrayLiteralAst])
{
$ValueAst.Elements.Value
}
elseif ($ValueAst -is [ArrayExpressionAst])
{
$ValueAst.SubExpression.Statements.PipelineElements.Expression.Value
}
elseif ($ValueAst -is [VariableExpressionAst])
{
$LastVarAssignment = $BaseAst.FindAll({
param($Ast)
$Ast.Extent.EndOffset -lt $ValueAst.Extent.StartOffset -and
$Ast -is [AssignmentStatementAst] -and
$Ast.Left -is [VariableExpressionAst] -and
$Ast.Left.UserPath -eq $ValueAst.UserPath
}, $true) | Select-Object -Last 1
GetAstValue -ValueAst $LastVarAssignment.Right.Expression -BaseAst $BaseAst
}
}
$BaseAst = [Parser]::ParseFile((Resolve-Path -Path $Path).ProviderPath,[ref] $null, [ref] $null)
$ArgumentCompleters = $BaseAst.FindAll({
param($Ast)
$Ast -is [CommandAst] -and
$Ast.GetCommandName() -eq "Register-ArgumentCompleter"
}, $true)
$Result = foreach ($Completer in $ArgumentCompleters)
{
$CommandName = ""
$ParameterName = ""
for ($i = 0; $i -lt $Completer.CommandElements.Count; $i++)
{
$Ast = $Completer.CommandElements[$i]
if ($Ast -is [CommandParameterAst] -and $Ast.ParameterName -in ("CommandName","ParameterName"))
{
$ParamValue = if ($null -eq $Ast.Argument)
{
$Completer.CommandElements[$i + 1]
}
else
{
$Ast.Argument
}
$Value = GetAstValue -ValueAst $ParamValue -BaseAst $BaseAst
if ($Ast.ParameterName -eq "CommandName")
{
$CommandName = $Value
}
else
{
$ParameterName = $Value
}
}
}
foreach ($Name in $CommandName)
{
[pscustomobject]@{
CommandName = $Name
ParameterName = $ParameterName
}
}
}
$Result | Sort-Object -Property CommandName | Export-Csv -Path $ExportPath -Force -NoTypeInformation -Delimiter ','