-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathupdate-all.sh
More file actions
executable file
·988 lines (861 loc) · 29.4 KB
/
update-all.sh
File metadata and controls
executable file
·988 lines (861 loc) · 29.4 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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
#!/usr/bin/env bash
# Universal Update All Script - Linux/macOS/Windows (Git Bash)
# Updates all package managers and tools
# On Windows (Git Bash), skips Linux-only package managers that require sudo
#
# NOTE: Verbose mode is the default - all package manager output is shown.
# No quiet/silent flags are used (--quiet, -q, etc.).
#
# Usage: ./update-all.sh [--skip-pip]
# Note: set -e is intentionally NOT used because this script has its own
# error handling via update_fail() and the failed counter. With set -e,
# package manager failures would cause the entire script to exit instead
# of continuing to update other package managers.
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m'
# Detect OS
detect_os() {
case "$(uname -s)" in
Linux*) echo "linux" ;;
Darwin*) echo "macos" ;;
MINGW* | MSYS* | CYGWIN*)
echo "windows"
;;
*) echo "unknown" ;;
esac
}
# Check if running on Windows (Git Bash/MSYS/MINGW/CYGWIN)
is_windows() {
# Note: Do NOT check for /mnt/c/Windows - that exists in WSL too
[[ -n "$MSYSTEM" ]] || [[ "$(uname -s)" =~ (MINGW|MSYS|CYGWIN) ]]
}
# Check if we should use sudo (false on Windows, true on Linux/macOS for system packages)
should_use_sudo() {
# Never use sudo on Windows (even if Windows sudo exists)
if is_windows; then
return 1
fi
return 0
}
# Command exists checker
cmd_exists() {
command -v "$1" >/dev/null 2>&1
}
# Find PowerShell 7+ (pwsh.exe) on Windows, fall back to powershell.exe (5.1)
get_pwsh() {
if ! is_windows; then
echo "pwsh"
return 0
fi
# Try pwsh.exe (PowerShell 7+) first
if command -v pwsh.exe >/dev/null 2>&1; then
echo "pwsh.exe"
return 0
fi
# Fallback to powershell.exe (PowerShell 5.1) - may not work for all scripts
echo "powershell.exe"
return 0
}
# Check if a script-installed tool needs update by comparing with npm registry version
# Usage: script_tool_needs_update <npm_package_name> <installed_version>
# Returns: 0 = needs update, 1 = up to date
script_tool_needs_update() {
local npm_package="$1"
local current_version="$2"
# Get latest version from npm registry
local latest_version
latest_version=$(npm view "$npm_package" version 2>/dev/null)
if [[ -z "$latest_version" ]]; then
# Couldn't determine latest version, assume update needed
return 0
fi
# Compare versions (simple string comparison should work for semantic versioning)
if [[ "$current_version" == "$latest_version" ]]; then
return 1 # Up to date
fi
return 0 # Needs update
}
# Run installer and verify version actually changed
# Usage: install_and_verify_version <install_command> <binary_name> <version_command> [npm_package_name]
# If npm_package_name is provided, verifies against npm registry as external source of truth
# This addresses installers that don't clearly indicate if an update occurred
install_and_verify_version() {
local install_cmd="$1"
local binary_name="$2"
local version_cmd="$3"
local npm_package="${4:-}" # Optional
# Get latest version from npm if package name provided (external source of truth)
local npm_version=""
if [[ -n "$npm_package" ]]; then
npm_version=$(npm view "$npm_package" version 2>/dev/null)
fi
# Get version before install
local version_before
version_before=$(eval "$version_cmd" 2>/dev/null | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -1)
# Skip if already at latest version (based on external npm check)
if [[ -n "$npm_version" ]] && [[ "$version_before" == "$npm_version" ]]; then
update_skip "$binary_name already at latest version ($version_before)"
return 0
fi
# Run the installer
local output
output=$(eval "$install_cmd" 2>&1)
local exit_code=$?
if [ $exit_code -ne 0 ]; then
update_fail "$binary_name" "$output"
return 1
fi
# Get version after install
local version_after
version_after=$(eval "$version_cmd" 2>/dev/null | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -1)
# Verify against npm version if available
if [[ -n "$npm_version" ]] && [[ -n "$version_after" ]] && [[ "$version_after" != "$npm_version" ]]; then
# Binary version doesn't match npm version - possible silent failure
echo -e "${YELLOW}⚠ Warning: Binary version ($version_after) differs from npm version ($npm_version)${NC}"
echo "$output" | grep -vE "^$|npm warn|\[?\[?" | head -20
update_success "$binary_name ($version_before -> $version_after, but npm says $npm_version)"
return 0
fi
# Compare versions
if [[ "$version_before" != "$version_after" ]] && [[ -n "$version_after" ]]; then
echo "$output" | grep -vE "^$|npm warn|\[?\[?" | head -20
update_success "$binary_name ($version_before -> $version_after)"
elif [[ -n "$version_before" ]]; then
update_skip "$binary_name already at latest version ($version_before)"
else
# Couldn't determine versions, show output and mark as updated
echo "$output" | grep -vE "^$|npm warn|\[?\[?" | head -20
update_success "$binary_name"
fi
}
# ============================================================================
# PARSE ARGUMENTS
# ============================================================================
SKIP_PIP=false
for arg in "$@"; do
case "$arg" in
--skip-pip) SKIP_PIP=true ;;
esac
done
# ============================================================================
# LOAD USER CONFIGURATION
# ============================================================================
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Source config library if available
if [[ -f "$SCRIPT_DIR/lib/config.sh" ]]; then
source "$SCRIPT_DIR/lib/config.sh"
CONFIG_FILE="$HOME/.dotfiles.config.yaml"
load_dotfiles_config "$CONFIG_FILE"
# Get config values (only when library is available)
CONFIG_CATEGORIES=$(get_config "categories" "$CONFIG_CATEGORIES")
CONFIG_SKIP_PACKAGES=$(get_config "skip_packages" "$CONFIG_SKIP_PACKAGES")
else
# Defaults if config library not available
CONFIG_EDITOR="nvim"
CONFIG_TERMINAL="wezterm"
CONFIG_THEME="rose-pine"
CONFIG_CATEGORIES="full"
CONFIG_SKIP_PACKAGES=""
fi
# Check if a package should be skipped
should_skip_package() {
local package="$1"
# Parse skip_packages list (space or comma separated)
local skip_list="$CONFIG_SKIP_PACKAGES"
if [[ -n "$skip_list" ]]; then
# Replace commas with spaces for consistency
skip_list="${skip_list//, / }"
for skip_pkg in $skip_list; do
if [[ "$skip_pkg" == "$package" ]]; then
return 0
fi
done
fi
return 1
}
# Counters
updated=0
skipped=0
failed=0
# Helpers
update_section() {
echo -e "\n${CYAN}[$(date '+%H:%M:%S')]${NC} ${BLUE}$1${NC}"
}
update_success() {
local msg="${1:-Done}"
echo -e "${GREEN}✓ $msg${NC}"
((updated++)) || true
}
update_skip() {
echo -e "${YELLOW}⊘ Skipped: $1${NC}"
((skipped++)) || true
}
update_fail() {
local name="$1"
local error_output="$2"
echo -e "${YELLOW}✗ Failed: $name${NC}"
if [[ -n "$error_output" ]]; then
# Show last 10 lines of error output for debugging
echo "$error_output" | tail -10 | sed 's/^/ /'
fi
((failed++)) || true
}
# ============================================================================
# ERROR HANDLING & TIMEOUTS
# ============================================================================
# Check if any package managers are available
check_prerequisites() {
local has_manager=false
echo -e "\n${CYAN}Checking prerequisites...${NC}"
# Check for system package managers (skip on Windows even if WSL is available)
if ! is_windows; then
if cmd_exists apt || cmd_exists dnf || cmd_exists pacman || cmd_exists zypper; then
has_manager=true
echo -e "${GREEN}✓ System package manager found${NC}"
fi
fi
# Check for language package managers
if cmd_exists brew; then
has_manager=true
echo -e "${GREEN}✓ Homebrew found${NC}"
fi
if cmd_exists npm; then
has_manager=true
echo -e "${GREEN}✓ npm found${NC}"
fi
if cmd_exists pip || cmd_exists pip3; then
has_manager=true
echo -e "${GREEN}✓ pip found${NC}"
fi
if cmd_exists go; then
has_manager=true
echo -e "${GREEN}✓ Go found${NC}"
fi
if cmd_exists cargo; then
has_manager=true
echo -e "${GREEN}✓ Cargo found${NC}"
fi
if cmd_exists dotnet; then
has_manager=true
echo -e "${GREEN}✓ dotnet found${NC}"
fi
if [[ "$has_manager" == "false" ]]; then
echo -e "\n${RED}Error: No package managers found!${NC}"
echo -e "${YELLOW}Please install a package manager (apt, brew, npm, etc.)${NC}"
return 1
fi
echo ""
}
# Run command with timeout
# Usage: run_with_timeout <timeout_seconds> <command>
run_with_timeout() {
local timeout="${1:-300}" # Default 5 minutes
local cmd="$2"
local output
if ! command -v timeout >/dev/null 2>&1; then
# If timeout command not available, just run normally
eval "$cmd"
return $?
fi
output=$(timeout $timeout bash -c "$cmd" 2>&1)
local exit_code=$?
if [ $exit_code -eq 124 ]; then
echo -e "${YELLOW}Command timed out after ${timeout}s${NC}"
return 124
fi
echo "$output"
return $exit_code
}
# Update helper: runs command directly, showing all output
update_and_report() {
local cmd="$1"
local name="$2"
# Safety: On Windows, strip sudo from commands to prevent accidental elevation prompts
# This is a defensive measure in case any Linux-only commands slip through guards
if is_windows; then
cmd="${cmd//sudo /}"
fi
eval "$cmd"
local exit_code=$?
if [ $exit_code -ne 0 ]; then
update_fail "$name" ""
return 1
fi
update_success "$name"
return 0
}
# Update helper for pip (handles list and update loop)
update_pip() {
local pip_cmd="$1"
local name="$2"
# Upgrade pip first
$pip_cmd install --upgrade pip
# Update user packages only
while IFS='=' read -r pkg _; do
if [[ -n "$pkg" ]] && [[ ! "$pkg" =~ ^(pip|setuptools|wheel)$ ]]; then
$pip_cmd install --upgrade --user "$pkg"
fi
done < <($pip_cmd list --user --format=freeze 2>/dev/null | grep -v '^(pip|setuptools|wheel)==')
update_success "$name"
}
# Update helper for dotnet tools (handles list and update loop)
update_dotnet_tools() {
while read -r tool; do
if [ -n "$tool" ] && [ "$tool" != "Package" ]; then
dotnet tool update "$tool"
fi
done < <(dotnet tool list 2>/dev/null | tail -n +3 | awk '{print $1}')
update_success "dotnet"
}
# ============================================================================
# MAIN EXECUTION
# ============================================================================
_main() {
OS=$(detect_os)
# Check for available package managers
echo ""
echo -e "${CYAN}Checking package managers...${NC}"
has_manager=false
if [[ "$OS" == "linux" ]]; then
if cmd_exists apt; then
has_manager=true
echo -e "${GREEN}✓ APT (Debian/Ubuntu)${NC}"
else
echo -e "${YELLOW}⊘ APT not found${NC}"
fi
if cmd_exists dnf; then
has_manager=true
echo -e "${GREEN}✓ DNF (Fedora)${NC}"
else
echo -e "${YELLOW}⊘ DNF not found${NC}"
fi
if cmd_exists pacman; then
has_manager=true
echo -e "${GREEN}✓ Pacman (Arch)${NC}"
else
echo -e "${YELLOW}⊘ Pacman not found${NC}"
fi
if cmd_exists zypper; then
has_manager=true
echo -e "${GREEN}✓ Zypper (openSUSE)${NC}"
else
echo -e "${YELLOW}⊘ Zypper not found${NC}"
fi
fi
if cmd_exists brew; then
has_manager=true
echo -e "${GREEN}✓ Homebrew${NC}"
else
echo -e "${YELLOW}⊘ Homebrew not found${NC}"
fi
if cmd_exists snap; then
has_manager=true
echo -e "${GREEN}✓ Snap${NC}"
else
echo -e "${YELLOW}⊘ Snap not found${NC}"
fi
if cmd_exists flatpak; then
has_manager=true
echo -e "${GREEN}✓ Flatpak${NC}"
else
echo -e "${YELLOW}⊘ Flatpak not found${NC}"
fi
if cmd_exists npm; then
has_manager=true
else
echo -e "${YELLOW}⊘ npm not found${NC}"
fi
if [[ "$SKIP_PIP" == "true" ]]; then
echo -e "${YELLOW}⊘ PIP (skipped by --skip-pip flag)${NC}"
fi
if [[ "$has_manager" == "false" ]]; then
echo -e "\n${RED}Error: No package managers found!${NC}"
echo -e "${YELLOW}Please install a package manager (apt, brew, npm, etc.)${NC}"
exit 1
fi
if cmd_exists pip || cmd_exists pip3; then
has_manager=true
else
echo -e "${YELLOW}⊘ pip not found${NC}"
fi
if cmd_exists go; then
has_manager=true
else
echo -e "${YELLOW}⊘ Go not found${NC}"
fi
if cmd_exists cargo; then
has_manager=true
else
echo -e "${YELLOW}⊘ Cargo not found${NC}"
fi
if cmd_exists dotnet; then
has_manager=true
else
echo -e "${YELLOW}⊘ dotnet not found${NC}"
fi
if cmd_exists spin; then
has_manager=true
else
echo -e "${YELLOW}⊘ Spin not found${NC}"
fi
echo ""
echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE} Universal Update All - $OS${NC}"
echo -e "${BLUE}========================================${NC}"
start_time=$(date +%s)
# Run prerequisite checks
check_prerequisites
# ============================================================================
# APT (Debian/Ubuntu) - Skip on Windows (even if WSL is available)
# ============================================================================
if is_windows; then
update_skip "apt (skipped on Windows)"
elif cmd_exists apt; then
update_section "APT (system packages)"
update_and_report "sudo apt update && sudo apt upgrade -y && sudo apt autoremove -y" "apt"
else
update_skip "apt not found"
fi
# ============================================================================
# DNF (Fedora) - Skip on Windows
# ============================================================================
if is_windows; then
update_skip "dnf (skipped on Windows)"
elif cmd_exists dnf; then
update_section "DNF (Fedora packages)"
update_and_report "sudo dnf upgrade -y" "dnf"
else
update_skip "dnf not found"
fi
# ============================================================================
# PACMAN (Arch Linux) - Skip on Windows
# ============================================================================
if is_windows; then
update_skip "pacman (skipped on Windows)"
elif cmd_exists pacman; then
update_section "PACMAN (Arch packages)"
update_and_report "sudo pacman -Syu --noconfirm" "pacman"
else
update_skip "pacman not found"
fi
# ============================================================================
# ZYPPER (openSUSE) - Skip on Windows
# ============================================================================
if is_windows; then
update_skip "zypper (skipped on Windows)"
elif cmd_exists zypper; then
update_section "ZYPPER (openSUSE packages)"
update_and_report "sudo zypper dup -y" "zypper"
else
update_skip "zypper not found"
fi
# ============================================================================
# HOMEBREW
# ============================================================================
if cmd_exists brew; then
update_section "HOMEBREW"
update_and_report "brew update && brew upgrade --greedy && brew cleanup --prune=all" "brew"
else
update_skip "brew not found"
fi
# ============================================================================
# SNAP - Skip on Windows (requires sudo)
# ============================================================================
if is_windows; then
update_skip "snap (skipped on Windows)"
elif cmd_exists snap; then
update_section "SNAP"
update_and_report "sudo snap refresh" "snap"
else
update_skip "snap not found"
fi
# ============================================================================
# FLATPAK
# ============================================================================
if cmd_exists flatpak; then
update_section "FLATPAK"
update_and_report "flatpak update -y && flatpak uninstall --unused -y" "flatpak"
else
update_skip "flatpak not found"
fi
# ============================================================================
# NPM (Node.js global packages)
# ============================================================================
update_section "NPM (Node.js global packages)"
# Skip if in skip list
if should_skip_package "npm"; then
update_skip "npm (in skip list)"
else
if cmd_exists npm; then
# Clean up invalid npm packages (names starting with dot from failed installs)
# These can't be removed via npm uninstall due to invalid names, must delete directly
if is_windows; then
# Multiple possible locations for npm global modules on Windows
local npm_locations=(
"$APPDATA/npm/node_modules"
"$HOME/scoop/persist/nodejs/bin/node_modules"
"$HOME/scoop/apps/nodejs/current/node_modules"
)
local total_invalid_count=0
for npm_global_modules in "${npm_locations[@]}"; do
if [[ -d "$npm_global_modules" ]]; then
local invalid_count=0
# Method 1: Use glob pattern (skip known valid dot dirs)
for pkg_dir in "$npm_global_modules"/.*; do
if [[ -d "$pkg_dir" ]]; then
local pkg_name
pkg_name=$(basename "$pkg_dir")
# Skip valid dot directories
[[ "$pkg_name" == "." || "$pkg_name" == ".." || "$pkg_name" == ".bin" || "$pkg_name" == ".github" || "$pkg_name" == ".modules.yaml" ]] && continue
echo -e " ${YELLOW}Removing invalid package: $pkg_name${NC}"
rm -rf "$pkg_dir" 2>/dev/null || true
((invalid_count++)) || true
fi
done
# Method 2: Also try ls to catch any missed
while IFS= read -r pkg_dir; do
[[ -z "$pkg_dir" ]] && continue
local pkg_name
pkg_name=$(basename "$pkg_dir")
[[ "$pkg_name" == .* ]]
# Skip valid dot directories
[[ "$pkg_name" == "." || "$pkg_name" == ".." || "$pkg_name" == ".bin" || "$pkg_name" == ".github" || "$pkg_name" == ".modules.yaml" ]] && continue
echo -e " ${YELLOW}Removing invalid package: $pkg_name${NC}"
rm -rf "$pkg_dir" 2>/dev/null || true
((invalid_count++)) || true
done < <(ls -d "$npm_global_modules"/.* 2>/dev/null)
((total_invalid_count += invalid_count)) || true
fi
done
if [[ $total_invalid_count -gt 0 ]]; then
echo -e " ${GREEN}Removed $total_invalid_count invalid package(s)${NC}"
fi
else
# Unix: try npm uninstall first, then manual cleanup
local invalid_packages
invalid_packages=$(npm list -g --depth=0 2>/dev/null | grep -oE '\.[a-zA-Z0-9_-]+' || true)
if [[ -n "$invalid_packages" ]]; then
echo -e " ${YELLOW}Cleaning up invalid npm packages...${NC}"
while IFS= read -r pkg; do
[[ -z "$pkg" ]] && continue
# Try npm uninstall first
npm uninstall -g "$pkg" >/dev/null 2>&1 || true
# If still exists, remove from filesystem
local npm_global="${NPM_CONFIG_PREFIX:-$HOME/.npm-global}/lib/node_modules"
if [[ -d "$npm_global/.$pkg" ]]; then
rm -rf "$npm_global/.$pkg" 2>/dev/null || true
fi
done <<< "$invalid_packages"
fi
fi
update_and_report "npm update -g" "npm"
else
update_skip "npm not found"
((skipped++))
fi
fi
# ============================================================================
# YARN (global packages)
# ============================================================================
update_section "YARN (global packages)"
if should_skip_package "yarn"; then
update_skip "yarn (in skip list)"
((skipped++))
else
if cmd_exists yarn; then
update_and_report "yarn global upgrade" "yarn"
else
update_skip "yarn not found"
((skipped++))
fi
fi
# ============================================================================
# PNPM
# ============================================================================
update_section "PNPM (global packages)"
if should_skip_package "pnpm"; then
update_skip "pnpm (in skip list)"
((skipped++))
else
if cmd_exists pnpm; then
update_and_report "pnpm update -g" "pnpm"
else
update_skip "pnpm not found"
((skipped++))
fi
fi
# ============================================================================
# BUN (JavaScript runtime and package manager)
# ============================================================================
update_section "BUN"
if should_skip_package "bun"; then
update_skip "bun (in skip list)"
((skipped++))
else
if cmd_exists bun; then
# First upgrade bun itself
bun_upgrade_output=$(bun upgrade 2>&1)
bun_exit_code=$?
# Check if there are global packages to update
global_packages=$(bun pm ls -g 2>/dev/null | grep -cE "^\w" || true)
if [[ $global_packages -gt 0 ]]; then
# Only run bun update -g if there are global packages
bun_update_output=$(bun update -g 2>&1)
bun_update_exit_code=$?
if [[ $bun_update_exit_code -eq 0 ]]; then
update_success "bun"
else
# bun update -g can fail if no packages need updating
if echo "$bun_update_output" | grep -qiE "No package.json|nothing to update|up to date"; then
update_success "bun (up to date)"
else
update_fail "bun" "$bun_update_output"
fi
fi
else
# No global packages, just report bun upgrade status
if [[ $bun_exit_code -eq 0 ]]; then
update_success "bun"
else
update_fail "bun" "$bun_upgrade_output"
fi
fi
else
update_skip "bun not found"
((skipped++))
fi
fi
# ============================================================================
# GUP (Go global packages)
# ============================================================================
if cmd_exists gup; then
update_section "GUP (Go global packages)"
update_and_report "gup update" "gup"
else
update_skip "gup not found"
fi
# ============================================================================
# GO (direct update)
# ============================================================================
if cmd_exists go && ! cmd_exists gup; then
update_section "GO (update all)"
update_and_report "go install all@latest" "go"
fi
# ============================================================================
# RUSTUP (Rust toolchain)
# ============================================================================
if cmd_exists rustup; then
update_section "RUSTUP (Rust toolchain)"
update_and_report "rustup update" "rustup"
else
update_skip "rustup not found"
fi
# ============================================================================
# CARGO-UPDATE (install if missing)
# ============================================================================
if cmd_exists cargo && ! cmd_exists cargo-install-update; then
update_section "CARGO-UPDATE (installing)"
update_and_report "cargo install cargo-update" "cargo-update"
fi
# ============================================================================
# CARGO (Rust packages)
# ============================================================================
if cmd_exists cargo; then
update_section "CARGO (Rust packages)"
if cmd_exists cargo-install-update; then
update_and_report "cargo install-update -a" "cargo"
else
update_skip "cargo-install-update not found (install: cargo install cargo-update)"
fi
else
update_skip "cargo not found"
fi
# ============================================================================
# DOTNET TOOLS
# ============================================================================
if cmd_exists dotnet; then
update_section "DOTNET TOOLS"
update_dotnet_tools
else
update_skip "dotnet not found"
fi
# ============================================================================
# PIP (Python packages)
# ============================================================================
if [[ "$SKIP_PIP" != "true" ]]; then
if cmd_exists pip; then
update_section "PIP (Python packages)"
update_pip "pip" "pip"
elif cmd_exists pip3; then
update_section "PIP3 (Python packages)"
update_pip "pip3" "pip3"
else
update_skip "pip/pip3 not found"
fi
else
update_skip "pip (skipped by --skip-pip flag)"
fi
# ============================================================================
# POETRY (Python)
# ============================================================================
if cmd_exists poetry; then
update_section "POETRY (Python packages)"
update_and_report "poetry self update" "poetry"
else
update_skip "poetry not found"
fi
# ============================================================================
# UV (Python package manager)
# ============================================================================
if cmd_exists uv; then
update_section "UV (Python package manager)"
update_and_report "uv self update" "uv"
else
update_skip "uv not found"
fi
# ============================================================================
# COMPOSER (PHP packages)
# ============================================================================
if cmd_exists composer; then
update_section "COMPOSER (PHP global packages)"
update_and_report "composer global update" "composer"
else
update_skip "composer not found"
fi
# ============================================================================
# SPIN (Rust toolchain alternative)
# ============================================================================
if cmd_exists spin; then
update_section "SPIN"
update_and_report "spin upgrade" "spin"
else
update_skip "spin not found"
fi
# ============================================================================
# CLAUDE CODE CLI
# ============================================================================
if cmd_exists claude; then
update_section "CLAUDE CODE CLI"
if is_windows; then
# Use bun on Windows (npm is deprecated, native installer has bugs)
if cmd_exists bun; then
# Remove old npm package if present, then install/update via bun
bun pm rm -g @anthropic-ai/claude-code 2>/dev/null || true
bun add -g @anthropic-ai/claude-code
update_success "claude-code (updated via bun)"
else
update_skip "bun not found, required for Claude Code updates on Windows"
fi
else
# Use bash script on Unix-like systems
install_and_verify_version "curl -fsSL https://claude.ai/install.sh | bash" "claude-code" "claude --version" "@anthropic-ai/claude-code"
fi
else
update_skip "claude-code not found"
fi
# ============================================================================
# OPENCODE AI CLI
# ============================================================================
update_section "OPENCODE AI CLI"
# The bash installer installs to $HOME/.opencode/bin/opencode
local opencode_exe="$HOME/.opencode/bin/opencode"
# First, clean up any old npm shims that might shadow the official binary
# This prevents confusion where `opencode --version` returns old version
local npm_bin npm_bin_alt
if [[ -d "$NPM_CONFIG_PREFIX" ]]; then
npm_bin="$NPM_CONFIG_PREFIX/bin"
else
npm_bin="$HOME/.npm-global/bin"
fi
# Also check standard npm location on Windows via Git Bash
if [[ -n "$APPDATA" ]]; then
npm_bin_alt="$APPDATA/npm"
fi
for shim_loc in "$npm_bin" "$npm_bin_alt"; do
[[ -d "$shim_loc" ]] || continue
for shim in "$shim_loc"/opencode "$shim_loc"/opencode.cmd; do
if [[ -f "$shim" ]]; then
log_info "Removing old npm shim: $(basename "$shim")"
rm -f "$shim" 2>/dev/null || true
fi
done
done
# Also remove npm/bun-installed opencode packages
if cmd_exists npm; then
npm uninstall -g opencode-ai 2>/dev/null || true
fi
if cmd_exists bun; then
bun pm rm -g opencode-ai 2>/dev/null || true
fi
# Check if opencode binary exists at the official installer location
if [[ -f "$opencode_exe" ]]; then
# Get current version by running the binary directly (not via PATH)
local version_before
version_before=$("$opencode_exe" --version 2>/dev/null | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -1)
# Get latest version from npm registry
local latest_version
latest_version=$(npm view opencode-ai version 2>/dev/null)
# Skip if already at latest
if [[ -n "$latest_version" ]] && [[ "$version_before" == "$latest_version" ]]; then
update_skip "opencode already at latest version ($version_before)"
else
# Stop any running opencode processes (Windows can't replace running executables)
if [[ "$OS" == "windows" ]] || [[ -n "$APPDATA" ]]; then
if cmd_exists taskkill; then
taskkill //F //IM opencode.exe 2>/dev/null || true
sleep 0.5
fi
elif cmd_exists pkill; then
pkill -x opencode 2>/dev/null || true
sleep 0.5
fi
# Run the official installer via bash
local output
output=$(curl -fsSL https://opencode.ai/install | bash 2>&1)
local exit_code=$?
if [[ $exit_code -eq 0 ]] && [[ -f "$opencode_exe" ]]; then
# Get version after update from the newly installed binary
local version_after
version_after=$("$opencode_exe" --version 2>/dev/null | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -1)
if [[ -n "$version_after" ]] && [[ "$version_before" != "$version_after" ]]; then
update_success "opencode ($version_before -> $version_after)"
elif [[ -n "$version_after" ]]; then
update_success "opencode (up to date at $version_after)"
else
update_success "opencode (installer completed)"
fi
else
update_fail "opencode" "$output"
fi
fi
else
update_skip "opencode not found at $opencode_exe"
fi
# ============================================================================
# SUMMARY
# ============================================================================
end_time=$(date +%s)
duration=$((end_time - start_time))
minutes=$((duration / 60))
seconds=$((duration % 60))
echo -e "\n${BLUE}========================================${NC}"
echo -e "${BLUE} Summary${NC}"
echo -e "${BLUE}========================================${NC}"
echo -e " ${GREEN}Completed:${NC} $updated"
echo -e " ${YELLOW}Skipped:${NC} $skipped"
if [ $failed -gt 0 ]; then
echo -e " ${YELLOW}Failed:${NC} $failed"
fi
echo -e " ${CYAN}Duration:${NC} ${minutes}m ${seconds}s"
echo -e "${BLUE}========================================${NC}"
exit 0
}
# Only run main if script is executed (not sourced)
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
_main
fi