-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·738 lines (632 loc) · 22.3 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·738 lines (632 loc) · 22.3 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
#!/bin/bash
# Set error handling
set -euo pipefail
RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
NC='\033[0m'
export RED GREEN BLUE YELLOW NC
info() { echo -e "${BLUE}[INFO]${NC} $1"; }
success() { echo -e "${GREEN}[SUCCESS]${NC} $1"; }
warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
error() { echo -e "${RED}[ERROR]${NC} $1"; }
export -f info success warn error
show_help() {
cat <<EOF
Usage:
$0 [options]
Options:
-y, --yes, --unattended Run in non-interactive mode using each prompt's default answer
--with-ollama Install Ollama and start its background service
-h, --help Show this help message
EOF
}
show_next_steps() {
cat <<'EOF'
Next steps:
1. Run 'exec fish -l' or restart your terminal.
2. Run 'tide configure' to set up your prompt, or use:
tide configure --auto \
--style=Lean \
--prompt_colors='16 colors' \
--show_time='24-hour format' \
--lean_prompt_height='Two lines' \
--prompt_connection=Disconnected \
--prompt_spacing=Sparse \
--icons='Many icons' \
--transient=Yes
3. All language runtimes, LSPs, and CLI tools have been auto-installed via mise.
(Check 'mise ls' to see the managed tools).
EOF
if [ "${SHOW_OLLAMA_NEXT_STEPS:-false}" = true ]; then
cat <<'EOF'
Optional local LLM:
- Pull at least one Ollama model, for example: ollama pull llama3.2
- Set ~/.fish.local.fish:
set -gx AICHAT_MODEL "local-llm:llama3.2"
EOF
fi
cat <<'EOF'
Manual follow-up:
- Edit ~/.fish.local.fish for private ENVs and API keys.
- Edit ~/.ghostty.local for machine-specific shortcuts or overrides.
- Edit ~/.gitconfig.local and ~/.gitconfig.work for Git identity.
- Run 'musicfox' (or 'mfx' inside Fish) to log in; its private config lives at ~/.local/share/go-musicfox/config.toml.
- macOS: Allow Ghostty in 'System Settings > Privacy & Security > Accessibility'.
EOF
}
fish_plugins_args() {
local fish_plugins_file="$1"
local plugins=()
local line=""
while IFS= read -r line || [ -n "$line" ]; do
line="${line#"${line%%[![:space:]]*}"}"
line="${line%"${line##*[![:space:]]}"}"
[ -z "$line" ] && continue
[[ "$line" =~ ^# ]] && continue
plugins+=("$line")
done <"$fish_plugins_file"
[ "${#plugins[@]}" -eq 0 ] && return 0
printf '%s\n' "${plugins[@]}"
}
update_fish_local_path_block() {
local fish_local_conf="$1"
shift || true
local begin_marker="# >>> dotfiles auto-migrated zsh PATH >>>"
local end_marker="# <<< dotfiles auto-migrated zsh PATH <<<"
local tmp_file=""
local escaped_path=""
local path_entry=""
tmp_file=$(mktemp)
if [ -f "$fish_local_conf" ]; then
awk -v begin="$begin_marker" -v end="$end_marker" '
$0 == begin { skip = 1; next }
$0 == end { skip = 0; next }
!skip { print }
' "$fish_local_conf" >"$tmp_file"
fi
if [ "$#" -gt 0 ]; then
if [ -s "$tmp_file" ]; then
printf '\n' >>"$tmp_file"
fi
printf '%s\n' "$begin_marker" >>"$tmp_file"
printf '%s\n' "# 从旧 zsh 配置中检测出的自定义 PATH;集中写入本地文件,避免污染 fish_user_paths universal state。" >>"$tmp_file"
for path_entry in "$@"; do
escaped_path="${path_entry//\\/\\\\}"
escaped_path="${escaped_path//\"/\\\"}"
escaped_path="${escaped_path//\$/\\$}"
printf '%s\n' "if test -d \"$escaped_path\"" >>"$tmp_file"
printf '%s\n' " fish_add_path --append --path \"$escaped_path\"" >>"$tmp_file"
printf '%s\n' "end" >>"$tmp_file"
done
printf '%s\n' "$end_marker" >>"$tmp_file"
fi
mv "$tmp_file" "$fish_local_conf"
}
zsh_config_has_user_content() {
local config_file="$1"
[ -f "$config_file" ] || return 1
awk '
/^[[:space:]]*#/ { next }
NF {
found = 1
exit
}
END {
exit(found ? 0 : 1)
}
' "$config_file"
}
zsh_usage_signal_summary() {
local summary=""
local config_file=""
local config_name=""
local -a zsh_config_files=(
"$HOME/.zshenv"
"$HOME/.zprofile"
"$HOME/.zshrc"
"$HOME/.zlogin"
)
if [[ "${SHELL:-}" == *"/zsh" ]]; then
summary="当前 SHELL 为 zsh"
fi
for config_file in "${zsh_config_files[@]}"; do
if zsh_config_has_user_content "$config_file"; then
config_name="$(basename "$config_file")"
if [ -n "$summary" ]; then
summary="${summary};检测到非空 ${config_name}"
else
summary="检测到非空 ${config_name}"
fi
fi
done
[ -n "$summary" ] || return 1
printf '%s\n' "$summary"
}
should_migrate_zsh_path() {
local signal_summary=""
local prompt=""
local default_answer="y"
if ! command -v zsh &>/dev/null; then
info "zsh not found, skipping PATH migration."
return 1
fi
if ! signal_summary="$(zsh_usage_signal_summary)"; then
info "No explicit zsh usage detected, skipping zsh PATH migration."
return 1
fi
info "Detected zsh usage signals: $signal_summary"
if [[ "${SHELL:-}" == *"/fish" ]]; then
info "Current shell is already fish; zsh PATH migration defaults to skip."
prompt="Legacy zsh configuration detected. Do you want to migrate extra zsh PATH into ~/.fish.local.fish?"
default_answer="n"
else
prompt="Do you want to migrate extra zsh PATH into ~/.fish.local.fish?"
default_answer="y"
fi
if ask_yes_no "$prompt" "$default_answer"; then
return 0
fi
info "Skipping zsh PATH migration."
return 1
}
# Parse arguments
NON_INTERACTIVE=false
INSTALL_OLLAMA=false
START_OLLAMA_SERVICE=false
while [[ "$#" -gt 0 ]]; do
case $1 in
-y | --yes | --unattended) NON_INTERACTIVE=true ;;
--with-ollama)
INSTALL_OLLAMA=true
START_OLLAMA_SERVICE=true
;;
-h | --help)
show_help
exit 0
;;
*)
error "Unknown parameter: $1"
exit 1
;;
esac
shift
done
ask_yes_no() {
local prompt="$1"
local default_answer="${2:-n}"
local prompt_suffix=""
local reply=""
case "$default_answer" in
y | Y)
default_answer="y"
prompt_suffix="[Y/n]"
;;
n | N)
default_answer="n"
prompt_suffix="[y/N]"
;;
*)
error "ask_yes_no received invalid default answer: $default_answer"
exit 1
;;
esac
if [ "$NON_INTERACTIVE" = true ]; then
info "${prompt} ${prompt_suffix} (auto-$default_answer)"
if [ "$default_answer" = "y" ]; then
return 0
fi
return 1
fi
while true; do
# 从 /dev/tty 直接读取终端输入,避免上一步命令(brew/zsh/fish 等)
# 向 stdin 写入残留字节导致 read 跳过用户输入的问题。
read -r -p "$prompt $prompt_suffix " reply </dev/tty
if [ -z "$reply" ]; then
reply="$default_answer"
fi
case "$reply" in
[Yy]) return 0 ;;
[Nn]) return 1 ;;
*)
warn "Please answer y or n."
;;
esac
done
}
brewfile_declared_taps() {
local brewfile="$1"
sed -nE '
/^[[:space:]]*#/d
s/^[[:space:]]*tap[[:space:]]+"([^"]+)".*/\1/p
' "$brewfile"
}
ensure_brewfile_taps() {
local brewfile="$1"
local tap_name=""
local -a declared_taps=()
while IFS= read -r tap_name; do
[ -n "$tap_name" ] || continue
declared_taps+=("$tap_name")
done < <(brewfile_declared_taps "$brewfile")
[ "${#declared_taps[@]}" -eq 0 ] && return 0
info "Preparing Homebrew taps declared in Brewfile..."
for tap_name in "${declared_taps[@]}"; do
if brew tap | grep -Fx -- "$tap_name" >/dev/null 2>&1; then
info " -> Tap already available: $tap_name"
continue
fi
info " -> Tapping $tap_name"
if brew tap "$tap_name"; then
success " -> Tap ready: $tap_name"
else
error "Failed to tap Homebrew tap: $tap_name"
return 1
fi
done
}
resolve_ollama_install_plan() {
if [ "$INSTALL_OLLAMA" = true ]; then
info "Ollama optional install enabled via CLI flag."
return 0
fi
if [ "$NON_INTERACTIVE" = true ]; then
info "Non-interactive mode detected, skipping optional Ollama installation by default."
return 0
fi
# Ollama 体积和后续模型下载成本都明显高于普通 CLI,因此仅在交互安装时按需启用。
if ask_yes_no "Do you want to install Ollama for local models?" "n"; then
INSTALL_OLLAMA=true
if ask_yes_no "Do you want to start Ollama service now?" "y"; then
START_OLLAMA_SERVICE=true
else
info "Skipping Ollama service startup. You can run 'brew services start ollama' later."
fi
fi
}
install_optional_ollama() {
local ollama_available=false
local ollama_managed_by_brew=false
[ "$INSTALL_OLLAMA" = true ] || return 0
if command -v ollama &>/dev/null; then
success "Ollama is already available."
ollama_available=true
else
info "Installing Ollama..."
if brew install ollama; then
success "Ollama installed."
ollama_available=true
else
warn "Ollama installation had issues. You can rerun manually: brew install ollama"
return 0
fi
fi
if brew list --formula ollama >/dev/null 2>&1; then
ollama_managed_by_brew=true
fi
if [ "$START_OLLAMA_SERVICE" = true ]; then
if [ "$ollama_managed_by_brew" = true ]; then
info "Starting Ollama service..."
if brew services start ollama; then
success "Ollama service started."
else
warn "Failed to start Ollama service automatically. You can rerun manually: brew services start ollama"
fi
else
warn "Ollama is not managed by Homebrew on this machine. Please start it manually with 'ollama serve' if needed."
fi
else
info "Skipping Ollama service startup. Run 'brew services start ollama' when you need local models."
fi
if [ "$ollama_available" = true ]; then
SHOW_OLLAMA_NEXT_STEPS=true
fi
}
configure_shottr_hotkeys() {
local shottr_hotkeys_script="$DOTFILES_DIR/bin/configure-shottr-hotkeys"
if ! brew list --cask shottr >/dev/null 2>&1; then
info "Shottr is not installed, skipping Shottr hotkey setup."
return 0
fi
if [ ! -x "$shottr_hotkeys_script" ]; then
warn "Shottr hotkey helper not found at $shottr_hotkeys_script, skipping."
return 0
fi
if [ "$NON_INTERACTIVE" = true ]; then
info "Non-interactive mode detected, skipping optional Shottr hotkey setup."
return 0
fi
# Shottr 的全局热键会抢占系统级按键事件,因此只在用户显式确认后写入。
if ask_yes_no "Do you want to apply the recommended Shottr hotkeys (Shift+Cmd+1/2/A/S)?" "n"; then
info "Configuring Shottr hotkeys..."
if "$shottr_hotkeys_script" --force; then
success "Shottr hotkeys configured."
else
warn "Shottr hotkey configuration had issues. You can rerun manually: $shottr_hotkeys_script --force"
fi
else
info "Skipping Shottr hotkeys. You can rerun manually later: $shottr_hotkeys_script --force"
fi
}
if [ "$(uname -s)" != "Darwin" ]; then
error "This dotfiles setup is only supported on macOS."
exit 1
fi
info "Starting dotfiles installation..."
if ! command -v brew &>/dev/null; then
warn "Homebrew not found."
if ask_yes_no "Install Homebrew from official source?" "y"; then
info "Installing Homebrew..."
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
eval "$(/opt/homebrew/bin/brew shellenv 2>/dev/null || /usr/local/bin/brew shellenv 2>/dev/null)"
success "Homebrew installed successfully."
else
error "Homebrew is required. Please install it manually from https://brew.sh"
exit 1
fi
else
success "Homebrew is already installed."
fi
DOTFILES_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
BREW_BUNDLE_CASK_SKIP=""
SKIPPED_PRIVILEGED_CASKS=""
info "Checking current status..."
if [ -f "$DOTFILES_DIR/Brewfile" ]; then
# `karabiner-elements` 通过 pkg 安装,Homebrew 可能触发 sudo。
# 在 -y / --unattended 模式下,如果当前没有可复用的 sudo 凭证,就跳过它,
# 避免整个安装流程卡死在密码提示。
if [ "$NON_INTERACTIVE" = true ] && ! sudo -n true >/dev/null 2>&1; then
SKIPPED_PRIVILEGED_CASKS="karabiner-elements"
BREW_BUNDLE_CASK_SKIP="$SKIPPED_PRIVILEGED_CASKS"
warn "Non-interactive mode detected without cached sudo credentials."
warn "Skipping privileged casks for now: $SKIPPED_PRIVILEGED_CASKS"
fi
# 刷新 Homebrew 元数据缓存:本仓库在 fish 中默认设置 HOMEBREW_NO_AUTO_UPDATE=1
# (见 fish/dot-config/fish/config.fish),该变量会被 install.sh 继承,使得后续
# `brew bundle` 不再自动 update。一旦本地 cask API 缓存陈旧、与新版 Homebrew 的解析
# 逻辑发生 schema 错位,加载某些 cask(典型如 openmtp/shottr)时会崩溃并报
# `undefined method 'first' for nil`,导致整个 bundle 失败。故在安装前显式 update 一次。
# 网络抖动等导致的 update 失败不应中断安装,降级为告警后继续。
info "Refreshing Homebrew metadata (brew update)..."
if ! brew update; then
warn "brew update failed; continuing with possibly stale Homebrew metadata."
fi
# 先显式准备 Brewfile 中声明的 tap,避免首次安装时第三方 formula
# 在 `brew bundle install` 解析阶段尚未可见,导致整个依赖安装提前失败。
if ! ensure_brewfile_taps "$DOTFILES_DIR/Brewfile"; then
error "Failed to prepare Brew taps declared in Brewfile."
exit 1
fi
if [ -n "$BREW_BUNDLE_CASK_SKIP" ]; then
BREW_BUNDLE_CHECK_CMD=(env "HOMEBREW_BUNDLE_CASK_SKIP=$BREW_BUNDLE_CASK_SKIP" brew bundle check --file="$DOTFILES_DIR/Brewfile")
BREW_BUNDLE_INSTALL_CMD=(env "HOMEBREW_BUNDLE_CASK_SKIP=$BREW_BUNDLE_CASK_SKIP" brew bundle install --file="$DOTFILES_DIR/Brewfile")
else
BREW_BUNDLE_CHECK_CMD=(brew bundle check --file="$DOTFILES_DIR/Brewfile")
BREW_BUNDLE_INSTALL_CMD=(brew bundle install --file="$DOTFILES_DIR/Brewfile")
fi
if ! "${BREW_BUNDLE_CHECK_CMD[@]}" >/dev/null 2>&1; then
info "Installing/Updating dependencies..."
if "${BREW_BUNDLE_INSTALL_CMD[@]}"; then
success "Brew dependencies installed."
else
error "Brew bundle install failed."
exit 1
fi
else
if [ -n "$SKIPPED_PRIVILEGED_CASKS" ]; then
success "All non-privileged Brew dependencies are already satisfied."
else
success "All Brew dependencies are already satisfied."
fi
fi
else
error "Brewfile not found at $DOTFILES_DIR/Brewfile"
exit 1
fi
info "Installing additional Fonts..."
if ask_yes_no "Do you want to install additional fonts (Maple Mono, Geist Mono)?" "n"; then
REQUIRED_CASKS=(
font-maple-mono-nf
font-geist-mono-nerd-font
)
if brew install --cask --force "${REQUIRED_CASKS[@]}"; then
success "Fonts installed."
else
warn "Font installation had issues."
fi
fi
resolve_ollama_install_plan
install_optional_ollama
configure_shottr_hotkeys
info "Linking configuration files with stow..."
mkdir -p "$HOME/.local/bin"
STOW_PACKAGES=(ghostty fish helix zellij mise karabiner bat btop git aichat)
# Clean up existing config directories and stow packages
for pkg in "${STOW_PACKAGES[@]}"; do
DIR="$HOME/.config/$pkg"
# Ensure any existing directory is either unlinked (if symlink) or backed up (if real dir).
# This guarantees stow creates a pure directory-level mapping to ~/dotfiles,
# ensuring locally generated files (like Karabiner/Btop UI saves) are tracked automatically.
if [ -L "$DIR" ]; then
warn "$DIR is a symlink, unlinking..."
unlink "$DIR"
elif [ -d "$DIR" ]; then
BACKUP_DIR="${DIR}.bak.$(date +%Y%m%d_%H%M%S)"
warn "Backing up existing real directory $DIR -> $BACKUP_DIR"
mv "$DIR" "$BACKUP_DIR"
fi
info "Stowing $pkg..."
stow --restow --target="$HOME" --dir="$DOTFILES_DIR" --dotfiles "$pkg"
done
# Always stow bin for scripts, they are useful in CLI
info "Stowing bin..."
stow --restow --target="$HOME/.local/bin" --dir="$DOTFILES_DIR" bin
info "Synchronizing AIChat model catalog..."
if command -v aichat &>/dev/null; then
if aichat --sync-models; then
success "AIChat model catalog synchronized."
else
warn "AIChat model sync had issues. You can rerun manually: aichat --sync-models"
fi
else
warn "AIChat not found, skipping model sync. You can rerun manually after installation: aichat --sync-models"
fi
info "Installing all runtimes and CLI tools managed by Mise..."
if command -v mise &>/dev/null; then
# 根据 ~/.config/mise/config.toml 声明,一键全量安装所有运行时、LSP 和 CLI 工具
if mise install --yes; then
success "Mise tools and runtimes installed."
else
warn "Mise installation had issues. You can rerun manually: mise install"
fi
else
warn "Mise not found, skipping tool installation."
fi
info "Setting up local configuration overrides..."
# --- 1. Git Local 基础信息模板 ---
if [ ! -f "$HOME/.gitconfig.local" ]; then
cp "$DOTFILES_DIR/local/dot-gitconfig.local.example" "$HOME/.gitconfig.local"
info " -> Created ~/.gitconfig.local (Please update it with your name/email)"
else
success " -> ~/.gitconfig.local already exists, skipping."
fi
# --- 2. Git 工作目录信息模板 ---
if [ ! -f "$HOME/.gitconfig.work" ]; then
cp "$DOTFILES_DIR/local/dot-gitconfig.work.example" "$HOME/.gitconfig.work"
info " -> Created ~/.gitconfig.work"
else
success " -> ~/.gitconfig.work already exists, skipping."
fi
# --- 3. Fish 私有环境变量模板 ---
FISH_LOCAL_CONF="$HOME/.fish.local.fish"
if [ ! -f "$FISH_LOCAL_CONF" ]; then
cp "$DOTFILES_DIR/local/config.local.fish.example" "$FISH_LOCAL_CONF"
info " -> Created $FISH_LOCAL_CONF (For private API keys and aliases)"
else
success " -> $FISH_LOCAL_CONF already exists, skipping."
fi
# --- 4. Ghostty 私有配置模板 ---
GHOSTTY_LOCAL_CONF="$HOME/.ghostty.local"
if [ ! -f "$GHOSTTY_LOCAL_CONF" ]; then
cp "$DOTFILES_DIR/local/ghostty.config.local.example" "$GHOSTTY_LOCAL_CONF"
info " -> Created $GHOSTTY_LOCAL_CONF (For private shortcuts and overrides)"
else
success " -> $GHOSTTY_LOCAL_CONF already exists, skipping."
fi
# --- 5. go-musicfox 私有配置模板 ---
MUSICFOX_ROOT_DIR="$HOME/.local/share/go-musicfox"
MUSICFOX_CONFIG="$MUSICFOX_ROOT_DIR/config.toml"
mkdir -p "$MUSICFOX_ROOT_DIR"
if [ ! -f "$MUSICFOX_CONFIG" ]; then
cp "$DOTFILES_DIR/local/go-musicfox.config.toml.example" "$MUSICFOX_CONFIG"
info " -> Created $MUSICFOX_CONFIG (For go-musicfox private settings and login state)"
else
success " -> $MUSICFOX_CONFIG already exists, skipping."
fi
if [[ "$SHELL" != *"fish"* ]]; then
if [ "$NON_INTERACTIVE" = true ]; then
info "Skipping default shell change in non-interactive mode."
info "Please run 'chsh -s $(which fish)' manually later if needed."
elif ask_yes_no "Do you want to set fish as your default shell?" "y"; then
FISH_PATH=$(which fish)
if ! grep -q "$FISH_PATH" /etc/shells; then
info "Adding $FISH_PATH to /etc/shells..."
echo "$FISH_PATH" | sudo tee -a /etc/shells
fi
info "Changing default shell to fish..."
chsh -s "$FISH_PATH"
fi
else
success "Fish is already the default shell."
fi
info "Installing fisher and plugins..."
# 清理 fisher_path 目录(与 config.fish 中 set -g fisher_path 保持一致)
# 该目录存放第三方插件的 functions/completions/conf.d,残留旧数据可能导致安装冲突
FISHER_DATA_DIR="$HOME/.local/share/fisher"
if [ -d "$FISHER_DATA_DIR" ]; then
warn "Cleaning existing fisher data: $FISHER_DATA_DIR"
rm -rf "$FISHER_DATA_DIR"
fi
if fish -c "type -q fisher" 2>/dev/null; then
success "Fisher already installed."
else
info "Installing fisher..."
FISHER_URL="https://raw.githubusercontent.com/jorgebucaran/fisher/main/functions/fisher.fish"
FISHER_TMP=$(mktemp)
if curl -fsSL "$FISHER_URL" -o "$FISHER_TMP"; then
fish -c "source '$FISHER_TMP' && fisher install jorgebucaran/fisher"
rm -f "$FISHER_TMP"
else
rm -f "$FISHER_TMP"
error "Failed to download fisher. Check your network connection."
exit 1
fi
fi
if [ -f "$DOTFILES_DIR/fish/dot-config/fish/fish_plugins" ]; then
info "Installing plugins from fish_plugins..."
FISH_PLUGINS=()
while IFS= read -r plugin; do
[ -z "$plugin" ] && continue
FISH_PLUGINS+=("$plugin")
done < <(fish_plugins_args "$DOTFILES_DIR/fish/dot-config/fish/fish_plugins")
if [ "${#FISH_PLUGINS[@]}" -eq 0 ]; then
warn "fish_plugins is empty, skipping plugin installation."
elif fish -c "fisher install $(printf '%q ' "${FISH_PLUGINS[@]}")"; then
success "Fisher plugins synchronized."
else
warn "Fisher plugin synchronization had issues."
fi
else
warn "fish_plugins not found at $DOTFILES_DIR/fish/dot-config/fish/fish_plugins, skipping plugin installation."
fi
# Migrate PATH from zsh to fish (for users switching from zsh)
if should_migrate_zsh_path; then
info "Migrating PATH from zsh to ~/.fish.local.fish (with 5-second timeout protection)..."
if command -v perl &>/dev/null; then
ZSH_PATHS=$(perl -e 'alarm 5; exec @ARGV' /bin/zsh -l -c 'echo "$PATH"' 2>/dev/null | tr ':' '\n' || true)
else
ZSH_PATHS=$(/bin/zsh -l -c 'echo "$PATH"' 2>/dev/null | tr ':' '\n' || true)
fi
if [ -z "$ZSH_PATHS" ]; then
warn "Could not read legacy zsh PATH or timed out. Skipping migration."
else
FISH_PATHS=$(fish -l -c 'string join \n $PATH' 2>/dev/null)
MIGRATED_PATHS=()
MIGRATED=0
while IFS= read -r p; do
[ -z "$p" ] && continue
[ ! -d "$p" ] && continue
# Skip system/default paths (already handled)
case "$p" in
/usr/bin | /bin | /usr/sbin | /sbin | /usr/local/bin | /opt/homebrew/bin | /opt/homebrew/sbin) continue ;;
esac
# Check if path is already in fish
if ! echo "$FISH_PATHS" | grep -qxF "$p"; then
MIGRATED_PATHS+=("$p")
info " Queued: $p"
MIGRATED=$((MIGRATED + 1))
fi
done <<<"$ZSH_PATHS"
if [ "$MIGRATED" -gt 0 ]; then
update_fish_local_path_block "$FISH_LOCAL_CONF" "${MIGRATED_PATHS[@]}"
success "Migrated $MIGRATED PATH entries into $FISH_LOCAL_CONF."
else
update_fish_local_path_block "$FISH_LOCAL_CONF"
success "No additional PATH entries to migrate from zsh."
fi
fi
fi
if [[ "$(uname)" == "Darwin" ]]; then
if ask_yes_no "Do you want to apply macOS system preferences (macos.sh)?" "n"; then
info "Applying macOS system preferences..."
bash "$DOTFILES_DIR/macos.sh"
fi
fi
success "Installation complete!"
show_next_steps
if [ -n "$SKIPPED_PRIVILEGED_CASKS" ]; then
warn "Skipped privileged casks in non-interactive mode: $SKIPPED_PRIVILEGED_CASKS"
info "Install them later in an interactive shell: brew install --cask $SKIPPED_PRIVILEGED_CASKS"
fi
success "Enjoy your new setup!"