1+ #! /bin/bash
2+
3+ # 配置应用脚本 - 通过软链接快速应用shell配置文件
4+ # 作者: mudssky
5+ # 用途: 快速应用config目录下的配置文件到用户主目录
6+
7+ # 获取脚本所在目录
8+ SCRIPT_DIR=" $( cd " $( dirname " ${BASH_SOURCE[0]} " ) " && pwd) "
9+ CONFIG_DIR=" $SCRIPT_DIR /config"
10+
11+ # 颜色定义
12+ RED=' \033[0;31m'
13+ GREEN=' \033[0;32m'
14+ YELLOW=' \033[1;33m'
15+ BLUE=' \033[0;34m'
16+ NC=' \033[0m' # No Color
17+
18+ # 日志函数
19+ log_info () {
20+ echo -e " ${BLUE} [INFO]${NC} $1 "
21+ }
22+
23+ log_success () {
24+ echo -e " ${GREEN} [SUCCESS]${NC} $1 "
25+ }
26+
27+ log_warning () {
28+ echo -e " ${YELLOW} [WARNING]${NC} $1 "
29+ }
30+
31+ log_error () {
32+ echo -e " ${RED} [ERROR]${NC} $1 "
33+ }
34+
35+ # 检查配置目录是否存在
36+ check_config_dir () {
37+ if [ ! -d " $CONFIG_DIR " ]; then
38+ log_error " 配置目录不存在: $CONFIG_DIR "
39+ exit 1
40+ fi
41+ }
42+
43+ # 备份现有配置文件
44+ backup_existing_config () {
45+ local config_file=" $1 "
46+ local home_path=" $HOME /$config_file "
47+
48+ if [ -e " $home_path " ] && [ ! -L " $home_path " ]; then
49+ local backup_path=" ${home_path} .backup.$( date +%Y%m%d_%H%M%S) "
50+ log_warning " 备份现有配置文件: $home_path -> $backup_path "
51+ mv " $home_path " " $backup_path "
52+ elif [ -L " $home_path " ]; then
53+ log_info " 移除现有软链接: $home_path "
54+ rm " $home_path "
55+ fi
56+ }
57+
58+ # 创建软链接
59+ create_symlink () {
60+ local config_file=" $1 "
61+ local source_path=" $CONFIG_DIR /$config_file "
62+ local target_path=" $HOME /$config_file "
63+
64+ if [ ! -f " $source_path " ]; then
65+ log_error " 配置文件不存在: $source_path "
66+ return 1
67+ fi
68+
69+ # 备份现有配置
70+ backup_existing_config " $config_file "
71+
72+ # 创建软链接
73+ ln -s " $source_path " " $target_path "
74+
75+ if [ $? -eq 0 ]; then
76+ log_success " 成功创建软链接: $target_path -> $source_path "
77+ return 0
78+ else
79+ log_error " 创建软链接失败: $target_path "
80+ return 1
81+ fi
82+ }
83+
84+ # 应用zsh配置
85+ apply_zsh_config () {
86+ log_info " 应用 Zsh 配置..."
87+
88+ if create_symlink " .zshrc" ; then
89+ if [ -f " $CONFIG_DIR /.p10k.zsh" ]; then
90+ create_symlink " .p10k.zsh"
91+ fi
92+ log_success " Zsh 配置应用完成"
93+ else
94+ log_error " Zsh 配置应用失败"
95+ return 1
96+ fi
97+ }
98+
99+ # 应用bash配置
100+ apply_bash_config () {
101+ log_info " 应用 Bash 配置..."
102+
103+ if create_symlink " .bashrc" ; then
104+ log_success " Bash 配置应用完成"
105+ else
106+ log_error " Bash 配置应用失败"
107+ return 1
108+ fi
109+ }
110+
111+ # 应用所有可用配置
112+ apply_all_configs () {
113+ log_info " 应用所有可用配置..."
114+
115+ local applied_count=0
116+
117+ # 检查并应用zsh配置
118+ if [ -f " $CONFIG_DIR /.zshrc" ]; then
119+ apply_zsh_config && (( applied_count++ ))
120+ fi
121+
122+ # 检查并应用bash配置
123+ if [ -f " $CONFIG_DIR /.bashrc" ]; then
124+ apply_bash_config && (( applied_count++ ))
125+ fi
126+
127+ # 应用其他配置文件
128+ for config_file in " $CONFIG_DIR " /.* ; do
129+ if [ -f " $config_file " ]; then
130+ local filename=$( basename " $config_file " )
131+ case " $filename " in
132+ " .zshrc" |" p10k.zsh" |" bashrc" )
133+ # 已经处理过的文件,跳过
134+ ;;
135+ " ." |" .." )
136+ # 跳过当前目录和父目录
137+ ;;
138+ * )
139+ if create_symlink " $filename " ; then
140+ (( applied_count++ ))
141+ fi
142+ ;;
143+ esac
144+ fi
145+ done
146+
147+ log_success " 总共应用了 $applied_count 个配置文件"
148+ }
149+
150+ # 列出可用配置
151+ list_configs () {
152+ log_info " 可用的配置文件:"
153+
154+ if [ -d " $CONFIG_DIR " ]; then
155+ for config_file in " $CONFIG_DIR " /.* ; do
156+ if [ -f " $config_file " ]; then
157+ local filename=$( basename " $config_file " )
158+ case " $filename " in
159+ " ." |" .." )
160+ # 跳过当前目录和父目录
161+ ;;
162+ * )
163+ local target_path=" $HOME /$filename "
164+ if [ -L " $target_path " ] && [ " $( readlink " $target_path " ) " = " $config_file " ]; then
165+ echo -e " ${GREEN} ✓${NC} $filename (已链接)"
166+ else
167+ echo -e " ${YELLOW} ○${NC} $filename (未链接)"
168+ fi
169+ ;;
170+ esac
171+ fi
172+ done
173+ else
174+ log_error " 配置目录不存在: $CONFIG_DIR "
175+ fi
176+ }
177+
178+ # 显示帮助信息
179+ show_help () {
180+ echo " 配置应用脚本 - 通过软链接快速应用shell配置文件"
181+ echo " "
182+ echo " 用法: $0 [选项]"
183+ echo " "
184+ echo " 选项:"
185+ echo " zsh, z 应用 Zsh 配置 (.zshrc, .p10k.zsh)"
186+ echo " bash, b 应用 Bash 配置 (.bashrc)"
187+ echo " all, a 应用所有可用配置"
188+ echo " list, l 列出所有可用配置"
189+ echo " help, h 显示此帮助信息"
190+ echo " "
191+ echo " 示例:"
192+ echo " $0 zsh # 应用 Zsh 配置"
193+ echo " $0 bash # 应用 Bash 配置"
194+ echo " $0 all # 应用所有配置"
195+ echo " $0 list # 列出可用配置"
196+ echo " "
197+ echo " 配置文件位置: $CONFIG_DIR "
198+ }
199+
200+ # 主函数
201+ main () {
202+ # 检查配置目录
203+ check_config_dir
204+
205+ # 解析命令行参数
206+ case " ${1:- help} " in
207+ " zsh" |" z" )
208+ apply_zsh_config
209+ ;;
210+ " bash" |" b" )
211+ apply_bash_config
212+ ;;
213+ " all" |" a" )
214+ apply_all_configs
215+ ;;
216+ " list" |" l" )
217+ list_configs
218+ ;;
219+ " help" |" h" |" " )
220+ show_help
221+ ;;
222+ * )
223+ log_error " 未知选项: $1 "
224+ echo " "
225+ show_help
226+ exit 1
227+ ;;
228+ esac
229+ }
230+
231+ # 执行主函数
232+ main " $@ "
0 commit comments