build_mac_universal.sh 2.41 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

# 清理旧构建
echo "Cleaning previous builds..."
rm -rf build dist *.spec

# 构建
echo "Building executable..."
pyinstaller --name="ZB100ImageGenerator" \
    --onedir \
    --windowed \
    --add-data "config.json:." \
    --icon=zb100_mac.icns \
    image_generator.py

# 验证构建结果
if [ -d "dist/ZB100ImageGenerator.app" ]; then
    echo "================================"
    echo "Build successful!"
    echo "Architecture: $ARCH"
    echo "Application: dist/ZB100ImageGenerator.app"
    echo "================================"

    # 显示 app 信息
    ls -lh dist/ZB100ImageGenerator.app/Contents/MacOS/
else
    echo "================================"
    echo "Build failed!"
    echo "================================"
    exit 1
fi