#!/usr/bin/env bash set -eo pipefail # ==================================================== # 配置变量 # ==================================================== TEMP_PATH="WeChatMac/temp" DOWNLOAD_LINK="${1:-https://dldir1.qq.com/weixin/mac/WeChatMac.dmg}" # ==================================================== # 函数定义 # ==================================================== # 打印分隔线 print_separator() { printf '%*s\n' 60 | tr ' ' '#' } # 彩色输出函数 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 } # 下载 WeChat DMG download_wechat() { print_separator echo_color "yellow" "Downloading the newest WeChatMac..." print_separator mkdir -p "$TEMP_PATH" wget -q "$DOWNLOAD_LINK" -O "${TEMP_PATH}/WeChatMac.dmg" if [ "$?" -ne 0 ]; then echo_color "red" "Download Failed, please check your network!" clean_data 1 fi } # 从 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) INFO_PLIST=${TEMP_PATH}/微信\ WeChat/WeChat.app/Contents/Info.plist 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" } # 计算 SHA256 compute_sha256() { local file_path="$1" shasum -a 256 "$file_path" | awk '{print $1}' } # 准备提交(复制 DMG 并创建 .sha256 文件) prepare_commit() { print_separator echo_color "yellow" "Preparing to commit new version..." print_separator 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" <