Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update bash script with English version and Add readme.file #103

Closed
wants to merge 9 commits into from
130 changes: 130 additions & 0 deletions tool/install-en.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# to avoid the operate system not supporting Chinese output, create an english version
install_dir=""
version="pre"
path_to_executable=""
default_install_path="/usr/local/bin"
binary_name="chsrc"
temp_install_dir=""

# display related info
info() {
echo "[INFO] $*"
}

# display error message
error() {
echo -e "[ERROR] $*" >&2
exit 1
}

# display help information for the chsrc installer,
# including usage instructions and available options.
help() {
echo "chsrc Installer"
echo
echo "Usage: install.sh [-h] [-d <install_directory>] [-v <version>]"
echo "Options:"
echo "-h Display this help information"
echo "-d Specify the installation directory, defaults to /usr/local/bin; overwrites old versions if already installed"
echo "-v Specify the version number, range is from 0.1.4 to 0.1.9 or 'pre'"
echo
}

# set install path, if use -d specify, check whether it exists.
set_install_path() {
if [ -n "$install_dir" ]; then
# Expand ~ symbol
install_dir="${install_dir/#\~/$HOME}"

# Check if the path exists; if not, create it
if [ ! -d "$install_dir" ]; then
echo "Directory $install_dir does not exist. Creating..."
mkdir -p "$install_dir" || { echo "Failed to create directory, please retry"; exit 1; }
temp_install_dir="$install_dir" # Record temporary installation directory
fi
elif existing_path=$(command -v "$binary_name" 2>/dev/null); then
info "$binary_name is already installed, updating path: ${existing_path}"
install_dir=$(dirname "$existing_path")
else
# Check default path
if [ -d "$default_install_path" ] && [ -w "$default_install_path" ]; then
install_dir="$default_install_path"
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."
fi
fi
}

# install specifying arch, os, version chsrc binary file from Gitee Repository
install() {
arch="$(uname -m | tr '[:upper:]' '[:lower:]')"

case "$arch" in
x86_64) arch="x64" ;;
aarch64|arm64) arch="aarch64" ;;
riscv64) arch="riscv64" ;;
armv7*) arch="armv7" ;;
*) error "Unsupported architecture: ${arch}" ;;
esac

platform="$(uname -s | awk '{print tolower($0)}')"

case "$platform" in
linux) platform="linux" ;;
darwin) platform="macos" ;;
*) error "Unsupported platform: ${platform}" ;;
esac

if [[ ! "$version" =~ ^(pre|0\.1\.([4-9]))$ ]]; then
# Version does not meet the criteria, report error
error "Unsupported version: ${version}. Version number must be between 0.1.4 and 0.1.9 or 'pre'"
fi

# generate download url
url="https://gitee.com/RubyMetric/chsrc/releases/download/${version}/${binary_name}-${arch}-${platform}"

path_to_executable="${install_dir}/${binary_name}"

info "Downloading ${binary_name} (${arch} architecture, ${platform} platform, version ${version}) to ${path_to_executable}"

if curl -sL "$url" -o "$path_to_executable"; then
chmod +x "$path_to_executable"
info "🎉 Installation completed, path: $path_to_executable"
else
error "Download failed, please check your network connection and proxy settings: ${url}"
fi
}

# Cleanup function
cleanup() {
if [ -n "$temp_install_dir" ] && [ -d "$temp_install_dir" ]; then
echo "Cleaning up created directory: $temp_install_dir"
rm -rf "$temp_install_dir"
fi
}

# Set trap to catch exit signals
trap cleanup EXIT

# Parser commandline argvs
while getopts ":hd:v:" option; do
case $option in
h)
help
exit 0
;;
d)
install_dir=${OPTARG}
;;
v)
version=${OPTARG}
;;
\?)
echo "Invalid command line option. Use -h for help"
exit 1
;;
esac
done

set_install_path
install
47 changes: 37 additions & 10 deletions tool/install.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,47 @@
# ---------------------------------------------------------------

install_dir=""
version="pre"
path_to_executable=""
default_install_path="/usr/local/bin"
binary_name="chsrc"
temp_install_dir="" # 用于存储临时安装目录

# 输出相关信息
info() {
echo "[INFO] $*"
}

# 输出错误到stdout和stderr
error() {
echo -e "[ERROR] $*" >&2
exit 1
}

# 显示 chsrc 安装程序的帮助信息,包括使用说明和可用选项
help() {
echo "chsrc Installer"
echo
echo "使用: install.sh [-h] [-d <安装目录>]"
echo "使用: install.sh [-h] [-d <安装目录>] [-v <版本号>]"
echo "选项:"
echo "-h 打印此帮助信息"
echo "-d 指定安装目录,默认为 /usr/local/bin;如果已安装,则覆盖旧版本"
echo "-v 指定版本号,范围是 0.1.4 到 0.1.9 或 'pre'"
echo
}


