## 新增功能 - 创建统一的公共函数库 (common/logging.sh, common/error_handler.sh) - 添加功能演示脚本 (common/demo_usage.sh) - 完善的使用文档 (common/README.md) ## 代码重构 - 重构所有脚本使用统一的公共库 - 为所有函数添加完整的文档注释 - 统一代码格式(4空格缩进、严格模式) - 标准化错误处理和日志输出 ## 文件重命名 - gcp/create_ai_project.sh → gcp/create_ai_projects.sh (单复数统一) - gcp/delete_all_project.sh → gcp/delete_all_projects.sh (单复数统一) - linux/install_ohmyzsh.sh → linux/install_oh_my_zsh.sh (专有名词规范) - linux/create_raid0_with_ext4.sh → linux/create_raid0_array.sh (简化命名) - common/example.sh → common/demo_usage.sh (更具描述性) ## 技术改进 - 使用 readonly 声明常量 - 启用 set -euo pipefail 严格模式 - 统一的 ANSI 颜色日志输出 - 完善的命令重试机制 - 栈追踪支持
107 lines
3.0 KiB
Bash
107 lines
3.0 KiB
Bash
#!/bin/bash
|
|
# ============================================================================
|
|
# 文件名: install_oh_my_zsh.sh
|
|
# 描述: 安装和配置 Oh My Zsh 及常用插件
|
|
# 作者: Cloud Tools Project
|
|
# 版本: 2.0.0
|
|
# ============================================================================
|
|
|
|
set -euo pipefail
|
|
|
|
# 获取脚本目录
|
|
readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
readonly PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"
|
|
|
|
# 加载公共库
|
|
source "${PROJECT_ROOT}/common/logging.sh"
|
|
source "${PROJECT_ROOT}/common/error_handler.sh"
|
|
|
|
# 配置路径
|
|
readonly INSTALL_DIR="${HOME}/.oh-my-zsh"
|
|
readonly ZSH_CONFIG="${HOME}/.zshrc"
|
|
readonly ZSH_CUSTOM="${INSTALL_DIR}/custom"
|
|
|
|
#
|
|
# 检查并安装缺失的软件包
|
|
#
|
|
# 参数:
|
|
# $@ - 要检查的软件包列表
|
|
#
|
|
check_and_install_packages() {
|
|
local packages=("$@")
|
|
local missing_packages=()
|
|
|
|
for package in "${packages[@]}"; do
|
|
if ! check_command "$package"; then
|
|
missing_packages+=("$package")
|
|
fi
|
|
done
|
|
|
|
if [[ ${#missing_packages[@]} -gt 0 ]]; then
|
|
log_warning "缺少以下软件包: ${missing_packages[*]}"
|
|
if command -v apt-get &>/dev/null; then
|
|
run_command "安装软件包" apt-get update
|
|
run_command "安装软件包" apt-get install -y "${missing_packages[@]}"
|
|
else
|
|
log_error "只支持 apt-get 包管理器"
|
|
exit 1
|
|
fi
|
|
fi
|
|
log_success "所有软件包已安装"
|
|
}
|
|
|
|
#
|
|
# 安装 Oh My Zsh
|
|
#
|
|
install_zsh() {
|
|
log_info "安装 zsh..."
|
|
run_command "更新软件包" sudo apt update
|
|
run_command "安装 zsh" sudo apt install zsh -y
|
|
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
|
|
}
|
|
|
|
#
|
|
# 主函数
|
|
#
|
|
main() {
|
|
log_info "============ Oh My Zsh 安装程序 ============"
|
|
|
|
check_and_install_packages "sudo" "git" "curl" "zsh"
|
|
|
|
if [ ! -d "$INSTALL_DIR" ]; then
|
|
install_zsh
|
|
else
|
|
log_success "oh-my-zsh 已安装"
|
|
fi
|
|
|
|
# 安装插件
|
|
if [ ! -d "$ZSH_CUSTOM/plugins/zsh-autosuggestions" ]; then
|
|
log_info "安装 zsh-autosuggestions..."
|
|
run_command "克隆插件" git clone https://github.com/zsh-users/zsh-autosuggestions "$ZSH_CUSTOM/plugins/zsh-autosuggestions"
|
|
fi
|
|
|
|
if [ ! -d "$ZSH_CUSTOM/plugins/zsh-syntax-highlighting" ]; then
|
|
log_info "安装 zsh-syntax-highlighting..."
|
|
run_command "克隆插件" git clone https://github.com/zsh-users/zsh-syntax-highlighting.git "$ZSH_CUSTOM/plugins/zsh-syntax-highlighting"
|
|
fi
|
|
|
|
log_success "安装完成"
|
|
|
|
# 配置 .zshrc
|
|
log_info "修改 .zshrc 文件..."
|
|
cp "$ZSH_CONFIG" "$ZSH_CONFIG.bak"
|
|
sed -i 's/plugins=(git)/plugins=(git zsh-autosuggestions zsh-syntax-highlighting extract)/' "$ZSH_CONFIG"
|
|
sed -i 's/ZSH_THEME="robbyrussell"/ZSH_THEME="random"/' "$ZSH_CONFIG"
|
|
log_success "配置完成"
|
|
|
|
# 设置默认 shell
|
|
log_info "设置 zsh 为默认 shell..."
|
|
chsh -s /bin/zsh
|
|
log_success "设置完成"
|
|
|
|
log_info "重启 zsh..."
|
|
zsh
|
|
}
|
|
|
|
# 执行主函数
|
|
main "$@" |