-
Notifications
You must be signed in to change notification settings - Fork 4
/
Invoke-Build.ps1
741 lines (679 loc) · 18.9 KB
/
Invoke-Build.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
<#
Copyright 2011-2018 Roman Kuzmin
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
#>
#.ExternalHelp InvokeBuild-Help.xml
param(
[Parameter(Position=0)][string[]]$Task,
[Parameter(Position=1)]$File,
$Result,
[switch]$Safe,
[switch]$Summary,
[switch]$WhatIf
)
dynamicparam {
function *Path($P) {
$PSCmdlet.GetUnresolvedProviderPathFromPSPath($P)
}
function *Die($M, $C=0) {
$PSCmdlet.ThrowTerminatingError((New-Object System.Management.Automation.ErrorRecord ([Exception]"$M"), $null, $C, $null))
}
function Get-BuildFile($Path) {
do {
if (($f = [System.IO.Directory]::GetFiles($Path, '*.build.ps1')).Length -eq 1) {return $f}
if ($_ = $f -match '[\\/]\.build\.ps1$') {return $_}
if ($f.Length -ge 2) {throw "Ambiguous default script in '$Path'."}
if ([System.IO.File]::Exists(($_ = $env:InvokeBuildGetFile)) -and ($_ = & $_ $Path)) {return $_}
} while($Path = Split-Path $Path)
}
if ($MyInvocation.InvocationName -eq '.') {return}
trap {*Die $_ 5}
New-Variable * -Description IB ([PSCustomObject]@{
All = [System.Collections.Specialized.OrderedDictionary]([System.StringComparer]::OrdinalIgnoreCase)
Tasks = [System.Collections.Generic.List[object]]@()
Errors = [System.Collections.Generic.List[object]]@()
Warnings = [System.Collections.Generic.List[object]]@()
Redefined = @()
Doubles = @()
Started = [DateTime]::Now
Elapsed = $null
Error = 'Invalid arguments.'
Task = $null
File = $BuildFile = $PSBoundParameters['File']
Safe = $PSBoundParameters['Safe']
Summary = $PSBoundParameters['Summary']
CD = *Path
DP = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
SP = @{}
P = $(if ($_ = $PSCmdlet.SessionState.PSVariable.Get('*')) {if ($_.Description -eq 'IB') {$_.Value}})
A = 1
B = 0
Q = 0
H = @{}
EnterBuild = $null
ExitBuild = $null
EnterTask = $null
ExitTask = $null
EnterJob = $null
ExitJob = $null
Header = {Write-Build 11 "Task $($args[0])"}
Footer = {Write-Build 11 "Done $($args[0]) $($Task.Elapsed)"}
Data = @{}
XBuild = $null
XTask = $null
})
if ($_ = $PSBoundParameters['Result']) {
if ($_ -is [string]) {
New-Variable $_ ${*} -Scope 1 -Force
}
elseif ($_ -is [hashtable]) {
${*}.XBuild = $_['XBuild']
${*}.XTask = $_['XTask']
$_.Value = ${*}
}
else {throw 'Invalid parameter Result.'}
}
$BuildTask = $PSBoundParameters['Task']
if ($BuildFile -is [scriptblock]) {
$BuildFile = $BuildFile.File
return
}
if ($BuildTask -eq '**') {
if (![System.IO.Directory]::Exists(($_ = *Path $BuildFile))) {throw "Missing directory '$_'."}
$BuildFile = @(Get-ChildItem -LiteralPath $_ -Filter *.test.ps1 -Recurse)
return
}
if ($BuildFile) {
if (![System.IO.File]::Exists(($BuildFile = *Path $BuildFile))) {throw "Missing script '$BuildFile'."}
}
elseif (!($BuildFile = Get-BuildFile ${*}.CD)) {
throw 'Missing default script.'
}
${*}.File = $BuildFile
if (!($_ = (Get-Command $BuildFile -ErrorAction 1).Parameters)) {
& $BuildFile
throw 'Invalid script.'
}
if ($_.Count) {&{
$c = 'Verbose', 'Debug', 'ErrorAction', 'WarningAction', 'ErrorVariable', 'WarningVariable', 'OutVariable', 'OutBuffer', 'PipelineVariable', 'InformationAction', 'InformationVariable'
$r = 'Task', 'File', 'Result', 'Safe', 'Summary', 'WhatIf'
foreach($p in $_.Values) {
if ($c -notcontains ($_ = $p.Name)) {
if ($r -contains $_) {throw "Script uses reserved parameter '$_'."}
${*}.DP.Add($_, (New-Object System.Management.Automation.RuntimeDefinedParameter $_, $p.ParameterType, $p.Attributes))
foreach($a in $p.Attributes) {if ($a -is [System.Management.Automation.ParameterAttribute]) {$a.Position = 0x80000000}}
}
}
${*}.DP
}}
} end {
#.ExternalHelp InvokeBuild-Help.xml
function Add-BuildTask(
[Parameter(Position=0, Mandatory=1)][string]$Name,
[Parameter(Position=1)]$Jobs,
[string[]]$After,
[string[]]$Before,
$If=-9,
$Inputs,
$Outputs,
$Data,
$Done,
$Source=$MyInvocation,
[switch]$Partial
)
{
trap {*Die "Task '$Name': $_" 5}
if ($Jobs -is [hashtable]) {
if ($PSBoundParameters.Count -ne 2) {throw 'Invalid parameters.'}
Add-BuildTask $Name @Jobs -Source:$Source
return
}
if ($Name[0] -eq '?') {throw 'Invalid task name.'}
if ($_ = ${*}.All[$Name]) {${*}.Redefined += $_}
${*}.All[$Name] = [PSCustomObject]@{
Name = $Name
Error = $null
Started = $null
Elapsed = $null
Jobs = $1 = [System.Collections.Generic.List[object]]@()
After = $After
Before = $Before
If = $If
Inputs = $Inputs
Outputs = $Outputs
Data = $Data
Done = $Done
Partial = $Partial
InvocationInfo = $Source
}
if (!$Jobs) {return}
$1.AddRange(@($Jobs))
$2 = @()
foreach($j in $1) {
$r, $null = *Job $j
if ($2 -contains $r) {${*}.Doubles += ,($Name, $r)}
$2 += $r
}
}
#.ExternalHelp InvokeBuild-Help.xml
function Assert-Build([Parameter()]$Condition, [string]$Message) {
if (!$Condition) {
*Die "Assertion failed.$(if ($Message) {" $Message"})" 7
}
}
#.ExternalHelp InvokeBuild-Help.xml
function Assert-BuildEquals([Parameter()]$A, $B) {
if (![Object]::Equals($A, $B)) {
*Die @"
Objects are not equal:
A:$(if ($null -ne $A) {" $A [$($A.GetType())]"})
B:$(if ($null -ne $B) {" $B [$($B.GetType())]"})
"@ 7
}
}
#.ExternalHelp InvokeBuild-Help.xml
function Get-BuildError([Parameter(Mandatory=1)][string]$Task) {
if (!($_ = ${*}.All[$Task])) {
*Die "Missing task '$Task'." 5
}
$_.Error
}
#.ExternalHelp InvokeBuild-Help.xml
function Get-BuildProperty([Parameter(Mandatory=1)][string]$Name, $Value) {
${*n} = $Name
${*v} = $Value
Remove-Variable Name, Value
if (($null -ne ($_ = $PSCmdlet.GetVariableValue(${*n})) -and '' -ne $_) -or ($_ = [Environment]::GetEnvironmentVariable(${*n}))) {return $_}
if ($null -eq ${*v}) {*Die "Missing property '${*n}'." 13}
${*v}
}
#.ExternalHelp InvokeBuild-Help.xml
function Get-BuildSynopsis([Parameter(Mandatory=1)]$Task, $Hash=${*}.H) {
$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}
}
}
for($n = $I.ScriptLineNumber; --$n -ge 1) {
if ($c = $d.C[$n]) {if ($c -match '(?m)^\s*#*\s*Synopsis\s*:(.*)') {return $Matches[1].Trim()}}
elseif ($d.T[$n - 1].Trim()) {break}
}
}
#.ExternalHelp InvokeBuild-Help.xml
function Invoke-BuildExec([Parameter(Mandatory=1)][scriptblock]$Command, [int[]]$ExitCode=0) {
${private:*c} = $Command
${private:*x} = $ExitCode
Remove-Variable Command, ExitCode
& ${*c}
if (${*x} -notcontains $global:LastExitCode) {
*Die "Command {${*c}} exited with code $global:LastExitCode." 8
}
}
#.ExternalHelp InvokeBuild-Help.xml
function Remove-BuildItem([Parameter(Mandatory=1)][string[]]$Path) {
if ($Path -match '^[.*/\\]*$') {*Die 'Not allowed paths.' 5}
try {
foreach($_ in $Path) {
if (Get-Item $_ -Force -ErrorAction 0) {
Remove-Item $_ -Force -Recurse
}
}
}
catch {
*Die $_
}
}
#.ExternalHelp InvokeBuild-Help.xml
function Test-BuildAsset([Parameter(Position=0)][string[]]$Variable, [string[]]$Environment, [string[]]$Property) {
Remove-Variable Variable, Environment, Property
if ($_ = $PSBoundParameters['Variable']) {foreach($_ in $_) {
if ($null -eq ($$ = $PSCmdlet.GetVariableValue($_)) -or '' -eq $$) {*Die "Missing variable '$_'." 13}
}}
if ($_ = $PSBoundParameters['Environment']) {foreach($_ in $_) {
if (!([Environment]::GetEnvironmentVariable($_))) {*Die "Missing environment variable '$_'." 13}
}}
if ($_ = $PSBoundParameters['Property']) {foreach($_ in $_) {
if ('' -eq (Get-BuildProperty $_ '')) {*Die "Missing property '$_'." 13}
}}
}
#.ExternalHelp InvokeBuild-Help.xml
function Use-BuildAlias([Parameter(Mandatory=1)][string]$Path, [string[]]$Name) {
trap {*Die $_ 5}
$d = switch -regex ($Path) {
'^\*|^\d+\.' {Split-Path (Resolve-MSBuild $_)}
^Framework {"$env:windir\Microsoft.NET\$_"}
default {*Path $_}
}
if (![System.IO.Directory]::Exists($d)) {throw "Cannot resolve '$Path'."}
foreach($_ in $Name) {
Set-Alias $_ (Join-Path $d $_) -Scope 1
}
}
#.ExternalHelp InvokeBuild-Help.xml
function Set-BuildFooter([Parameter()][scriptblock]$Script) {${*}.Footer = $Script}
#.ExternalHelp InvokeBuild-Help.xml
function Set-BuildHeader([Parameter()][scriptblock]$Script) {${*}.Header = $Script}
#.ExternalHelp InvokeBuild-Help.xml
function Write-Build([ConsoleColor]$Color, [string]$Text) {
$i = $Host.UI.RawUI
$_ = $i.ForegroundColor
try {
$i.ForegroundColor = $Color
$Text
}
finally {
$i.ForegroundColor = $_
}
}
if ($env:APPVEYOR) {
function Write-Build([ConsoleColor]$Color, [string]$Text) {Write-Host $Text -ForegroundColor $Color}
}
else {
try {$null = Write-Build 0} catch {function Write-Build($Color, [string]$Text) {$Text}}
}
function *My {
$_.InvocationInfo.ScriptName -eq $MyInvocation.ScriptName
}
function *SL($P=$BuildRoot) {
Set-Location -LiteralPath $P -ErrorAction 1
}
function *Fin([Parameter()]$M, $C=0) {
*Die $M $C
}
function *Run($_) {
if ($_) {
*SL
. $_ @args
}
}
function *At($I) {
$I.InvocationInfo.PositionMessage.Trim()
}
function *Msg($M, $I) {
@"
$M
$(*At $I)
"@
}
function *Job($J) {
if ($J -is [string]) {if ($J[0] -eq '?') {$J.Substring(1), 1} else {$J}}
elseif ($J -is [scriptblock]) {$J}
else {*Fin 'Invalid job.' 5}
}
function *Unsafe($N, $J) {
if ($J -contains $N) {return 1}
foreach($_ in $J) {
$r, $null = *Job $_
if ($r -ne $N -and ($t = ${*}.All[$r]) -and $t.If -and (*Unsafe $N $t.Jobs)) {
return 1
}
}
}
function *Amend($X, $J, $B) {
$n = $X.Name
foreach($_ in $J) {
$r, $s = *Job $_
if (!($t = ${*}.All[$r])) {*Fin (*Msg "Task '$n': Missing task '$r'." $X) 5}
$j = $t.Jobs
$i = $j.Count
if ($B) {
for($k = -1; ++$k -lt $i -and $j[$k] -is [string]) {}
$i = $k
}
$j.Insert($i, $(if ($s) {"?$n"} else {$n}))
}
}
function *Check($J, $T, $P=@()) {
foreach($_ in $J) { if ($_ -is [string]) {
$_, $null = *Job $_
if (!($r = ${*}.All[$_])) {
$_ = "Missing task '$_'."
*Fin $(if ($T) {*Msg "Task '$($T.Name)': $_" $T} else {$_}) 5
}
if ($P -contains $r) {
*Fin (*Msg "Task '$($T.Name)': Cyclic reference to '$_'." $T) 5
}
*Check $r.Jobs $r ($P + $r)
}}
}
filter *Help {
$r = 1 | Select-Object Name, Jobs, Synopsis
$r.Name = $_.Name
$r.Jobs = foreach($j in $_.Jobs) {if ($j -is [string]) {$j} else {'{}'}}
$r.Synopsis = Get-BuildSynopsis $_
$r
}
function *Root($A) {
*Check $A.Keys
$h = @{}
foreach($_ in $A.Values) {foreach($_ in $_.Jobs) {
if ($_ -is [string]) {
$_, $null = *Job $_
$h[$_] = 1
}
}}
foreach($_ in $A.Keys) {if (!$h[$_]) {$_}}
}
function *Err($T) {
${*}.Errors.Add([PSCustomObject]@{Error = $_; File = $BuildFile; Task = $T})
Write-Build 12 "ERROR: $(if (*My) {$_} else {*Msg $_ $_})"
if ($T) {$T.Error = $_}
}
function *IO {
if ((${private:*i} = $Task.Inputs) -is [scriptblock]) {
*SL
${*i} = @(& ${*i})
}
*SL
${private:*p} = [System.Collections.Generic.List[object]]@()
${*i} = foreach($_ in ${*i}) {
if ($_ -isnot [System.IO.FileInfo]) {$_ = [System.IO.FileInfo](*Path $_)}
if (!$_.Exists) {*Fin "Missing input '$_'." 13}
${*p}.Add($_.FullName)
$_
}
if (!${*p}) {return 2, 'Skipping empty input.'}
${private:*o} = $Task.Outputs
if ($Task.Partial) {
${*o} = @(
if (${*o} -is [scriptblock]) {
${*p} | & ${*o}
*SL
}
else {
${*o}
}
)
if (${*p}.Count -ne ${*o}.Count) {*Fin "Different Inputs/Outputs counts: $(${*p}.Count)/$(${*o}.Count)." 6}
$k = -1
$Task.Inputs = $i = [System.Collections.Generic.List[object]]@()
$Task.Outputs = $o = [System.Collections.Generic.List[object]]@()
foreach($_ in ${*i}) {
$f = *Path ($p = ${*o}[++$k])
if (![System.IO.File]::Exists($f) -or $_.LastWriteTime -gt [System.IO.File]::GetLastWriteTime($f)) {
$i.Add(${*p}[$k])
$o.Add($p)
}
}
if ($i) {return $null, "Out-of-date outputs: $($o.Count)/$(${*p}.Count)."}
}
else {
if (${*o} -is [scriptblock]) {
$Task.Outputs = ${*o} = ${*p} | & ${*o}
*SL
}
if (!${*o}) {*Fin 'Outputs must not be empty.' 5}
$Task.Inputs = ${*p}
$m = (${*i} | .{process{$_.LastWriteTime.Ticks}} | Measure-Object -Maximum).Maximum
foreach($_ in ${*o}) {
$p = *Path $_
if (![System.IO.File]::Exists($p)) {return $null, "Missing output '$_'."}
if ($m -gt [System.IO.File]::GetLastWriteTime($p).Ticks) {return $null, "Out-of-date output '$_'."}
}
}
2, 'Skipping up-to-date output.'
}
function *Task {
${private:*p} = "$($args[1])/$($args[0])"
${private:*n}, ${private:*s} = *Job $args[0]
New-Variable Task (${*}.Task = ${*}.All[${*n}]) -Option Constant
if ($Task.Elapsed) {
Write-Build 8 "Done ${*p}"
return
}
$Task.Started = [DateTime]::Now
if (${*}.XTask) {& ${*}.XTask}
if ((${private:*x} = $Task.If) -is [scriptblock]) {
*SL
try {
${*x} = & ${*x}
}
catch {
*Err $Task
${*}.Tasks.Add($Task)
Write-Build 14 (*At $Task)
$Task.Elapsed = [TimeSpan]::Zero
throw
}
}
if (!${*x}) {
Write-Build 8 "Task ${*p} skipped."
return
}
${private:*i} = , [int]($null -ne $Task.Inputs)
try {
. *Run ${*}.EnterTask
foreach(${private:*j} in $Task.Jobs) {
if (${*j} -is [string]) {
*Task ${*j} ${*p}
continue
}
& ${*}.Header ${*p}
if (1 -eq ${*i}[0]) {
try {
${*i} = *IO
}
catch {
*Err $Task
throw
}
Write-Build 8 ${*i}[1]
}
if (${*i}[0]) {
continue
}
try {
. *Run ${*}.EnterJob
*SL
if (0 -eq ${*i}[0]) {
& ${*j}
}
else {
$Inputs = $Task.Inputs
$Outputs = $Task.Outputs
if ($Task.Partial) {
${*x} = 0
$Inputs | .{process{
$2 = $Outputs[${*x}++]
$_
}} | & ${*j}
}
else {
$Inputs | & ${*j}
}
}
}
catch {
*Err $Task
throw
}
finally {
. *Run ${*}.ExitJob
}
}
}
catch {
$Task.Error = $_
if (!${*s} -or (*Unsafe ${*n} $BuildTask)) {throw}
}
finally {
$Task.Elapsed = [DateTime]::Now - $Task.Started
${*}.Tasks.Add($Task)
if ($Task.Error) {
Write-Build 14 (*At $Task)
}
else {
& ${*}.Footer ${*p}
}
*Run $Task.Done
. *Run ${*}.ExitTask
}
}
Set-Alias assert Assert-Build
Set-Alias equals Assert-BuildEquals
Set-Alias error Get-BuildError
Set-Alias exec Invoke-BuildExec
Set-Alias property Get-BuildProperty
Set-Alias remove Remove-BuildItem
Set-Alias requires Test-BuildAsset
Set-Alias task Add-BuildTask
Set-Alias use Use-BuildAlias
Set-Alias Invoke-Build ($_ = $MyInvocation.MyCommand.Path)
$_ = Split-Path $_
Set-Alias Show-TaskHelp (Join-Path $_ Show-TaskHelp.ps1)
Set-Alias Build-Parallel (Join-Path $_ Build-Parallel.ps1)
Set-Alias Resolve-MSBuild (Join-Path $_ Resolve-MSBuild.ps1)
if ($MyInvocation.InvocationName -eq '.') {
Remove-Variable Task, File, Result, Safe, Summary, WhatIf
return
}
function Write-Warning([Parameter()]$Message) {
$PSCmdlet.WriteWarning($Message)
${*}.Warnings.Add([PSCustomObject]@{Message = $Message; File = $BuildFile; Task = ${*}.Task})
}
$ErrorActionPreference = 'Stop'
foreach($_ in $PSBoundParameters.Keys) {
if (${*}.DP.ContainsKey($_)) {
${*}.SP[$_] = $PSBoundParameters[$_]
}
}
if (${*}.Q = $BuildTask -eq '?' -or $BuildTask -eq '??') {
$WhatIf = $true
}
Remove-Variable Task, File, Result, Safe, Summary
${*}.Error = $null
try {
if ($BuildTask -eq '**') {
${*}.A = 0
foreach($_ in $BuildFile) {
Invoke-Build * $_.FullName -Safe:${*}.Safe
}
${*}.B = 1
exit
}
function Enter-Build([Parameter()][scriptblock]$Script) {${*}.EnterBuild = $Script}
function Exit-Build([Parameter()][scriptblock]$Script) {${*}.ExitBuild = $Script}
function Enter-BuildTask([Parameter()][scriptblock]$Script) {${*}.EnterTask = $Script}
function Exit-BuildTask([Parameter()][scriptblock]$Script) {${*}.ExitTask = $Script}
function Enter-BuildJob([Parameter()][scriptblock]$Script) {${*}.EnterJob = $Script}
function Exit-BuildJob([Parameter()][scriptblock]$Script) {${*}.ExitJob = $Script}
function Set-BuildData([Parameter()]$Key, $Value) {${*}.Data[$Key] = $Value}
*SL ($BuildRoot = if ($BuildFile) {Split-Path $BuildFile} else {${*}.CD})
$_ = ${*}.SP
${private:**} = @(. ${*}.File @_)
foreach($_ in ${**}) {
Write-Warning "Unexpected output: $_."
if ($_ -is [scriptblock]) {*Fin "Dangling scriptblock at $($_.File):$($_.StartPosition.StartLine)" 6}
}
if (!(${**} = ${*}.All).Count) {*Fin "No tasks in '$BuildFile'." 6}
foreach($_ in ${**}.Values) {
if ($_.Before) {*Amend $_ $_.Before 1}
}
foreach($_ in ${**}.Values) {
if ($_.After) {*Amend $_ $_.After}
}
if (${*}.Q) {
*Check ${**}.Keys
if ($BuildTask -eq '?') {
${**}.Values | *Help
}
else {
${**}
}
exit
}
if ($BuildTask -eq '*') {
$BuildTask = *Root ${**}
}
else {
if (!$BuildTask -or '.' -eq $BuildTask) {
$BuildTask = if (${**}['.']) {'.'} else {${**}.Item(0).Name}
}
*Check $BuildTask
}
if ($WhatIf) {
Show-TaskHelp
exit
}
New-Variable BuildRoot (*Path $BuildRoot) -Option Constant -Force
if (![System.IO.Directory]::Exists($BuildRoot)) {*Fin "Missing build root '$BuildRoot'." 13}
Write-Build 11 "Build $($BuildTask -join ', ') $BuildFile"
foreach($_ in ${*}.Redefined) {
Write-Build 8 "Redefined task '$($_.Name)'."
}
foreach($_ in ${*}.Doubles) {
if (${*}.All[$_[1]].If -isnot [scriptblock]) {
Write-Warning "Task '$($_[0])' always skips '$($_[1])'."
}
}
${*}.A = 0
try {
. *Run ${*}.EnterBuild
if (${*}.XBuild) {. ${*}.XBuild}
foreach($_ in $BuildTask) {
*Task $_ ''
}
}
finally {
${*}.Task = $null
. *Run ${*}.ExitBuild
}
${*}.B = 1
exit
}
catch {
${*}.B = 2
${*}.Error = $_
if (!${*}.Errors) {*Err}
if ($_.FullyQualifiedErrorId -eq 'PositionalParameterNotFound,Add-BuildTask') {
Write-Warning 'Check task parameters: Name and comma separated Jobs.'
}
if (${*}.Safe) {
exit
}
elseif (*My) {
$PSCmdlet.ThrowTerminatingError($_)
}
throw
}
finally {
*SL ${*}.CD
if (${*}.B -and !${*}.Q) {
$t = ${*}.Tasks
$e = ${*}.Errors
if (${*}.Summary) {
Write-Build 11 'Build summary:'
foreach($_ in $t) {
'{0,-16} {1} - {2}:{3}' -f $_.Elapsed, $_.Name, $_.InvocationInfo.ScriptName, $_.InvocationInfo.ScriptLineNumber
if ($_ = $_.Error) {
Write-Build 12 "ERROR: $(if (*My) {$_} else {*Msg $_ $_})"
}
}
}
if ($w = ${*}.Warnings) {
foreach($_ in $w) {
"WARNING: $($_.Message)$(if ($_.Task) {" Task: $($_.Task.Name)."}) File: $($_.File)."
}
}
if ($_ = ${*}.P) {
$_.Tasks.AddRange($t)
$_.Errors.AddRange($e)
$_.Warnings.AddRange($w)
}
$c, $m = if (${*}.A) {12, "Build ABORTED $BuildFile"}
elseif (${*}.B -eq 2) {12, 'Build FAILED'}
elseif ($e) {14, 'Build completed with errors'}
elseif ($w) {14, 'Build succeeded with warnings'}
else {10, 'Build succeeded'}
Write-Build $c "$m. $($t.Count) tasks, $($e.Count) errors, $($w.Count) warnings $((${*}.Elapsed = [DateTime]::Now - ${*}.Started))"
}
}
}