Setting the file. One moment. Batch · Batch Script Progress · github/awesome-copilot · Skills Docs(opens in a new tab)
examples/batch.ps1
PowerShell·75 lines·3 KB
'empty'
) { $count
=
0
}
8if ($Scenario -eq 'mixed') { $count = 4 }
9$total = $count
10if ($Scenario -eq 'unknown') { $total = $null }
11$stats = @{ processed = 0; succeeded = 0; failed = 0; skipped = 0 }
12$clock = [System.Diagnostics.Stopwatch]::StartNew()
13$lastReport = 0
14$results = [System.Collections.Generic.List[object]]::new()
15$status = 'completed'
16
17function Write-EventRecord {
18 param([string]$EventName, [hashtable]$Fields = @{})
19 $record = [ordered]@{ event = $EventName; total = $total }
20 foreach ($key in @('processed', 'succeeded', 'failed', 'skipped')) {
21 $record[$key] = $stats[$key]
22 }
23 $record['elapsed_ms'] = $clock.ElapsedMilliseconds
24 foreach ($key in $Fields.Keys) { $record[$key] = $Fields[$key] }
25 [Console]::Error.WriteLine(($record | ConvertTo-Json -Compress))
26 [Console]::Error.Flush()
27}
28
29Write-EventRecord 'start'
30for ($index = 1; $index -le $count; $index++) {
31 if ($Scenario -eq 'cancelled' -and $index -eq 2) {
32 $status = 'cancelled'
33 break
34 }
35 if ($Scenario -eq 'mixed' -and $index -eq 2) {
36 $stats.skipped++
37 } else {
38 try {
39 if (($Scenario -eq 'mixed' -and $index -eq 3) -or ($Scenario -in @('fail-fast', 'partial-zero') -and $index -eq 2)) {
40 throw 'fixture-rejected'
41 }
42 $results.Add(@{ id = "item-$index"; value = $index * 2 })
43 $stats.succeeded++
44 } catch {
45 $stats.failed++
46 $stats.processed++
47 Write-EventRecord 'item-failed' @{ item_id = "item-$index"; reason = 'fixture-rejected'; will_continue = ($Scenario -ne 'fail-fast') }
48 if ($Scenario -eq 'fail-fast') {
49 $status = 'failed'
50 break
51 }
52 $stats.processed--
53 }
54 }
55 $stats.processed++
56 if ($stats.processed % 2 -eq 0 -or $clock.ElapsedMilliseconds - $lastReport -ge 1000 -or $Scenario -eq 'live') {
57 Write-EventRecord 'progress'
58 $lastReport = $clock.ElapsedMilliseconds
59 }
60 if ($Scenario -eq 'live' -and $index -eq 1) {
61 if ($null -eq [Console]::In.ReadLine()) { throw 'Fixture release input missing' }
62 }
63}
64if ($status -eq 'completed' -and $stats.failed -gt 0) { $status = 'partial' }
65$exitCode = 0
66if ($stats.failed -gt 0 -and $Scenario -ne 'partial-zero') { $exitCode = 1 }
67if ($status -eq 'cancelled') { $exitCode = 130 }
68$unprocessed = $null
69if ($null -ne $total) { $unprocessed = $total - $stats.processed }
70Write-EventRecord 'summary' @{ status = $status; exit_code = $exitCode; unprocessed = $unprocessed }
71if ($status -ne 'cancelled') {
72 [Console]::Out.WriteLine((@{ results = @($results.ToArray()) } | ConvertTo-Json -Compress -Depth 4))
73 [Console]::Out.Flush()
74}
75exit $exitCode