-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev-tools.sh
More file actions
executable file
·1007 lines (908 loc) · 35.4 KB
/
dev-tools.sh
File metadata and controls
executable file
·1007 lines (908 loc) · 35.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
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env zsh
# macOS Development Tools Installation Script
# Installs language package managers, version managers, and language runtimes
#
# Usage:
# ./dev-tools.sh # Interactive installation
# ./dev-tools.sh check # Check what would be installed (dry-run)
# ./dev-tools.sh test # Test detection of all tools
set +e # Allow optional components to fail
# Concurrent-run protection
LOCK_FILE="/tmp/macos-dev-setup-devtools.lock"
_acquire_lock() {
if [[ -f "$LOCK_FILE" ]]; then
local lock_pid=""
lock_pid="$(<"$LOCK_FILE")"
if [[ -n "$lock_pid" ]] && kill -0 "$lock_pid" 2>/dev/null; then
echo "ERROR: Another instance of dev-tools.sh is already running (PID $lock_pid)"
echo " If this is a mistake, remove the lock file: rm $LOCK_FILE"
exit 1
fi
rm -f "$LOCK_FILE"
fi
echo $$ > "$LOCK_FILE"
trap 'rm -f "$LOCK_FILE"' EXIT INT TERM HUP
}
_acquire_lock
# Ensure standard Unix tools are in PATH
export PATH="/usr/bin:/bin:/usr/sbin:/sbin:$PATH"
# Check for check/test mode
CHECK_MODE=false
TEST_MODE=false
if [[ "${1:-}" == "check" ]]; then
CHECK_MODE=true
elif [[ "${1:-}" == "test" ]]; then
TEST_MODE=true
fi
if [[ "$TEST_MODE" == false ]] && [[ "$CHECK_MODE" == false ]]; then
echo "🛠️ macOS Development Tools Installation"
echo "=========================================="
echo ""
elif [[ "$TEST_MODE" == true ]]; then
echo "🧪 Testing Tool Detection"
echo "=========================="
echo ""
elif [[ "$CHECK_MODE" == true ]]; then
echo "🔍 Checking Installed Tools (Dry Run)"
echo "======================================"
echo ""
fi
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
install_warnings=0
warn() {
((install_warnings++))
echo "${YELLOW}⚠️ $1${NC}"
}
# Ask user for confirmation with input validation
_ask_user() {
local prompt="$1"
local default="${2:-N}"
# Validate inputs
[[ -z "$prompt" ]] && { echo "${RED}Error: _ask_user called without prompt${NC}" >&2; return 1; }
[[ "$default" != "Y" && "$default" != "N" ]] && default="N"
# In CI/non-interactive mode, automatically answer "yes" to all prompts
# Allow FORCE_INTERACTIVE=1 to run real prompts in CI (e.g., yes-piped tests)
if [[ -n "${FORCE_INTERACTIVE:-}" ]]; then
: # Proceed to prompt
elif [[ -n "${NONINTERACTIVE:-}" ]] || [[ -n "${CI:-}" ]]; then
echo "$prompt [Auto: yes]"
return 0
fi
echo -n "$prompt "
if [[ "$default" == "Y" ]]; then
echo -n "[Y/n]: "
else
echo -n "[y/N]: "
fi
# Read input with validation
local response=""
IFS= read -r response || return 1
# Sanitize input: remove leading/trailing whitespace, limit length
response=$(echo "$response" | /usr/bin/tr -d '\r\n' | /usr/bin/sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
[[ ${#response} -gt 10 ]] && response="${response:0:10}" # Limit to 10 chars
# Validate: only allow y, Y, n, N, yes, Yes, YES, no, No, NO, or empty
if [[ -n "$response" ]] && [[ ! "$response" =~ ^[YyNn]$ ]] && [[ ! "$response" =~ ^[Yy][Ee][Ss]$ ]] && [[ ! "$response" =~ ^[Nn][Oo]$ ]]; then
echo "${RED}Invalid input. Please enter y/n/yes/no or press Enter for default.${NC}" >&2
return 1
fi
if [[ -z "$response" ]]; then
response="$default"
fi
case "$response" in
[Yy]|[Yy][Ee][Ss]) return 0 ;;
*) return 1 ;;
esac
}
# Check if running on macOS
if [[ "$(uname -s)" != "Darwin" ]]; then
echo "${RED}❌ Error: This script is designed for macOS only${NC}"
exit 1
fi
# Detect Homebrew installation prefix
_detect_brew_prefix() {
if [[ -d /opt/homebrew ]]; then
echo /opt/homebrew
elif [[ -d /usr/local/Homebrew ]]; then
echo /usr/local
else
echo ""
fi
}
# Cache Homebrew prefix once at script start (used throughout)
HOMEBREW_PREFIX="$(_detect_brew_prefix)"
# Ensure Homebrew is in PATH for subsequent commands and installer scripts
if [[ -n "$HOMEBREW_PREFIX" ]] && [[ -x "$HOMEBREW_PREFIX/bin/brew" ]]; then
case ":$PATH:" in
*":$HOMEBREW_PREFIX/bin:"*) ;;
*) export PATH="$HOMEBREW_PREFIX/bin:$HOMEBREW_PREFIX/sbin:$PATH" ;;
esac
fi
# Function to install Conda/Miniforge
install_conda() {
local conda_installed=false
# Check if conda is available as a command
if command -v conda >/dev/null 2>&1; then
conda_installed=true
fi
# Check if conda/miniforge is installed via Homebrew
if [[ -n "$HOMEBREW_PREFIX" ]] && [[ -x "$HOMEBREW_PREFIX/bin/brew" ]]; then
if "$HOMEBREW_PREFIX/bin/brew" list --cask miniforge >/dev/null 2>&1 || \
"$HOMEBREW_PREFIX/bin/brew" list --cask anaconda >/dev/null 2>&1 || \
"$HOMEBREW_PREFIX/bin/brew" list --cask miniconda >/dev/null 2>&1; then
conda_installed=true
fi
fi
if [[ "$conda_installed" == true ]]; then
echo "${GREEN}✅ Conda already installed${NC}"
return 0
fi
if [[ "$CHECK_MODE" == true ]]; then
echo "${YELLOW}📦 Conda/Miniforge: Would install via Homebrew${NC}"
return 0
fi
if [[ -n "$HOMEBREW_PREFIX" ]] && [[ -x "$HOMEBREW_PREFIX/bin/brew" ]]; then
if _ask_user "${YELLOW}📦 Conda/Miniforge not found. Install Miniforge via Homebrew?" "N"; then
if "$HOMEBREW_PREFIX/bin/brew" install --cask miniforge; then
echo "${GREEN}✅ Miniforge installed${NC}"
else
warn "Miniforge installation failed"
fi
fi
else
echo "${YELLOW}⚠️ Conda installation requires Homebrew${NC}"
fi
}
# Function to install pipx
install_pipx() {
local pipx_installed=false
# Check if pipx is available as a command
if command -v pipx >/dev/null 2>&1; then
pipx_installed=true
fi
# Check if pipx is installed via Homebrew
if [[ -n "$HOMEBREW_PREFIX" ]] && [[ -x "$HOMEBREW_PREFIX/bin/brew" ]]; then
if "$HOMEBREW_PREFIX/bin/brew" list pipx >/dev/null 2>&1; then
pipx_installed=true
fi
fi
if [[ "$pipx_installed" == true ]]; then
echo "${GREEN}✅ pipx already installed${NC}"
return 0
fi
if [[ "$CHECK_MODE" == true ]]; then
echo "${YELLOW}📦 pipx: Would install via Homebrew${NC}"
return 0
fi
if [[ -n "$HOMEBREW_PREFIX" ]] && [[ -x "$HOMEBREW_PREFIX/bin/brew" ]]; then
if _ask_user "${YELLOW}📦 pipx not found. Install pipx via Homebrew?" "Y"; then
if "$HOMEBREW_PREFIX/bin/brew" install pipx; then
echo "${GREEN}✅ pipx installed${NC}"
else
warn "pipx installation failed"
fi
fi
else
echo "${YELLOW}⚠️ pipx installation requires Homebrew${NC}"
fi
}
# Function to install pyenv
install_pyenv() {
local pyenv_installed=false
# Check if pyenv is available as a command
if command -v pyenv >/dev/null 2>&1; then
pyenv_installed=true
# Check if pyenv is installed via Homebrew
elif [[ -n "$HOMEBREW_PREFIX" ]] && [[ -x "$HOMEBREW_PREFIX/bin/brew" ]]; then
if "$HOMEBREW_PREFIX/bin/brew" list pyenv >/dev/null 2>&1; then
pyenv_installed=true
# Add pyenv to PATH if not already there
if [[ -d "$HOMEBREW_PREFIX/opt/pyenv" ]]; then
export PATH="$HOMEBREW_PREFIX/opt/pyenv/bin:$PATH"
# Note: eval is required for pyenv initialization (standard practice)
# pyenv init outputs shell configuration that must be evaluated
eval "$(pyenv init -)" 2>/dev/null || true
fi
fi
# Check if pyenv exists in common location
elif [[ -d "$HOME/.pyenv" ]] && [[ -f "$HOME/.pyenv/bin/pyenv" ]]; then
pyenv_installed=true
export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init -)" 2>/dev/null || true
fi
if [[ "$pyenv_installed" == true ]]; then
echo "${GREEN}✅ pyenv already installed${NC}"
# Check if Python is installed via pyenv
if pyenv versions --bare 2>/dev/null | /usr/bin/grep -q .; then
echo " ${BLUE}INFO:${NC} Python versions already installed via pyenv"
else
echo " ${BLUE}INFO:${NC} Installing latest Python via pyenv..."
local latest_python
latest_python=$(pyenv install --list 2>/dev/null | /usr/bin/grep -E "^\s+3\.[0-9]+\.[0-9]+$" | /usr/bin/grep -v "dev\|a\|b\|rc" | /usr/bin/tail -1 | /usr/bin/xargs)
if [[ -n "$latest_python" ]]; then
echo " ${BLUE}INFO:${NC} Installing Python $latest_python (this may take a few minutes)..."
if pyenv install "$latest_python" 2>/dev/null; then
pyenv global "$latest_python" 2>/dev/null || true
echo " ${GREEN}✅ Python $latest_python installed and set as global${NC}"
else
echo " ${YELLOW}⚠️ Failed to install Python via pyenv (you can install manually later with: pyenv install <version>)${NC}"
fi
else
echo " ${YELLOW}⚠️ Could not determine latest Python version (you can install manually later with: pyenv install <version>)${NC}"
fi
fi
return 0
fi
if [[ "$CHECK_MODE" == true ]]; then
echo "${YELLOW}📦 pyenv: Would install via Homebrew${NC}"
return 0
fi
if [[ -n "$HOMEBREW_PREFIX" ]] && [[ -x "$HOMEBREW_PREFIX/bin/brew" ]]; then
if _ask_user "${YELLOW}📦 pyenv not found. Install pyenv via Homebrew?" "Y"; then
if "$HOMEBREW_PREFIX/bin/brew" install pyenv; then
echo "${GREEN}✅ pyenv installed${NC}"
# Install latest Python after pyenv is installed
echo " ${BLUE}INFO:${NC} Installing latest Python via pyenv..."
# Source pyenv: check Homebrew location first, then ~/.pyenv
if [[ -d "$HOMEBREW_PREFIX/opt/pyenv" ]]; then
export PATH="$HOMEBREW_PREFIX/opt/pyenv/bin:$PATH"
eval "$(pyenv init -)" 2>/dev/null || true
elif [[ -f "$HOME/.pyenv/bin/pyenv" ]]; then
export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init -)" 2>/dev/null || true
fi
# Brief delay for pyenv shims to initialize after sourcing
sleep 1
local latest_python
latest_python=$(pyenv install --list 2>/dev/null | /usr/bin/grep -E "^\s+3\.[0-9]+\.[0-9]+$" | /usr/bin/grep -v "dev\|a\|b\|rc" | /usr/bin/tail -1 | /usr/bin/xargs)
if [[ -n "$latest_python" ]]; then
echo " ${BLUE}INFO:${NC} Installing Python $latest_python (this may take a few minutes)..."
if pyenv install "$latest_python" 2>/dev/null; then
pyenv global "$latest_python" 2>/dev/null || true
echo " ${GREEN}✅ Python $latest_python installed and set as global${NC}"
else
echo " ${YELLOW}⚠️ Failed to install Python via pyenv (you can install manually later with: pyenv install <version>)${NC}"
fi
else
echo " ${YELLOW}⚠️ Could not determine latest Python version (you can install manually later with: pyenv install <version>)${NC}"
fi
else
warn "pyenv installation failed"
fi
fi
else
echo "${YELLOW}⚠️ pyenv installation requires Homebrew${NC}"
fi
}
# Function to install nvm
install_nvm() {
local NVM_DIR="${NVM_DIR:-$HOME/.nvm}"
if [[ -s "$NVM_DIR/nvm.sh" ]] || type nvm >/dev/null 2>&1; then
echo "${GREEN}✅ nvm already installed${NC}"
# Check if Node.js is installed via nvm
if [[ -s "$NVM_DIR/nvm.sh" ]]; then
source "$NVM_DIR/nvm.sh" 2>/dev/null || true
if nvm list 2>/dev/null | /usr/bin/grep -qE "v[0-9]+\.[0-9]+\.[0-9]+"; then
echo " ${BLUE}INFO:${NC} Node.js versions already installed via nvm"
else
echo " ${BLUE}INFO:${NC} Installing Node.js LTS via nvm..."
if nvm install --lts 2>/dev/null; then
nvm use --lts 2>/dev/null || true
echo " ${GREEN}✅ Node.js LTS installed and activated${NC}"
else
echo " ${YELLOW}⚠️ Failed to install Node.js via nvm (you can install manually later)${NC}"
fi
fi
fi
return 0
fi
if [[ "$CHECK_MODE" == true ]]; then
echo "${YELLOW}📦 nvm: Would install via curl${NC}"
return 0
fi
if _ask_user "${YELLOW}📦 nvm not found. Install nvm?" "Y"; then
echo " Installing nvm..."
# Get latest nvm version dynamically from GitHub API
local nvm_version=""
nvm_version="$(/usr/bin/curl -fsSL --connect-timeout 10 https://api.github.com/repos/nvm-sh/nvm/releases/latest 2>/dev/null | /usr/bin/grep '"tag_name"' | /usr/bin/sed -E 's/.*"([^"]+)".*/\1/' || echo "")"
if [[ -z "$nvm_version" || ! "$nvm_version" =~ ^v[0-9] ]]; then
echo "${RED}❌ Failed to determine latest nvm version from GitHub API${NC}"
warn "nvm installation failed (could not fetch version)"
return 1
fi
echo " ${BLUE}INFO:${NC} Installing nvm $nvm_version..."
if /usr/bin/curl --connect-timeout 15 --max-time 120 --retry 3 --retry-delay 2 -fsSL "https://raw.githubusercontent.com/nvm-sh/nvm/${nvm_version}/install.sh" | /bin/bash; then
echo "${GREEN}✅ nvm installed${NC}"
# Install Node.js LTS after nvm is installed
if [[ -s "$NVM_DIR/nvm.sh" ]]; then
source "$NVM_DIR/nvm.sh" 2>/dev/null || true
echo " ${BLUE}INFO:${NC} Installing Node.js LTS via nvm..."
if nvm install --lts 2>/dev/null; then
nvm use --lts 2>/dev/null || true
echo " ${GREEN}✅ Node.js LTS installed and activated${NC}"
else
echo " ${YELLOW}⚠️ Failed to install Node.js via nvm (you can install manually later)${NC}"
fi
fi
else
warn "nvm installation failed"
fi
fi
}
# Function to install chruby and ruby-install
install_chruby() {
local chruby_installed=false
local chruby_script=""
# Check if chruby is available as a function or command
if type chruby >/dev/null 2>&1 || command -v chruby >/dev/null 2>&1; then
chruby_installed=true
fi
# Check common chruby.sh locations
local possible_paths=(
"/usr/local/share/chruby/chruby.sh"
"$HOME/.local/share/chruby/chruby.sh"
"/opt/homebrew/share/chruby/chruby.sh"
"/usr/local/opt/chruby/share/chruby/chruby.sh"
)
# Also check via Homebrew prefix
if [[ -n "$HOMEBREW_PREFIX" ]]; then
possible_paths+=("$HOMEBREW_PREFIX/share/chruby/chruby.sh")
possible_paths+=("$HOMEBREW_PREFIX/opt/chruby/share/chruby/chruby.sh")
fi
# Check if chruby is installed via Homebrew (most reliable method)
if [[ -n "$HOMEBREW_PREFIX" ]] && [[ -x "$HOMEBREW_PREFIX/bin/brew" ]]; then
if "$HOMEBREW_PREFIX/bin/brew" list chruby >/dev/null 2>&1; then
chruby_installed=true
# Find the actual chruby.sh location via Homebrew
local chruby_prefix
chruby_prefix=$("$HOMEBREW_PREFIX/bin/brew" --prefix chruby 2>/dev/null)
if [[ -n "$chruby_prefix" ]] && [[ -f "$chruby_prefix/share/chruby/chruby.sh" ]]; then
chruby_script="$chruby_prefix/share/chruby/chruby.sh"
else
# Fallback: try common paths
for path in "${possible_paths[@]}"; do
if [[ -f "$path" ]]; then
chruby_script="$path"
break
fi
done
fi
fi
fi
# Check file locations
for path in "${possible_paths[@]}"; do
if [[ -f "$path" ]]; then
chruby_installed=true
chruby_script="$path"
break
fi
done
if [[ "$chruby_installed" == false ]]; then
if [[ "$CHECK_MODE" == true ]]; then
echo "${YELLOW}📦 chruby: Would install via Homebrew${NC}"
elif [[ -n "$HOMEBREW_PREFIX" ]] && [[ -x "$HOMEBREW_PREFIX/bin/brew" ]]; then
if _ask_user "${YELLOW}📦 chruby not found. Install chruby and ruby-install via Homebrew?" "Y"; then
if "$HOMEBREW_PREFIX/bin/brew" install chruby ruby-install; then
echo "${GREEN}✅ chruby and ruby-install installed${NC}"
# Find chruby script via Homebrew prefix (works on both Intel and Apple Silicon)
local chruby_prefix
chruby_prefix=$("$HOMEBREW_PREFIX/bin/brew" --prefix chruby 2>/dev/null || echo "")
if [[ -n "$chruby_prefix" ]] && [[ -f "$chruby_prefix/share/chruby/chruby.sh" ]]; then
chruby_script="$chruby_prefix/share/chruby/chruby.sh"
fi
else
warn "chruby installation failed"
fi
fi
else
echo "${YELLOW}⚠️ chruby installation requires Homebrew${NC}"
fi
else
echo "${GREEN}✅ chruby already installed${NC}"
fi
# Check for ruby-install separately
local ruby_install_installed=false
if command -v ruby-install >/dev/null 2>&1; then
ruby_install_installed=true
elif [[ -n "$HOMEBREW_PREFIX" ]] && [[ -x "$HOMEBREW_PREFIX/bin/brew" ]]; then
# Check if ruby-install is installed via Homebrew
if "$HOMEBREW_PREFIX/bin/brew" list ruby-install >/dev/null 2>&1; then
ruby_install_installed=true
fi
fi
if [[ "$ruby_install_installed" == false ]]; then
if [[ "$CHECK_MODE" == true ]]; then
echo "${YELLOW}📦 ruby-install: Would install via Homebrew${NC}"
return 0
elif [[ -n "$HOMEBREW_PREFIX" ]] && [[ -x "$HOMEBREW_PREFIX/bin/brew" ]]; then
if _ask_user "${YELLOW}📦 ruby-install not found. Install ruby-install?" "Y"; then
if "$HOMEBREW_PREFIX/bin/brew" install ruby-install; then
echo "${GREEN}✅ ruby-install installed${NC}"
else
warn "ruby-install installation failed"
fi
fi
fi
else
echo "${GREEN}✅ ruby-install already installed${NC}"
fi
# Skip Ruby installation in check mode
if [[ "$CHECK_MODE" == true ]]; then
return 0
fi
# Install Ruby if chruby and ruby-install are available
if command -v ruby-install >/dev/null 2>&1; then
# Check if Ruby is already installed
local ruby_installed=false
if [[ -n "$chruby_script" ]] && [[ -f "$chruby_script" ]]; then
source "$chruby_script" 2>/dev/null || true
if chruby 2>/dev/null | /usr/bin/grep -qE "ruby-[0-9]+\.[0-9]+\.[0-9]+"; then
ruby_installed=true
echo " ${BLUE}INFO:${NC} Ruby versions already installed via ruby-install"
fi
fi
if [[ "$ruby_installed" == false ]]; then
echo " ${BLUE}INFO:${NC} Installing latest Ruby via ruby-install..."
# Get latest stable Ruby version using same method as maintain-system.sh
local latest_ruby
latest_ruby=$(ruby-install --list ruby 2>/dev/null | /usr/bin/awk '/^ruby [0-9]+\.[0-9]+\.[0-9]+$/ {print $2}' | /usr/bin/sort -V | /usr/bin/tail -n1)
if [[ -n "$latest_ruby" ]]; then
echo " ${BLUE}INFO:${NC} Installing Ruby $latest_ruby (this may take a few minutes)..."
if ruby-install ruby "$latest_ruby" 2>/dev/null; then
echo " ${GREEN}✅ Ruby $latest_ruby installed${NC}"
if [[ -n "$chruby_script" ]] && [[ -f "$chruby_script" ]]; then
source "$chruby_script" 2>/dev/null || true
chruby "ruby-$latest_ruby" 2>/dev/null || true
fi
else
echo " ${YELLOW}⚠️ Failed to install Ruby via ruby-install (you can install manually later with: ruby-install ruby <version>)${NC}"
fi
else
echo " ${YELLOW}⚠️ Could not determine latest Ruby version (you can install manually later with: ruby-install ruby <version>)${NC}"
echo " ${BLUE}INFO:${NC} Try: ruby-install --list ruby (to see available versions)"
fi
fi
fi
}
# Function to install rustup
install_rustup() {
local rustup_installed=false
# Check if rustup is available as a command
if command -v rustup >/dev/null 2>&1; then
rustup_installed=true
# Check if rustup exists in common cargo location
elif [[ -f "$HOME/.cargo/bin/rustup" ]]; then
rustup_installed=true
# Add cargo bin to PATH if not already there
if [[ ":$PATH:" != *":$HOME/.cargo/bin:"* ]]; then
export PATH="$HOME/.cargo/bin:$PATH"
fi
# Check if cargo directory exists (indicates rustup might be installed)
elif [[ -d "$HOME/.cargo" ]] && [[ -f "$HOME/.cargo/env" ]]; then
# Source cargo env to make rustup available
if [[ -f "$HOME/.cargo/env" ]]; then
source "$HOME/.cargo/env" 2>/dev/null || true
if command -v rustup >/dev/null 2>&1; then
rustup_installed=true
fi
fi
fi
if [[ "$rustup_installed" == true ]]; then
echo "${GREEN}✅ rustup already installed${NC}"
# Check if Rust is installed
if rustup toolchain list 2>/dev/null | /usr/bin/grep -qE "stable|default"; then
echo " ${BLUE}INFO:${NC} Rust toolchain already installed"
else
echo " ${BLUE}INFO:${NC} Installing Rust stable toolchain..."
if rustup install stable 2>/dev/null; then
rustup default stable 2>/dev/null || true
echo " ${GREEN}✅ Rust stable installed and set as default${NC}"
else
echo " ${YELLOW}⚠️ Failed to install Rust via rustup (you can install manually later)${NC}"
fi
fi
return 0
fi
if [[ "$CHECK_MODE" == true ]]; then
echo "${YELLOW}📦 rustup: Would install via curl${NC}"
return 0
fi
if _ask_user "${YELLOW}📦 rustup not found. Install rustup (Rust toolchain manager)?" "Y"; then
echo " Installing rustup..."
if /usr/bin/curl --connect-timeout 15 --max-time 120 --retry 3 --retry-delay 2 --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | /bin/bash -s -- -y; then
echo "${GREEN}✅ rustup installed${NC}"
# Source cargo env if available
if [[ -f "$HOME/.cargo/env" ]]; then
source "$HOME/.cargo/env" 2>/dev/null || true
fi
# Install Rust stable after rustup is installed
echo " ${BLUE}INFO:${NC} Installing Rust stable toolchain..."
if rustup install stable 2>/dev/null; then
rustup default stable 2>/dev/null || true
echo " ${GREEN}✅ Rust stable installed and set as default${NC}"
else
echo " ${YELLOW}⚠️ Failed to install Rust via rustup (you can install manually later)${NC}"
fi
echo " ${BLUE}INFO:${NC} Restart your terminal or run: source \$HOME/.cargo/env"
else
warn "rustup installation failed"
fi
fi
}
# Function to install swiftly
install_swiftly() {
local swiftly_installed=false
local swiftly_path=""
# Check if swiftly is available as a command
if command -v swiftly >/dev/null 2>&1; then
swiftly_installed=true
swiftly_path=$(command -v swiftly)
# Check common swiftly locations (swiftly installs to $HOME/.swiftly/bin/swiftly)
elif [[ -f "$HOME/.swiftly/bin/swiftly" ]]; then
swiftly_installed=true
swiftly_path="$HOME/.swiftly/bin/swiftly"
# Add to PATH if not already there
if [[ ":$PATH:" != *":$HOME/.swiftly/bin:"* ]]; then
export PATH="$HOME/.swiftly/bin:$PATH"
fi
elif [[ -f "$HOME/.local/bin/swiftly" ]]; then
swiftly_installed=true
swiftly_path="$HOME/.local/bin/swiftly"
# Add to PATH if not already there
if [[ ":$PATH:" != *":$HOME/.local/bin:"* ]]; then
export PATH="$HOME/.local/bin:$PATH"
fi
# Check if .swiftly directory exists (indicates swiftly might be installed)
elif [[ -d "$HOME/.swiftly" ]]; then
# Try to find swiftly in common locations
local possible_paths=(
"$HOME/.swiftly/bin/swiftly"
"$HOME/.local/bin/swiftly"
"$HOME/bin/swiftly"
"/usr/local/bin/swiftly"
)
for path in "${possible_paths[@]}"; do
if [[ -f "$path" ]]; then
swiftly_installed=true
swiftly_path="$path"
# Add directory to PATH if not already there
local dir_path=$(dirname "$path")
if [[ ":$PATH:" != *":$dir_path:"* ]]; then
export PATH="$dir_path:$PATH"
fi
break
fi
done
fi
if [[ "$swiftly_installed" == true ]]; then
echo "${GREEN}✅ swiftly already installed${NC}"
# Check if Swift is installed
if swiftly list installed 2>/dev/null | /usr/bin/grep -qE "[0-9]+\.[0-9]+"; then
echo " ${BLUE}INFO:${NC} Swift versions already installed via swiftly"
else
echo " ${BLUE}INFO:${NC} Installing latest Swift via swiftly..."
local latest_swift
# swiftly list-available outputs "Swift X.Y.Z" format, extract version number (2nd field)
latest_swift=$(swiftly list-available 2>/dev/null | /usr/bin/grep -E '^Swift [0-9]+\.[0-9]+\.[0-9]+' | /usr/bin/awk '{print $2}' | /usr/bin/sort -V | /usr/bin/tail -1)
if [[ -n "$latest_swift" && "$latest_swift" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo " ${BLUE}INFO:${NC} Installing Swift $latest_swift (this may take a few minutes)..."
if swiftly install --assume-yes "$latest_swift" 2>/dev/null; then
swiftly use --assume-yes "$latest_swift" 2>/dev/null || true
echo " ${GREEN}✅ Swift $latest_swift installed and activated${NC}"
else
echo " ${YELLOW}⚠️ Failed to install Swift via swiftly (you can install manually later with: swiftly install <version>)${NC}"
fi
else
echo " ${YELLOW}⚠️ Could not determine latest Swift version (you can install manually later with: swiftly install <version>)${NC}"
fi
fi
return 0
fi
if [[ "$CHECK_MODE" == true ]]; then
echo "${YELLOW}📦 swiftly: Would install via curl${NC}"
return 0
fi
if _ask_user "${YELLOW}📦 swiftly not found. Install swiftly (Swift toolchain manager)?" "N"; then
echo " Installing swiftly..."
if /usr/bin/curl --connect-timeout 15 --max-time 120 --retry 3 --retry-delay 2 -fsSL https://swiftlang.org/swiftly-install.sh | /bin/bash; then
echo "${GREEN}✅ swiftly installed${NC}"
# Install latest Swift after swiftly is installed
echo " ${BLUE}INFO:${NC} Installing latest Swift via swiftly..."
local latest_swift
# swiftly list-available outputs "Swift X.Y.Z" format, extract version number (2nd field)
latest_swift=$(swiftly list-available 2>/dev/null | /usr/bin/grep -E '^Swift [0-9]+\.[0-9]+\.[0-9]+' | /usr/bin/awk '{print $2}' | /usr/bin/sort -V | /usr/bin/tail -1)
if [[ -n "$latest_swift" && "$latest_swift" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo " ${BLUE}INFO:${NC} Installing Swift $latest_swift (this may take a few minutes)..."
if swiftly install --assume-yes "$latest_swift" 2>/dev/null; then
swiftly use --assume-yes "$latest_swift" 2>/dev/null || true
echo " ${GREEN}✅ Swift $latest_swift installed and activated${NC}"
else
echo " ${YELLOW}⚠️ Failed to install Swift via swiftly (you can install manually later with: swiftly install <version>)${NC}"
fi
else
echo " ${YELLOW}⚠️ Could not determine latest Swift version (you can install manually later with: swiftly install <version>)${NC}"
fi
else
warn "swiftly installation failed"
fi
fi
}
# Function to install Go
install_go() {
local go_installed=false
# Check if go is available as a command
if command -v go >/dev/null 2>&1; then
go_installed=true
fi
# Check if Go is installed via Homebrew
if [[ -n "$HOMEBREW_PREFIX" ]] && [[ -x "$HOMEBREW_PREFIX/bin/brew" ]]; then
if "$HOMEBREW_PREFIX/bin/brew" list go >/dev/null 2>&1; then
go_installed=true
fi
fi
if [[ "$go_installed" == true ]]; then
echo "${GREEN}✅ Go already installed${NC}"
return 0
fi
if [[ "$CHECK_MODE" == true ]]; then
echo "${YELLOW}📦 Go: Would install via Homebrew${NC}"
return 0
fi
if [[ -n "$HOMEBREW_PREFIX" ]] && [[ -x "$HOMEBREW_PREFIX/bin/brew" ]]; then
if _ask_user "${YELLOW}📦 Go not found. Install Go via Homebrew?" "Y"; then
if "$HOMEBREW_PREFIX/bin/brew" install go; then
echo "${GREEN}✅ Go installed${NC}"
else
warn "Go installation failed"
fi
fi
else
echo "${YELLOW}⚠️ Go installation requires Homebrew, or install manually from https://go.dev/dl/${NC}"
fi
}
# Function to install Java
install_java() {
local java_installed=false
# Check if java is available as a command
if command -v java >/dev/null 2>&1; then
java_installed=true
fi
# Check if Java/OpenJDK is installed via Homebrew
if [[ -n "$HOMEBREW_PREFIX" ]] && [[ -x "$HOMEBREW_PREFIX/bin/brew" ]]; then
if "$HOMEBREW_PREFIX/bin/brew" list openjdk >/dev/null 2>&1 || \
"$HOMEBREW_PREFIX/bin/brew" list --cask temurin >/dev/null 2>&1 || \
"$HOMEBREW_PREFIX/bin/brew" list --cask zulu >/dev/null 2>&1 || \
"$HOMEBREW_PREFIX/bin/brew" list --cask java >/dev/null 2>&1; then
java_installed=true
fi
fi
if [[ "$java_installed" == true ]]; then
echo "${GREEN}✅ Java already installed${NC}"
return 0
fi
if [[ "$CHECK_MODE" == true ]]; then
echo "${YELLOW}📦 Java: Would install via Homebrew${NC}"
return 0
fi
if [[ -n "$HOMEBREW_PREFIX" ]] && [[ -x "$HOMEBREW_PREFIX/bin/brew" ]]; then
if _ask_user "${YELLOW}📦 Java not found. Install OpenJDK via Homebrew?" "N"; then
if "$HOMEBREW_PREFIX/bin/brew" install openjdk; then
echo "${GREEN}✅ OpenJDK installed${NC}"
else
warn "OpenJDK installation failed"
fi
fi
else
echo "${YELLOW}⚠️ Java installation requires Homebrew, or install manually${NC}"
fi
}
# Function to install .NET SDK
install_dotnet() {
local dotnet_installed=false
# Check if dotnet is available as a command
if command -v dotnet >/dev/null 2>&1; then
dotnet_installed=true
fi
# Check if .NET SDK is installed via Homebrew
if [[ -n "$HOMEBREW_PREFIX" ]] && [[ -x "$HOMEBREW_PREFIX/bin/brew" ]]; then
if "$HOMEBREW_PREFIX/bin/brew" list --cask dotnet-sdk >/dev/null 2>&1 || \
"$HOMEBREW_PREFIX/bin/brew" list --cask dotnet >/dev/null 2>&1; then
dotnet_installed=true
fi
fi
if [[ "$dotnet_installed" == true ]]; then
echo "${GREEN}✅ .NET SDK already installed${NC}"
return 0
fi
if [[ "$CHECK_MODE" == true ]]; then
echo "${YELLOW}📦 .NET SDK: Would install via Homebrew${NC}"
return 0
fi
if [[ -n "$HOMEBREW_PREFIX" ]] && [[ -x "$HOMEBREW_PREFIX/bin/brew" ]]; then
if _ask_user "${YELLOW}📦 .NET SDK not found. Install .NET SDK via Homebrew?" "N"; then
if "$HOMEBREW_PREFIX/bin/brew" install --cask dotnet-sdk; then
echo "${GREEN}✅ .NET SDK installed${NC}"
else
warn ".NET SDK installation failed"
fi
fi
else
echo "${YELLOW}⚠️ .NET SDK installation requires Homebrew, or install manually from https://dotnet.microsoft.com/download${NC}"
fi
}
# Test detection function
test_detection() {
local all_found=0
local all_missing=0
echo "Testing detection of all tools..."
echo ""
# Test each tool
local tools=(
"conda:Conda/Miniforge"
"pipx:pipx"
"pyenv:pyenv"
"nvm:nvm"
"chruby:chruby"
"ruby-install:ruby-install"
"rustup:rustup"
"swiftly:swiftly"
"go:Go"
"java:Java"
"dotnet:.NET SDK"
)
for tool_info in "${tools[@]}"; do
local tool="${tool_info%%:*}"
local name="${tool_info##*:}"
if command -v "$tool" >/dev/null 2>&1; then
echo "${GREEN}✅ $name: Found via command${NC}"
((all_found++))
else
# Check via Homebrew for tools that might be installed there
local found_via_brew=false
if [[ -n "$HOMEBREW_PREFIX" ]] && [[ -x "$HOMEBREW_PREFIX/bin/brew" ]]; then
case "$tool" in
conda)
if "$HOMEBREW_PREFIX/bin/brew" list --cask miniforge >/dev/null 2>&1 || \
"$HOMEBREW_PREFIX/bin/brew" list --cask anaconda >/dev/null 2>&1 || \
"$HOMEBREW_PREFIX/bin/brew" list --cask miniconda >/dev/null 2>&1; then
found_via_brew=true
fi
;;
pipx|pyenv|go|chruby|ruby-install)
if "$HOMEBREW_PREFIX/bin/brew" list "$tool" >/dev/null 2>&1; then
found_via_brew=true
fi
;;
java)
if "$HOMEBREW_PREFIX/bin/brew" list openjdk >/dev/null 2>&1 || \
"$HOMEBREW_PREFIX/bin/brew" list --cask temurin >/dev/null 2>&1 || \
"$HOMEBREW_PREFIX/bin/brew" list --cask zulu >/dev/null 2>&1 || \
"$HOMEBREW_PREFIX/bin/brew" list --cask java >/dev/null 2>&1; then
found_via_brew=true
fi
;;
dotnet)
if "$HOMEBREW_PREFIX/bin/brew" list --cask dotnet-sdk >/dev/null 2>&1 || \
"$HOMEBREW_PREFIX/bin/brew" list --cask dotnet >/dev/null 2>&1; then
found_via_brew=true
fi
;;
esac
fi
# Special checks for tools with custom locations
case "$tool" in
nvm)
if [[ -s "$HOME/.nvm/nvm.sh" ]]; then
echo "${GREEN}✅ $name: Found at $HOME/.nvm${NC}"
((all_found++))
continue
fi
;;
rustup)
if [[ -f "$HOME/.cargo/bin/rustup" ]]; then
echo "${GREEN}✅ $name: Found at $HOME/.cargo${NC}"
((all_found++))
continue
fi
;;
swiftly)
if [[ -f "$HOME/.swiftly/bin/swiftly" ]]; then
echo "${GREEN}✅ $name: Found at $HOME/.swiftly${NC}"
((all_found++))
continue
fi
;;
pyenv)
if [[ -d "$HOME/.pyenv" ]] || [[ -d "$HOMEBREW_PREFIX/opt/pyenv" ]]; then
echo "${GREEN}✅ $name: Found in custom location${NC}"
((all_found++))
continue
fi
;;
esac
if [[ "$found_via_brew" == true ]]; then
echo "${GREEN}✅ $name: Found via Homebrew${NC}"
((all_found++))
else
echo "${YELLOW}❌ $name: Not found${NC}"
((all_missing++))
fi
fi
done
echo ""
echo "Summary:"
echo " ${GREEN}Found: $all_found${NC}"
echo " ${YELLOW}Missing: $all_missing${NC}"
echo ""
if [[ $all_missing -eq 0 ]]; then
echo "${GREEN}✅ All tools detected correctly!${NC}"
return 0
else
echo "${YELLOW}⚠️ Some tools not detected. This is normal if they're not installed.${NC}"
return 1
fi
}
# Main installation
main() {
if [[ "$TEST_MODE" == true ]]; then
test_detection
return $?
fi
if [[ "$CHECK_MODE" == false ]]; then
echo ""
echo "This script will help you install development tools:"
echo " - Language package managers: Conda, pipx"
echo " - Language version managers: pyenv, nvm, chruby, rustup, swiftly"
echo " - Language runtimes: Go, Java, .NET"
echo ""
echo "Note: Version managers will also install the latest/latest LTS version of each language."
echo " Some tools require Homebrew to be installed first."
echo " Run './install.sh' to install system package managers (Homebrew, MacPorts, Nix, mas)."
echo ""
fi
# Language Package Managers
echo "${BLUE}=== Language Package Managers ===${NC}"
install_conda
install_pipx
echo ""
echo "${BLUE}=== Language Version Managers & Runtimes ===${NC}"
install_pyenv
install_nvm
install_chruby
install_rustup
install_swiftly
install_go
install_java
install_dotnet
if [[ "$CHECK_MODE" == true ]]; then
echo ""
echo "${GREEN}✅ Check complete!${NC}"
echo ""
echo "This was a dry-run. No tools were installed."
echo "Run './dev-tools.sh' without arguments to actually install missing tools."
return 0
fi
echo ""
if [[ $install_warnings -gt 0 ]]; then
echo "${YELLOW}⚠️ Installation completed with $install_warnings warning(s)${NC}"
else
echo "${GREEN}✅ Installation complete!${NC}"
fi
echo ""
echo "Next steps:"
echo " 1. Restart your terminal or run: source ~/.zshrc"
echo " 2. Language versions have been installed automatically, but you can install additional versions:"
echo " - Python: pyenv install <version>"
echo " - Node.js: nvm install <version>"
echo " - Ruby: ruby-install ruby <version>, then chruby <version>"
echo " - Rust: rustup install <version>"