From 11e00c9239d7f7baabf97bc7abf3737c155fe674 Mon Sep 17 00:00:00 2001 From: zsbai Date: Tue, 1 Oct 2024 15:36:57 +0100 Subject: [PATCH] update --- scripts/destVersionForMac.sh | 282 ++++++++++++++++++++++++----------- 1 file changed, 193 insertions(+), 89 deletions(-) diff --git a/scripts/destVersionForMac.sh b/scripts/destVersionForMac.sh index c953c57..eb4635f 100644 --- a/scripts/destVersionForMac.sh +++ b/scripts/destVersionForMac.sh @@ -2,125 +2,229 @@ set -eo pipefail -temp_path="WeChatMac/temp" -latest_path="WeChatMac/latest" +# ==================================================== +# 配置变量 +# ==================================================== +TEMP_PATH="WeChatMac/temp" +DOWNLOAD_LINK="${1:-https://dldir1.qq.com/weixin/mac/WeChatMac.dmg}" -download_link="$1" -if [ -z "$1" ]; then - >&2 echo -e "Missing argument. Using default download link" - download_link="https://dldir1.qq.com/weixin/mac/WeChatMac.dmg" -fi +# ==================================================== +# 函数定义 +# ==================================================== -function install_depends() { - printf "#%.0s" {1..60} - echo - echo -e "## \033[1;33mInstalling 7zip, shasum, wget, curl, git\033[0m" - printf "#%.0s" {1..60} - echo - - apt install -y p7zip-full p7zip-rar libdigest-sha-perl wget curl git python3 python3-pip - pip install lxml request +# 打印分隔线 +print_separator() { + printf '%*s\n' 60 | tr ' ' '#' } -function login_gh() { - printf "#%.0s" {1..60} - echo - echo -e "## \033[1;33mLogin to github to use github-cli...\033[0m" - printf "#%.0s" {1..60} - echo - if [ -z $GHTOKEN ]; then - >&2 echo -e "\033[1;31mMissing Github Token! Please get a BotToken from 'Github Settings->Developer settings->Personal access tokens' and set it in Repo Secrect\033[0m" +# 彩色输出函数 +echo_color() { + local color="$1" + shift + local message="$*" + case "$color" in + yellow) + echo -e "\033[1;33m$message\033[0m" + ;; + red) + echo -e "\033[1;31m$message\033[0m" >&2 + ;; + green) + echo -e "\033[1;32m$message\033[0m" + ;; + *) + echo "$message" + ;; + esac +} + +# 安装依赖项 +install_depends() { + print_separator + echo_color "yellow" "Installing dependencies: 7zip, wget, curl, git, gh, dmg2img, shasum" + print_separator + + sudo apt update + sudo apt install -y p7zip-full p7zip-rar libdigest-sha-perl wget curl git gh dmg2img + + # 验证 gh CLI 是否已安装 + if ! command -v gh >/dev/null 2>&1; then + echo_color "red" "GitHub CLI (gh) is not installed. Exiting." exit 1 fi - echo $GHTOKEN > WeChatMac/temp/GHTOKEN - gh auth login --with-token < WeChatMac/temp/GHTOKEN - if [ "$?" -ne 0 ]; then - >&2 echo -e "\033[1;31mLogin Failed, please check your network or token!\033[0m" - clean_data 1 + # 验证 gh CLI 是否已认证 + if ! gh auth status >/dev/null 2>&1; then + if [ -n "${GHTOKEN:-}" ]; then + echo "$GHTOKEN" | gh auth login --with-token + else + echo_color "red" "Missing GitHub Token! Please set the GHTOKEN environment variable." + exit 1 + fi fi - rm -rfv WeChatMac/temp/GHTOKEN } -function download_wechat() { - printf "#%.0s" {1..60} - echo - echo -e "## \033[1;33mDownloading the newest WeChatMac...\033[0m" - printf "#%.0s" {1..60} - echo +# 下载 WeChat DMG +download_wechat() { + print_separator + echo_color "yellow" "Downloading the newest WeChatMac..." + print_separator - wget -q "$download_link" -O ${temp_path}/WeChatMac.dmg + mkdir -p "$TEMP_PATH" + + wget -q "$DOWNLOAD_LINK" -O "${TEMP_PATH}/WeChatMac.dmg" if [ "$?" -ne 0 ]; then - >&2 echo -e "\033[1;31mDownload Failed, please check your network!\033[0m" + echo_color "red" "Download Failed, please check your network!" clean_data 1 fi } -function get_version() { - dest_version=`python3 scripts/getVersion.py` +# 从 Info.plist 提取版本信息 +get_version() { + print_separator + echo_color "yellow" "Extracting version from Info.plist..." + print_separator + + # 使用 dmg2img 将 DMG 转换为 IMG + dmg2img "${TEMP_PATH}/WeChatMac.dmg" "${TEMP_PATH}/WeChatMac.img" + + # 使用 7z x 解压 IMG 文件,保留目录结构 + 7z x "${TEMP_PATH}/WeChatMac.img" -o"${TEMP_PATH}" >/dev/null || { + echo_color "red" "Failed to extract IMG file." + clean_data 1 + } + + # 查找 Info.plist + INFO_PLIST=$(find "${TEMP_PATH}" -type f -name "Info.plist" | head -n 1) + + if [ -z "$INFO_PLIST" ] || [ ! -f "$INFO_PLIST" ]; then + echo_color "red" "Info.plist not found in the IMG!" + clean_data 1 + fi + + # 使用 grep 和 sed 提取版本号 + VERSION=$(grep -A1 'CFBundleShortVersionString' "$INFO_PLIST" | grep '' | sed -E 's/.*([^<]+)<\/string>.*/\1/') + + if [ -z "$VERSION" ]; then + VERSION=$(grep -A1 'CFBundleVersion' "$INFO_PLIST" | grep '' | sed -E 's/.*([^<]+)<\/string>.*/\1/') + fi + + if [ -z "$VERSION" ]; then + echo_color "red" "Version information not found in Info.plist!" + clean_data 1 + fi + + echo "Version: $VERSION" } - -# rename and replace -function prepare_commit() { - printf "#%.0s" {1..60} - echo - echo -e "## \033[1;33mPrepare to commit new version\033[0m" - printf "#%.0s" {1..60} - echo - - mkdir -p WeChatMac/$dest_version - cp $temp_path/WeChatMac.dmg WeChatMac/$dest_version/WeChatMac-$dest_version.dmg - echo "DestVersion: $dest_version" > WeChatMac/$dest_version/WeChatMac-$dest_version.dmg.sha256 - echo "Sha256: $now_sum256" >> WeChatMac/$dest_version/WeChatMac-$dest_version.dmg.sha256 - echo "UpdateTime: $(date -u '+%Y-%m-%d %H:%M:%S') (UTC)" >> WeChatMac/$dest_version/WeChatMac-$dest_version.dmg.sha256 - echo "DownloadFrom: $download_link" >> WeChatMac/$dest_version/WeChatMac-$dest_version.dmg.sha256 - +# 计算 SHA256 +compute_sha256() { + local file_path="$1" + shasum -a 256 "$file_path" | awk '{print $1}' } -function clean_data() { - printf "#%.0s" {1..60} - echo - echo -e "## \033[1;33mClean runtime and exit...\033[0m" - printf "#%.0s" {1..60} - echo +# 准备提交(复制 DMG 并创建 .sha256 文件) +prepare_commit() { + print_separator + echo_color "yellow" "Preparing to commit new version..." + print_separator - rm -rfv WeChatMac/* - exit $1 + VERSION_DIR="WeChatMac/$VERSION" + mkdir -p "$VERSION_DIR" + + cp "${TEMP_PATH}/WeChatMac.dmg" "$VERSION_DIR/WeChatMac-$VERSION.dmg" + + NOW_SUM256=$(compute_sha256 "$VERSION_DIR/WeChatMac-$VERSION.dmg") + + cat > "$VERSION_DIR/WeChatMac-$VERSION.dmg.sha256" <&2 echo -e "\n\033[1;32mThis is the newest Version!\033[0m\n" + # 提取版本信息 + get_version + + # 准备提交(复制 DMG 并创建 .sha256 文件) + prepare_commit + + # 获取最新的 GitHub Release 信息 + get_latest_release_info + + # 比较 SHA256 值 + if [ "$NOW_SUM256" = "$LATEST_SUM256" ] && [ -n "$LATEST_SUM256" ]; then + echo_color "green" "This is the newest Version!" clean_data 0 fi - ## if not the newest - get_version - prepare_commit - # if dest_version is the same as latest_version - if [ "$dest_version" = "$latest_version" ]; then - version="$dest_version"_`date -u '+%Y%m%d'` - echo -e $dest_version - else - version="$dest_version" - fi - - gh release create v$version ./WeChatMac/$dest_version/WeChatMac-$dest_version.dmg -F ./WeChatMac/$dest_version/WeChatMac-$dest_version.dmg.sha256 -t "Wechat For Mac v$version" - # gh auth logout --hostname github.com | echo "y" + # 创建新的 GitHub Release + create_release + # 清理临时数据并退出 clean_data 0 } +# 执行主流程 main -