-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInvokeNovaModuleSelfUpdateWorkflow.ps1
More file actions
220 lines (177 loc) · 6.81 KB
/
Copy pathInvokeNovaModuleSelfUpdateWorkflow.ps1
File metadata and controls
220 lines (177 loc) · 6.81 KB
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
function Invoke-NovaModuleSelfUpdateOrStop {
[CmdletBinding()]
param(
[Parameter(Mandatory)][pscustomobject]$Plan
)
try {
$null = Invoke-NovaModuleSelfUpdate -ModuleName $Plan.ModuleName -AllowPrerelease:$Plan.UsedAllowPrerelease
} catch {
$message = "NovaModuleTools self-update failed: $( $_.Exception.Message ) Confirm that the PowerShell Gallery is reachable and that this session can update installed modules, then rerun Update-NovaModuleTool."
Stop-NovaOperation -Message $message -ErrorId 'Nova.Dependency.ModuleSelfUpdateFailed' -Category InvalidOperation -TargetObject $Plan.ModuleName
}
}
function Complete-NovaModuleSelfUpdateResult {
[CmdletBinding()]
param(
[Parameter(Mandatory)][pscustomobject]$Plan,
[AllowNull()][string]$ReleaseNotesUri
)
if ($Plan.PSObject.Properties.Name -contains 'ReleaseNotesUri') {
$Plan.ReleaseNotesUri = $ReleaseNotesUri
return $Plan
}
$Plan | Add-Member -NotePropertyName 'ReleaseNotesUri' -NotePropertyValue $ReleaseNotesUri
return $Plan
}
function Invoke-NovaModuleSelfUpdateWorkflow {
[CmdletBinding()]
param(
[Parameter(Mandatory)][pscustomobject]$WorkflowContext,
[switch]$ShouldRun
)
$progressActivity = 'Updating NovaModuleTools'
$result = $null
if (Test-NovaModuleSelfUpdateWorkflowShouldSkipExecution -WorkflowContext $WorkflowContext -ShouldRun:$ShouldRun) {
$result = Complete-NovaModuleSelfUpdateResult -Plan $WorkflowContext.Plan -ReleaseNotesUri $null
Write-NovaModuleSelfUpdateWorkflowResult -Result $result -WorkflowContext $WorkflowContext
return $result
}
try {
Invoke-NovaModuleSelfUpdateWorkflowStep -Activity $progressActivity -Status (Get-NovaModuleSelfUpdateWorkflowUpdateStatus -WorkflowContext $WorkflowContext) -PercentComplete 80 -Action {
Invoke-NovaModuleSelfUpdateOrStop -Plan $WorkflowContext.Plan
}
$WorkflowContext.Plan.Updated = $true
$releaseNotesUri = Invoke-NovaModuleSelfUpdateWorkflowStep -Activity $progressActivity -Status 'Reading release notes from the updated module' -PercentComplete 95 -Action {
Get-NovaModuleReleaseNotesUri
}
$result = Complete-NovaModuleSelfUpdateResult -Plan $WorkflowContext.Plan -ReleaseNotesUri $releaseNotesUri
} finally {
Write-Progress -Activity $progressActivity -Completed
}
Write-NovaModuleSelfUpdateWorkflowResult -Result $result -WorkflowContext $WorkflowContext
return $result
}
function Test-NovaModuleSelfUpdateWorkflowShouldSkipExecution {
[CmdletBinding()]
param(
[Parameter(Mandatory)][pscustomobject]$WorkflowContext,
[switch]$ShouldRun
)
return (-not $WorkflowContext.Plan.UpdateAvailable) -or (-not $ShouldRun)
}
function Invoke-NovaModuleSelfUpdateWorkflowStep {
[CmdletBinding()]
param(
[Parameter(Mandatory)][string]$Activity,
[Parameter(Mandatory)][string]$Status,
[Parameter(Mandatory)][int]$PercentComplete,
[Parameter(Mandatory)][scriptblock]$Action
)
Write-Progress -Activity $Activity -Status $Status -PercentComplete $PercentComplete
& $Action
}
function Write-NovaModuleSelfUpdateWorkflowResult {
[CmdletBinding()]
param(
[Parameter(Mandatory)][pscustomobject]$Result,
[Parameter(Mandatory)][pscustomobject]$WorkflowContext
)
$whatIfEnabled = Test-NovaModuleSelfUpdateWorkflowWhatIfEnabled -WorkflowContext $WorkflowContext
Write-Message (Get-NovaModuleSelfUpdateWorkflowStatusMessage -Result $Result -WhatIfEnabled:$whatIfEnabled) -color (Get-NovaModuleSelfUpdateWorkflowStatusColor -Result $Result)
Write-Message "Current version: $( $Result.CurrentVersion )"
$targetVersionLine = Get-NovaModuleSelfUpdateWorkflowTargetVersionLine -Result $Result
if (-not [string]::IsNullOrWhiteSpace($targetVersionLine)) {
Write-Message $targetVersionLine
}
$repositoryLine = Get-NovaModuleSelfUpdateWorkflowRepositoryLine -Result $Result
if (-not [string]::IsNullOrWhiteSpace($repositoryLine)) {
Write-Message $repositoryLine
}
foreach ($line in (Get-NovaModuleSelfUpdateWorkflowNextStepLine -Result $Result -WhatIfEnabled:$whatIfEnabled)) {
Write-Message $line
}
}
function Test-NovaModuleSelfUpdateWorkflowWhatIfEnabled {
[CmdletBinding()]
param(
[Parameter(Mandatory)][pscustomobject]$WorkflowContext
)
return $WorkflowContext.WorkflowParams.ContainsKey('WhatIf') -and $WorkflowContext.WorkflowParams.WhatIf
}
function Get-NovaModuleSelfUpdateWorkflowUpdateStatus {
[CmdletBinding()]
param(
[Parameter(Mandatory)][pscustomobject]$WorkflowContext
)
if ($WorkflowContext.Plan.IsPrereleaseTarget) {
return "Installing prerelease version $( $WorkflowContext.Plan.TargetVersion )"
}
return "Installing version $( $WorkflowContext.Plan.TargetVersion )"
}
function Get-NovaModuleSelfUpdateWorkflowStatusMessage {
[CmdletBinding()]
param(
[Parameter(Mandatory)][pscustomobject]$Result,
[switch]$WhatIfEnabled
)
if (-not $Result.UpdateAvailable) {
return 'NovaModuleTools is already up to date.'
}
if ($WhatIfEnabled) {
return 'Self-update plan ready for NovaModuleTools'
}
if ($Result.Cancelled) {
return 'Self-update cancelled for NovaModuleTools.'
}
return "Updated NovaModuleTools to version $( $Result.TargetVersion )."
}
function Get-NovaModuleSelfUpdateWorkflowStatusColor {
[CmdletBinding()]
param(
[Parameter(Mandatory)][pscustomobject]$Result
)
if ($Result.Cancelled) {
return 'Blue'
}
return 'Green'
}
function Get-NovaModuleSelfUpdateWorkflowTargetVersionLine {
[CmdletBinding()]
param(
[Parameter(Mandatory)][pscustomobject]$Result
)
if (-not $Result.UpdateAvailable) {
return $null
}
return "Target version: $( $Result.TargetVersion )"
}
function Get-NovaModuleSelfUpdateWorkflowRepositoryLine {
[CmdletBinding()]
param(
[Parameter(Mandatory)][pscustomobject]$Result
)
if ([string]::IsNullOrWhiteSpace($Result.LookupRepository)) {
return $null
}
return "Repository: $( $Result.LookupRepository )"
}
function Get-NovaModuleSelfUpdateWorkflowNextStepLine {
[CmdletBinding()]
param(
[Parameter(Mandatory)][pscustomobject]$Result,
[switch]$WhatIfEnabled
)
if (-not $Result.UpdateAvailable -or $Result.Cancelled) {
return @()
}
if ($WhatIfEnabled) {
return @(
'Next step:'
"Run Update-NovaModuleTool without -WhatIf when you are ready to install version $( $Result.TargetVersion )."
)
}
return @(
'Next step:'
'Get-NovaProjectInfo -InstalledNovaVersion'
)
}