-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathinstall.ps1
More file actions
602 lines (502 loc) · 19.7 KB
/
Copy pathinstall.ps1
File metadata and controls
602 lines (502 loc) · 19.7 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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
# Github: https://github.com/Karmenzind/dotfiles-and-scripts
param(
[ValidateSet("1", "2", "3", "4", "5", "q")]
[string]$Action
)
$ErrorActionPreference = "Stop"
Set-StrictMode -Version Latest
$script:RepoRoot = Split-Path -Parent $MyInvocation.MyCommand.Path
$script:IsAdmin = if ($IsWindows) {
$principal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
$principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
} else {
$false
}
function Write-Title {
param([string]$Message)
Write-Host ""
Write-Host "== $Message ==" -ForegroundColor Magenta
}
function Write-Step {
param([string]$Message)
Write-Host "🔎 $Message" -ForegroundColor Cyan
}
function Write-Install {
param([string]$Message)
Write-Host "📦 $Message" -ForegroundColor Blue
}
function Write-Ok {
param([string]$Message)
Write-Host "✅ $Message" -ForegroundColor Green
}
function Write-Warn {
param([string]$Message)
Write-Host "⚠️ $Message" -ForegroundColor Yellow
}
function Write-Fail {
param([string]$Message)
Write-Host "❌ $Message" -ForegroundColor Red
}
function Test-Command {
param([Parameter(Mandatory)][string]$Name)
$null -ne (Get-Command -Name $Name -ErrorAction SilentlyContinue)
}
function Invoke-SelfElevated {
param([Parameter(Mandatory)][string]$SelectedAction)
if ($script:IsAdmin) {
return $false
}
Write-Warn "This action needs an elevated PowerShell session. Requesting administrator permission..."
$args = @(
"-NoProfile",
"-ExecutionPolicy", "Bypass",
"-File", "`"$PSCommandPath`"",
"-Action", $SelectedAction
)
Start-Process -FilePath "pwsh.exe" -ArgumentList $args -Verb RunAs -Wait
return $true
}
function Invoke-LoggedCommand {
param(
[Parameter(Mandatory)][string]$FilePath,
[Parameter(Mandatory)][string[]]$ArgumentList,
[Parameter(Mandatory)][string]$Label
)
Write-Install $Label
& $FilePath @ArgumentList
if ($LASTEXITCODE -ne 0) {
throw "$Label failed with exit code $LASTEXITCODE."
}
}
function Test-WingetPackageInstalled {
param(
[Parameter(Mandatory)][string]$Id,
[string]$Source
)
if (-not (Test-Command winget)) {
return $false
}
$args = @("list", "--id", $Id, "--exact", "--disable-interactivity", "--accept-source-agreements")
if ($Source) {
$args += @("--source", $Source)
}
$output = & winget @args 2>$null
return ($LASTEXITCODE -eq 0) -and (($output -join "`n") -match [regex]::Escape($Id))
}
function Ensure-WingetPackage {
param(
[Parameter(Mandatory)][string]$Name,
[Parameter(Mandatory)][string]$Id,
[string]$Source = "winget",
[string]$FallbackChocoId
)
if (Test-WingetPackageInstalled -Id $Id -Source $Source) {
Write-Ok "$Name is already installed."
return $true
}
if (-not (Test-Command winget)) {
Write-Warn "winget is not available."
} else {
try {
$args = @(
"install",
"--id", $Id,
"--exact",
"--source", $Source,
"--accept-package-agreements",
"--accept-source-agreements",
"--disable-interactivity"
)
Invoke-LoggedCommand -FilePath "winget" -ArgumentList $args -Label "Installing $Name with winget..."
Write-Ok "$Name installed."
return $true
} catch {
Write-Warn $_.Exception.Message
}
}
if ($FallbackChocoId) {
return Ensure-ChocoPackageFallback -Name $Name -Id $FallbackChocoId
}
Write-Fail "$Name was not installed."
return $false
}
function Ensure-ChocoPackageFallback {
param(
[Parameter(Mandatory)][string]$Name,
[Parameter(Mandatory)][string]$Id
)
if (-not (Test-Command choco)) {
Write-Warn "Chocolatey is not available. Skipping fallback for $Name."
return $false
}
$installed = (& choco list --local-only --limit-output 2>$null) -match "^$([regex]::Escape($Id))\|"
if ($installed) {
Write-Ok "$Name is already installed by Chocolatey."
return $true
}
try {
Invoke-LoggedCommand -FilePath "choco" -ArgumentList @("install", "-y", $Id) -Label "Installing $Name with Chocolatey fallback..."
Write-Ok "$Name installed."
return $true
} catch {
Write-Fail $_.Exception.Message
return $false
}
}
function Ensure-PwshModule {
param([Parameter(Mandatory)][string]$Name)
if (Get-Module -ListAvailable -Name $Name) {
Write-Ok "PowerShell module $Name is already installed."
return
}
Write-Install "Installing PowerShell module $Name..."
Install-Module -Name $Name -Scope CurrentUser -Repository PSGallery -Force -AllowClobber
Write-Ok "PowerShell module $Name installed."
}
function Ensure-UvTool {
param(
[Parameter(Mandatory)][string]$Name,
[string]$Package = $Name
)
if (-not (Test-Command uv)) {
Write-Warn "uv is not available. Skipping Python tool $Name."
return
}
$tools = & uv tool list 2>$null
if (($tools -join "`n") -match "(?m)^$([regex]::Escape($Name))\s") {
Write-Ok "uv tool $Name is already installed."
return
}
Invoke-LoggedCommand -FilePath "uv" -ArgumentList @("tool", "install", $Package) -Label "Installing uv tool $Package..."
Write-Ok "uv tool $Package installed."
}
function Ensure-PnpmGlobalPackage {
param(
[Parameter(Mandatory)][string]$Command,
[Parameter(Mandatory)][string]$Package
)
if (Test-Command $Command) {
Write-Ok "$Command is already available."
return
}
if (-not (Test-Command pnpm)) {
Write-Warn "pnpm is not available. Skipping $Package."
return
}
Invoke-LoggedCommand -FilePath "pnpm" -ArgumentList @("add", "-g", $Package) -Label "Installing $Package with pnpm..."
Write-Ok "$Package installed."
}
function Ensure-FnmNodeLts {
Ensure-WingetPackage -Name "Fast Node Manager" -Id "Schniz.fnm" | Out-Null
if (-not (Test-Command fnm)) {
Write-Warn "fnm is not available after installation. Open a new shell and run this script again."
return
}
Invoke-LoggedCommand -FilePath "fnm" -ArgumentList @("install", "--lts") -Label "Installing Node.js LTS with fnm..."
Invoke-LoggedCommand -FilePath "fnm" -ArgumentList @("default", "lts-latest") -Label "Setting Node.js LTS as the fnm default..."
$fnmEnv = & fnm env --shell powershell
$fnmEnv | Invoke-Expression
Write-Ok "Node.js LTS is managed by fnm."
}
function Enable-CorepackPnpm {
if (-not (Test-Command corepack)) {
Write-Warn "corepack is not available. Ensure Node.js is active through fnm, then rerun this step."
return
}
Invoke-LoggedCommand -FilePath "corepack" -ArgumentList @("enable") -Label "Enabling corepack..."
Invoke-LoggedCommand -FilePath "corepack" -ArgumentList @("prepare", "pnpm@latest", "--activate") -Label "Activating latest pnpm through corepack..."
Write-Ok "pnpm is managed by corepack."
}
function Set-UserEnvironment {
Write-Step "Configuring user environment variables..."
[Environment]::SetEnvironmentVariable("EDITOR", "nvim.exe", "User")
[Environment]::SetEnvironmentVariable("VISUAL", "nvim.exe", "User")
[Environment]::SetEnvironmentVariable("SHELL", "pwsh.exe", "User")
$env:EDITOR = "nvim.exe"
$env:VISUAL = "nvim.exe"
$env:SHELL = "pwsh.exe"
Write-Ok "User environment variables configured."
}
function Set-WindowsTerminalDefaultPwsh {
Write-Step "Configuring Windows Terminal default profile..."
$settingsPaths = @(
Join-Path $env:LOCALAPPDATA "Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState\settings.json",
Join-Path $env:LOCALAPPDATA "Packages\Microsoft.WindowsTerminalPreview_8wekyb3d8bbwe\LocalState\settings.json"
)
foreach ($path in $settingsPaths) {
if (-not (Test-Path $path)) {
continue
}
try {
$settings = Get-Content -Raw -Path $path | ConvertFrom-Json
$profiles = @($settings.profiles.list)
$pwshProfile = $profiles | Where-Object {
$commandlineProperty = $_.PSObject.Properties["commandline"]
$nameProperty = $_.PSObject.Properties["name"]
$commandline = if ($commandlineProperty) { [string]$commandlineProperty.Value } else { "" }
$profileName = if ($nameProperty) { [string]$nameProperty.Value } else { "" }
($commandline -match "(^|\\)pwsh(\.exe)?(\s|$)") -or ($profileName -match "PowerShell")
} | Select-Object -First 1
if (-not $pwshProfile) {
Write-Warn "No pwsh profile found in $path."
continue
}
$guidProperty = $pwshProfile.PSObject.Properties["guid"]
$nameProperty = $pwshProfile.PSObject.Properties["name"]
if (-not $guidProperty) {
Write-Warn "The pwsh profile in $path does not have a guid."
continue
}
$settings.defaultProfile = $guidProperty.Value
$settings | ConvertTo-Json -Depth 100 | Set-Content -Path $path -Encoding UTF8
$profileName = if ($nameProperty) { $nameProperty.Value } else { $guidProperty.Value }
Write-Ok "Windows Terminal default profile set to $profileName."
} catch {
Write-Warn "Failed to update Windows Terminal settings at $path`: $($_.Exception.Message)"
}
}
}
function Get-RegisteredFileAssociations {
param([Parameter(Mandatory)][string]$CapabilityPath)
$key = "HKLM:\$CapabilityPath\FileAssociations"
if (-not (Test-Path $key)) {
$key = "HKCU:\$CapabilityPath\FileAssociations"
}
if (-not (Test-Path $key)) {
return @{}
}
$props = Get-ItemProperty -Path $key
$result = @{}
foreach ($prop in $props.PSObject.Properties) {
if ($prop.Name -like "PS*") {
continue
}
$result[$prop.Name] = [string]$prop.Value
}
return $result
}
function Set-MediaDefaultsBestEffort {
Write-Step "Configuring default media apps best-effort..."
$registered = Get-ItemProperty "HKLM:\Software\RegisteredApplications" -ErrorAction SilentlyContinue
$potProperty = if ($registered) { $registered.PSObject.Properties["PotPlayerMini64"] } else { $null }
$foobarProperty = if ($registered) { $registered.PSObject.Properties["foobar2000"] } else { $null }
$potCapability = if ($potProperty) { [string]$potProperty.Value } else { $null }
$foobarCapability = if ($foobarProperty) { [string]$foobarProperty.Value } else { $null }
if (-not $potCapability -or -not $foobarCapability) {
Write-Warn "PotPlayer or foobar2000 registration was not found. Opening Default apps settings."
Start-Process "ms-settings:defaultapps"
return
}
$videoExts = @(".mp4", ".mkv", ".avi", ".mov", ".wmv", ".flv", ".webm", ".m4v")
$audioExts = @(".mp3", ".flac", ".wav", ".m4a", ".aac", ".ogg", ".opus", ".wma")
$potAssociations = Get-RegisteredFileAssociations -CapabilityPath $potCapability
$foobarAssociations = Get-RegisteredFileAssociations -CapabilityPath $foobarCapability
$rows = New-Object System.Collections.Generic.List[string]
foreach ($ext in $videoExts) {
if ($potAssociations.ContainsKey($ext)) {
$rows.Add(" <Association Identifier=`"$ext`" ProgId=`"$($potAssociations[$ext])`" ApplicationName=`"PotPlayer`" />")
}
}
foreach ($ext in $audioExts) {
if ($foobarAssociations.ContainsKey($ext)) {
$rows.Add(" <Association Identifier=`"$ext`" ProgId=`"$($foobarAssociations[$ext])`" ApplicationName=`"foobar2000`" />")
}
}
if ($rows.Count -eq 0) {
Write-Warn "No media associations were discovered. Opening Default apps settings."
Start-Process "ms-settings:defaultapps"
return
}
$xmlPath = Join-Path $env:TEMP "dotfiles-media-defaults.xml"
$xml = @(
'<?xml version="1.0" encoding="UTF-8"?>',
'<DefaultAssociations>',
$rows,
'</DefaultAssociations>'
)
$xml | Set-Content -Path $xmlPath -Encoding UTF8
if ($script:IsAdmin) {
try {
Invoke-LoggedCommand -FilePath "dism.exe" -ArgumentList @("/Online", "/Import-DefaultAppAssociations:$xmlPath") -Label "Importing default app associations..."
Write-Ok "Default app associations were imported. Windows may still require confirmation for the current user."
} catch {
Write-Warn $_.Exception.Message
}
} else {
Write-Warn "Default association import requires an elevated shell."
}
Write-Warn "If the defaults did not change, choose PotPlayer for video and foobar2000 for audio in Windows Settings."
Start-Process "ms-settings:defaultapps"
}
function Install-MonacoNerdFont {
Write-Step "Checking Monaco Nerd Font..."
if (-not (Test-Command git)) {
Write-Warn "git is not available. Skipping font installation."
return
}
$fontsDlDir = Join-Path $env:USERPROFILE "Downloads\fonts"
$monacoDir = Join-Path $fontsDlDir "monaco-nerd"
$fontSourceDir = Join-Path $monacoDir "fonts"
New-Item -ItemType Directory -Force -Path $fontsDlDir | Out-Null
if (-not (Test-Path $monacoDir)) {
Invoke-LoggedCommand -FilePath "git" -ArgumentList @("clone", "https://github.com/Karmenzind/monaco-nerd-fonts", $monacoDir) -Label "Downloading Monaco Nerd Font..."
}
if (-not (Test-Path $fontSourceDir)) {
Write-Warn "Font source folder not found: $fontSourceDir"
return
}
$shell = New-Object -ComObject Shell.Application
$fontsFolder = $shell.Namespace(0x14)
foreach ($font in Get-ChildItem -Path $fontSourceDir -File) {
$target = Join-Path "C:\Windows\Fonts" $font.Name
if (Test-Path $target) {
Write-Ok "$($font.Name) is already installed."
continue
}
Write-Install "Installing font $($font.Name)..."
$fontsFolder.CopyHere($font.FullName)
}
}
function Install-PackageSet {
param([Parameter(Mandatory)][array]$Packages)
foreach ($package in $Packages) {
Ensure-WingetPackage @package | Out-Null
}
}
$script:CommonPackages = @(
@{ Name = "Google Chrome"; Id = "Google.Chrome" },
@{ Name = "Windows Terminal"; Id = "Microsoft.WindowsTerminal" },
@{ Name = "Microsoft PowerToys"; Id = "Microsoft.PowerToys" },
@{ Name = "CCleaner"; Id = "Piriform.CCleaner" },
@{ Name = "Neovim"; Id = "Neovim.Neovim"; FallbackChocoId = "neovim" },
@{ Name = "Spotify"; Id = "Spotify.Spotify" },
@{ Name = "7-Zip"; Id = "7zip.7zip" },
@{ Name = "Bitwarden"; Id = "Bitwarden.Bitwarden" },
@{ Name = "ChatGPT"; Id = "9NT1R1C2HH7J"; Source = "msstore" },
@{ Name = "CPUID CPU-Z"; Id = "CPUID.CPU-Z" },
@{ Name = "FFmpeg"; Id = "Gyan.FFmpeg"; FallbackChocoId = "ffmpeg" },
@{ Name = "ImageMagick"; Id = "ImageMagick.ImageMagick" },
@{ Name = "Yazi"; Id = "sxyazi.yazi" },
@{ Name = "RMUX"; Id = "Helvesec.RMUX" },
@{ Name = "Git"; Id = "Git.Git" },
@{ Name = "Codex GUI app"; Id = "9PLM9XGG6VKS"; Source = "msstore" }
)
$script:DevPackages = @(
@{ Name = "PowerShell"; Id = "Microsoft.PowerShell" },
@{ Name = "uv"; Id = "astral-sh.uv" },
@{ Name = "Oh My Posh"; Id = "JanDeDobbeleer.OhMyPosh" },
@{ Name = "fzf"; Id = "junegunn.fzf" },
@{ Name = "fd"; Id = "sharkdp.fd" },
@{ Name = "bat"; Id = "sharkdp.bat" },
@{ Name = "ripgrep"; Id = "BurntSushi.ripgrep.MSVC" },
@{ Name = "jq"; Id = "jqlang.jq" },
@{ Name = "zoxide"; Id = "ajeetdsouza.zoxide" },
@{ Name = "lazygit"; Id = "JesseDuffield.lazygit" },
@{ Name = "Lua"; Id = "DEVCOM.Lua" },
@{ Name = "clangd"; Id = "LLVM.clangd" },
@{ Name = "Go"; Id = "GoLang.Go" },
@{ Name = "Temurin JDK 17"; Id = "EclipseAdoptium.Temurin.17.JDK" },
@{ Name = "Vim"; Id = "vim.vim" },
@{ Name = "universal-ctags"; Id = "UniversalCtags.Ctags"; FallbackChocoId = "universal-ctags" },
@{ Name = "StyLua"; Id = "JohnnyMorganz.StyLua"; FallbackChocoId = "stylua" },
@{ Name = "Neovide"; Id = "Neovide.Neovide"; FallbackChocoId = "neovide" },
@{ Name = "lf"; Id = "gokcehan.lf" },
@{ Name = "Typora"; Id = "appmakes.Typora" },
@{ Name = "Starship"; Id = "Starship.Starship" }
)
$script:MediaPackages = @(
@{ Name = "PotPlayer"; Id = "Daum.PotPlayer" },
@{ Name = "foobar2000"; Id = "PeterPawlowski.foobar2000" }
)
$script:PwshModules = @("PSReadLine", "Terminal-Icons", "PSCompletions", "PsFZF")
$script:UvTools = @(
@{ Name = "ipython"; Package = "ipython" },
@{ Name = "debugpy"; Package = "debugpy" },
@{ Name = "black"; Package = "black" },
@{ Name = "isort"; Package = "isort" },
@{ Name = "autoflake"; Package = "autoflake" },
@{ Name = "ruff"; Package = "ruff" },
@{ Name = "pgcli"; Package = "pgcli" },
@{ Name = "markdown-live-preview"; Package = "markdown-live-preview" },
@{ Name = "pynvim"; Package = "pynvim" }
)
function Install-CommonBaseline {
Write-Title "Common baseline apps"
Install-PackageSet -Packages $script:CommonPackages
Set-UserEnvironment
}
function Install-DevelopmentEnvironment {
Install-CommonBaseline
Write-Title "Development environment"
Install-PackageSet -Packages $script:DevPackages
Ensure-FnmNodeLts
Enable-CorepackPnpm
foreach ($module in $script:PwshModules) {
Ensure-PwshModule -Name $module
}
foreach ($tool in $script:UvTools) {
Ensure-UvTool @tool
}
Install-MonacoNerdFont
Set-WindowsTerminalDefaultPwsh
}
function Install-PersonalMediaEnvironment {
Install-CommonBaseline
Write-Title "Personal and media environment"
Install-PackageSet -Packages $script:MediaPackages
Set-MediaDefaultsBestEffort
}
function Configure-DefaultsOnly {
Write-Title "Default configuration"
Set-UserEnvironment
Set-WindowsTerminalDefaultPwsh
Set-MediaDefaultsBestEffort
}
function Install-SystemHealthMonitorTask {
Write-Title "System health monitor"
$installer = Join-Path $script:RepoRoot "scripts\windows\system-health\Install-SystemHealthMonitor.ps1"
& $installer -SkipElevation
Write-Ok "System health monitor installed or updated."
}
function Show-Menu {
Write-Host ""
Write-Host "What do you want to do? (default: 1)" -ForegroundColor Cyan
Write-Host ""
Write-Host "1) 🧑💻 Install development environment"
Write-Host "2) 🎧 Install personal/media environment"
Write-Host "3) 🧰 Install common baseline apps only"
Write-Host "4) ⚙️ Configure defaults only"
Write-Host "5) 🩺 Install/update system health monitor"
Write-Host "q) Quit"
Write-Host ""
}
function Invoke-Main {
if (-not $IsWindows) {
Write-Fail "This script only supports Windows."
return
}
if ($Action) {
$answer = $Action
} else {
Show-Menu
$answer = Read-Host "Select an option"
}
if ([string]::IsNullOrWhiteSpace($answer)) {
$answer = "1"
}
if (($answer -ne "q") -and ($answer -in @("1", "2", "3", "4", "5")) -and (Invoke-SelfElevated -SelectedAction $answer)) {
return
}
switch ($answer.Trim().ToLowerInvariant()) {
"1" { Install-DevelopmentEnvironment }
"2" { Install-PersonalMediaEnvironment }
"3" { Install-CommonBaseline }
"4" { Configure-DefaultsOnly }
"5" { Install-SystemHealthMonitorTask }
"q" { Write-Warn "No action selected." }
default { Write-Fail "Invalid option: $answer" }
}
Write-Host ""
Write-Ok "Done."
}
Invoke-Main