Build-Checkpoint.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 |
<# 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 [CmdletBinding(DefaultParameterSetName='Default')] param( [Parameter(Position=0, Mandatory=1)][string]$Checkpoint, [Parameter(Position=1)][hashtable]$Build, [switch]$Preserve, [Parameter(ParameterSetName='Resume', Mandatory=1)] [switch]$Resume, [Parameter(ParameterSetName='Auto', Mandatory=1)] [switch]$Auto ) try { $ErrorActionPreference = 'Stop' $Checkpoint = $PSCmdlet.GetUnresolvedProviderPathFromPSPath($Checkpoint) $Build = if ($Build) {@{} + $Build} else {@{}} if ($Build['WhatIf']) {throw 'WhatIf is not supported.'} ${*checkpoint} = @{ Checkpoint = $Checkpoint Preserve = $Preserve Result = $Build['Result'] Data = $null } $Build.Remove('Result') $Auto = $PSCmdlet.ParameterSetName -eq 'Auto' -and $Auto $Resume = $PSCmdlet.ParameterSetName -eq 'Resume' -and $Resume if ($Auto) { $Resume = [System.IO.File]::Exists($Checkpoint) } if ($Resume) { if (![System.IO.File]::Exists($Checkpoint)) {throw "Missing checkpoint '$Checkpoint'."} ${*checkpoint}.Data = try {Import-Clixml $Checkpoint} catch {throw 'Invalid checkpoint file?'} foreach($_ in @($Build.get_Keys())) { if ($_ -ne 'Safe' -and $_ -ne 'Summary') { $Build.Remove($_) } } $Build.Task = ${*checkpoint}.Data.Task $Build.File = ${*checkpoint}.Data.File $Build = $Build + ${*checkpoint}.Data.Prm1 } ${*checkpoint}.XBuild = { if (${*checkpoint}.Data) { foreach($_ in ${*checkpoint}.Data.Done) { ${*}.All[$_].Elapsed = [TimeSpan]::Zero } foreach($_ in ${*checkpoint}.Data.Prm2.GetEnumerator()) { Set-Variable $_.Key $_.Value -Scope Script } if ($_ = ${*}.Data['Checkpoint.Import']) { . *Run $_ ${*checkpoint}.Data.User } } } ${*checkpoint}.XCheck = { Export-Clixml ${*checkpoint}.Checkpoint -InputObject @{ User = *Run ${*}.Data['Checkpoint.Export'] Task = $BuildTask File = $BuildFile Prm1 = ${*}.SP Prm2 = $( $r = @{} foreach($_ in ${*}.DP.get_Keys()) { $r[$_] = Get-Variable -Name $_ -Scope Script -ValueOnly } $r ) Done = @( foreach($t in ${*}.All.get_Values()) { if ($t.Elapsed -and !$t.Error) { $t.Name } } ) } } $_ = $Build Remove-Variable Checkpoint, Build, Preserve, Resume, Auto Set-Alias Invoke-Build (Join-Path (Split-Path $MyInvocation.MyCommand.Path) Invoke-Build.ps1) Invoke-Build @_ -Result ${*checkpoint} if (!${*checkpoint}.Value.Error -and !${*checkpoint}.Preserve) { [System.IO.File]::Delete(${*checkpoint}.Checkpoint) } } catch { if ($_.InvocationInfo.ScriptName -notmatch '\b(Invoke-Build|Build-Checkpoint)\.ps1$') {throw} $PSCmdlet.ThrowTerminatingError($_) } finally { if ($r = ${*checkpoint}.Result) { if ($r -is [string]) { New-Variable $r ${*checkpoint}.Value -Scope 1 -Force } else { $r.Value = ${*checkpoint}.Value } } } |