-
-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathShow-TaskHelp.ps1
300 lines (262 loc) · 7.68 KB
/
Show-TaskHelp.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
298
299
300
<#
.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.
It is used as true for PowerShell v2.
.Parameter Format
Specifies the custom task help formatter.
.Link
https://github.com/nightroman/Invoke-Build
#>
param(
[Parameter(Position=0)][string[]]$Task
,
[Parameter(Position=1)]$File
,
[object]$Format = 'Format-TaskHelp'
,
[switch]$NoCode
)
trap {$PSCmdlet.ThrowTerminatingError($_)}
$ErrorActionPreference = 1
if ([System.IO.Path]::GetFileName($MyInvocation.ScriptName) -eq 'Invoke-Build.ps1') {
$all = ${*}.All
Remove-Variable Task
}
else {
$BuildTask = $Task
$BuildFile = $File
. Invoke-Build
### resolve file
if ($BuildFile) {
$BuildFile = *Path $BuildFile
if (![System.IO.File]::Exists($BuildFile)) {*Fin "Missing file '$BuildFile'." 5}
}
else {
$BuildFile = Get-BuildFile (*Path)
if (!$BuildFile) {*Fin 'Missing default script.' 5}
}
### resolve task
$all = Invoke-Build ?? $BuildFile
if (!$BuildTask -or '.' -eq $BuildTask) {
$BuildTask = if ($all['.']) {'.'} else {$all.Item(0).Name}
}
}
### get script parameter help
$Help = @(&{
Set-StrictMode -Off
if ($r = Get-Help $BuildFile) {if ($r = $r.parameters) {if ($r = $r.parameter) {$r}}}
})
### get script parameters
$CommonParameters = 'Verbose', 'Debug', 'ErrorAction', 'WarningAction', 'ErrorVariable', 'WarningVariable', 'OutVariable', 'OutBuffer', 'PipelineVariable', 'InformationAction', 'InformationVariable'
$Parameters = (Get-Command $BuildFile).Parameters
foreach($name in @($Parameters.get_Keys())) {
if ($CommonParameters -contains $name) {
$null = $Parameters.Remove($name)
}
}
# amend options
$NoCode = $NoCode -or $PSVersionTable.PSVersion.Major -le 2
# globals
$Hash = @{}
$BuildJobs = @()
$MapParameter = @{}
$MapEnvironment = @{}
# collect jobs to do in $BuildJobs
function Add-TaskJob($Jobs, $Task) {
foreach($job in $Jobs) {
$job, $null = *Job $job
if ($job -is [string]) {
$task2 = $all[$job]
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 ($Parameters.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 ($Parameters.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) {
Write-Build White Task:
foreach($r in $TaskHelp.Task) {
if ($r.Synopsis) {
Write-Build Gray (' {0} - {1}' -f $r.Name, $r.Synopsis)
}
else {
Write-Build Gray (' {0}' -f $r.Name)
}
}
Write-Build White Jobs:
foreach($r in $TaskHelp.Jobs) {
Write-Build Gray (' {0} - {1}' -f $r.Name, $r.Location)
}
if ($TaskHelp.Parameters) {
Write-Build White Parameters:
foreach($param in $TaskHelp.Parameters) {
Write-Build Gray (' [{0}] {1}' -f $param.Type, $param.Name)
if ($param.Description) {
Write-Build Gray (' {0}' -f $param.Description)
}
}
}
if ($TaskHelp.Environment) {
Write-Build White Environment:
Write-Build Gray (' {0}' -f ($TaskHelp.Environment -join ', '))
}
}
### .Task
$TaskHelp = 1 | Select-Object Task, Jobs, Synopsis, Location, Parameters, Environment
$TaskHelp.Task = @(
foreach($job in $BuildTask) {
$job, $null = *Job $job
$task = $all[$job]
if (!$task) {*Fin "Missing task '$job' in '$BuildFile'." 5}
$r = 1 | Select-Object Name, Synopsis
$r.Name = $task.Name
$r.Synopsis = Get-BuildSynopsis $task $Hash
$r
}
)
### .Jobs
Add-TaskJob $BuildTask
$TaskHelp.Jobs = @(
foreach($name in $BuildJobs) {
$task = $all[$name]
$r = 1 | Select-Object Name, Location
$r.Name = $task.Name
$r.Location = '{0}:{1}' -f $task.InvocationInfo.ScriptName, $task.InvocationInfo.ScriptLineNumber
$r
}
)
### .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()) {
$param = $Parameters[$name]
$r = 1 | Select-Object Name, Type, Description
$r.Name = $name
$type = $param.ParameterType.Name
if ($type -eq 'SwitchParameter') {
$r.Type = 'switch'
}
else {
$r.Type = $type
}
$r.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)
# finish
& $Format $TaskHelp