build_mac_universal.sh
2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/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