-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuninstall.sh
More file actions
510 lines (436 loc) · 13.8 KB
/
uninstall.sh
File metadata and controls
510 lines (436 loc) · 13.8 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
#!/bin/bash
# ghcp uninstaller script
set -e
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
print_info() { echo -e "${BLUE}$1${NC}"; }
print_success() { echo -e "${GREEN}$1${NC}"; }
print_warning() { echo -e "${YELLOW}$1${NC}"; }
print_error() { echo -e "${RED}$1${NC}" >&2; }
# 使用與安裝腳本相同的 shell 檢測邏輯
detect_shell() {
# 如果用戶手動指定了 shell 類型,優先使用
if [ -n "${GHCP_SHELL_TYPE:-}" ]; then
case "${GHCP_SHELL_TYPE}" in
zsh|bash|fish)
echo "$GHCP_SHELL_TYPE"
return 0
;;
*)
print_warning "⚠️ Invalid GHCP_SHELL_TYPE: $GHCP_SHELL_TYPE (using auto-detection)"
;;
esac
fi
# 優先檢查 $SHELL 環境變量
if [ -n "$SHELL" ]; then
case "$SHELL" in
*/zsh) echo "zsh" ;;
*/bash) echo "bash" ;;
*/fish) echo "fish" ;;
*/dash) echo "bash" ;; # dash 當作 bash 處理
*/sh) echo "bash" ;; # sh 當作 bash 處理
*)
# 如果 $SHELL 路徑不清楚,檢查版本變量
if [ -n "$ZSH_VERSION" ]; then
echo "zsh"
elif [ -n "$FISH_VERSION" ]; then
echo "fish"
elif [ -n "$BASH_VERSION" ]; then
echo "bash"
else
echo "unknown"
fi
;;
esac
else
# 沒有 $SHELL 環境變量,回退到版本檢查
if [ -n "$ZSH_VERSION" ]; then
echo "zsh"
elif [ -n "$FISH_VERSION" ]; then
echo "fish"
elif [ -n "$BASH_VERSION" ]; then
echo "bash"
else
echo "unknown"
fi
fi
}
# 檢測當前安裝狀態
detect_installation() {
print_info "📋 Checking installation status..."
local detected_shell
detected_shell=$(detect_shell)
print_info "Shell detection:"
print_info " \$SHELL environment variable: ${SHELL:-not set}"
print_info " Current executing shell: $(ps -p $ -o comm= 2>/dev/null || echo "unknown")"
print_info " Detected user shell: $detected_shell"
echo ""
local found_installations=()
# 檢查 standalone 安裝
local -a standalone_locations=(
"$HOME/.local/bin/ghcp"
"/usr/local/bin/ghcp"
"/opt/homebrew/bin/ghcp"
"/usr/bin/ghcp"
)
print_info "Checking standalone installation:"
for location in "${standalone_locations[@]}"; do
if [ -f "$location" ]; then
if [ -L "$location" ]; then
local target
target=$(readlink "$location")
print_warning " ✓ Found symbolic link: $location -> $target"
found_installations+=("standalone_link:$location")
else
print_success " ✓ Found executable file: $location"
found_installations+=("standalone:$location")
fi
else
print_info " - Not found: $location"
fi
done
echo ""
# 檢查 shell function 安裝
print_info "Checking shell function installation:"
# 檢查 .ghcp 目錄
if [ -d "$HOME/.ghcp" ]; then
print_success " ✓ Found ghcp directory: $HOME/.ghcp"
found_installations+=("ghcp_dir:$HOME/.ghcp")
for shell_type in zsh bash fish; do
if [ -f "$HOME/.ghcp/ghcp.$shell_type" ]; then
print_success " ✓ Found $shell_type function: ghcp.$shell_type"
found_installations+=("function:$shell_type")
fi
if [ -f "$HOME/.ghcp/ghcp_completion.$shell_type" ]; then
print_success " ✓ Found $shell_type completion: ghcp_completion.$shell_type"
found_installations+=("completion:$shell_type")
fi
done
else
print_info " - Not found ghcp directory"
fi
# 檢查 zsh 補全特殊位置
if [ -f "$HOME/.zsh/completions/_ghcp" ]; then
print_success " ✓ Found zsh completion: ~/.zsh/completions/_ghcp"
found_installations+=("zsh_completion:$HOME/.zsh/completions/_ghcp")
fi
# 檢查 fish 特殊安裝位置
if [ -f "$HOME/.config/fish/functions/ghcp.fish" ]; then
print_success " ✓ Found fish function: ~/.config/fish/functions/ghcp.fish"
found_installations+=("fish_function:$HOME/.config/fish/functions/ghcp.fish")
fi
if [ -f "$HOME/.config/fish/completions/ghcp.fish" ]; then
print_success " ✓ Found fish completion: ~/.config/fish/completions/ghcp.fish"
found_installations+=("fish_completion:$HOME/.config/fish/completions/ghcp.fish")
fi
# 檢查 shell 配置文件
print_info "Checking shell configuration file references:"
local config_files=()
[ -f "$HOME/.zshrc" ] && config_files+=("$HOME/.zshrc")
[ -f "$HOME/.bashrc" ] && config_files+=("$HOME/.bashrc")
[ -f "$HOME/.config/fish/config.fish" ] && config_files+=("$HOME/.config/fish/config.fish")
for config_file in "${config_files[@]}"; do
if grep -q "ghcp" "$config_file" 2>/dev/null; then
print_warning " ✓ Found ghcp reference in configuration file: $config_file"
found_installations+=("config:$config_file")
else
print_info " - Not found reference in configuration file: $config_file"
fi
done
echo ""
# 檢查 Oh My Zsh plugin
local omz_plugin_dir="${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/plugins/ghcp"
if [ -d "$omz_plugin_dir" ]; then
print_success " ✓ Found Oh My Zsh plugin: $omz_plugin_dir"
found_installations+=("omz_plugin:$omz_plugin_dir")
fi
# 檢查 recovery 文件
if [ -f "$HOME/.ghcp_recovery" ]; then
print_info " ✓ Found recovery file: $HOME/.ghcp_recovery"
found_installations+=("recovery:$HOME/.ghcp_recovery")
fi
# 檢查 Homebrew 安裝
if command -v brew >/dev/null 2>&1; then
if brew list ghcp >/dev/null 2>&1; then
print_success " ✓ Found Homebrew installation: brew list ghcp"
found_installations+=("homebrew:ghcp")
fi
fi
if [ ${#found_installations[@]} -eq 0 ]; then
print_warning "⚠️ No ghcp installations found"
return 1
fi
print_success "✅ Found ${#found_installations[@]} installations"
return 0
}
remove_standalone() {
print_info "🗑️ Removing standalone executable files..."
local removed=false
# 檢查常見安裝位置
local -a locations=(
"$HOME/.local/bin/ghcp"
"/usr/local/bin/ghcp"
"/opt/homebrew/bin/ghcp"
"/usr/bin/ghcp"
)
for location in "${locations[@]}"; do
if [ -f "$location" ] || [ -L "$location" ]; then
if rm -f "$location" 2>/dev/null; then
print_success "✅ Removed: $location"
removed=true
else
print_warning "⚠️ Cannot remove: $location (permission denied)"
fi
fi
done
if [ "$removed" = false ]; then
print_info "No standalone executable files found"
fi
}
remove_shell_function() {
local shell_type="$1"
print_info "🗑️ Removing $shell_type shell function..."
local function_file="$HOME/.ghcp/ghcp.$shell_type"
local completion_file="$HOME/.ghcp/ghcp_completion.$shell_type"
local removed=false
# 移除函數文件
if [ -f "$function_file" ]; then
rm -f "$function_file"
print_success "✅ Removed: $function_file"
removed=true
fi
# 處理不同 shell 的補全移除
case "$shell_type" in
zsh)
# 移除 zsh 補全文件
local zsh_completion="$HOME/.zsh/completions/_ghcp"
if [ -f "$zsh_completion" ]; then
rm -f "$zsh_completion"
print_success "✅ Removed: $zsh_completion"
removed=true
fi
# 移除舊格式的補全文件(如果存在)
if [ -f "$completion_file" ]; then
rm -f "$completion_file"
print_success "✅ Removed: $completion_file"
removed=true
fi
;;
bash)
if [ -f "$completion_file" ]; then
rm -f "$completion_file"
print_success "✅ Removed: $completion_file"
removed=true
fi
;;
fish)
# 移除 fish 補全文件
local fish_function="$HOME/.config/fish/functions/ghcp.fish"
local fish_completion="$HOME/.config/fish/completions/ghcp.fish"
if [ -f "$fish_function" ]; then
rm -f "$fish_function"
print_success "✅ Removed: $fish_function"
removed=true
fi
if [ -f "$fish_completion" ]; then
rm -f "$fish_completion"
print_success "✅ Removed: $fish_completion"
removed=true
fi
# 移除舊格式的補全文件(如果存在)
if [ -f "$completion_file" ]; then
rm -f "$completion_file"
print_success "✅ Removed: $completion_file"
removed=true
fi
;;
esac
# 從 shell 配置文件中移除
local config_file
case "$shell_type" in
zsh)
config_file="$HOME/.zshrc"
;;
bash)
config_file="$HOME/.bashrc"
;;
fish)
config_file="$HOME/.config/fish/config.fish"
;;
esac
if [ -f "$config_file" ] && grep -q "ghcp" "$config_file" 2>/dev/null; then
# 創建備份
cp "$config_file" "$config_file.ghcp-uninstall-backup-$(date +%Y%m%d-%H%M%S)"
# 移除 ghcp 相關行
case "$shell_type" in
zsh)
# 移除 ghcp 相關的所有行:函數、補全、fpath
if sed '/# ghcp/,+3d; /fpath.*\.zsh\/completions/d' "$config_file" > "$config_file.tmp" && mv "$config_file.tmp" "$config_file"; then
print_success "✅ Removed ghcp configuration from $config_file"
removed=true
fi
;;
fish)
# Fish shell 處理
if grep -v "ghcp" "$config_file" > "$config_file.tmp"; then
mv "$config_file.tmp" "$config_file"
print_success "✅ Removed ghcp configuration from $config_file"
removed=true
fi
;;
bash)
# Bash 處理
if sed '/# ghcp/,+2d' "$config_file" > "$config_file.tmp" && mv "$config_file.tmp" "$config_file"; then
print_success "✅ Removed ghcp configuration from $config_file"
removed=true
fi
;;
esac
print_info " (Created backup file)"
fi
if [ "$removed" = false ]; then
print_info "No $shell_type shell function installation found"
fi
}
remove_oh_my_zsh_plugin() {
print_info "🗑️ Removing Oh My Zsh plugin..."
local plugin_dir="${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/plugins/ghcp"
if [ -d "$plugin_dir" ]; then
rm -rf "$plugin_dir"
print_success "✅ Removed: $plugin_dir"
# 檢查 .zshrc 中的插件列表
if [ -f "$HOME/.zshrc" ] && grep -q "plugins.*ghcp" "$HOME/.zshrc"; then
print_warning "⚠️ Please manually remove 'ghcp' from the plugins list in ~/.zshrc"
print_info " Look for similar: plugins=(... ghcp ...)"
fi
else
print_info "No Oh My Zsh plugin found"
fi
}
remove_homebrew_package() {
print_info "🗑️ Removing Homebrew package..."
if command -v brew >/dev/null 2>&1; then
if brew list ghcp >/dev/null 2>&1; then
brew uninstall ghcp
print_success "✅ Removed Homebrew package"
else
print_info "No Homebrew package found"
fi
else
print_info "No Homebrew package found"
fi
}
remove_recovery_files() {
print_info "🗑️ Removing recovery and temporary files..."
local -a files=(
"$HOME/.ghcp_recovery"
"$HOME/.ghcp"
)
for file in "${files[@]}"; do
if [ -e "$file" ]; then
rm -rf "$file"
print_success "✅ Removed: $file"
fi
done
}
get_uninstall_mode() {
# 檢查環境變量
if [ -n "${GHCP_UNINSTALL_MODE:-}" ]; then
echo "$GHCP_UNINSTALL_MODE"
return 0
fi
# 默認移除所有
echo "all"
}
show_uninstall_summary() {
echo ""
print_info "📋 Uninstall summary:"
print_info "============"
# 驗證移除結果
local remaining_items=0
# 檢查是否還有安裝
for location in "$HOME/.local/bin/ghcp" "/usr/local/bin/ghcp" "/opt/homebrew/bin/ghcp" "/usr/bin/ghcp"; do
if [ -f "$location" ]; then
print_warning "⚠️ Still exists: $location"
remaining_items=$((remaining_items + 1))
fi
done
if [ -d "$HOME/.ghcp" ]; then
print_warning "⚠️ Still exists: $HOME/.ghcp"
remaining_items=$((remaining_items + 1))
fi
if [ "$remaining_items" -eq 0 ]; then
print_success "✅ All ghcp components have been completely removed!"
else
print_warning "⚠️ Found $remaining_items remaining items"
print_info "If these are custom installation locations, please manually remove them"
fi
echo ""
print_info "💡 Next steps:"
echo "1. Restart terminal or run 'source ~/.bashrc' (or ~/.zshrc)"
echo "2. If there are custom installation locations, please manually remove them"
echo "3. Check other shell configuration files for any remaining configurations"
}
main() {
print_info "🗑️ ghcp uninstaller"
print_info "=================="
echo ""
# 檢測安裝狀態
if ! detect_installation; then
print_info "No uninstallable content found"
exit 0
fi
echo ""
# 獲取卸載模式
local mode
mode=$(get_uninstall_mode)
case "$mode" in
all)
print_info "🗑️ Performing full uninstall..."
# 移除所有組件
remove_standalone
echo ""
local detected_shell
detected_shell=$(detect_shell)
if [ "$detected_shell" != "unknown" ]; then
remove_shell_function "$detected_shell"
echo ""
fi
# 也移除其他 shell 的函數(如果存在)
for shell_type in zsh bash fish; do
if [ "$shell_type" != "$detected_shell" ] && [ -f "$HOME/.ghcp/ghcp.$shell_type" ]; then
remove_shell_function "$shell_type"
echo ""
fi
done
remove_oh_my_zsh_plugin
echo ""
remove_homebrew_package
echo ""
remove_recovery_files
;;
standalone)
remove_standalone
;;
function)
local detected_shell
detected_shell=$(detect_shell)
if [ "$detected_shell" = "unknown" ]; then
print_error "Cannot detect shell type"
exit 1
fi
remove_shell_function "$detected_shell"
;;
*)
print_error "Invalid uninstall mode: $mode"
print_info "Valid options: all, standalone, function"
exit 1
;;
esac
show_uninstall_summary
}
main "$@"