Build-Parallel.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
<#
Copyright (c) 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)][hashtable[]]$Build,
    $Result,
    [int]$Timeout=[int]::MaxValue,
    [int]$MaximumBuilds=[Environment]::ProcessorCount,
    [switch]$FailHard,
    [string[]]$ShowParameter
)

# info, result
$info = [PSCustomObject]@{
    Tasks = [System.Collections.Generic.List[object]]@()
    Errors = [System.Collections.Generic.List[object]]@()
    Warnings = [System.Collections.Generic.List[object]]@()
    Started = [DateTime]::Now
    Elapsed = $null
}
if ($Result) {
    if ($Result -is [string]) {
        New-Variable $Result $info -Scope 1 -Force
    }
    else {
        $Result.Value = $info
    }
}

# no builds
if (!$Build) {return}

# get and source the engine
$ib = Join-Path (Split-Path $MyInvocation.MyCommand.Path) Invoke-Build.ps1
try {. $ib} catch {$PSCmdlet.ThrowTerminatingError($_)}
$ErrorActionPreference = 'Stop'

### make works, check scripts
$works = @()
for ($1 = 0; $1 -lt $Build.Count) {
    $b = @{} + $Build[$1]
    $Build[$1++] = $b

    if ($file = $b['File']) {
        if (![System.IO.File]::Exists(($file = *Path $file))) {*Die "Missing script '$file'." 13}
    }
    elseif (!($file = Get-BuildFile (*Path))) {
        *Die "Missing default script in build $1." 5
    }

    $b.Result = @{}
    $b.File = $file
    $b.Safe = $true

    $work = @{}
    $works += $work
    $work.Build = $b
    $work.Done = $false
    $work.Error1 = $null
    $work.Error2 = $null
    $work.Title = "($1/$($Build.Count)) $file"
    if ($ShowParameter) {
        foreach($p in $ShowParameter) {
            $work.Title += " $($p)='$($b[$p])'"
        }
    }
}

### prepare logs and make shells
foreach($work in $works) {
    $b = $work.Build
    if ($log = $b['Log']) {
        $work.Temp = $false
        $b.Remove('Log')
        [System.IO.File]::Delete(($log = *Path $log))
    }
    else {
        $work.Temp = $true
        $log = [System.IO.Path]::GetTempFileName()
    }
    $work.Log = $log
    $work.PS = [PowerShell]::Create().AddCommand($ib).AddParameters($b).AddCommand('Out-File').AddParameter('FilePath', $log).AddParameter('Encoding', 'UTF8')
}

function Write-Log($work) {
    $file = $work.Log
    if ($work.Temp) {
        try {
            $read = [System.IO.File]::OpenText($file)
            while($null -ne ($_ = $read.ReadLine())) {$_}
            $read.Close()
        }
        catch {
            Write-Warning $_
        }
        [System.IO.File]::Delete($file)
    }
    else {
        "Log: $file"
    }
}

### main
if ($MaximumBuilds -lt 1) {*Die "MaximumBuilds should be a positive number." 5}
$MaximumBuilds = [math]::Min($MaximumBuilds, $Build.Count)
$stage = [System.Collections.Generic.List[object]]@()
$iWork = 0
$abort = ''
$failures = @()
$stopwatch = [Diagnostics.Stopwatch]::StartNew()
try {
    for() {
        ### stage next work(s), BeginInvoke
        while($stage.Count -lt $MaximumBuilds -and $iWork -lt $works.Count) {
            $work = $works[$iWork++]
            $stage.Add($work)

            Write-Build Cyan "Start $($work.Title)"
            $work.Job = $work.PS.BeginInvoke()
        }
        if (!$stage) {break}

        ### wait for any staged
        $waits = New-Object System.Collections.Generic.List[System.Threading.WaitHandle]
        foreach($work in $stage) {$waits.Add($work.Job.AsyncWaitHandle)}
        $time = $Timeout - $stopwatch.ElapsedMilliseconds
        $index = [System.Threading.WaitHandle]::WaitAny($waits.ToArray(), $time, $true)
        if ($index -eq [System.Threading.WaitHandle]::WaitTimeout) {
            $abort = 'Build timed out.'
            break
        }

        ### unstage done, EndInvoke
        $work = $stage[$index]
        $stage.RemoveAt($index)
        $work.Done = $true
        try {
            $work.PS.EndInvoke($work.Job)
            $work.Error2 = $work.Build.Result.Value.Error
        }
        catch {
            $work.Error1 = "Invalid build arguments or script. Error: $_"
        }

        ### abort?
        if (($work.Error1 -or $work.Error2) -and $FailHard) {
            $abort = "Aborted by $($work.Title)"
            break
        }
    }

    ### finish works
    foreach($work in $works) {
        Write-Build Cyan "Build $($work.Title):"

        ### get error and dispose
        $runtime = $false
        $_ = try {
            if ($work.Done) {
                if ($work.Error1) {
                    $work.Error1
                }
                else {
                    $runtime = $true
                    $r = $work.Build.Result.Value
                    $info.Tasks.AddRange($r.Tasks)
                    $info.Errors.AddRange($r.Errors)
                    $info.Warnings.AddRange($r.Warnings)
                    $r.Error
                }
            }
            else {
                $work.PS.Stop()
                $abort
            }
        }
        catch {
            $_
        }
        finally {
            $work.PS.Dispose()
        }

        ### log
        Write-Log $work

        ### result
        if ($_) {
            Write-Build Cyan "Build $($work.Title) FAILED."
            $_ = if ($_ -is [System.Management.Automation.ErrorRecord] -and $runtime) {*Msg $_ $_} else {"$_"}
            Write-Build Red "ERROR: $_"
            $failures += @{
                File = $work.Title
                Error = $_
            }
        }
        else {
            Write-Build Cyan "Build $($work.Title) succeeded."
        }
    }

    ### fail?
    if ($failures) {
        *Die ($(
            "Failed builds:"
            foreach($_ in $failures) {
                "Build: $($_.File)"
                "ERROR: $($_.Error)"
            }
        ) -join [Environment]::NewLine)
    }
}
finally {
    $errors = $info.Errors.Count
    $warnings = $info.Warnings.Count
    $info.Elapsed = [DateTime]::Now - $info.Started

    if (($up = $PSCmdlet.SessionState.PSVariable.Get('*')) -and ($up = if ($up.Description -eq 'IB') {$up.Value})) {
        $up.Tasks.AddRange($info.Tasks)
        $up.Errors.AddRange($info.Errors)
        $up.Warnings.AddRange($info.Warnings)
    }

    $color, $text = if ($failures) {12, 'Builds FAILED'}
    elseif ($errors) {14, 'Builds completed with errors'}
    elseif ($warnings) {14, 'Builds succeeded with warnings'}
    else {10, 'Builds succeeded'}

    Write-Build $color @"
Tasks: $($info.Tasks.Count) tasks, $errors errors, $warnings warnings
$text. $($Build.Count) builds, $($failures.Count) failed $($info.Elapsed)
"@

}