-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-dev-machine.sh
More file actions
executable file
·1375 lines (1170 loc) · 52.1 KB
/
setup-dev-machine.sh
File metadata and controls
executable file
·1375 lines (1170 loc) · 52.1 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
#!/bin/bash
# A script to automate the setup of a new dev machine.
# The return value of a pipeline is the status of the last command to exit with a non-zero status.
set -o pipefail
# --- XDG Base Directory Standards ---
export XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-$HOME/.config}"
export XDG_DATA_HOME="${XDG_DATA_HOME:-$HOME/.local/share}"
export XDG_STATE_HOME="${XDG_STATE_HOME:-$HOME/.local/state}"
export XDG_BIN_HOME="${HOME}/.local/bin"
# Colors & Styles
C_RED=$'\033[31m'
C_GREEN=$'\033[32m'
C_YELLOW=$'\033[33m'
C_L_RED=$'\033[31;1m'
C_L_GREEN=$'\033[32m'
C_L_YELLOW=$'\033[33m'
C_L_BLUE=$'\033[34m'
C_L_MAGENTA=$'\033[35;1m'
C_L_CYAN=$'\033[36m'
C_GRAY=$'\033[38;5;244m'
T_RESET=$'\033[0m'
T_BOLD=$'\033[1m'
T_ULINE=$'\033[4m'
T_CURSOR_HIDE=$'\033[?25l'
T_CURSOR_SHOW=$'\033[?25h'
# Icons
T_ERR_ICON="[${T_BOLD}${C_RED}✗${T_RESET}]"
T_OK_ICON="[${T_BOLD}${C_GREEN}✓${T_RESET}]"
T_INFO_ICON="[${T_BOLD}${C_YELLOW}i${T_RESET}]"
T_WARN_ICON="/${T_BOLD}${C_YELLOW}!${T_RESET}\\"
T_QST_ICON="[${T_BOLD}${C_L_CYAN}?${T_RESET}]"
# Key Codes
KEY_ENTER="ENTER"
KEY_ESC=$'\033'
# Logging
printMsg() { printf '%b\n' "$1"; }
printMsgNoNewline() { printf '%b' "$1"; }
printErrMsg() { printMsg "${T_ERR_ICON}${T_BOLD}${C_L_RED} ${1} ${T_RESET}"; }
printOkMsg() { printMsg "${T_OK_ICON} ${1}${T_RESET}"; }
printInfoMsg() { printMsg "${T_INFO_ICON} ${1}${T_RESET}"; }
printWarnMsg() { printMsg "${T_WARN_ICON} ${1}${T_RESET}"; }
# Banner Utils
strip_ansi_codes() {
local s="$1"; local esc=$'\033'
if [[ "$s" != *"$esc"* ]]; then echo -n "$s"; return; fi
local pattern="$esc\\[[0-9;]*[a-zA-Z]"
while [[ $s =~ $pattern ]]; do s="${s/${BASH_REMATCH[0]}/}"; done
echo -n "$s"
}
_truncate_string() {
local input_str="$1"; local max_len="$2"; local trunc_char="${3:-…}"; local trunc_char_len=${#trunc_char}
local stripped_str; stripped_str=$(strip_ansi_codes "$input_str"); local len=${#stripped_str}
if (( len <= max_len )); then echo -n "$input_str"; return; fi
local truncate_to_len=$(( max_len - trunc_char_len )); local new_str=""; local visible_count=0; local i=0; local in_escape=false
while (( i < ${#input_str} && visible_count < truncate_to_len )); do
local char="${input_str:i:1}"; new_str+="$char"
if [[ "$char" == $'\033' ]]; then in_escape=true; elif ! $in_escape; then (( visible_count++ )); fi
if $in_escape && [[ "$char" =~ [a-zA-Z] ]]; then in_escape=false; fi; ((i++))
done
echo -n "${new_str}${trunc_char}"
}
generate_banner_string() {
local text="$1"
local color="${2:-$C_L_BLUE}"
local line_char="${3:-━}"
local prefix="${4:-┏}"
local total_width=70; local line
printf -v line '%*s' "$((total_width - 1))" ""; line="${line// /${line_char}}"; printf '%s' "${color}${prefix}${line}${T_RESET}"; printf '\r'
local text_to_print; text_to_print=$(_truncate_string "$text" $((total_width - 3)))
printf '%s' "${color}${prefix} ${text_to_print} ${T_RESET}"
}
printBanner() { printMsg "$(generate_banner_string "$1" "${C_L_BLUE}" "─" "─")"; }
printPhaseBanner() { printMsg "$(generate_banner_string "$1" "${C_L_MAGENTA}" "━" "┏")"; }
# Terminal Control
clear_current_line() { printf '\033[2K\r' >/dev/tty; }
clear_lines_up() {
local lines=${1:-1}; for ((i = 0; i < lines; i++)); do printf '\033[1A\033[2K'; done; printf '\r'
} >/dev/tty
# User Input
read_single_char() {
local char; local seq; IFS= read -rsn1 char < /dev/tty
if [[ -z "$char" ]]; then echo "$KEY_ENTER"; return; fi
if [[ "$char" == "$KEY_ESC" ]]; then
if IFS= read -rsn1 -t 0.001 seq < /dev/tty; then
char+="$seq"
if [[ "$seq" == "[" || "$seq" == "O" ]]; then
while IFS= read -rsn1 -t 0.001 seq < /dev/tty; do char+="$seq"; if [[ "$seq" =~ [a-zA-Z~] ]]; then break; fi; done
fi
fi
fi
echo "$char"
}
show_timed_message() {
local message="$1"; local duration="${2:-1.8}"; local message_lines; message_lines=$(echo -e "$message" | wc -l)
printMsg "$message" >/dev/tty; sleep "$duration"; clear_lines_up "$message_lines" >/dev/tty
}
prompt_yes_no() {
local question="$1"; local default_answer="${2:-}"; local has_error=false; local answer; local prompt_suffix
if [[ "$default_answer" == "y" ]]; then prompt_suffix="(Y/n)"; elif [[ "$default_answer" == "n" ]]; then prompt_suffix="(y/N)"; else prompt_suffix="(y/n)"; fi
local question_lines; question_lines=$(echo -e "$question" | wc -l)
_clear_all_prompt_content() { clear_current_line >/dev/tty; if (( question_lines > 1 )); then clear_lines_up $(( question_lines - 1 )); fi; if $has_error; then clear_lines_up 1; fi; }
printf '%b' "${T_QST_ICON} ${question} ${prompt_suffix} " >/dev/tty
while true; do
answer=$(read_single_char); if [[ "$answer" == "$KEY_ENTER" ]]; then answer="$default_answer"; fi
case "$answer" in
[Yy]|[Nn]) _clear_all_prompt_content; if [[ "$answer" =~ [Yy] ]]; then return 0; else return 1; fi ;;
"$KEY_ESC"|"q") _clear_all_prompt_content; show_timed_message " ${C_L_YELLOW}-- cancelled --${T_RESET}" 1; return 2 ;;
*) _clear_all_prompt_content; printErrMsg "Invalid input. Please enter 'y' or 'n'." >/dev/tty; has_error=true; printf '%b' "${T_QST_ICON} ${question} ${prompt_suffix} " >/dev/tty ;;
esac
done
}
# Spinners
SPINNER_OUTPUT=""
_run_with_spinner_non_interactive() {
local desc="$1"; shift; local cmd=("$@"); printMsgNoNewline "${desc} " >&2
if SPINNER_OUTPUT=$("${cmd[@]}" 2>&1); then printf '%s\n' "${C_L_GREEN}Done.${T_RESET}" >&2; return 0
else local exit_code=$?; printf '%s\n' "${C_RED}Failed.${T_RESET}" >&2
while IFS= read -r line; do printf ' %s\n' "$line"; done <<< "$SPINNER_OUTPUT" >&2; return $exit_code; fi
}
_run_with_spinner_interactive() {
local desc="$1"; shift; local cmd=("$@"); local temp_output_file; temp_output_file=$(mktemp)
if [[ ! -f "$temp_output_file" ]]; then printErrMsg "Failed to create temp file."; return 1; fi
local spinner_chars="⣾⣷⣯⣟⡿⢿⣻⣽"; local i=0; "${cmd[@]}" &> "$temp_output_file" &
local pid=$!; printMsgNoNewline "${T_CURSOR_HIDE}" >&2; trap 'printMsgNoNewline "${T_CURSOR_SHOW}" >&2; rm -f "$temp_output_file"; exit 130' INT TERM
while ps -p $pid > /dev/null; do
printf '\r\033[2K' >&2; local line; line=$(tail -n 1 "$temp_output_file" 2>/dev/null | tr -d '\r' || true)
printf ' %s%s%s %s' "${C_L_BLUE}" "${spinner_chars:$i:1}" "${T_RESET}" "${desc}" >&2
if [[ -n "$line" ]]; then printf ' %s[%s]%s' "${C_GRAY}" "${line:0:70}" "${T_RESET}" >&2; fi
i=$(((i + 1) % ${#spinner_chars})); sleep 0.1; done
wait $pid; local exit_code=$?; SPINNER_OUTPUT=$(<"$temp_output_file"); rm "$temp_output_file";
printMsgNoNewline "${T_CURSOR_SHOW}" >&2; trap - INT TERM; clear_current_line >&2
if [[ $exit_code -eq 0 ]]; then printOkMsg "${desc}" >&2
else printErrMsg "Task failed: ${desc}" >&2
while IFS= read -r line; do printf ' %s\n' "$line"; done <<< "$SPINNER_OUTPUT" >&2; fi
return $exit_code
}
run_with_spinner() {
if [[ ! -t 1 ]]; then _run_with_spinner_non_interactive "$@"; else _run_with_spinner_interactive "$@"; fi
}
# --- Global Variables ---
SCRIPT_DIR=""
VERIFY_MODE=false
# --- Verification Helper ---
report_verify() {
local item="$1"
local status="$2"
local details="$3"
local color="${C_GREEN}"
local icon="${T_OK_ICON}"
if [[ "$status" == "Missing" ]]; then
color="${C_RED}"; icon="${T_ERR_ICON}"
elif [[ "$status" == "Outdated" || "$status" == "Modified" || "$status" == "Differs" ]]; then
color="${C_YELLOW}"; icon="${T_WARN_ICON}"
fi
printf " %s %-25s %s%-12s%s %s\n" "$icon" "$item" "$color" "$status" "$T_RESET" "$details"
}
# --- Script Functions ---
print_usage() {
printPhaseBanner "Developer Machine Setup Script"
printMsg "This script automates the setup of a new developer environment by installing"
printMsg "essential tools and setting up a complete LazyVim configuration."
printMsg "\n${T_ULINE}Usage:${T_RESET}"
printMsg " $(basename "$0") [options]"
printMsg "\n${T_ULINE}Options:${T_RESET}"
printMsg " ${C_L_BLUE}-h, --help${T_RESET} Show this help message"
printMsg " ${C_L_BLUE}--verify${T_RESET} Check system state without making changes"
printMsg " ${C_L_BLUE}--no-vim${T_RESET} Skip Neovim installation and configuration"
printMsg " ${C_L_BLUE}--only-vim${T_RESET} Run ONLY Neovim installation and configuration"
printMsg "\n${T_ULINE}What it does:${T_RESET}"
printMsg " 1. Checks for a compatible system (Debian/Ubuntu-based Linux)."
printMsg " 2. Installs essential CLI tools (curl, git, tmux, etc.)."
printMsg " 3. Installs modern utilities (ripgrep, fd, bat, eza) from GitHub."
printMsg " 4. Sets up shell environment (zoxide, starship, .bash_aliases)."
printMsg " 5. Installs dev tools (Go, lazygit, lazydocker, delta)."
printMsg " 6. Installs Neovim and configures LazyVim."
printMsg "\nRun without arguments to start the setup."
}
# Installs a package if it's not already installed.
# Usage: install_package <package_name> [command_to_check]
install_package() {
local package_name="$1"
local command_to_check="${2:-$1}"
if [[ "$VERIFY_MODE" == "true" ]]; then
if command -v "$command_to_check" &>/dev/null; then
report_verify "$package_name" "Installed" ""
else
report_verify "$package_name" "Missing" "Action: Install via apt"
fi
return
fi
if command -v "$command_to_check" &>/dev/null; then
printInfoMsg "'${package_name}' is already installed. Skipping."
return
fi
printInfoMsg "Installing '${package_name}'..."
if ! sudo apt-get install -y "$package_name"; then
printErrMsg "Failed to install '${package_name}'. Please try installing it manually."
# We don't exit here to allow the rest of the setup to continue.
else
printOkMsg "Successfully installed '${package_name}'."
fi
}
# (Private) Fetches the latest version tag from GitHub API
_gh_get_latest_version() {
local repo="$1"
curl -s "https://api.github.com/repos/${repo}/releases/latest" | jq -r '.tag_name'
}
# (Private) Gets the installed version of a binary
_gh_get_installed_version() {
local binary_name="$1"
if ! command -v "$binary_name" &>/dev/null; then
echo "Not installed"
return
fi
local raw_version_output
raw_version_output=$("$binary_name" --version 2>/dev/null || "$binary_name" -v 2>/dev/null || echo "unknown")
local installed_version_string
installed_version_string=$(echo "$raw_version_output" | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -n 1)
if [[ -z "$installed_version_string" ]]; then installed_version_string="$raw_version_output"; fi
echo "$installed_version_string"
}
# (Private) Finds the download URL for the correct asset
_gh_find_download_url() {
local repo="$1"
local asset_regex="${2:-}"
curl -s "https://api.github.com/repos/${repo}/releases/latest" | \
jq -r --arg regex "$asset_regex" '.assets[] | select(.name | test("linux"; "i") and (test("amd64"; "i") or test("x86_64"; "i"))) | select(.name | test("\\.tar\\.gz$|\\.zip$"; "i")) | select($regex == "" or (.name | test($regex; "i"))) | .browser_download_url' | head -n 1
}
# (Private) Downloads and installs the binary
_gh_download_and_install() {
local download_url="$1"
local binary_name="$2"
local version="$3"
local temp_dir; temp_dir=$(mktemp -d); trap 'rm -rf "$temp_dir"' RETURN
local archive_name; archive_name=$(basename "$download_url")
if ! run_with_spinner "Downloading ${binary_name} ${version}..." curl -L -f "$download_url" -o "${temp_dir}/${archive_name}"; then
printErrMsg "Failed to download ${binary_name}. Please try installing it manually."
return 1
fi
if [[ "$archive_name" == *.tar.gz ]]; then
run_with_spinner "Extracting tarball..." tar -xzf "${temp_dir}/${archive_name}" -C "$temp_dir"
elif [[ "$archive_name" == *.zip ]]; then
run_with_spinner "Extracting zip..." unzip -o "${temp_dir}/${archive_name}" -d "$temp_dir"
else
printErrMsg "Unsupported archive format: ${archive_name}"; return 1
fi
local found_bin; found_bin=$(find "$temp_dir" -type f -name "$binary_name" | head -n 1)
if [[ -n "$found_bin" ]]; then
mkdir -p "${XDG_BIN_HOME}"
run_with_spinner "Installing to ${XDG_BIN_HOME}/${binary_name}..." mv "$found_bin" "${XDG_BIN_HOME}/${binary_name}"
chmod +x "${XDG_BIN_HOME}/${binary_name}"
printOkMsg "Successfully installed ${binary_name} ${version}."
else
printErrMsg "Binary '${binary_name}' not found in extracted archive."; ls -R "$temp_dir"; return 1
fi
}
# Generic installer for GitHub release binaries
# Usage: install_github_binary "owner/repo" "binary_name" [ "asset_regex" ]
install_github_binary() {
local repo="$1"
local binary_name="$2"
# Optional regex to match specific assets (e.g., "musl", "amd64"). Case-insensitive.
local asset_regex="${3:-}"
if [[ "$VERIFY_MODE" == "true" ]]; then
local installed_version_string
installed_version_string=$(_gh_get_installed_version "$binary_name")
if [[ "$installed_version_string" == "Not installed" ]]; then
report_verify "$binary_name" "Missing" "Repo: $repo"
else
# Fetch latest to compare
local latest_version; latest_version=$(_gh_get_latest_version "$repo")
local norm_latest="${latest_version#v}"; local norm_installed="${installed_version_string#v}"
if [[ "$norm_latest" == "$norm_installed" ]]; then report_verify "$binary_name" "Installed" "v$norm_installed";
else report_verify "$binary_name" "Outdated" "v$norm_installed -> $latest_version"; fi
fi
return
fi
if [[ "$(uname -m)" != "x86_64" ]]; then
printErrMsg "Unsupported architecture for ${binary_name}: $(uname -m). Only x86_64 is supported."
return 1
fi
printBanner "Install/Update ${binary_name} from ${repo}"
local latest_version
latest_version=$(_gh_get_latest_version "$repo")
if [[ -z "$latest_version" || "$latest_version" == "null" ]]; then
printErrMsg "Could not determine latest ${binary_name} version from GitHub API."
return 1
fi
printInfoMsg "Latest version: ${C_L_GREEN}${latest_version}${T_RESET}"
local installed_version_string
installed_version_string=$(_gh_get_installed_version "$binary_name")
printInfoMsg "Installed version: ${C_L_YELLOW}${installed_version_string}${T_RESET}"
local norm_latest="${latest_version#v}"
local norm_installed="${installed_version_string#v}"
if [[ "$norm_latest" == "$norm_installed" ]]; then
printOkMsg "You already have the latest version of ${binary_name} (${latest_version}). Skipping."
return 0
fi
if ! prompt_yes_no "Do you want to install/update to version ${latest_version}?" "y"; then
printInfoMsg "${binary_name} installation skipped."
return 0
fi
local download_url
download_url=$(_gh_find_download_url "$repo" "$asset_regex")
if [[ -z "$download_url" ]]; then
printErrMsg "Could not find a compatible download asset for ${repo} on Linux x86_64."
return 1
fi
_gh_download_and_install "$download_url" "$binary_name" "$latest_version"
}
# Installs or updates Go (Golang) to the latest stable version.
install_golang() {
printBanner "Install/Update Go (Golang)"
if [[ "$VERIFY_MODE" == "true" ]]; then
local installed_version="Not installed"
if command -v go &>/dev/null; then installed_version=$(go version | awk '{print $3}'); fi
if [[ "$installed_version" == "Not installed" ]]; then
report_verify "Go (Golang)" "Missing" ""
else
local latest_version; latest_version=$(curl -s "https://go.dev/dl/?mode=json" | jq -r '.[0].version')
if [[ "$installed_version" == "$latest_version" ]]; then report_verify "Go (Golang)" "Installed" "$installed_version";
else report_verify "Go (Golang)" "Outdated" "$installed_version -> $latest_version"; fi
fi
return
fi
# Determine architecture for download URL
local arch
if [[ "$(uname -m)" == "x86_64" ]]; then
arch="amd64"
else
printErrMsg "Unsupported architecture for Go: $(uname -m). Only x86_64 is supported."
return 1
fi
# Get latest version from the official Go JSON endpoint
local latest_version
printInfoMsg "Fetching latest Go version..."
latest_version=$(curl -s "https://go.dev/dl/?mode=json" | jq -r '.[0].version')
if [[ -z "$latest_version" ]]; then
printErrMsg "Could not determine the latest Go version from go.dev."
return
fi
printInfoMsg "Latest version: ${C_L_GREEN}${latest_version}${T_RESET}"
local installed_version="Not installed"
if command -v go &>/dev/null; then
# 'go version' output is like: go version go1.22.1 linux/amd64
installed_version=$(go version | awk '{print $3}')
fi
printInfoMsg "Installed version: ${C_L_YELLOW}${installed_version}${T_RESET}"
if [[ "$installed_version" == "$latest_version" ]]; then
printOkMsg "You already have the latest version of Go. Skipping."
return
fi
if ! prompt_yes_no "Do you want to install/update to version ${latest_version}?" "y"; then
printInfoMsg "Go installation skipped."
return
fi
local tarball_name="${latest_version}.linux-${arch}.tar.gz"
local download_url="https://go.dev/dl/${tarball_name}"
local install_path="/usr/local"
local temp_dir; temp_dir=$(mktemp -d)
trap 'rm -rf "$temp_dir"' RETURN
if run_with_spinner "Downloading Go ${latest_version}..." curl -L -f "$download_url" -o "${temp_dir}/${tarball_name}"; then
printInfoMsg "Removing any previous Go installation from ${install_path}..."
if [[ -d "${install_path}/go" ]]; then
sudo rm -rf "${install_path}/go"
fi
printInfoMsg "Extracting to ${install_path}..."
if sudo tar -C "$install_path" -xzf "${temp_dir}/${tarball_name}"; then
printOkMsg "Successfully installed Go ${latest_version}."
fi
else
printErrMsg "Failed to download Go. Please try installing it manually."
fi
}
# Installs zoxide (smarter cd) using the official install script
install_zoxide() {
printBanner "Install/Update zoxide"
local repo="ajeetdsouza/zoxide"
local binary_name="zoxide"
if [[ "$VERIFY_MODE" == "true" ]]; then
local installed_version_string; installed_version_string=$(_gh_get_installed_version "$binary_name")
if [[ "$installed_version_string" == "Not installed" ]]; then report_verify "zoxide" "Missing" "";
else
local latest_version; latest_version=$(_gh_get_latest_version "$repo")
local norm_latest="${latest_version#v}"; local norm_installed="${installed_version_string#v}"
if [[ "$norm_latest" == "$norm_installed" ]]; then report_verify "zoxide" "Installed" "v$norm_installed";
else report_verify "zoxide" "Outdated" "v$norm_installed -> $latest_version"; fi
fi
return
fi
local latest_version
latest_version=$(_gh_get_latest_version "$repo")
if [[ -z "$latest_version" || "$latest_version" == "null" ]]; then
printErrMsg "Could not determine latest zoxide version from GitHub API."
return 1
fi
printInfoMsg "Latest version: ${C_L_GREEN}${latest_version}${T_RESET}"
local installed_version_string
installed_version_string=$(_gh_get_installed_version "$binary_name")
printInfoMsg "Installed version: ${C_L_YELLOW}${installed_version_string}${T_RESET}"
local norm_latest="${latest_version#v}"
local norm_installed="${installed_version_string#v}"
if [[ "$norm_latest" == "$norm_installed" ]]; then
printOkMsg "You already have the latest version of zoxide (${latest_version}). Skipping."
return 0
fi
if ! prompt_yes_no "Do you want to install/update zoxide to version ${latest_version}?" "y"; then
printInfoMsg "zoxide installation skipped."
return 0
fi
if run_with_spinner "Installing zoxide via official script..." bash -c "curl -sS https://raw.githubusercontent.com/ajeetdsouza/zoxide/main/install.sh | BIN_DIR='${XDG_BIN_HOME}' bash"; then
printOkMsg "Successfully installed zoxide."
else
printErrMsg "Failed to install zoxide."
return 1
fi
}
# Installs starship (custom prompt) using the official install script
install_starship() {
printBanner "Install/Update Starship"
local repo="starship/starship"
local binary_name="starship"
if [[ "$VERIFY_MODE" == "true" ]]; then
local installed_version_string; installed_version_string=$(_gh_get_installed_version "$binary_name")
if [[ "$installed_version_string" == "Not installed" ]]; then report_verify "starship" "Missing" "";
else
local latest_version; latest_version=$(_gh_get_latest_version "$repo")
local norm_latest="${latest_version#v}"; local norm_installed="${installed_version_string#v}"
if [[ "$norm_latest" == "$norm_installed" ]]; then report_verify "starship" "Installed" "v$norm_installed";
else report_verify "starship" "Outdated" "v$norm_installed -> $latest_version"; fi
fi
return
fi
local latest_version
latest_version=$(_gh_get_latest_version "$repo")
if [[ -z "$latest_version" || "$latest_version" == "null" ]]; then
printErrMsg "Could not determine latest starship version from GitHub API."
return 1
fi
printInfoMsg "Latest version: ${C_L_GREEN}${latest_version}${T_RESET}"
local installed_version_string
installed_version_string=$(_gh_get_installed_version "$binary_name")
printInfoMsg "Installed version: ${C_L_YELLOW}${installed_version_string}${T_RESET}"
local norm_latest="${latest_version#v}"
local norm_installed="${installed_version_string#v}"
if [[ "$norm_latest" == "$norm_installed" ]]; then
printOkMsg "You already have the latest version of starship (${latest_version}). Skipping."
return 0
fi
if ! prompt_yes_no "Do you want to install/update starship to version ${latest_version}?" "y"; then
printInfoMsg "starship installation skipped."
return 0
fi
if run_with_spinner "Installing starship via official script..." sh -c "curl -sS https://starship.rs/install.sh | sh -s -- -y -b '${XDG_BIN_HOME}'"; then
printOkMsg "Successfully installed starship."
else
printErrMsg "Failed to install starship."
return 1
fi
}
# Downloads and installs the latest stable version of Neovim.
install_neovim() {
printBanner "Installing/Updating Neovim (Latest Stable)"
if [[ "$VERIFY_MODE" == "true" ]]; then
local installed_version="0"
local version_file="${XDG_STATE_HOME}/nvim-version"
if [[ -f "$version_file" ]]; then installed_version=$(<"$version_file"); fi
if [[ "$installed_version" == "0" ]]; then
report_verify "Neovim (AppImage)" "Missing" ""
else
local latest_version_tag; latest_version_tag=$(_gh_get_latest_version "neovim/neovim")
local latest_version="${latest_version_tag#v}"
if [[ "$installed_version" == "$latest_version" ]]; then report_verify "Neovim" "Installed" "v$installed_version";
else report_verify "Neovim" "Outdated" "v$installed_version -> $latest_version"; fi
fi
return
fi
local bin_dir="${XDG_BIN_HOME}"
local version_file="${XDG_STATE_HOME}/nvim-version"
mkdir -p "$bin_dir"
mkdir -p "$(dirname "$version_file")"
printInfoMsg "Checking for latest Neovim version..."
local latest_version_tag
latest_version_tag=$(_gh_get_latest_version "neovim/neovim")
if [[ -z "$latest_version_tag" || "$latest_version_tag" == "null" ]]; then
printErrMsg "Could not determine latest Neovim version from GitHub API."
return 1
fi
local latest_version="${latest_version_tag#v}"
printInfoMsg "Latest stable version is: ${latest_version_tag}"
local installed_version="0"
if [[ -f "$version_file" ]]; then
installed_version=$(<"$version_file")
fi
# Also check for a system-installed nvim to report its version
if command -v nvim &>/dev/null; then
local system_version_output
system_version_output=$(nvim --version 2>/dev/null | head -n 1)
local system_version
system_version=$(echo "$system_version_output" | awk '{print $2}')
printInfoMsg "Found installed Neovim version: ${system_version} (managed by this script: v${installed_version})"
fi
if [[ "$installed_version" == "$latest_version" ]]; then
printOkMsg "You already have the latest version of Neovim (v${installed_version}). Skipping."
return 0
fi
if ! prompt_yes_no "Do you want to install/update Neovim to ${latest_version_tag}?" "y"; then
printInfoMsg "Neovim installation skipped."
return 0
fi
# Use AppImage for Linux
local nvim_appimage_path="${bin_dir}/nvim-linux-x86_64.appimage"
local nvim_url="https://github.com/neovim/neovim/releases/download/${latest_version_tag}/nvim-linux-x86_64.appimage"
if run_with_spinner "Downloading Neovim AppImage ${latest_version_tag}..." curl -L -f "$nvim_url" -o "$nvim_appimage_path"; then
chmod +x "$nvim_appimage_path"
ln -sf "$nvim_appimage_path" "${bin_dir}/nvim"
echo "$latest_version" > "$version_file"
printOkMsg "Neovim ${latest_version_tag} installed to ${bin_dir}/nvim"
else
printErrMsg "Failed to download Neovim AppImage."
return 1
fi
}
# Backs up existing Neovim config and clones the LazyVim starter.
setup_lazyvim() {
printBanner "Setting up LazyVim"
local nvim_config_dir="${XDG_CONFIG_HOME}/nvim"
local lazyvim_json_path="${nvim_config_dir}/lazyvim.json"
if [[ "$VERIFY_MODE" == "true" ]]; then
if [[ -f "$lazyvim_json_path" ]]; then
report_verify "LazyVim Config" "Installed" ""
else
report_verify "LazyVim Config" "Missing" "lazyvim.json not found"
fi
return
fi
# Check if LazyVim is already installed by looking for lazyvim.json
if [[ -f "$lazyvim_json_path" ]]; then
printInfoMsg "LazyVim is already installed (found lazyvim.json). Skipping setup."
return
fi
# If not, check if a generic nvim config directory exists
if [[ -d "$nvim_config_dir" ]]; then
printWarnMsg "Found an existing Neovim configuration that is not LazyVim."
if prompt_yes_no "Do you want to back it up and replace it with the LazyVim starter?" "y"; then
local backup_dir="${nvim_config_dir}.bak_$(date +"%Y%m%d_%H%M%S")"
printInfoMsg "Backing up current config to ${backup_dir}..."
mv "$nvim_config_dir" "$backup_dir"
printOkMsg "Backup complete."
else
printInfoMsg "Skipping LazyVim setup as requested."
return
fi
fi
# Clone LazyVim starter
printInfoMsg "Cloning the LazyVim starter repository..."
if run_with_spinner "Cloning LazyVim..." git clone https://github.com/LazyVim/starter "$nvim_config_dir"; then
printOkMsg "LazyVim starter cloned to ${nvim_config_dir}."
printInfoMsg "You can now start Neovim by running: ${C_L_CYAN}nvim${T_RESET}"
else
printErrMsg "Failed to clone LazyVim starter repository."
return 1
fi
}
# Copies custom LazyVim plugin configs
setup_lazyvim_plugins() {
printBanner "Setting up Custom LazyVim Plugins"
local source_plugins_dir="${SCRIPT_DIR}/config/nvim/lua/plugins"
local dest_plugins_dir="${XDG_CONFIG_HOME}/nvim/lua/plugins"
if [[ "$VERIFY_MODE" == "true" ]]; then
# Just check if dest dir exists for now, deep check is complex
if [[ -d "$dest_plugins_dir" ]]; then report_verify "LazyVim Plugins" "Installed" ""; else report_verify "LazyVim Plugins" "Missing" ""; fi
return
fi
if [[ ! -d "$source_plugins_dir" ]] || [[ -z "$(ls -A "$source_plugins_dir")" ]]; then
printInfoMsg "No custom LazyVim plugins found to install. Skipping."
return
fi
# This function should only run if LazyVim is installed.
if [[ ! -d "${XDG_CONFIG_HOME}/nvim/lua" ]]; then
printWarnMsg "LazyVim installation not found at '${XDG_CONFIG_HOME}/nvim'. Skipping custom plugin setup."
return
fi
printInfoMsg "Copying nvim plugin configs to ${dest_plugins_dir}..."
mkdir -p "$dest_plugins_dir"
local file_copied=false
for src_file in "$source_plugins_dir"/*.lua; do
if [[ ! -f "$src_file" ]]; then continue; fi
local filename; filename=$(basename "$src_file")
local dest_file="${dest_plugins_dir}/${filename}"
# For plugins, we just want to ensure they exist.
# We won't prompt for overwrite, just copy if it's not there.
if [[ ! -f "$dest_file" ]]; then
cp "$src_file" "$dest_file"
printOkMsg "Copied new plugin config '${filename}'."
file_copied=true
else
printInfoMsg "Plugin config '${filename}' already exists. Skipping."
fi
done
}
# Clones and installs fzf from the official GitHub repository.
install_fzf_from_source() {
printBanner "Installing fzf (from source)"
local fzf_dir="${XDG_DATA_HOME}/fzf"
if [[ "$VERIFY_MODE" == "true" ]]; then
if [[ -d "$fzf_dir" ]]; then report_verify "fzf (source)" "Installed" ""; else report_verify "fzf (source)" "Missing" ""; fi
return
fi
if [[ -d "$fzf_dir" ]]; then
printInfoMsg "fzf is already installed. Updating..."
if ! run_with_spinner "Updating fzf repo..." git -C "$fzf_dir" pull; then
printErrMsg "Failed to update fzf."
return 1
fi
else
printInfoMsg "Cloning fzf repository..."
mkdir -p "$(dirname "$fzf_dir")"
local fzf_repo="https://github.com/junegunn/fzf.git"
if ! run_with_spinner "Cloning fzf..." git clone --depth 1 "$fzf_repo" "$fzf_dir"; then
printErrMsg "Failed to clone fzf repository."
return 1
fi
fi
# Run the fzf install script non-interactively.
printInfoMsg "Running fzf install script..."
if ! run_with_spinner "Installing fzf binaries..." "${fzf_dir}/install" --all; then
printErrMsg "fzf install script failed."
return 1
fi
}
# Sets up custom fzf configuration and preview script.
setup_fzf_config() {
printBanner "Setting up Custom FZF Configuration"
local bin_dir="${XDG_BIN_HOME}"
mkdir -p "$bin_dir"
if [[ "$VERIFY_MODE" == "true" ]]; then
if [[ -f "${bin_dir}/fzf-preview.sh" ]]; then report_verify "fzf-preview.sh" "Installed" ""; else report_verify "fzf-preview.sh" "Missing" ""; fi
return
fi
# --- Download fzf-preview.sh script ---
local preview_script_path="${bin_dir}/fzf-preview.sh"
local preview_script_url="https://raw.githubusercontent.com/junegunn/fzf/master/bin/fzf-preview.sh"
if [[ ! -f "$preview_script_path" ]]; then
if run_with_spinner "Downloading fzf-preview.sh..." curl -L -f -o "$preview_script_path" "$preview_script_url"; then
chmod +x "$preview_script_path"
printOkMsg "fzf-preview.sh downloaded successfully."
else
printErrMsg "Failed to download fzf-preview.sh."
fi
fi
}
# Configures git to use delta as the default pager
configure_git_delta() {
# Ensure local bin is in PATH so we can detect delta if it was just installed
if [[ "$VERIFY_MODE" == "true" ]]; then
local current_pager; current_pager=$(git config --global core.pager || true)
if [[ "$current_pager" == "delta" ]]; then report_verify "Git Delta" "Configured" ""; else report_verify "Git Delta" "Not Configured" "core.pager != delta"; fi
return
fi
export PATH="${XDG_BIN_HOME}:${PATH}"
if ! command -v delta &>/dev/null; then
printInfoMsg "delta not found. Skipping git configuration."
return
fi
printBanner "Configuring Delta (Git Pager)"
# Check if core.pager is already delta
local current_pager
current_pager=$(git config --global core.pager || true)
if [[ "$current_pager" == "delta" ]]; then
printInfoMsg "Git is already configured to use delta. Skipping."
return
fi
if prompt_yes_no "Configure 'delta' as the default git pager?" "y"; then
printInfoMsg "Setting git config..."
git config --global core.pager "delta"
git config --global interactive.diffFilter "delta --color-only"
git config --global delta.navigate true
git config --global delta.light false
git config --global merge.conflictstyle "diff3"
git config --global diff.colorMoved "default"
printOkMsg "Git configured to use delta."
fi
}
# Configures .bashrc with a consolidated block of environment settings
configure_shell_environment() {
printBanner "Configuring Shell Environment"
local bashrc="${HOME}/.bashrc"
local marker_start="# --- DEV MACHINE SETUP START ---"
local marker_end="# --- DEV MACHINE SETUP END ---"
if [[ "$VERIFY_MODE" == "true" ]]; then
if grep -qF "$marker_start" "$bashrc"; then report_verify ".bashrc config" "Present" ""; else report_verify ".bashrc config" "Missing" "Setup block not found"; fi
return
fi
if [[ ! -f "$bashrc" ]]; then
printWarnMsg "Could not find ${bashrc}. Skipping shell configuration."
return
fi
# Ensure local bin is in PATH so we can detect newly installed tools
export PATH="${XDG_BIN_HOME}:${PATH}"
local block_exists=false
if grep -qF "$marker_start" "$bashrc"; then
block_exists=true
fi
# Construct the configuration block
local config_block=""
config_block+="${marker_start}\n"
config_block+="[[ \":\$PATH:\" != *\":\$HOME/.local/bin:\"* ]] && export PATH=\"\$HOME/.local/bin:\$PATH\"\n"
config_block+="[[ \":\$PATH:\" != *\":/usr/local/go/bin:\"* ]] && export PATH=\"\$PATH:/usr/local/go/bin\"\n"
config_block+="[[ \":\$PATH:\" != *\":\$HOME/go/bin:\"* ]] && export PATH=\"\$PATH:\$HOME/go/bin\"\n"
# Tool Integrations
if command -v starship &>/dev/null; then
config_block+="\n# Starship (prompt)\n"
config_block+="eval \"\$(starship init bash)\"\n"
fi
if command -v zoxide &>/dev/null; then
config_block+="\n# Zoxide (better cd)\n"
config_block+="eval \"\$(zoxide init bash)\"\n"
fi
config_block+="${marker_end}"
# If block exists, check if it's identical to what we would write.
if $block_exists; then
local existing_block
existing_block=$(sed -n "/^${marker_start}$/,/^${marker_end}$/p" "$bashrc")
# Compare existing block with the one we want to write.
if [[ "$existing_block" == "$(echo -e "${config_block}")" ]]; then
printInfoMsg "Shell configuration is already up to date. Skipping."
return
fi
fi
local prompt_msg="Add environment configuration to .bashrc?"
if $block_exists; then
prompt_msg="Your shell configuration is out of date. Update it?"
fi
printMsg ""
if prompt_yes_no "$prompt_msg" "y"; then
local backup_file="${bashrc}.bak_$(date +"%Y%m%d_%H%M%S")"
cp "$bashrc" "$backup_file"
printInfoMsg "Backup created at: ${backup_file}"
if $block_exists; then
sed -i "/^${marker_start}$/,/^${marker_end}$/d" "$bashrc"
fi
echo -e "\n${config_block}" >> "$bashrc"
printOkMsg "Injected/Updated shell configuration in .bashrc."
printInfoMsg "Please run '${C_L_CYAN}source ~/.bashrc${T_RESET}' to apply changes."
fi
}
# Copies custom binaries/scripts to ~/.local/bin
setup_binaries() {
printBanner "Setting up Custom Binaries"
local source_bin_path="${SCRIPT_DIR}/bin"
local dest_bin_path="${XDG_BIN_HOME}"
if [[ "$VERIFY_MODE" == "true" ]]; then
# Simple check: count files
if [[ -d "$dest_bin_path" ]]; then report_verify "Custom Binaries" "Installed" "$(ls "$dest_bin_path"/dv-* 2>/dev/null | wc -l) scripts"; else report_verify "Custom Binaries" "Missing" ""; fi
return
fi
if [[ ! -d "$source_bin_path" ]] || [[ -z "$(ls -A "$source_bin_path")" ]]; then
printInfoMsg "No custom binaries found in '${source_bin_path}'. Skipping."
return
fi
printInfoMsg "Copying binaries to ${dest_bin_path}..."
mkdir -p "$dest_bin_path"
cp "$source_bin_path"/* "$dest_bin_path/"
# chmod +x only dv-* files to avoid touching unrelated files
chmod +x "${dest_bin_path}"/dv-* 2>/dev/null || true
# Ensure library files are not executable
if [[ -f "${dest_bin_path}/dv-common.sh" ]]; then
chmod -x "${dest_bin_path}/dv-common.sh"
fi
printOkMsg "Custom binaries installed."
}
# Copies the .bash_aliases file to the user's home directory.
setup_bash_aliases() {
printBanner "Setting up .bash_aliases"
local source_aliases_path="${SCRIPT_DIR}/config/bash/.bash_aliases"
local dest_aliases_path="${HOME}/.bash_aliases"
if [[ "$VERIFY_MODE" == "true" ]]; then
if [[ ! -f "$dest_aliases_path" ]]; then report_verify ".bash_aliases" "Missing" "";
elif cmp -s "$source_aliases_path" "$dest_aliases_path"; then report_verify ".bash_aliases" "Synced" "";
else report_verify ".bash_aliases" "Differs" "Content mismatch"; fi
return
fi
if [[ ! -f "$source_aliases_path" ]]; then
printErrMsg "Could not find '.bash_aliases' in the script directory: ${SCRIPT_DIR}"
return 1
fi
if [[ -f "$dest_aliases_path" ]]; then
if cmp -s "$source_aliases_path" "$dest_aliases_path"; then
printInfoMsg "'~/.bash_aliases' is identical to source. Skipping."
return
fi
if prompt_yes_no "File '~/.bash_aliases' already exists. Back it up and overwrite it?" "y"; then
local backup_file
backup_file="${dest_aliases_path}.bak_$(date +"%Y%m%d_%H%M%S")"
printInfoMsg "Backing up current file to ${backup_file}..."
cp "$dest_aliases_path" "$backup_file"
cp "$source_aliases_path" "$dest_aliases_path"
printOkMsg "Backup created and '~/.bash_aliases' has been overwritten."
else
printInfoMsg "Skipping '.bash_aliases' setup."
fi
else
cp "$source_aliases_path" "$dest_aliases_path"
printOkMsg "Copied '.bash_aliases' to your home directory."
fi
}
# Copies the tmux configuration to ~/.config/tmux/tmux.conf
setup_tmux_config() {
printBanner "Setting up Tmux Configuration"
local source_conf_path="${SCRIPT_DIR}/config/tmux/tmux.conf"
local dest_conf_dir="${XDG_CONFIG_HOME}/tmux"
local dest_conf_path="${dest_conf_dir}/tmux.conf"
if [[ "$VERIFY_MODE" == "true" ]]; then
if [[ ! -f "$dest_conf_path" ]]; then report_verify "tmux.conf" "Missing" "";
elif cmp -s "$source_conf_path" "$dest_conf_path"; then report_verify "tmux.conf" "Synced" "";
else report_verify "tmux.conf" "Differs" "Content mismatch"; fi
return
fi
if [[ ! -f "$source_conf_path" ]]; then
printWarnMsg "Could not find 'tmux.conf' in: ${source_conf_path}"
return
fi
if [[ ! -d "$dest_conf_dir" ]]; then
printInfoMsg "Creating directory: ${dest_conf_dir}"
mkdir -p "$dest_conf_dir"
fi
if [[ -f "$dest_conf_path" ]]; then
if cmp -s "$source_conf_path" "$dest_conf_path"; then
printInfoMsg "'tmux.conf' is identical to source. Skipping."
# Fall through to script setup
elif prompt_yes_no "File '${dest_conf_path}' already exists. Back it up and overwrite it?" "y"; then
local backup_file
backup_file="${dest_conf_path}.bak_$(date +"%Y%m%d_%H%M%S")"
printInfoMsg "Backing up current file to ${backup_file}..."
cp "$dest_conf_path" "$backup_file"
cp "$source_conf_path" "$dest_conf_path"
printOkMsg "Backup created and 'tmux.conf' has been overwritten."
else
printInfoMsg "Skipping 'tmux.conf' setup."
fi
else
cp "$source_conf_path" "$dest_conf_path"
printOkMsg "Copied 'tmux.conf' to '${dest_conf_path}'."
fi
# Setup Tmux Scripts
local source_scripts_dir="${SCRIPT_DIR}/config/tmux/scripts/dv"