build_mac_universal.sh 3.19 KB
#!/bin/bash
# macOS Build Script - Universal (Intel + Apple Silicon)
# 自动检测架构,自动安装依赖

set -e  # 遇错即停

echo "================================"
echo "Building Gemini Image Generator"
echo "================================"

# 检测架构
ARCH=$(uname -m)
echo "Detected architecture: $ARCH"

# 设置 Homebrew 路径
if [ "$ARCH" = "arm64" ]; then
    BREW_PREFIX="/opt/homebrew"
else
    BREW_PREFIX="/usr/local"
fi

# 检查 Homebrew 是否安装
if ! command -v brew &> /dev/null; then
    echo "Homebrew not found. Installing..."
    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

    # 添加到当前 shell 的 PATH
    eval "$($BREW_PREFIX/bin/brew shellenv)"
fi

# 确保 brew 在 PATH 中
if ! command -v brew &> /dev/null; then
    eval "$($BREW_PREFIX/bin/brew shellenv)"
fi

# 检查 Python 3.11 是否安装
PYTHON_CMD="$BREW_PREFIX/bin/python3.11"
if [ ! -f "$PYTHON_CMD" ]; then
    echo "Python 3.11 not found. Installing via Homebrew..."
    brew install python@3.11
fi

# 验证 Python 可用
if [ ! -f "$PYTHON_CMD" ]; then
    echo "Error: Failed to install Python 3.11"
    exit 1
fi

echo "Using Python: $PYTHON_CMD"
$PYTHON_CMD --version

# 检查虚拟环境是否有效(macOS 用 bin/activate,Windows 用 Scripts/activate)
if [ ! -f ".venv/bin/activate" ]; then
    echo "Valid virtual environment not found, creating..."
    rm -rf .venv
    "$PYTHON_CMD" -m venv .venv
fi

# 激活虚拟环境
echo "Activating virtual environment..."
source .venv/bin/activate

# 安装依赖
echo "Installing dependencies..."
pip install --upgrade pip
pip install -r requirements.txt
pip install pyinstaller

# 清理旧构建 (保留 spec 文件,它是构建配置的唯一真相源)
echo "Cleaning previous builds..."
rm -rf build dist

# 构建 (所有配置都在 ZB100ImageGenerator.spec 里)
echo "Building executable..."
pyinstaller ZB100ImageGenerator.spec

# 验证 .app 构建结果
if [ ! -d "dist/ZB100ImageGenerator.app" ]; then
    echo "================================"
    echo "Build failed! (.app not produced)"
    echo "================================"
    exit 1
fi

# 打包 DMG 作为分发格式
# 为什么必须: .app 里有嵌套 symlink (PIL/.dylibs -> __dot__dylibs),
# cp/NAS/SMB/微信 等传输方式可能吞掉内层 symlink 导致目标机启动失败.
# .dmg 是 HFS+ 镜像, symlink 原样保存, 用户挂载拖拽即用.
echo "Creating DMG..."
DMG_PATH="dist/ZB100ImageGenerator.dmg"
rm -f "$DMG_PATH"
hdiutil create \
    -volname "ZB100ImageGenerator" \
    -srcfolder "dist/ZB100ImageGenerator.app" \
    -ov -format UDZO \
    "$DMG_PATH"

if [ ! -f "$DMG_PATH" ]; then
    echo "================================"
    echo "Build partially failed: .app OK but DMG creation failed"
    echo "================================"
    exit 1
fi

echo "================================"
echo "Build successful!"
echo "Architecture: $ARCH"
echo "  App:  dist/ZB100ImageGenerator.app  ($(du -sh dist/ZB100ImageGenerator.app | awk '{print $1}'))"
echo "  DMG:  $DMG_PATH  ($(du -sh "$DMG_PATH" | awk '{print $1}'))"
echo "================================"
echo "分发请用 DMG, 不要直接拷贝 .app 目录 (symlink 易丢)."