mirror of
https://github.com/RubyMetric/chsrc
synced 2025-06-08 03:04:03 +08:00
parent
15c0ae6204
commit
c6a0801b14
@ -20,7 +20,7 @@ noroot_default_install_path="$HOME/.local/bin"
|
|||||||
binary_name="chsrc"
|
binary_name="chsrc"
|
||||||
temp_install_dir="" # 用于存储临时安装目录
|
temp_install_dir="" # 用于存储临时安装目录
|
||||||
helpflag=0
|
helpflag=0
|
||||||
lan="zh"
|
lang="zh"
|
||||||
|
|
||||||
# 输出相关信息
|
# 输出相关信息
|
||||||
info() {
|
info() {
|
||||||
@ -35,7 +35,7 @@ error() {
|
|||||||
|
|
||||||
# 显示 chsrc 安装程序的帮助信息,包括使用说明和可用选项
|
# 显示 chsrc 安装程序的帮助信息,包括使用说明和可用选项
|
||||||
help() {
|
help() {
|
||||||
if [ "$lan" = "zh" ]; then
|
if [ "$lang" = "zh" ]; then
|
||||||
echo "chsrc-installer: 在任何类Unix操作系统上安装 chsrc"
|
echo "chsrc-installer: 在任何类Unix操作系统上安装 chsrc"
|
||||||
echo
|
echo
|
||||||
echo "使用: install.sh [选项]"
|
echo "使用: install.sh [选项]"
|
||||||
@ -58,7 +58,7 @@ help() {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# 确定下载路径
|
|
||||||
set_install_path() {
|
set_install_path() {
|
||||||
if [ -n "$install_dir" ]; then
|
if [ -n "$install_dir" ]; then
|
||||||
# 扩展 ~ 符号
|
# 扩展 ~ 符号
|
||||||
@ -67,14 +67,14 @@ set_install_path() {
|
|||||||
# 检查路径是否存在,如果不存在则创建该路径
|
# 检查路径是否存在,如果不存在则创建该路径
|
||||||
if [ ! -d "$install_dir" ]; then
|
if [ ! -d "$install_dir" ]; then
|
||||||
# 多种语言输出
|
# 多种语言输出
|
||||||
if [ "$lan" = "zh" ]; then
|
if [ "$lang" = "zh" ]; then
|
||||||
echo "目录 $install_dir 不存在,正在创建..."
|
echo "目录 $install_dir 不存在,正在创建..."
|
||||||
else
|
else
|
||||||
echo "Directory $install_dir does not exist. Creating..."
|
echo "Directory $install_dir does not exist. Creating..."
|
||||||
fi
|
fi
|
||||||
# 多语言输出
|
# 多语言输出
|
||||||
if ! mkdir -p "$install_dir"; then
|
if ! mkdir -p "$install_dir"; then
|
||||||
if [ "$lan" = "zh" ]; then
|
if [ "$lang" = "zh" ]; then
|
||||||
echo "创建目录失败,请重试"
|
echo "创建目录失败,请重试"
|
||||||
else
|
else
|
||||||
echo "Failed to create directory, please try again"
|
echo "Failed to create directory, please try again"
|
||||||
@ -87,7 +87,7 @@ set_install_path() {
|
|||||||
|
|
||||||
elif existing_path=$(command -v "$binary_name" 2>/dev/null); then
|
elif existing_path=$(command -v "$binary_name" 2>/dev/null); then
|
||||||
|
|
||||||
if [ "$lan" = "zh"]; then
|
if [ "$lang" = "zh" ]; then
|
||||||
info "$binary_name 已安装,更新路径: ${existing_path}"
|
info "$binary_name 已安装,更新路径: ${existing_path}"
|
||||||
else
|
else
|
||||||
info "$binary_name is already installed, updating path: ${existing_path}"
|
info "$binary_name is already installed, updating path: ${existing_path}"
|
||||||
@ -98,20 +98,19 @@ set_install_path() {
|
|||||||
# 检查默认路径
|
# 检查默认路径
|
||||||
if [ -d "$default_install_path" ] && [ -w "$default_install_path" ]; then
|
if [ -d "$default_install_path" ] && [ -w "$default_install_path" ]; then
|
||||||
install_dir="$default_install_path"
|
install_dir="$default_install_path"
|
||||||
else if [ -d "$noroot_default_install_path" ] && [ -w "$noroot_default_install_path" ]; then
|
elif [ -d "$noroot_default_install_path" ] && [ -w "$noroot_default_install_path" ]; then
|
||||||
install_dir="$noroot_default_install_path"
|
install_dir="$noroot_default_install_path"
|
||||||
else
|
else
|
||||||
if [ "$lan" = "zh"]; then
|
if [ "$lang" = "zh"]; then
|
||||||
error "默认下载路径 /usr/local/bin 不可写,请使用 sudo 命令运行脚本;或通过 -d 参数指定其它路径安装"
|
error "默认下载路径 /usr/local/bin 不可写,请使用 sudo 命令运行脚本;或通过 -d 参数指定其它路径安装"
|
||||||
else
|
else
|
||||||
error "Default download path /usr/local/bin is not writable. Please run the script with sudo; or specify another path using the -d option."
|
error "Default download path /usr/local/bin is not writable. Please run the script with sudo; or specify another path using the -d option."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# 从Gitee仓库安装 指定架构,操作系统,版本 的chsrc二进制文件
|
|
||||||
install() {
|
install() {
|
||||||
arch="$(uname -m | tr '[:upper:]' '[:lower:]')"
|
arch="$(uname -m | tr '[:upper:]' '[:lower:]')"
|
||||||
|
|
||||||
@ -121,7 +120,7 @@ install() {
|
|||||||
riscv64) arch="riscv64" ;;
|
riscv64) arch="riscv64" ;;
|
||||||
armv7*) arch="armv7" ;;
|
armv7*) arch="armv7" ;;
|
||||||
*)
|
*)
|
||||||
if [ "$lan" = "zh" ]; then
|
if [ "$lang" = "zh" ]; then
|
||||||
error "不支持的架构: ${arch}"
|
error "不支持的架构: ${arch}"
|
||||||
else
|
else
|
||||||
error "Unsupported architecture: ${arch}"
|
error "Unsupported architecture: ${arch}"
|
||||||
@ -135,7 +134,7 @@ install() {
|
|||||||
linux) platform="linux" ;;
|
linux) platform="linux" ;;
|
||||||
darwin) platform="macos" ;;
|
darwin) platform="macos" ;;
|
||||||
*)
|
*)
|
||||||
if [ "$lan" = "zh" ]; then
|
if [ "$lang" = "zh" ]; then
|
||||||
error "不支持的平台: ${platform}"
|
error "不支持的平台: ${platform}"
|
||||||
else
|
else
|
||||||
error "Unsupported platform: ${platform}"
|
error "Unsupported platform: ${platform}"
|
||||||
@ -145,7 +144,7 @@ install() {
|
|||||||
|
|
||||||
if [[ ! "$version" =~ ^(pre|0\.1\.([4-9]))$ ]]; then
|
if [[ ! "$version" =~ ^(pre|0\.1\.([4-9]))$ ]]; then
|
||||||
# version 不符合条件,报错
|
# version 不符合条件,报错
|
||||||
if [ "$lan" = "zh" ]; then
|
if [ "$lang" = "zh" ]; then
|
||||||
error "不支持的版本: ${version},版本号必须在 0.1.4 到 0.1.9 之间或为 'pre'"
|
error "不支持的版本: ${version},版本号必须在 0.1.4 到 0.1.9 之间或为 'pre'"
|
||||||
else
|
else
|
||||||
error "Unsupported version: ${version}. Version number must be between 0.1.4 and 0.1.9 or 'pre'"
|
error "Unsupported version: ${version}. Version number must be between 0.1.4 and 0.1.9 or 'pre'"
|
||||||
@ -156,7 +155,7 @@ install() {
|
|||||||
|
|
||||||
path_to_executable="${install_dir}/${binary_name}"
|
path_to_executable="${install_dir}/${binary_name}"
|
||||||
|
|
||||||
if [ "$lan" = "zh" ]; then
|
if [ "$lang" = "zh" ]; then
|
||||||
info "下载 ${binary_name} (${arch} 架构, ${platform} 平台, ${version}版本) 到 ${path_to_executable}"
|
info "下载 ${binary_name} (${arch} 架构, ${platform} 平台, ${version}版本) 到 ${path_to_executable}"
|
||||||
else
|
else
|
||||||
info "Downloading ${binary_name} (${arch} architecture, ${platform} platform, version ${version}) to ${path_to_executable}"
|
info "Downloading ${binary_name} (${arch} architecture, ${platform} platform, version ${version}) to ${path_to_executable}"
|
||||||
@ -165,14 +164,14 @@ install() {
|
|||||||
if curl -sL "$url" -o "$path_to_executable"; then
|
if curl -sL "$url" -o "$path_to_executable"; then
|
||||||
chmod +x "$path_to_executable"
|
chmod +x "$path_to_executable"
|
||||||
|
|
||||||
if [ "$lan" = "zh" ]; then
|
if [ "$lang" = "zh" ]; then
|
||||||
info "🎉 安装完成,版本: $version,路径: $path_to_executable"
|
info "🎉 安装完成,版本: $version,路径: $path_to_executable"
|
||||||
else
|
else
|
||||||
info "🎉 Installation completed, path: $path_to_executable"
|
info "🎉 Installation completed, path: $path_to_executable"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
else
|
else
|
||||||
if [ "$lan" = "zh" ]; then
|
if [ "$lang" = "zh" ]; then
|
||||||
error "下载失败,请检查您的网络连接和代理设置: ${url}"
|
error "下载失败,请检查您的网络连接和代理设置: ${url}"
|
||||||
else
|
else
|
||||||
error "Download failed, please check your network connection and proxy settings: ${url}"
|
error "Download failed, please check your network connection and proxy settings: ${url}"
|
||||||
@ -185,7 +184,7 @@ install() {
|
|||||||
cleanup() {
|
cleanup() {
|
||||||
if [ -n "$temp_install_dir" ] && [ -d "$temp_install_dir" ]; then
|
if [ -n "$temp_install_dir" ] && [ -d "$temp_install_dir" ]; then
|
||||||
|
|
||||||
if [ "$lan" = "zh" ]; then
|
if [ "$lang" = "zh" ]; then
|
||||||
echo "清理创建的目录: $temp_install_dir"
|
echo "清理创建的目录: $temp_install_dir"
|
||||||
else
|
else
|
||||||
echo "Cleaning up created directory: $temp_install_dir"
|
echo "Cleaning up created directory: $temp_install_dir"
|
||||||
@ -197,6 +196,7 @@ cleanup() {
|
|||||||
# 设置 trap 以捕获退出信号
|
# 设置 trap 以捕获退出信号
|
||||||
trap cleanup EXIT
|
trap cleanup EXIT
|
||||||
|
|
||||||
|
|
||||||
# 从命令行读取 安装路径与版本号
|
# 从命令行读取 安装路径与版本号
|
||||||
while getopts ":hd:v:l:" option; do
|
while getopts ":hd:v:l:" option; do
|
||||||
case $option in
|
case $option in
|
||||||
@ -210,7 +210,7 @@ while getopts ":hd:v:l:" option; do
|
|||||||
version=${OPTARG}
|
version=${OPTARG}
|
||||||
;;
|
;;
|
||||||
l)
|
l)
|
||||||
lan=${OPTARG}
|
lang=${OPTARG}
|
||||||
;;
|
;;
|
||||||
\?)
|
\?)
|
||||||
echo "无效的命令行选项,请使用 -h 查看帮助"
|
echo "无效的命令行选项,请使用 -h 查看帮助"
|
||||||
@ -220,8 +220,8 @@ while getopts ":hd:v:l:" option; do
|
|||||||
done
|
done
|
||||||
|
|
||||||
# 判断语言的类型,不符合直接退出
|
# 判断语言的类型,不符合直接退出
|
||||||
if [[ "$lan" != "zh" && "$lan" != "en" ]]; then
|
if [[ "$lang" != "zh" && "$lang" != "en" ]]; then
|
||||||
error "无效的语言选项: $lan。支持的选项是 zh 和 en"
|
error "无效的语言选项: $lang,支持的选项为 zh 和 en"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ "$helpflag" -eq 1 ]; then
|
if [ "$helpflag" -eq 1 ]; then
|
||||||
|
Loading…
x
Reference in New Issue
Block a user