/
ast.ps1
92 lines (74 loc) · 3.54 KB
/
ast.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
91
92
Import-Module "$PSScriptRoot\..\controls\textarea.ps1"
New-UDPage -Name "AST Explorer" -Icon tree -Content {
New-UDLayout -Columns 1 {
New-UDHeading -Size 3 -Content {
New-UDIcon -Icon tree
" Abstract Syntax Tree Explorer"
}
New-UDHeading -Text "Enter a script in the below textbox to explore its abstract syntax tree." -Size 5
}
New-UDRow -Columns {
New-UDColumn -Size 12 -Content {
New-UDTextarea -Id "txtScript" -Label "Enter PowerShell Script"
}
}
New-UDRow -Columns {
New-UDColumn -Size 12 -Content {
New-UDButton -Text "Parse" -OnClick {
$Element = Get-UDElement -Id "txtScript"
$ScriptBlock = [ScriptBlock]::Create($Element.Attributes["value"])
Set-UDElement -Id "astexplorer" -Content {
$Session:Object = $ScriptBlock.Ast
$Root = New-UDTreeNode -Name 'AST' -Id '$Session:Object'
New-UDTreeView -ActiveBackgroundColor '#DFE8E4' -Node $Root -OnNodeClicked {
param($Body)
$Obj = $Body | ConvertFrom-Json
$Object = Invoke-Expression $Obj.NodeId
$Object | Get-Member -MemberType Properties | ForEach-Object {
$Name = $_.Name
New-UDTreeNode -Name "$Name" -Id "$($Obj.NodeId).$Name"
} | ConvertTo-JsonEx
Set-UDElement -Id "astproperties" -Content {
New-UDGrid -Title "Properties" -Headers @("Name", "Value", "Type") -Properties @("Name", "Value", "Type") -Endpoint {
if ($ArgumentList -eq $null) {
return
}
$Items = $ArgumentList[0] | Get-Member -MemberType Properties | ForEach-Object {
$Name = $_.Name
$Value = $ArgumentList[0].$Name
$ValueType = $null
$ValueString = $null
if ($Value -ne $null) {
$ValueType = $Value.GetType().Name
$ValueString = $Value.ToString()
}
[PSCustomObject]@{
Name = $Name
Value = $Value
Type = $ValueType
}
}
@{
data = $Items
recordsTotal = $Items.Length
recordsFiltered = $Items.Length
draw = $drawId
} | ConvertTo-JsonEx -Depth 2
} -ArgumentList $Object
}
}
}
}
}
}
New-UDRow -Columns {
New-UDColumn -Size 3 -Content {
New-UDCard -Title "AST Nodes" -Content {
New-UDElement -Tag "div" -Id "astexplorer" -Content {}
}
}
New-UDColumn -Size 9 -Content {
New-UDElement -Tag "div" -Id "astproperties" -Content {}
}
}
}