-
-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathShow-TaskHelp.ps1
More file actions
277 lines (239 loc) · 6.87 KB
/
Show-TaskHelp.ps1
File metadata and controls
277 lines (239 loc) · 6.87 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
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
<#
.Synopsis
Shows Invoke-Build task help information.
Copyright (c) Roman Kuzmin
.Description
The command shows the specified tasks help information as task names with
synopses, jobs with their locations, script parameters and environment. By
default, it analyses the code in order to extract variables in addition to
optionally documented in comments.
Synopsis is defined in task comments as # Synopsis: ...
Parameters are defined as # Parameters: name1, name2, ...
Environment variables are defined as # Environment: name1, name2, ...
Parameters names match build parameters and separated by spaces or commas.
Parameter descriptions are taken from the build script comment based help.
Help info members (for custom formatters):
Task: [object[]] Tasks to do.
Name: [string] Task name.
Synopsis: [string] Task synopsis, may be null.
Jobs: [object[]] Jobs to do.
Name: [string] Task name.
Location: [string] Task location as "<file>:<line>".
Parameters: [object[]] Tasks parameters, may be empty.
Name: [string] Parameter name.
Type: [string] Parameter type.
Description: [string] Parameter help, may be null or empty.
Environment: [string[]] Tasks environment variables, may be empty.
.Parameter Task
Build task name(s). The default is the usual default task.
.Parameter File
Build script path. The default is the usual default script.
.Parameter NoCode
Tells to skip code analysis for parameters and environment.
.Parameter Format
Specifies the custom task help formatter.
.Link
https://github.com/nightroman/Invoke-Build
#>
param(
[Parameter(Position=0)]
[string[]]$Task
,
[Parameter(Position=1)]
[string]$File
,
[object]$Format = $(if ($Format = $PSCmdlet.GetVariableValue('Format')) {$Format} else {'Format-TaskHelp'})
,
[switch]$NoCode = $PSCmdlet.GetVariableValue('NoCode')
)
$ErrorActionPreference=1; trap {$PSCmdlet.ThrowTerminatingError($_)}
# recall by IB
if ([System.IO.Path]::GetFileName($MyInvocation.ScriptName) -ne 'Invoke-Build.ps1') {
Invoke-Build $Task $File -WhatIf
return
}
$All = ${*}.All
$BB = ${*}.BB
$DP = ${*}.DP
Remove-Variable Task
### script parameters help
function get_help($File) {
Set-StrictMode -Off
@(if ($r = Get-Help $File) {if ($r = $r.parameters) {if ($r = $r.parameter) {$r}}})
}
$Help = @(
foreach($b in $BB) {
get_help $b.FS
}
)
$Hash = @{}
$BuildJobs = @()
$MapParameter = @{}
$MapEnvironment = @{}
# collect jobs in $BuildJobs
function Add-TaskJob($Jobs, $Task) {
foreach($job in $Jobs) {
if ($job -is [string]) {
$task2 = $All[$job.TrimStart('?')]
Add-TaskJob $task2.Jobs $task2
}
else {
if ($BuildJobs -notcontains $Task.Name) {
$script:BuildJobs += $Task.Name
}
}
}
if ($Task -and $Task.If -is [scriptblock]) {
if ($BuildJobs -notcontains $Task.Name) {
$script:BuildJobs += $Task.Name
}
}
}
# get parameters and environment from comments
function Get-TaskComment($Task) {
$f = ($I = $Task.InvocationInfo).ScriptName
if (!($d = $Hash[$f])) {
$Hash[$f] = $d = @{T = Get-Content -LiteralPath $f; C = @{}}
foreach($_ in [System.Management.Automation.PSParser]::Tokenize($d.T, [ref]$null)) {
if ($_.Type -eq 15) {$d.C[$_.EndLine] = $_.Content}
}
}
$r = @{Parameters = @(); Environment = @()}
for($n = $I.ScriptLineNumber; --$n -ge 1) {
if ($c = $d.C[$n]) {
if ($c -match '(?m)^\s*#*\s*Parameters\s*:(.*)') {$r.Parameters = $Matches[1].Trim() -split '[\s,]+'}
elseif ($c -match '(?m)^\s*#*\s*Environment\s*:(.*)') {$r.Environment = $Matches[1].Trim() -split '[\s,]+'}
}
elseif ($d.T[$n - 1].Trim()) {
break
}
}
$r
}
function Add-VariablePath($Path) {
$index = $Path.IndexOf(':')
if ($index -ge 0) {
$prefix = $Path.Substring(0, $index)
$name = $Path.Substring($index + 1)
}
else {
$prefix = ''
$name = $Path
}
if (!$prefix -or $prefix -eq 'script') {
if ($DP.ContainsKey($name)) {
$MapParameter[$name] = 1
}
}
elseif ($prefix -eq 'env') {
$MapEnvironment[$name] = 1
}
}
function Add-BlockVariable($Block) {
foreach($variable in $Block.Ast.FindAll({$args[0] -is [System.Management.Automation.Language.VariableExpressionAst]}, $true)) {
if ($variable.Parent -isnot [System.Management.Automation.Language.AssignmentStatementAst]) {
Add-VariablePath $variable.VariablePath.UserPath
}
}
}
function Add-TaskVariable($Jobs) {
foreach($job in $Jobs) {
$task = $All[$job]
$info = Get-TaskComment $task
foreach($name in $info.Environment) {
$MapEnvironment[$name] = 1
}
foreach($name in $info.Parameters) {
if ($DP.ContainsKey($name)) {
$MapParameter[$name] = 1
}
else {
Write-Warning "Task '$($task.Name)': unknown parameter '$name'."
}
}
if (!$NoCode) {
foreach($job in @($Task.If; $Task.Inputs; $Task.Outputs; $task.Jobs)) {
if ($job -is [scriptblock]) {
Add-BlockVariable $job
}
}
}
}
}
function Format-TaskHelp($TaskHelp) {
print White Task:
foreach($r in $TaskHelp.Task) {
if ($synopsis = $r.Synopsis) {
print Gray (' {0} - {1}' -f $r.Name, $synopsis)
}
else {
print Gray (' {0}' -f $r.Name)
}
}
print White Jobs:
foreach($r in $TaskHelp.Jobs) {
if ($synopsis = Get-BuildSynopsis ($All[$r.Name]) $Hash) {
print Gray (' {0} - {1} At {2}' -f $r.Name, $synopsis, $r.Location)
}
else {
print Gray (' {0} - At {1}' -f $r.Name, $r.Location)
}
}
if ($TaskHelp.Parameters) {
print White Parameters:
foreach($p in $TaskHelp.Parameters) {
print Gray (' [{0}] {1}' -f $p.Type, $p.Name)
if ($p.Description) {
print Gray (' {0}' -f $p.Description)
}
}
}
if ($TaskHelp.Environment) {
print White Environment:
print Gray (' {0}' -f ($TaskHelp.Environment -join ', '))
}
}
### .Task
$TaskHelp = [pscustomobject]@{Task=$null; Jobs=$null; Synopsis=$null; Location=$null; Parameters=$null; Environment=$null}
$TaskHelp.Task = @(
foreach($job in $BuildTask) {
$task = $All[$job.TrimStart('?')]
[pscustomobject]@{
Name = $task.Name
Synopsis = Get-BuildSynopsis $task $Hash
}
}
)
### .Jobs
Add-TaskJob $BuildTask
$TaskHelp.Jobs = @(
foreach($name in $BuildJobs) {
$task = $All[$name]
[pscustomobject]@{
Name = $task.Name
Location = '{0}:{1}' -f $task.InvocationInfo.ScriptName, $task.InvocationInfo.ScriptLineNumber
}
}
)
### .Parameters and .Environment
Add-TaskVariable $BuildJobs
$TaskHelp.Environment = @($MapEnvironment.get_Keys() | Sort-Object)
# make parameter objects with help
$TaskHelp.Parameters = @()
foreach($name in $MapParameter.get_Keys()) {
$p = $DP[$name]
$r = [pscustomobject]@{
Name = $name
Type = $(if (($type = $p.ParameterType.Name) -eq 'SwitchParameter') {'switch'} else {$type})
Description = foreach($_ in $Help) {
if ($_.name -eq $name -and $_.PSObject.Properties['description']) {
($_.description | Out-String).Trim()
break
}
}
}
$TaskHelp.Parameters += $r
}
$TaskHelp.Parameters = @($TaskHelp.Parameters | Sort-Object {$_.Type -eq 'switch'}, Name)
### format
& $Format $TaskHelp