Repository navigation
Expand file tree
/
Copy pathdev-config.ps1
More file actions
235 lines (207 loc) · 9.46 KB
/
Copy pathdev-config.ps1
File metadata and controls
235 lines (207 loc) · 9.46 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<#
.SYNOPSIS
Configures or cleans up a Windows developer workstation, or applies one developer workload.
.DESCRIPTION
-Workload picks a definition from workloads\. The default, devconfig, is the complete
Windows Dev Config setup; other workloads, such as winui, reuse the same phases, helpers,
elevation, logging, and summary.
#>
[CmdletBinding()]
param(
[switch] $NoElevate,
[switch] $Resumed,
[switch] $AllowUnsigned,
[switch] $ApplyTerminalFont,
[ValidateSet('Full', 'Partial', 'Uninstall')] [string] $Action = 'Full',
[ValidatePattern('^[a-z0-9]+(-[a-z0-9]+)*$')] [string] $Workload = 'devconfig'
)
$ErrorActionPreference = 'Stop'
Set-StrictMode -Version Latest
# A child shell can inherit incompatible built-in modules from another PowerShell edition.
$env:PSModulePath = "$PSHOME\Modules;$env:PSModulePath"
$stepsDir = Join-Path $PSScriptRoot 'steps'
$securityCode = [IO.File]::ReadAllText((Join-Path $stepsDir '_security.ps1'))
if (-not $AllowUnsigned) {
# Verify and execute the same text to avoid a file-swap race.
$signature = Get-AuthenticodeSignature -Content ([Text.Encoding]::Unicode.GetBytes($securityCode)) -SourcePathOrExtension '.ps1'
if ($signature.Status -ne 'Valid' -or -not $signature.SignerCertificate -or
$signature.SignerCertificate.Subject -ne 'CN=Microsoft Corporation, O=Microsoft Corporation, L=Redmond, S=Washington, C=US') {
throw 'The setup security helper failed Microsoft signature verification. Run bootstrap.ps1 to reinstall; use -AllowUnsigned only for development.'
}
}
. ([scriptblock]::Create($securityCode))
if (-not $AllowUnsigned) {
Assert-DevConfigProtectedTree -Directory $PSScriptRoot
Assert-DevConfigMicrosoftSigned -Directory $PSScriptRoot
}
# Windows PowerShell 5.1 defaults to ANSI; force UTF-8 for console symbols.
try {
$utf8NoBom = [System.Text.UTF8Encoding]::new($false)
[Console]::OutputEncoding = $utf8NoBom
$OutputEncoding = $utf8NoBom
} catch {
Write-Verbose "Could not force UTF-8 console encoding: $($_.Exception.Message)"
}
. (Join-Path $stepsDir '_console.ps1')
. (Join-Path $stepsDir '_step-runner.ps1')
. (Join-Path $stepsDir '_elevation.ps1')
. (Join-Path $stepsDir '_reboot-resume.ps1')
. (Join-Path $stepsDir '_registry.ps1')
. (Join-Path $stepsDir '_environment.ps1')
. (Join-Path $stepsDir '_retry.ps1')
. (Join-Path $stepsDir '_terminal.ps1')
. (Join-Path $stepsDir '_winget.ps1')
. (Join-Path $stepsDir '_pwsh-bootstrap.ps1')
. (Join-Path $stepsDir '_workload.ps1')
$Script:DevConfigAllowUnsigned = [bool]$AllowUnsigned
if ($ApplyTerminalFont) {
. (Join-Path $stepsDir 'fonts.ps1')
$pendingPath = Get-DevConfigPendingTerminalFontPath
if (-not (Test-Path -LiteralPath $pendingPath)) {
Write-Host 'No Terminal font update is pending.'
exit 0
}
$logPath = [IO.Path]::ChangeExtension($pendingPath, '.log')
Start-DevConfigLog -Path $logPath
$failure = $null
try {
Invoke-DevConfigPendingTerminalFont
} catch {
$failure = $_
Write-Host "The Terminal font update failed: $($_.Exception.Message)" -ForegroundColor Red
Write-Host "Full log: $logPath" -ForegroundColor DarkGray
} finally {
Stop-DevConfigLog
}
if ($failure) {
Wait-DevConfigKeyPress -TimeoutSeconds 60
exit 1
}
exit 0
}
# The workload decides which phases run; everything else in this script is shared by every workload.
$Workload = $Workload.ToLowerInvariant()
$Script:DevConfigWorkload = $Workload
try {
$definition = Get-DevConfigWorkload -Directory (Join-Path $PSScriptRoot 'workloads') -Workload $Workload -Action $Action
} catch {
# Pause so the reason stays readable when this runs in a window that closes on exit.
Write-Host $_.Exception.Message -ForegroundColor Red
Wait-DevConfigKeyPress
exit 1
}
$workloadName = $definition['Name']
# TLS is configured before any download step runs.
Enable-DevConfigModernTls
Invoke-DevConfigElevate -ScriptPath $PSCommandPath -NoElevate:$NoElevate -Resumed:$Resumed -AllowUnsigned:$AllowUnsigned -Action $Action -Workload $Workload
if ($Action -eq 'Uninstall') {
Invoke-DevConfigEnsureCleanupShell -ScriptPath $PSCommandPath -AllowUnsigned:$AllowUnsigned -Workload $Workload
} else {
# WinGet module behavior is more consistent in PowerShell 7 than in Windows PowerShell 5.1.
Invoke-DevConfigEnsurePwsh -ScriptPath $PSCommandPath -Resumed:$Resumed -AllowUnsigned:$AllowUnsigned -Action $Action -Workload $Workload
}
# The lock starts after relaunches so the worker process owns the log file.
# Workloads share one lock because they install through the same WinGet and registry paths.
if (-not (Enter-DevConfigSingleInstance)) {
Write-Host ''
Write-Host 'Setup is already running in another window.' -ForegroundColor Yellow
Write-Host 'Switch to it rather than starting a second copy -- they would fight over the same installs.' -ForegroundColor DarkGray
Wait-DevConfigKeyPress
exit 1
}
Start-DevConfigLog -Path (Join-Path $PSScriptRoot "$Workload-log.txt") -Append:$Resumed
# Any prior resume task for this workload is stale once this run starts.
Clear-DevConfigResume
$Script:DevConfigResumed = [bool]$Resumed -and $Action -ne 'Uninstall'
$Script:DevConfigAction = $Action
if ($Script:DevConfigResumed) {
# Restore the pre-reboot tally so the final summary covers the whole run.
Restore-DevConfigTally -Path (Get-DevConfigTallyPath -Directory $PSScriptRoot)
}
$phases = @($definition['Phases'])
$operation = if ($Action -eq 'Uninstall') { 'cleanup' } else { 'setup' }
Write-Host ''
if ($Action -eq 'Uninstall') {
Write-Host "$workloadName cleanup -- resetting settings and removing developer tools" -ForegroundColor Cyan
if ($definition['UninstallWarning']) {
Write-Host $definition['UninstallWarning'] -ForegroundColor Yellow
}
Write-Host 'Some uninstallers may request Administrator approval.' -ForegroundColor DarkGray
} elseif ($Script:DevConfigResumed) {
Write-Host "Welcome back. Resuming $workloadName setup ($Action) after the reboot..." -ForegroundColor Cyan
} else {
$setupNote = if ($definition['SetupNote']) { ", $($definition['SetupNote'])" } else { '' }
Write-Host "$workloadName setup ($Action) -- $($phases.Count) phases$setupNote" -ForegroundColor Cyan
}
$failure = $null
try {
# Load phase files and check function and parameter names before running any phase.
foreach ($phase in $phases) {
$path = Join-Path $stepsDir $phase.File
if (-not (Test-Path -LiteralPath $path)) {
$rerun = "bootstrap.ps1 -Action $Action"
if ($Workload -ne 'devconfig') { $rerun += " -Workload $Workload" }
throw "The $operation script is missing: $path. Run $rerun to reinstall it."
}
. $path
$null = Resolve-DevConfigWorkloadPhase -Phase $phase -OrchestratorPath $PSCommandPath
}
$phaseIndex = 0
foreach ($phase in $phases) {
$phaseIndex++
# Script-scoped phase metadata avoids passing header state through every phase file.
$Script:DevConfigPhaseIndex = $phaseIndex
$Script:DevConfigPhaseTotal = $phases.Count
$Script:DevConfigPhaseTitle = $phase.Title
$Script:DevConfigPhaseHeaderShown = $false
Invoke-DevConfigWorkloadPhase -Phase $phase -OrchestratorPath $PSCommandPath
# New tool locations are visible in this process only after PATH is refreshed.
Update-DevConfigSessionPath
}
Show-DevConfigSilentSkipSummary
Write-Host ''
Write-Host "$workloadName $operation complete." -ForegroundColor Green
$tally = $Script:DevConfigTally
$summaryParts = @("$($tally.Done) changed", "$($tally.AlreadyOk) already up to date")
if ($tally.Warned -gt 0) {
$summaryParts += "$($tally.Warned) flagged"
}
Write-Host " $($summaryParts -join ', ')" -ForegroundColor DarkGray
# Names are shown because the detailed flags may have scrolled off screen.
if ($tally.Warned -gt 0) {
Write-Host " Flagged: $($Script:DevConfigWarnedSteps -join ', ')" -ForegroundColor Yellow
Write-Host ' These were skipped or could not be confirmed. Running this again retries just those.' -ForegroundColor DarkGray
}
# Notes raised by steps come before the workload's standing notes.
foreach ($note in $Script:DevConfigNotes) {
$color = if ($note.Warning) { 'Yellow' } else { 'DarkGray' }
Write-Host " $($note.Message)" -ForegroundColor $color
}
foreach ($note in @($definition['Notes'] | Where-Object { $_ })) {
Write-Host " $note" -ForegroundColor DarkGray
}
} catch {
$failure = $_
}
if ($failure) {
Write-Host ''
Write-Host "$workloadName $operation stopped early." -ForegroundColor Red
Write-Host " $($failure.Exception.Message)" -ForegroundColor Red
$origin = $failure.InvocationInfo
if ($origin -and $origin.ScriptName) {
Write-Host " ($(Split-Path -Leaf $origin.ScriptName) line $($origin.ScriptLineNumber))" -ForegroundColor DarkGray
}
Write-Host ' Nothing already applied was undone -- running this again picks up where it left off.' -ForegroundColor DarkGray
}
$logPath = Get-DevConfigLogPath
if ($logPath) {
Write-Host " Full log: $logPath" -ForegroundColor DarkGray
}
# Close the log before releasing the lock so another run can start while this window waits.
Stop-DevConfigLog
Exit-DevConfigSingleInstance
# The elevated window owns the final pause on both the initial and resumed runs.
Wait-DevConfigKeyPress
if ($failure) {
exit 1
}