Repository navigation
Expand file tree
/
Copy pathbuild.sh
More file actions
202 lines (176 loc) · 6.51 KB
/
Copy pathbuild.sh
File metadata and controls
202 lines (176 loc) · 6.51 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/env bash
set -euo pipefail
# ===== Tauri 跨平台构建脚本 =====
# 用法:
# ./build.sh # 构建当前平台
# ./build.sh macos # macOS
# ./build.sh linux # Linux (需要 Linux 环境)
# ./build.sh windows # Windows (需要 Windows 环境)
# ./build.sh all # 当前等同于 macOS 构建(仅 macOS)
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
log_info() { echo -e "${GREEN}[INFO]${NC} $1"; }
log_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
log_error() { echo -e "${RED}[ERROR]${NC} $1"; }
PLATFORM="${1:-}"
# 检查 Docker 是否运行(cross 需要)
check_docker() {
if ! docker info > /dev/null 2>&1; then
log_error "Docker 未运行,请先启动 Docker Desktop"
exit 1
fi
}
# 检查 cross 是否安装
check_cross() {
if ! command -v cross &> /dev/null; then
log_error "cross 未安装,请运行: cargo install cross --git https://github.com/cross-rs/cross"
exit 1
fi
}
# 读取版本号
get_version() {
grep '"version"' src-tauri/tauri.conf.json | sed 's/.*"version": "\([^"]*\)".*/\1/' | tr -d ' \r\n'
}
# 收集产物到版本目录
collect_artifacts() {
local version="$1"
local output_dir="releases/${version}"
local platform="$2"
mkdir -p "${output_dir}/${platform}"
case "$platform" in
darwin)
local dmg=$(find src-tauri/target/release/bundle/dmg -name "*.dmg" 2>/dev/null | head -1)
[[ -n "$dmg" ]] && cp "$dmg" "${output_dir}/${platform}/"
local app_dir=$(find src-tauri/target/release/bundle/app -name "*.app" 2>/dev/null | head -1)
if [[ -n "$app_dir" ]]; then
tar -czf "${output_dir}/${platform}/$(basename "$app_dir").tar.gz" -C "$(dirname "$app_dir")" "$(basename "$app_dir")"
fi
;;
linux)
local appimage=$(find src-tauri/target/x86_64-unknown-linux-gnu/release/bundle/appimage -name "*.AppImage" 2>/dev/null | head -1)
[[ -n "$appimage" ]] && cp "$appimage" "${output_dir}/${platform}/"
local deb=$(find src-tauri/target/x86_64-unknown-linux-gnu/release/bundle/deb -name "*.deb" 2>/dev/null | head -1)
[[ -n "$deb" ]] && cp "$deb" "${output_dir}/${platform}/"
;;
windows)
local exe=$(find src-tauri/target/x86_64-pc-windows-gnu/release/bundle/nsis -name "*.exe" 2>/dev/null | head -1)
[[ -n "$exe" ]] && cp "$exe" "${output_dir}/${platform}/"
local msi=$(find src-tauri/target/x86_64-pc-windows-gnu/release/bundle -name "*.msi" 2>/dev/null | head -1)
[[ -n "$msi" ]] && cp "$msi" "${output_dir}/${platform}/"
;;
esac
log_info "${platform} 产物已收集到 ${output_dir}/${platform}/"
}
# macOS 原生构建
build_macos() {
log_info "开始 macOS 原生构建..."
pnpm install --frozen-lockfile
# CI mode makes Tauri skip Finder AppleScript, which is unreliable in
# headless shells while producing the same installable DMG contents.
CI=true pnpm tauri build --bundles app,dmg
log_info "macOS 构建完成 ✓"
}
# Linux 交叉编译 (Rust 代码)
build_linux_cross() {
log_info "开始 Linux x86_64 交叉编译 (仅 Rust 代码)..."
check_docker
check_cross
pnpm install --frozen-lockfile
pnpm build
cross build --release --manifest-path src-tauri/Cargo.toml --target x86_64-unknown-linux-gnu
log_info "Linux Rust 代码编译完成 ✓"
log_warn "提示: 如需 .AppImage/.deb 安装包,请在 Linux 原生环境运行 ./build.sh linux"
}
# Windows 交叉编译 (Rust 代码)
build_windows_cross() {
log_info "开始 Windows x86_64 交叉编译 (仅 Rust 代码)..."
check_docker
check_cross
pnpm install --frozen-lockfile
pnpm build
cross build --release --manifest-path src-tauri/Cargo.toml --target x86_64-pc-windows-gnu
log_info "Windows Rust 代码编译完成 ✓"
log_warn "提示: 如需 .exe/.msi 安装包,请在 Windows 原生环境运行 ./build.sh windows"
}
# Linux 原生完整构建
build_linux_native() {
log_info "开始 Linux 原生完整构建..."
pnpm install --frozen-lockfile
pnpm tauri build --bundles deb,appimage
log_info "Linux 构建完成 ✓"
}
# Windows 原生完整构建
build_windows_native() {
log_info "开始 Windows 原生完整构建..."
pnpm install --frozen-lockfile
pnpm tauri build --bundles nsis
log_info "Windows 构建完成 ✓"
}
# 构建所有平台
build_all() {
local version
version=$(get_version)
local os="$(uname -s)"
if [[ "$os" == "Darwin" ]]; then
build_macos
collect_artifacts "$version" "darwin"
log_warn "Linux/Windows 交叉编译仅生成 Rust 二进制文件,不包含安装包"
log_warn "请在对应平台运行 ./build.sh linux 或 ./build.sh windows 获取完整安装包"
else
log_error "all 模式仅支持 macOS 运行"
exit 1
fi
log_info "=========================================="
log_info "构建完成!版本: $version"
log_info "产物目录: releases/${version}/"
log_info "=========================================="
}
case "$PLATFORM" in
macos|mac|macOS)
version=$(get_version)
build_macos
collect_artifacts "$version" "darwin"
;;
linux)
version=$(get_version)
os="$(uname -s)"
if [[ "$os" == "Linux" ]]; then
build_linux_native
collect_artifacts "$version" "linux"
else
build_linux_cross
collect_artifacts "$version" "linux"
fi
;;
windows)
version=$(get_version)
os="$(uname -s)"
if [[ "$os" == *"MINGW"* || "$os" == *"MSYS"* || "$os" == *"CYGWIN"* ]]; then
build_windows_native
else
build_windows_cross
fi
collect_artifacts "$version" "windows"
;;
all)
build_all
;;
"")
version=$(get_version)
os="$(uname -s)"
case "$os" in
Darwin) build_macos; collect_artifacts "$version" "darwin" ;;
Linux) build_linux_native; collect_artifacts "$version" "linux" ;;
MINGW*|MSYS*|CYGWIN*) build_windows_native; collect_artifacts "$version" "windows" ;;
*) log_error "未知平台: $os"; exit 1 ;;
esac
;;
*)
log_error "未知目标: $PLATFORM"
echo "用法: $0 [macos|linux|windows|all]"
exit 1
;;
esac
log_info "产物目录: releases/${version}/"