77 lines
2.8 KiB
Bash
77 lines
2.8 KiB
Bash
#!/bin/bash
|
||
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||
# 一键启动Project
|
||
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||
|
||
# 默认分支
|
||
BRANCH="dev"
|
||
# 是否直接初始化配置仓库
|
||
# INIT=""
|
||
|
||
# 检查是否安装git
|
||
if ! command -v git >/dev/null 2>&1; then
|
||
echo "git命令未找到,请安装git"
|
||
exit 1
|
||
else
|
||
echo "git命令已安装,继续执行"
|
||
git config --global credential.helper store
|
||
fi
|
||
|
||
# 使用getopts处理参数
|
||
while getopts ":hib:" opt; do
|
||
case $opt in
|
||
b) BRANCH="$OPTARG" ;;
|
||
i) INIT="enable" ;;
|
||
h)
|
||
echo "使用方法: $0 [-b <branch>] [-ih]"
|
||
# echo " -d 开发模式"
|
||
echo " -i 初始化配置仓库"
|
||
echo " -b 指定分支,默认分支为 dev"
|
||
echo " -h 显示帮助, 并退出. 建议放最后一个参数."
|
||
exit 0
|
||
;;
|
||
\?)
|
||
echo "无效选项: -$opt $OPTARG" >&2
|
||
exit 1
|
||
;;
|
||
esac
|
||
done
|
||
|
||
# = clone 到 tmp/.config 并加载profile入口
|
||
# == 如果已安装过,则不再执行
|
||
test -f "$HOME"/.config/.bin/.Runtime/SELF_MARK_AS_INSTALLED_FROM_SCRIPT && {
|
||
echo "local.env.ini already exists, end the install. please remove it first if need re-install."
|
||
exit 1
|
||
}
|
||
# == 移走原有配置目录~/.config到/tmp/.config
|
||
[ -d "/tmp/.config" ] && rm -rf /tmp/.config # 如果存在, 删除 /tmp/.config
|
||
[ -d "$HOME/.config" ] && mv "$HOME/.config" /tmp/.config # 移动原有 $HOME/.config 配置到 /tmp/.config
|
||
# 克隆,如果失败则退出并恢复原有配置
|
||
git clone --depth 1 --branch "$BRANCH" --single-branch https://gitea.yever.top/Hawkin/.config.git "$HOME"/.config
|
||
test $? -ne 0 && {
|
||
echo -e "\033[031mgit clone failed, please check your network.\033[0m"
|
||
# 删除克隆的目录,恢复原有配置
|
||
echo -e "\033[034mdelete the unfinished clone dir: $HOME/.config"
|
||
[ -d "$HOME/.config" ] && rm -rf "$HOME/.config"
|
||
if test -d /tmp/.config; then
|
||
echo -e "\033[034mrestoring the old config to $HOME/.config.\033[0m"
|
||
mv /tmp/.config "$HOME/.config"
|
||
else
|
||
echo -e "\033[032mno old config found, skip restore config dir..\033[0m"
|
||
fi
|
||
exit 1
|
||
}
|
||
|
||
# = 标记已安装
|
||
test -d "$HOME"/.config/.bin/.Runtime || mkdir -p "$HOME"/.config/.bin/.Runtime
|
||
cat >"$HOME"/.config/.bin/.Runtime/SELF_MARK_AS_INSTALLED_FROM_SCRIPT <<EOF
|
||
此文件存在时自动跳过克隆安装, 在安装完成时创建.
|
||
如果你想要重新安装, 请删除此文件——下次安装时会将当前配置仓库移动到备份目录(默认为~/.local/share/Origin_Config_Backup).
|
||
EOF
|
||
|
||
# = 通过目标仓库的初始化脚本进行初始化
|
||
if test "$INIT" = "enable"; then
|
||
echo -e "\033[032mgit clone success, init project by script itself...\033[0m"
|
||
bash "$HOME"/.config/.bin/bin/Init.sh
|
||
fi
|