This repository has been archived by the owner on May 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShow-ModuleTree.ps1
297 lines (263 loc) · 7.71 KB
/
Show-ModuleTree.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<#
.SYNOPSIS
It even checks for .SYNOPSIS blocks on .ps1 files!
#>
[CmdletBinding()]
param()
function Show-ModuleTree {
<#
.SYNOPSIS
Returns a formatted tree
#>
param(
[ValidateScript({
Test-Path $_ -PathType Container
})]
# Directory that will be recursiveely searched, defaults to current dir
[String]$Path = (Get-Location)
,
# If you just want a nice tree, but use tree.com at this point
[Switch]$NoHyperlinks
,
[Int]$Indentation = 2 # The spacing at each level before the - bullet point
,
[Switch]$Force # Will also use -Force on Get-ChildItem to show hidden files
,
# Character used for bullet point, does not affect final rendered output
[ValidateSet('-', '*')]
[Char]$BulletChar = '-'
,
# Working hyperlink requires
[String]$RepoURL
,
# Where it starts linking, defaults to root (/)
[String]$PathInRepo = [IO.Path]::DirectorySeparatorChar
,
# Use raw.githubusercontent for hyperlinks
[Switch]$LinkRaw
,
# Branch of your repository to link
[String]$Branch = "master"
,
# Files you wish to excluse from being put into the tree
[String[]]$Exclude
)
$SEP = [IO.Path]::DirectorySeparatorChar
if (!$RepoURL -and !$NoHyperlinks) {
if ((Test-Path "$Path\.git") -and (Get-Command git -ErrorAction Ignore)) {
Push-Location $Path
$RepoURL = (git config remote.origin.url) -replace '^.+?/|\.git$' -replace "/github.com/", ""
Pop-Location
}
}
elseif ($RepoURL) {
if ($RepoURL -like "*://gitlab.com/*") {
throw "GitLab isn't supported yet, but feel free to PR!"
}
$RepoURL = ($RepoURL) -replace '^.+?/|\.git$' -replace "/github.com/", ""
}
$RepoBase = if ($LinkRaw) {
"https://raw.githubusercontent.com/$RepoURL/$Branch/"
}
else {
"https://github.com/$RepoURL/blob/$Branch/"
}
if ($PathInRepo -ne $SEP) {
$RepoBase += $PathInRepo
}
function ConvertTo-AbstractSyntaxTree {
<#
Converts multi-line strings, scriptblocks, or filepaths to parseable ASTs
#>
[CmdletBinding()]
[OutputType([System.Management.Automation.Language.ScriptBlockAst])]
Param(
[Parameter(ValueFromPipeline = $True)]
$InputObject
)
Process {
$Type = $InputObject.GetType().Name
$Converted = switch ($Type) {
FileInfo {
# If it it is
if (Get-Item $InputObject) {
# If it already is a FileInfo
(Get-Item $InputObject) # Return it as is
}
}
String {
# Either a filepath, function name or multi-line strinng
if (Test-Path $InputObject) {
# If it's the path to a string
(Get-Item $InputObject) # Return it as FileInfo
}
elseif ($FuncObj = Get-Command -Name $InputObject -CommandType Function -ErrorAction Ignore) {
[ScriptBlock]::Create((($FuncObj).ScriptBlock.Ast.Extent.Text))
# Then it's a command, it's extent scriptblock is returned
}
elseif ($InputObject -Like "*`n*") {
# Multi-line string get converted to scriptblocks
[ScriptBlock]::Create(($InputObject))
}
}
FunctionInfo {
if ($AstText = [ScriptBlock]::Create((($InputObject).ScriptBlock.Ast.Extent.Text))) {
$AstText
}
else {
throw "Could not parse scriptblock from function `"$($InputObject.Name)`""
}
}
ScriptBlock {
# Already is a ScriptBlock
$InputObject
}
default {
# If an int, char or whatever else is passed
throw "Could not find what to do with input of type `"$Type`""
}
}
$Type = $Converted.GetType().Name
$ret = switch ($Type) {
ScriptBlock {
[System.Management.Automation.Language.Parser]::ParseInput($Converted, [Ref]$Null, [Ref]$Null)
}
FileInfo {
[System.Management.Automation.Language.Parser]::ParseFile($Converted, [Ref]$Null, [Ref]$Null)
}
}
return $ret
}
}
function Get-Synopsis {
<#
.SYNOPSIS
Gets a function's synopsis
#>
[CmdletBinding()]
param(
$Function,
$Path,
$ScriptBlock
)
if ($Path) {
if ((Get-Item $Path).Extension -notin '.ps1', '.psm1') {
return $null
}
$AST = [System.Management.Automation.Language.Parser]::ParseFile($Path, [ref]$null, [ref]$null)
}
elseif ($Function -or $ScriptBlock) {
if ($Function -and !$ScriptBlock) {
$ScriptBlock = [ScriptBlock]::Create(((Get-Command $Function).ScriptBlock.Ast.Extent.Text))
}
elseif ($ScriptBlock) {
if ($Script -isnot [scriptblock]) {
$ScriptBlock = [ScriptBlock]::Create($ScriptBlock)
}
}
$AST = [System.Management.Automation.Language.Parser]::ParseInput($ScriptBlock, [ref]$null, [ref]$null)
}
$Definition = $AST.FindAll({
param ($node)
$node.GetType().Name -eq 'FunctionDefinitionAst'
}, $true)
if ($Definition) {
return ($Definition | Select-Object -First 1).GetHelpContent().Synopsis.Trim()
}
else {
return $null
}
}
function Get-Depth ($Path) {
<#
.SYNOPSIS
Return how much directory separators are present
#>
$Path = (Get-Item $Path -Force).FullName.TrimEnd($SEP)
$Count = ($Path.ToCharArray() | Where-Object { $_ -eq $SEP }).Count
Write-Verbose "$Path has $Count slashes"
return $Count
}
function Get-ScriptDeclarations {
<#
.SYNOPSIS
Parse a PowerShell .ps1 file to return and format the functions it's declaring (via AST)
#>
param(
[ValidateScript({
Test-Path $_ -PathType Leaf
})]
$Path,
$ScriptBlock,
$Padding,
$PathToItem
)
$AST = if ($Path) {
if ((Get-Item $Path).Extension -notin '.ps1', '.psm1') {
return $null
}
[System.Management.Automation.Language.Parser]::ParseFile($Path, [ref]$null, [ref]$null)
}
elseif ($ScriptBlock) {
[System.Management.Automation.Language.Parser]::ParseInput($Block, [ref]$null, [ref]$null)
}
$Declared = $AST.FindAll({
param ($node)
$node.GetType().Name -eq 'FunctionDefinitionAst'
}, $true)
# return $Declared
ForEach ($Declaration in $Declared) {
$Start, $End = $Declaration.Extent.StartLineNumber, $Declaration.Extent.EndLineNumber
$Name = $Declaration.Name
& ([ScriptBlock]::Create($Declaration.Extent.Text))
# Imports the function to use Get-Help to find if it has a
$Formatted = "$Padding- Function [``$Name``]($PathToItem#L$Start-L$End)"
if (Get-Command $Name -ErrorAction Ignore -and ($Synopsis = (Get-Help $Name -ErrorAction Ignore).Synopsis)) {
$Formatted += ": $($Synopsis -replace "`n", ", ")"
}
Write-Output $Formatted
}
}
$BaseDepth = Get-Depth $Path
# How many slashes (or backslashes) the base directory contains
# This will be used to determine the needed indentation
$BasePath = (Get-Item $Path).FullName
function Invoke-TreeRender ($Path) {
<#
.SYNOPSIS
Formats each file in given directory, sub-function used to support recursion
#>
Get-ChildItem $Path -Force:$Force | ForEach-Object {
$Padding = (" " * $Indentation) * ((Get-Depth $PSItem) - $BaseDepth - 1)
$Name = $_.Name
if (Test-Path $_ -PathType Container) { $Name = $SEP + $Name + $SEP }
if (!$NoHyperlinks -and $RepoBase) {
$PathToItem = "$($_.FullName -Replace [Regex]::Escape($BasePath), '')"
$PathToItem = $PathToItem -replace "\\", "" -replace "//", ""
$PathToItem = [IO.Path]::Join($RepoBase, $PathToItem)
$Name = "$BulletChar [$($Name.Replace("\","\\"))]($PathToItem)"
}
if ($_.Extension -eq '.ps1' -and ($Synopsis = (Get-Help -Name $_ -ErrorAction Ignore).Synopsis)) {
# Both assigns
if ($Name -ne $_.Name) {
$Name += ": $Synopsis"
}
}
Write-Output "$Padding$Name"
if (Test-Path $_ -PathType Container) {
if (Get-ChildItem $_ -Force:$Force) {
Invoke-TreeRender $_
}
}
elseif (Test-Path $_ -PathType Leaf) {
$Parameters = @{
Path = $_
Padding = $Padding + (" " * $Indentation)
PathToItem = $PathToItem
}
Get-ScriptDeclarations @Parameters
}
}
}
Invoke-TreeRender $Path
}