# 确定下载路径
set_install_path() {
if [ -n "$install_dir" ]; then
# 扩展 ~ 符号
install_dir="${install_dir/#\~/$HOME}"

# 检查路径是否存在,如果不存在则创建该路径
if [ ! -d "$install_dir" ]; then
echo "目录 $install_dir 不存在,正在创建..."
mkdir -p "$install_dir" || { echo "创建目录失败,请重试"; exit 1; }
temp_install_dir="$install_dir" # 记录临时安装目录
fi
elif existing_path=$(command -v "$binary_name" 2>/dev/null); then
info "$binary_name 已安装,更新路径: ${existing_path}"
install_dir=$(dirname "$existing_path")
Expand All @@ -54,13 +67,13 @@ set_install_path() {
fi
}


# 从Gitee仓库安装 指定架构,操作系统,版本 的chsrc二进制文件
install() {
arch="$(uname -m | tr '[:upper:]' '[:lower:]')"

case "$arch" in
x86_64) arch="x64" ;;
aarch64) arch="aarch64" ;;
aarch64|arm64) arch="aarch64" ;;
riscv64) arch="riscv64" ;;
armv7*) arch="armv7" ;;
*) error "不支持的架构: ${arch}" ;;
Expand All @@ -74,13 +87,17 @@ install() {
*) error "不支持的平台: ${platform}" ;;
esac

url="https://gitee.com/RubyMetric/chsrc/releases/download/pre/${binary_name}-${arch}-${platform}"
if [[ ! "$version" =~ ^(pre|0\.1\.([4-9]))$ ]]; then
# version 不符合条件,报错
error "不支持的版本: ${version},版本号必须在 0.1.4 到 0.1.9 之间或为 'pre'"
fi

url="https://gitee.com/RubyMetric/chsrc/releases/download/${version}/${binary_name}-${arch}-${platform}"

path_to_executable="${install_dir}/${binary_name}"

info "下载 ${binary_name} (${arch} 架构, ${platform} 平台) 到 ${path_to_executable}"
info "下载 ${binary_name} (${arch} 架构, ${platform} 平台, ${version}版本) 到 ${path_to_executable}"

# 下载文件并设置权限
if curl -sL "$url" -o "$path_to_executable"; then
chmod +x "$path_to_executable"
info "🎉 安装完成,路径: $path_to_executable"
Expand All @@ -89,9 +106,19 @@ install() {
fi
}

# 清理函数
cleanup() {
if [ -n "$temp_install_dir" ] && [ -d "$temp_install_dir" ]; then
echo "清理创建的目录: $temp_install_dir"
rm -rf "$temp_install_dir"
fi
}

# 设置 trap 以捕获退出信号
trap cleanup EXIT

# main
while getopts ":hd:" option; do
# 从命令行读取 安装路径与版本号
while getopts ":hd:v:" option; do
case $option in
h)
help
Expand All @@ -111,4 +138,4 @@ while getopts ":hd:" option; do
done

set_install_path
install
install
63 changes: 63 additions & 0 deletions tool/readme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
###########################################################################
# Chsrc 相关工具
# 脚本工具
###########################################################################

************
1. 概述
************

此文件夹包含许多与安装 Chsrc 二进制文件相关的脚本工具,您可以使用这些工具指定目录路径和发布版本。

********
2. 文件
********

install.sh Bash 安装工具(中文版)。
install-en.sh Bash 安装工具(英文版)。
install.ps1 Powershell 安装工具。
reademe.txt 此文件

*******************
3. 安装
*******************

步骤1:输入“sudo chmod u+x ./install.sh”以使脚本可执行。

步骤2:输入“./install.sh [-h] [-d <install_directory>] [-v <version>]”指定安装路径和发布版本。

步骤3:如果出现错误,请检查您的网络连接并确保您可以访问Gitee。



#####################################################################
# Chsrc realated tools
# Scripts Tools
# English Version
#####################################################################

************
1. Overview
************

This folder contains many script tools realated to install Chsrc binary file,
you can use the tools to specify directory path and release version.

********
2. Files
********

install.sh Bash install tool(Chinese Version).
install-en.sh Bash install tool(English Version).
install.ps1 Powershell install tool.
reademe.txt This file

*******************
3. Install
*******************

Step 1: Type "sudo chmod u+x ./install.sh" to make scrpit executable.

Step 2: Type "./install.sh [-h] [-d <install_directory>] [-v <version>]" to specify install path and release version.

Step 3: If an error occurs, please check your internet connection and ensure that you can access Gitee.