-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathupdate-all.ps1
More file actions
635 lines (579 loc) · 19 KB
/
Copy pathupdate-all.ps1
File metadata and controls
635 lines (579 loc) · 19 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
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
# Native Windows Update All Script
# Updates all package managers and tools on Windows (no WSL, no sudo)
# VERSION POLICY: Always updates to LATEST available versions
#
# NOTE: Verbose mode is the default - all package manager output is shown.
# No quiet/silent flags are used.
#
# Usage: .\update-all.ps1 [-SkipPip]
param([switch]$SkipPip)
# Don't treat native command stderr as errors with Stop preference
$PSNativeCommandUseErrorActionPreference = $false
$ErrorActionPreference = 'Continue'
# Colors
function Write-Step { Write-Host "`n[$(Get-Date -Format 'HH:mm:ss')] $args" -ForegroundColor Cyan }
function Write-Success { Write-Host " $args" -ForegroundColor Green }
function Write-Skip { Write-Host " Skipped: $args" -ForegroundColor Yellow }
function Write-Fail { Write-Host " Failed: $args" -ForegroundColor Yellow }
function Write-Info { Write-Host " $args" -ForegroundColor Gray }
# Counters
$script:updated = 0
$script:skipped = 0
$script:failed = 0
# Command exists checker
function Test-Command {
param([string]$Name)
# Special handling for winget (Windows Store wrapper)
if ($Name -eq "winget") {
$wingetPath = Join-Path $env:LOCALAPPDATA "Microsoft\WindowsApps\winget.exe"
if (Test-Path $wingetPath) {
# Verify it actually works by trying to get version
$result = & $wingetPath --version 2>&1
return $LASTEXITCODE -eq 0
}
return $false
}
# Standard command detection
$null = Get-Command $Name -ErrorAction SilentlyContinue
return $?
}
# Run command directly, showing all output
function Invoke-Command {
param(
[string]$Command,
[string]$Name
)
try {
Invoke-Expression $Command
$exitCode = $LASTEXITCODE
if ($exitCode -ne 0) {
Write-Fail $Name
$script:failed++
return $false
}
Write-Success $Name
$script:updated++
return $true
}
catch {
Write-Fail $Name
$script:failed++
return $false
}
}
# Main execution
function Main {
Write-Host "`n========================================" -ForegroundColor Blue
Write-Host " Native Windows Update All" -ForegroundColor Blue
Write-Host "========================================" -ForegroundColor Blue
$startTime = Get-Date
# Check prerequisites
Write-Step "Checking package managers..."
$hasManager = $false
if (Test-Command scoop) {
Write-Success "Scoop"
$hasManager = $true
}
else {
Write-Skip "Scoop not found"
}
if (Test-Command winget) {
Write-Success "winget"
$hasManager = $true
}
else {
Write-Skip "winget not found"
}
if (Test-Command choco) {
Write-Success "Chocolatey"
$hasManager = $true
}
else {
Write-Skip "Chocolatey not found"
}
if ($SkipPip) {
Write-Skip "PIP (skipped by -SkipPip flag)"
}
if (-not $hasManager) {
Write-Host "`nError: No package managers found!" -ForegroundColor Red
Write-Host "Please install Scoop, winget, or Chocolatey" -ForegroundColor Yellow
return
}
# ============================================================================
# SCOOP
# ============================================================================
Write-Step "SCOOP"
if (Test-Command scoop) {
try {
# Update scoop buckets and all apps in one command (same as manual `scoop update -a`)
scoop update -a
Write-Success "scoop"
$script:updated++
}
catch {
Write-Fail "scoop"
$script:failed++
}
}
else {
Write-Skip "Scoop not found"
$script:skipped++
}
# ============================================================================
# WINGET
# ============================================================================
Write-Step "WINGET"
if (Test-Command winget) {
try {
# winget upgrade --all runs interactively with prompts
# Use --accept-source-agreements --accept-package-agreements to auto-accept
# Use full path since winget is in WindowsApps which may not be in PATH
$wingetExe = Join-Path $env:LOCALAPPDATA "Microsoft\WindowsApps\winget.exe"
& $wingetExe upgrade --all --include-unknown --accept-source-agreements --accept-package-agreements
Write-Success "winget"
$script:updated++
}
catch {
Write-Fail "winget"
$script:failed++
}
}
else {
Write-Skip "winget not found"
$script:skipped++
}
# ============================================================================
# CHOCOLATEY
# ============================================================================
Write-Step "CHOCOLATEY"
if (Test-Command choco) {
try {
choco upgrade all -y
Write-Success "Chocolatey"
$script:updated++
}
catch {
Write-Fail "Chocolatey"
$script:failed++
}
}
else {
Write-Skip "Chocolatey not found"
$script:skipped++
}
# ============================================================================
# NPM (Node.js global packages)
# ============================================================================
Write-Step "NPM (Node.js global packages)"
if (Test-Command npm) {
try {
Write-Info "Updating npm itself..."
npm install -g npm@latest
npm update -g
Write-Success "npm"
$script:updated++
}
catch {
Write-Fail "npm"
$script:failed++
}
}
else {
Write-Skip "npm not found"
$script:skipped++
}
# ============================================================================
# PNPM
# ============================================================================
Write-Step "PNPM"
if (Test-Command pnpm) {
try {
pnpm update -g
Write-Success "pnpm"
$script:updated++
}
catch {
Write-Fail "pnpm"
$script:failed++
}
}
else {
Write-Skip "pnpm not found"
$script:skipped++
}
# ============================================================================
# BUN (JavaScript runtime and package manager)
# ============================================================================
Write-Step "BUN"
if (Test-Command bun) {
try {
# First upgrade bun itself
bun upgrade
# Check if there are global packages to update
$globalPackages = bun pm ls -g 2>&1 | Select-String -Pattern "^\w" | Measure-Object
$hasGlobalPackages = $globalPackages.Count -gt 0
if ($hasGlobalPackages) {
# Only run bun update -g if there are global packages
bun update -g
}
Write-Success "bun"
$script:updated++
}
catch {
Write-Fail "bun"
$script:failed++
}
}
else {
Write-Skip "bun not found"
$script:skipped++
}
# ============================================================================
# YARN
# ============================================================================
Write-Step "YARN"
if (Test-Command yarn) {
try {
yarn global upgrade
Write-Success "yarn"
$script:updated++
}
catch {
Write-Fail "yarn"
$script:failed++
}
}
else {
Write-Skip "yarn not found"
$script:skipped++
}
# ============================================================================
# GUP (Go global packages)
# ============================================================================
Write-Step "GUP (Go global packages)"
if (Test-Command gup) {
try {
gup update
Write-Success "gup"
$script:updated++
}
catch {
Write-Fail "gup"
$script:failed++
}
}
else {
Write-Skip "gup not found"
$script:skipped++
}
# ============================================================================
# GO (direct update)
# ============================================================================
Write-Step "GO (update all)"
if (Test-Command go) {
if (-not (Test-Command gup)) {
try {
go install all@latest
Write-Success "go"
$script:updated++
}
catch {
Write-Fail "go"
$script:failed++
}
}
else {
Write-Skip "go (using gup instead)"
$script:skipped++
}
}
else {
Write-Skip "go not found"
$script:skipped++
}
# ============================================================================
# CARGO (Rust packages)
# ============================================================================
Write-Step "CARGO (Rust packages)"
if (Test-Command cargo) {
if (Test-Command cargo-install-update) {
try {
cargo install-update -a
Write-Success "cargo"
$script:updated++
}
catch {
Write-Fail "cargo"
$script:failed++
}
}
else {
Write-Skip "cargo-install-update not found (install: cargo install cargo-update)"
$script:skipped++
}
}
else {
Write-Skip "cargo not found"
$script:skipped++
}
# ============================================================================
# RUSTUP
# ============================================================================
Write-Step "RUSTUP"
if (Test-Command rustup) {
try {
rustup update
Write-Success "rustup"
$script:updated++
}
catch {
Write-Fail "rustup"
$script:failed++
}
}
else {
Write-Skip "rustup not found"
$script:skipped++
}
# ============================================================================
# DOTNET TOOLS
# ============================================================================
Write-Step "DOTNET TOOLS"
if (Test-Command dotnet) {
try {
$tools = dotnet tool list 2>&1 | Select-Object -Skip 2 | Where-Object { $_ -match '^\S+' }
if ($tools) {
foreach ($tool in $tools) {
$toolName = ($tool -split '\s+')[0]
if ($toolName -eq 'Package') { continue }
dotnet tool update $toolName
}
Write-Success "dotnet"
$script:updated++
}
else {
Write-Skip "No dotnet tools installed"
$script:skipped++
}
}
catch {
Write-Fail "dotnet"
$script:failed++
}
}
else {
Write-Skip "dotnet not found"
$script:skipped++
}
# ============================================================================
# PIP (Python packages)
# ============================================================================
if (-not $SkipPip) {
Write-Step "PIP (Python packages)"
if (Test-Command pip) {
try {
# Upgrade pip first using python -m pip (recommended on Windows)
python -m pip install --upgrade pip
# Update all packages using the simple one-liner
pip freeze 2>&1 | ForEach-Object { $_.Split('==')[0] } | ForEach-Object { python -m pip install --upgrade $_ }
Write-Success "pip"
$script:updated++
}
catch {
Write-Fail "pip"
$script:failed++
}
}
else {
Write-Skip "pip not found"
$script:skipped++
}
}
else {
Write-Skip "pip (skipped by -SkipPip flag)"
$script:skipped++
}
# ============================================================================
# POETRY
# ============================================================================
Write-Step "POETRY"
if (Test-Command poetry) {
try {
poetry self update
Write-Success "poetry"
$script:updated++
}
catch {
Write-Fail "poetry"
$script:failed++
}
}
else {
Write-Skip "poetry not found"
$script:skipped++
}
# ============================================================================
# UV (Python package manager)
# ============================================================================
Write-Step "UV"
if (Test-Command uv) {
try {
irm https://astral.sh/uv/install.ps1 | iex
Write-Success "uv"
$script:updated++
}
catch {
Write-Fail "uv"
$script:failed++
}
}
else {
Write-Skip "uv not found"
$script:skipped++
}
# ============================================================================
# CLAUDE CODE CLI
# ============================================================================
Write-Step "CLAUDE CODE CLI"
if (Test-Command claude) {
# Verify claude actually works before trying to update
$claudeWorks = $false
try {
$null = & claude --version 2>&1 | Out-Null
if ($LASTEXITCODE -eq 0) {
$claudeWorks = $true
}
} catch {
# claude command doesn't work
}
if ($claudeWorks) {
# Get version before update
$versionBefore = ""
try {
$versionOutput = claude --version 2>$null
if ($versionOutput -match '(\d+\.\d+\.\d+)') {
$versionBefore = $matches[1]
}
}
catch {
# If version extraction fails, continue anyway
}
# Remove old bun/npm global packages
if (Test-Command bun) {
bun remove -g @anthropic-ai/claude-code 2>$null
}
if (Test-Command npm) {
npm rm -g @anthropic-ai/claude-code 2>$null
}
# Update via native installer (idempotent)
Write-Info "claude-code updating via native installer..."
irm https://claude.ai/install.ps1 | iex
# Ensure native installer bin dir is in PATH
$claudeBin = Join-Path $env:USERPROFILE ".claude\local\bin"
if (Test-Path $claudeBin) {
$envPath = [Environment]::GetEnvironmentVariable("Path", "User")
if ($envPath -notlike "*$claudeBin*") {
[Environment]::SetEnvironmentVariable("Path", "$envPath;$claudeBin", "User")
}
}
# Verify the update worked
$versionAfter = ""
$updateWorks = $false
try {
$null = & claude --version 2>&1 | Out-Null
if ($LASTEXITCODE -eq 0) {
$updateWorks = $true
$versionOutput = claude --version 2>$null
if ($versionOutput -match '(\d+\.\d+\.\d+)') {
$versionAfter = $matches[1]
}
}
}
catch {
# Verification failed
}
if ($updateWorks -and $versionAfter -and $versionAfter -ne $versionBefore) {
Write-Success "claude-code ($versionBefore -> $versionAfter)"
$script:updated++
}
elseif ($updateWorks) {
Write-Skip "claude-code already up to date"
$script:updated++
}
else {
Write-Fail "claude-code (update verification failed)"
$script:failed++
}
}
else {
Write-Fail "claude-code found but not functional"
$script:failed++
}
}
else {
Write-Skip "claude-code not found"
$script:skipped++
}
# ============================================================================
# OPENCODE AI CLI
# ============================================================================
Write-Step "OPENCODE AI CLI"
if (Test-Command bun) {
# Get current version if installed
$currentVersion = ""
if (Test-Command opencode) {
try {
$versionOutput = opencode --version 2>$null
if ($versionOutput -match '(\d+\.\d+\.\d+)') {
$currentVersion = $matches[1]
}
} catch {}
}
# Get latest version from npm registry
$latestVersion = ""
try {
$latestVersion = npm view opencode-ai version 2>$null
} catch {}
if ($currentVersion -and $latestVersion -and $currentVersion -eq $latestVersion) {
Write-Skip "opencode already at latest version ($currentVersion)"
$script:skipped++
}
else {
if ($currentVersion -and $latestVersion) {
Write-Info "opencode update available: $currentVersion -> $latestVersion"
}
# Install/update via bun
$result = bun install -g opencode-ai 2>&1
if ($LASTEXITCODE -eq 0) {
Write-Success "opencode ($currentVersion -> $latestVersion)"
$script:updated++
}
else {
Write-Fail "opencode (bun install failed)"
$script:failed++
}
}
}
else {
Write-Skip "opencode (bun not found)"
$script:skipped++
}
# ============================================================================
# SUMMARY
# ============================================================================
$endTime = Get-Date
$duration = $endTime - $startTime
Write-Host "`n========================================" -ForegroundColor Blue
Write-Host " Summary" -ForegroundColor Blue
Write-Host "========================================" -ForegroundColor Blue
Write-Host " Completed: " -NoNewline; Write-Success $script:updated
Write-Host " Skipped: " -NoNewline; Write-Skip $script:skipped
if ($script:failed -gt 0) {
Write-Host " Failed: " -NoNewline; Write-Fail $script:failed
}
Write-Host " Duration: " -NoNewline; Write-Info "$($duration.TotalSeconds.ToString('F0'))s"
Write-Host "========================================" -ForegroundColor Blue
return
}
# Run main
Main