forked from ModuleBuild/ModuleBuild
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNew-CommentBasedHelp.ps1
More file actions
218 lines (196 loc) · 9.64 KB
/
Copy pathNew-CommentBasedHelp.ps1
File metadata and controls
218 lines (196 loc) · 9.64 KB
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
function Script:New-CommentBasedHelp {
<#
.SYNOPSIS
Create comment based help for a function.
.DESCRIPTION
Create comment based help for a function.
.PARAMETER Code
Multi-line or piped lines of code to process.
.PARAMETER ScriptParameters
Process the script parameters as the source of the comment based help.
.EXAMPLE
PS > $testfile = 'C:\temp\test.ps1'
PS > $test = Get-Content $testfile -raw
PS > $test | New-CommentBasedHelp | clip
Takes C:\temp\test.ps1 as input, creates basic comment based help and puts the result in the clipboard
to be pasted elsewhere for review.
.EXAMPLE
PS > $CBH = Get-Content 'C:\EWSModule\Get-EWSContact.ps1' -Raw | New-CommentBasedHelp -Verbose -Advanced
PS > ($CBH | Where {$FunctionName -eq 'Get-EWSContact'}).CBH
Consumes Get-EWSContact.ps1 and generates advanced CBH templates for all functions found within. Print out to the screen the advanced
CBH for just the Get-EWSContact function.
.NOTES
Author: Zachary Loeber
Site: http://www.the-little-things.net/
Requires: Powershell 3.0
Version History
1.0.0 - Initial release
1.0.1 - Updated for ModuleBuild
#>
[CmdletBinding()]
param(
[parameter(Position=0, ValueFromPipeline=$true, HelpMessage='Lines of code to process.')]
[string[]]$Code,
[parameter(Position=1, HelpMessage='Process the script parameters as the source of the comment based help.')]
[switch]$ScriptParameters
)
begin {
$FunctionName = $MyInvocation.MyCommand.Name
Write-Verbose "$($FunctionName): Begin."
function Get-FunctionParameter {
<#
.SYNOPSIS
Return all parameters for each function found in a code block.
.DESCRIPTION
Return all parameters for each function found in a code block.
.PARAMETER Code
Multi-line or piped lines of code to process.
.PARAMETER Name
Name of fuction to process. If no funciton is given first the entire script will be processed for general parameters. If none are found every function in the script will be processed.
.PARAMETER ScriptParameters
Parse for script parameters only.
.EXAMPLE
PS > $testfile = 'C:\temp\test.ps1'
PS > $test = Get-Content $testfile -raw
PS > $test | Get-FunctionParameter -ScriptParameters
Takes C:\temp\test.ps1 as input, gathers any script's parameters and prints the output to the screen.
.NOTES
Author: Zachary Loeber
Site: http://www.the-little-things.net/
Requires: Powershell 3.0
Version History
1.0.0 - Initial release
1.0.1 - Updated function name to remove plural format
Added Name parameter and logic for getting script parameters if no function is defined.
Added ScriptParameters parameter to include parameters for a script (not just ones associated with defined functions)
#>
[CmdletBinding()]
param(
[parameter(ValueFromPipeline=$true, HelpMessage='Lines of code to process.')]
[string[]]$Code,
[parameter(Position=1, HelpMessage='Name of function to process.')]
[string]$Name,
[parameter(Position=2, HelpMessage='Try to parse for script parameters as well.')]
[switch]$ScriptParameters
)
begin {
$FunctionName = $MyInvocation.MyCommand.Name
Write-Verbose "$($FunctionName): Begin."
$Codeblock = @()
$ParseError = $null
$Tokens = $null
# These are essentially our AST filters
$functionpredicate = { ($args[0] -is [System.Management.Automation.Language.FunctionDefinitionAst]) }
$parampredicate = { ($args[0] -is [System.Management.Automation.Language.ParameterAst]) }
$typepredicate = { ($args[0] -is [System.Management.Automation.Language.TypeConstraintAst]) }
$paramattributes = { ($args[0] -is [System.Management.Automation.Language.NamedAttributeArgumentAst]) }
$output = @()
}
process {
$Codeblock += $Code
}
end {
$ScriptText = ($Codeblock | Out-String).trim("`r`n")
Write-Verbose "$($FunctionName): Attempting to parse AST."
$AST = [System.Management.Automation.Language.Parser]::ParseInput($ScriptText, [ref]$Tokens, [ref]$ParseError)
if($ParseError) {
$ParseError | Write-Error
throw "$($FunctionName): Will not work properly with errors in the script, please modify based on the above errors and retry."
}
if (-not $ScriptParameters) {
$functions = $ast.FindAll($functionpredicate, $true)
if (-not [string]::IsNullOrEmpty($Name)) {
$functions = $functions | where {$_.Name -eq $Name}
}
# get the begin and end positions of every for loop
foreach ($function in $functions) {
Write-Verbose "$($FunctionName): Processing function - $($function.Name.ToString())"
$Parameters = $function.FindAll($parampredicate, $true)
foreach ($p in $Parameters) {
$ParamType = $p.FindAll($typepredicate, $true)
Write-Verbose "$($FunctionName): Processing Parameter of type [$($ParamType.typeName.FullName)] - $($p.Name.VariablePath.ToString())"
$OutProps = @{
'FunctionName' = $function.Name.ToString()
'ParameterName' = $p.Name.VariablePath.ToString()
'ParameterType' = $ParamType[0].typeName.FullName
}
# This will add in any other parameter attributes if they are specified (default attributes are thus not included and output may not be normalized)
$p.FindAll($paramattributes, $true) | Foreach {
$OutProps.($_.ArgumentName) = $_.Argument.Value
}
$Output += New-Object -TypeName PSObject -Property $OutProps
}
}
}
else {
Write-Verbose "$($FunctionName): Processing Script parameters"
if ($ast.ParamBlock -ne $null) {
$scriptparams = $ast.ParamBlock
$Parameters = $scriptparams.FindAll($parampredicate, $true)
foreach ($p in $Parameters) {
$ParamType = $p.FindAll($typepredicate, $true)
Write-Verbose "$($FunctionName): Processing Parameter of type [$($ParamType.typeName.FullName)] - $($p.Name.VariablePath.ToString())"
$OutProps = @{
'FunctionName' = 'Script'
'ParameterName' = $p.Name.VariablePath.ToString()
'ParameterType' = $ParamType[0].typeName.FullName
}
# This will add in any other parameter attributes if they are specified (default attributes are thus not included and output may not be normalized)
$p.FindAll($paramattributes, $true) | Foreach {
$OutProps.($_.ArgumentName) = $_.Argument.Value
}
$Output += New-Object -TypeName PSObject -Property $OutProps
}
}
else {
Write-Verbose "$($FunctionName): There were no script parameters found"
}
}
$Output
Write-Verbose "$($FunctionName): End."
}
}
$CBH_PARAM = @'
.PARAMETER %%PARAM%%
%%PARAMHELP%%
'@
$CBHTemplate = @'
<#
.SYNOPSIS
TBD
.DESCRIPTION
TBD
%%PARAMETER%%
.EXAMPLE
TBD
#>
'@
$Codeblock = @()
}
process {
$Codeblock += $Code
}
end {
$ScriptText = ($Codeblock | Out-String).trim("`r`n")
Write-Verbose "$($FunctionName): Attempting to parse parameters."
$FuncParams = @{}
if ($ScriptParameters) {
$FuncParams.ScriptParameters = $true
}
$AllParams = Get-FunctionParameter @FuncParams -Code $Codeblock | Sort-Object -Property FunctionName
$AllFunctions = @($AllParams.FunctionName | Select -unique)
foreach ($f in $AllFunctions) {
$OutCBH = @{}
$OutCBH.FunctionName = $f
[string]$OutParams = ''
$fparams = @($AllParams | Where {$_.FunctionName -eq $f} | Sort-Object -Property Position)
$fparams | foreach {
$ParamHelpMessage = if ([string]::IsNullOrEmpty($_.HelpMessage)) {$_.ParameterName + " explanation`n`r"} else { $_.HelpMessage + "`n`r"}
$OutParams += $CBH_PARAM -replace '%%PARAM%%',$_.ParameterName -replace '%%PARAMHELP%%',$ParamHelpMessage
}
$OutCBH.CBH = $CBHTemplate -replace '%%PARAMETER%%',$OutParams
New-Object PSObject -Property $OutCBH
}
Write-Verbose "$($FunctionName): End."
}
}