Repository navigation
Expand file tree
/
Copy pathinstall.ps1
More file actions
470 lines (400 loc) · 18.6 KB
/
Copy pathinstall.ps1
File metadata and controls
470 lines (400 loc) · 18.6 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
[CmdletBinding()]
param(
[Alias("u")]
[switch]$Update,
[switch]$NonInteractive,
[string]$RepoRoot
)
$ErrorActionPreference = "Stop"
$NonInteractive = $NonInteractive -or $env:DOTFILES_NONINTERACTIVE -eq "1"
if ([string]::IsNullOrWhiteSpace($RepoRoot)) {
$RepoRoot = $PSScriptRoot
}
$RepoRoot = (Resolve-Path -LiteralPath $RepoRoot).Path
$managedModulesPath = Join-Path $RepoRoot "scripts\windows\managed-modules.txt"
if (-not (Test-Path -LiteralPath $managedModulesPath -PathType Leaf)) {
throw "The managed PowerShell module list is missing from $RepoRoot."
}
$ManagedModules = @(Get-Content -LiteralPath $managedModulesPath | Where-Object { $_.Trim() -and -not $_.Trim().StartsWith('#') })
$managedAppsPath = Join-Path $RepoRoot "scripts\windows\managed-apps.csv"
if (-not (Test-Path -LiteralPath $managedAppsPath -PathType Leaf)) {
throw "The managed WinGet application catalog is missing from $RepoRoot."
}
$ManagedApps = @(Import-Csv -LiteralPath $managedAppsPath)
if ($ManagedApps.Count -eq 0) {
throw "The managed WinGet application catalog is empty."
}
function Refresh-Path {
$seen = [System.Collections.Generic.HashSet[string]]::new([System.StringComparer]::OrdinalIgnoreCase)
$pathEntries = foreach ($pathValue in @(
$PSHOME
$env:Path
[Environment]::GetEnvironmentVariable("Path", "Machine")
[Environment]::GetEnvironmentVariable("Path", "User")
)) {
if ([string]::IsNullOrWhiteSpace($pathValue)) { continue }
foreach ($entry in $pathValue -split [IO.Path]::PathSeparator) {
$entry = $entry.Trim()
if ($entry -and $seen.Add($entry)) { $entry }
}
}
$env:Path = $pathEntries -join [IO.Path]::PathSeparator
}
function Check-RequiredApps {
if ($PSVersionTable.PSVersion.Major -lt 7) {
throw "This installer requires PowerShell 7 or newer."
}
foreach ($command in @("git", "winget")) {
if (-not (Get-Command -Name $command -ErrorAction SilentlyContinue)) {
throw "$command is not available. Run bootstrap.cmd first."
}
}
}
function Invoke-SigningHelper {
param(
[Parameter(Mandatory)][ValidateSet("ProtectFiles", "ProtectModule")][string]$Action,
[string[]]$Path,
[string]$ModuleName
)
if ($env:DOTFILES_SIGNING_REQUIRED -ne "1" -and (Get-ExecutionPolicy) -ne "AllSigned") {
return
}
$helper = Join-Path $RepoRoot "scripts\windows\signing.ps1"
$bridge = Join-Path $RepoRoot "scripts\windows\invoke-ps-script.cmd"
if (-not (Test-Path -LiteralPath $helper) -or -not (Test-Path -LiteralPath $bridge)) {
throw "The centralized PowerShell signing helper is missing from $RepoRoot."
}
$arguments = @($helper, "-Action", $Action)
if ($Path) { $arguments += @("-Path") + $Path }
if ($ModuleName) { $arguments += @("-ModuleName", $ModuleName) }
& $bridge @arguments
if ($LASTEXITCODE -ne 0) {
throw "The centralized PowerShell signing helper failed for $Action."
}
}
function Save-ManagedModuleForAllSigned {
param([Parameter(Mandatory)][string]$Name)
$windowsPowerShell = Join-Path $env:SystemRoot "System32\WindowsPowerShell\v1.0\powershell.exe"
if (-not (Test-Path -LiteralPath $windowsPowerShell -PathType Leaf)) {
throw "Windows PowerShell is required to provision modules under AllSigned."
}
# Download directly into the PowerShell 7 current-user module root without
# loading PowerShellGet or PackageManagement inside pwsh under AllSigned.
$moduleRoot = Join-Path $HOME "Documents\PowerShell\Modules"
New-Item -ItemType Directory -Path $moduleRoot -Force | Out-Null
$escapedName = $Name.Replace("'", "''")
$escapedRoot = $moduleRoot.Replace("'", "''")
$command = @(
"`$ErrorActionPreference = 'Stop'"
"[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12"
"Save-Module -Name '$escapedName' -Path '$escapedRoot' -Repository PSGallery -Force -AcceptLicense"
) -join [Environment]::NewLine
$encodedCommand = [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($command))
$originalPSModulePath = $env:PSModulePath
try {
$env:PSModulePath = @(
(Join-Path $HOME "Documents\WindowsPowerShell\Modules")
(Join-Path $env:ProgramFiles "WindowsPowerShell\Modules")
(Join-Path $env:SystemRoot "System32\WindowsPowerShell\v1.0\Modules")
) -join [IO.Path]::PathSeparator
& $windowsPowerShell -NoProfile -NonInteractive -EncodedCommand $encodedCommand
if ($LASTEXITCODE -ne 0) {
throw "Windows PowerShell could not save module '$Name' for PowerShell 7 (exit code $LASTEXITCODE)."
}
}
finally {
$env:PSModulePath = $originalPSModulePath
}
}
function Install-WithWinget {
param(
[Parameter(Mandatory)][string]$AppId,
[string]$Alias,
[string]$Scope,
[switch]$Update
)
$installed = if ($Alias) {
$null -ne (Get-Command -Name $Alias -ErrorAction SilentlyContinue)
}
else {
& winget list --id $AppId --exact --source winget --accept-source-agreements *> $null
$LASTEXITCODE -eq 0
}
$scopeArgs = @()
if (-not [string]::IsNullOrWhiteSpace($Scope)) {
$scopeArgs = @("--scope", $Scope)
}
if (-not $installed) {
Write-Host "Installing $AppId..." -ForegroundColor Cyan
& winget install --id $AppId --exact --source winget @scopeArgs --silent --disable-interactivity --accept-source-agreements --accept-package-agreements
if ($LASTEXITCODE -ne 0) { throw "WinGet could not install $AppId in scope '$Scope' (exit code $LASTEXITCODE)." }
return
}
if (-not $Update) {
Write-Host "$AppId is already installed" -ForegroundColor Green
return
}
$upgradeCandidates = @(
& winget list --upgrade-available --id $AppId --exact --source winget --accept-source-agreements 2>&1 |
ForEach-Object { [string]$_ }
)
$upgradeAvailable = @($upgradeCandidates | Where-Object { $_ -match [regex]::Escape($AppId) }).Count -gt 0
if (-not $upgradeAvailable) {
Write-Host "$AppId is already up to date" -ForegroundColor Green
return
}
Write-Host "Updating $AppId..." -ForegroundColor Yellow
& winget upgrade --id $AppId --exact --source winget @scopeArgs --silent --disable-interactivity --accept-source-agreements --accept-package-agreements
if ($LASTEXITCODE -ne 0) { throw "WinGet could not update $AppId (exit code $LASTEXITCODE)." }
}
function Install-MustHaveApps {
Write-Host "Installing baseline apps..." -ForegroundColor Cyan
$coreApps = @($ManagedApps | Where-Object Category -eq "core")
foreach ($app in $coreApps) {
Install-WithWinget -AppId $app.AppId -Alias $app.Alias -Scope $app.Scope -Update:$Update
}
if (-not $NonInteractive) {
foreach ($app in @($ManagedApps | Where-Object Category -eq "workstation")) {
Install-WithWinget -AppId $app.AppId -Alias $app.Alias -Scope $app.Scope -Update:$Update
}
}
else {
Write-Host "Skipping workstation applications in non-interactive mode." -ForegroundColor Yellow
}
Refresh-Path
foreach ($app in $coreApps) {
if ([string]::IsNullOrWhiteSpace($app.Alias)) { continue }
if (-not (Get-Command -Name $app.Alias -ErrorAction SilentlyContinue)) {
throw "Baseline CLI tool '$($app.Alias)' is unavailable after WinGet provisioning."
}
}
if ($Update) {
& mise use -g starship@latest
if ($LASTEXITCODE -ne 0) { throw "mise could not update starship." }
}
else {
& mise which starship *> $null
if ($LASTEXITCODE -ne 0) {
& mise use -g starship@latest
if ($LASTEXITCODE -ne 0) { throw "mise could not install starship." }
}
}
$allSigned = $env:DOTFILES_SIGNING_REQUIRED -eq "1"
$userModuleRoots = @(
(Join-Path $HOME "Documents\PowerShell\Modules"),
(Join-Path $HOME ".local\share\powershell\Modules")
)
foreach ($module in $ManagedModules) {
$installedModule = @(Get-Module -ListAvailable -Name $module | Where-Object {
$moduleBase = [IO.Path]::GetFullPath($_.ModuleBase).TrimEnd([IO.Path]::DirectorySeparatorChar)
@($userModuleRoots | Where-Object {
$root = [IO.Path]::GetFullPath($_).TrimEnd([IO.Path]::DirectorySeparatorChar)
$moduleBase.Equals($root, [StringComparison]::OrdinalIgnoreCase) -or
$moduleBase.StartsWith($root + [IO.Path]::DirectorySeparatorChar, [StringComparison]::OrdinalIgnoreCase)
}).Count -gt 0
} | Select-Object -First 1)
if ($installedModule.Count -eq 0 -or $Update) {
$verb = if ($installedModule.Count -eq 0) { "Installing" } else { "Updating" }
Write-Host "$verb $module module..." -ForegroundColor Cyan
if ($allSigned) {
Save-ManagedModuleForAllSigned -Name $module
}
else {
Install-Module -Name $module -Repository PSGallery -Scope CurrentUser -Force -AllowClobber -AcceptLicense -Confirm:$false
}
}
else {
Write-Host "$module module is already installed" -ForegroundColor Green
}
Invoke-SigningHelper -Action ProtectModule -ModuleName $module
}
}
function Install-OptionalApps {
if ($NonInteractive) {
Write-Host "Skipping optional applications in non-interactive mode." -ForegroundColor Yellow
return
}
$optionalApps = @($ManagedApps | Where-Object Category -eq "optional")
Write-Host " Optionals"
Write-Host "-----------------------------------" -ForegroundColor Cyan
for ($i = 0; $i -lt $optionalApps.Count; $i++) {
Write-Host ("{0}. Install {1}" -f ($i + 1), $optionalApps[$i].Name)
}
Write-Host "You can use ranges like 1-4 or individual numbers separated by commas" -ForegroundColor Yellow
$rawOptions = Read-Host "Select options [e.g. 1-4,8,10]"
$options = @()
foreach ($option in $rawOptions -split ',') {
$option = $option.Trim()
if ($option -match '^(\d+)-(\d+)$' -and [int]$Matches[1] -le [int]$Matches[2]) {
$options += [int]$Matches[1]..[int]$Matches[2]
}
elseif ($option -match '^\d+$') {
$options += [int]$option
}
}
$options = @($options | Where-Object { $_ -gt 0 -and $_ -le $optionalApps.Count } | Select-Object -Unique | Sort-Object)
foreach ($index in $options) {
$app = $optionalApps[$index - 1]
Write-Host "Installing $($app.Name)..." -ForegroundColor Cyan
Install-WithWinget -AppId $app.AppId -Alias $app.Alias -Scope $app.Scope -Update:$Update
}
if ($options.Count -eq 0) { Write-Host "Skipping optional installs..." -ForegroundColor Yellow }
Refresh-Path
}
function Expand-FontArchive {
param(
[Parameter(Mandatory)][string]$Archive,
[Parameter(Mandatory)][string]$Destination
)
$tar = Get-Command tar.exe -ErrorAction SilentlyContinue
if ($null -eq $tar) {
throw "tar.exe is required to extract font archives."
}
Remove-Item -LiteralPath $Destination -Recurse -Force -ErrorAction SilentlyContinue
New-Item -ItemType Directory -Path $Destination -Force | Out-Null
& $tar.Source -xf $Archive -C $Destination
if ($LASTEXITCODE -ne 0) {
throw "Font archive extraction failed for $Archive (exit code $LASTEXITCODE)."
}
}
function Get-ManagedFontPatterns {
$manifest = Join-Path $RepoRoot "scripts\windows\managed-fonts.txt"
if (-not (Test-Path -LiteralPath $manifest -PathType Leaf)) {
throw "The managed font manifest is missing from $RepoRoot."
}
return @(Get-Content -LiteralPath $manifest | Where-Object {
$_.Trim() -and -not $_.Trim().StartsWith("#")
} | ForEach-Object { $_.Trim() })
}
function Download-Fonts {
$fonts = Join-Path $RepoRoot "fonts"
New-Item -ItemType Directory -Force -Path $fonts | Out-Null
if (@(Get-ChildItem -LiteralPath $fonts -Filter "CascadiaCode*.ttf" -File -ErrorAction SilentlyContinue).Count -eq 0) {
$release = Invoke-RestMethod -Uri "https://api.github.com/repos/microsoft/cascadia-code/releases/latest" -Headers @{ "User-Agent" = "PowerShell" }
$asset = @($release.assets | Where-Object name -Match '^CascadiaCode-.*\.zip$' | Select-Object -First 1)
if ($asset.Count -ne 1) {
throw "Unable to locate the Cascadia Code ZIP asset in the latest GitHub release."
}
$archive = Join-Path $fonts "CascadiaCode.zip"
$extract = Join-Path $fonts ".extract-CascadiaCode"
try {
Write-Host "Downloading Cascadia Code..." -ForegroundColor Cyan
Invoke-WebRequest -Uri $asset[0].browser_download_url -OutFile $archive
Expand-FontArchive -Archive $archive -Destination $extract
Remove-Item -Recurse -Force (Join-Path $extract "ttf\static") -ErrorAction SilentlyContinue
$fontFiles = @(Get-ChildItem -LiteralPath $extract -Filter *.ttf -Recurse -File)
if ($fontFiles.Count -eq 0) {
throw "Cascadia Code archive did not contain any TTF files."
}
$fontFiles | Move-Item -Destination $fonts -Force
}
finally {
Remove-Item -LiteralPath $archive -Force -ErrorAction SilentlyContinue
Remove-Item -LiteralPath $extract -Recurse -Force -ErrorAction SilentlyContinue
}
}
foreach ($font in @(
@{ Name = "Caskaydia Cove Nerd Font"; Asset = "CascadiaCode"; Pattern = "CaskaydiaCove*.ttf" },
@{ Name = "Caskaydia Mono Nerd Font"; Asset = "CascadiaMono"; Pattern = "CaskaydiaMono*.ttf" }
)) {
if (@(Get-ChildItem -LiteralPath $fonts -Filter $font.Pattern -File -ErrorAction SilentlyContinue).Count -gt 0) {
continue
}
$archive = Join-Path $fonts "$($font.Asset).tar.xz"
$extract = Join-Path $fonts ".extract-$($font.Asset)"
try {
Write-Host "Downloading $($font.Name)..." -ForegroundColor Cyan
Invoke-WebRequest -Uri "https://github.com/ryanoasis/nerd-fonts/releases/latest/download/$($font.Asset).tar.xz" -OutFile $archive
Expand-FontArchive -Archive $archive -Destination $extract
$fontFiles = @(Get-ChildItem -LiteralPath $extract -Filter *.ttf -Recurse -File)
if ($fontFiles.Count -eq 0) {
throw "$($font.Name) archive did not contain any TTF files."
}
$fontFiles | Move-Item -Destination $fonts -Force
}
finally {
Remove-Item -LiteralPath $archive -Force -ErrorAction SilentlyContinue
Remove-Item -LiteralPath $extract -Recurse -Force -ErrorAction SilentlyContinue
}
}
foreach ($requiredPattern in Get-ManagedFontPatterns) {
if (@(Get-ChildItem -LiteralPath $fonts -Filter $requiredPattern -File -ErrorAction SilentlyContinue).Count -eq 0) {
throw "Expected font files are missing after download: $requiredPattern"
}
}
}
function Install-UserFonts {
$sourceDir = Join-Path $RepoRoot "fonts"
$userFontsDir = Join-Path $env:LOCALAPPDATA "Microsoft\Windows\Fonts"
$fontRegistrySubKey = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts"
if (-not (Test-Path -LiteralPath $sourceDir -PathType Container)) {
throw "Font source directory not found: $sourceDir"
}
New-Item -ItemType Directory -Path $userFontsDir -Force | Out-Null
$sourceFonts = @(Get-ChildItem -Path $sourceDir -Include *.otc,*.otf,*.ttc,*.ttf -Recurse -File | Sort-Object Name -Unique)
if ($sourceFonts.Count -eq 0) {
throw "No font files were downloaded to $sourceDir."
}
$registryKey = [Microsoft.Win32.Registry]::CurrentUser.CreateSubKey($fontRegistrySubKey)
if ($null -eq $registryKey) {
throw "Unable to open the current-user font registry key."
}
try {
foreach ($font in $sourceFonts) {
$destination = Join-Path $userFontsDir $font.Name
$registryName = "$($font.Name) (dotfiles)"
if (-not (Test-Path -LiteralPath $destination -PathType Leaf)) {
Copy-Item -LiteralPath $font.FullName -Destination $destination
}
$registeredPath = $registryKey.GetValue(
$registryName,
$null,
[Microsoft.Win32.RegistryValueOptions]::DoNotExpandEnvironmentNames
)
if ([string]$registeredPath -ne $destination) {
$registryKey.SetValue($registryName, $destination, [Microsoft.Win32.RegistryValueKind]::String)
}
}
}
finally {
$registryKey.Dispose()
}
Write-Host "Installed/registered $($sourceFonts.Count) current-user font files." -ForegroundColor Green
}
function Configure-Git {
$localConfig = Join-Path $HOME ".gitconfig.local"
if (-not (Test-Path -LiteralPath $localConfig)) { New-Item -ItemType File -Path $localConfig | Out-Null }
Write-Host "Git successfully configured!" -ForegroundColor Green
}
function Configure-WindowsTerminal {
$source = Join-Path $RepoRoot "assets\win-terminal\config.json"
$destination = Join-Path $env:LOCALAPPDATA "Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState\settings.json"
if (-not (Test-Path -LiteralPath $destination)) { Write-Host "Windows Terminal settings file not found. Skipping configuration." -ForegroundColor Yellow; return }
if (-not (Get-Command jq -ErrorAction SilentlyContinue)) { Write-Host "jq is unavailable. Skipping Windows Terminal configuration." -ForegroundColor Yellow; return }
& jq --indent 4 --slurpfile src $source '. as $original | $src[0] | to_entries | map(select(.key != "profiles")) | reduce .[] as $item ($original; . * {($item.key): $item.value}) | . * {"profiles": {"defaults": $src[0].profiles.defaults}}' $destination | Set-Content -Path $destination
if ($LASTEXITCODE -ne 0) { throw "Windows Terminal configuration merge failed." }
}
function Configure-Wsl {
if ($NonInteractive) {
Write-Host "Skipping WSL installation in non-interactive mode." -ForegroundColor Yellow
return
}
if (Get-Command wsl -ErrorAction SilentlyContinue) {
Write-Host "Installing WSL..." -ForegroundColor Cyan
& wsl --install --no-distribution
}
}
Refresh-Path
Check-RequiredApps
Download-Fonts
Install-UserFonts
Configure-Git
Install-MustHaveApps
if (-not $NonInteractive) {
Configure-WindowsTerminal
Install-OptionalApps
}
else {
Write-Host "Skipping terminal configuration and optional applications in non-interactive mode." -ForegroundColor Yellow
}
Configure-Wsl