Files

94 lines
4.1 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
set -euo pipefail
# ── 配置 ──────────────────────────────────────────────────────────────────────
APP_NAME="tmeet"
OUTPUT_DIR="dist"
MAIN_PKG="."
# 版本号:必须通过第一个参数传入
if [ $# -ge 1 ] && [ -n "$1" ]; then
VERSION="$1"
else
echo "❌ 请传入版本号例如bash build.sh v1.0.0"
exit 1
fi
BUILD_TIME=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
LDFLAGS="-s -w -X tmeet/cmd.Version=${VERSION} -X tmeet/cmd.BuildTime=${BUILD_TIME}"
# ── 单元测试(失败即中止,依赖脚本顶部的 set -e─────────────────────────────
echo "🧪 运行单元测试: go test -count=1 ./..."
echo "────────────────────────────────────────"
go test -count=1 ./...
echo "────────────────────────────────────────"
echo "✅ 单元测试全部通过"
echo ""
# ── 编译目标 ──────────────────────────────────────────────────────────────────
# 格式GOOS/GOARCH/平台友好名称
TARGETS=(
"darwin/amd64/macOS-Intel"
"darwin/arm64/macOS-AppleSilicon"
"linux/amd64/Linux-x86_64"
"linux/arm64/Linux-ARM64"
"windows/amd64/Windows-x86_64"
)
# ── 清理并创建输出目录 ────────────────────────────────────────────────────────
rm -rf "${OUTPUT_DIR}"
mkdir -p "${OUTPUT_DIR}"
echo "版本: ${VERSION} 构建时间: ${BUILD_TIME}"
echo "────────────────────────────────────────"
# ── 开始编译 ──────────────────────────────────────────────────────────────────
for TARGET in "${TARGETS[@]}"; do
GOOS=$(echo "${TARGET}" | cut -d/ -f1)
GOARCH=$(echo "${TARGET}" | cut -d/ -f2)
PLATFORM=$(echo "${TARGET}" | cut -d/ -f3)
# Windows 可执行文件加 .exe 后缀
EXT=""
if [ "${GOOS}" = "windows" ]; then
EXT=".exe"
fi
OUTPUT="${OUTPUT_DIR}/${APP_NAME}-${PLATFORM}${EXT}"
printf "编译 %-30s → %s\n" "${GOOS}/${GOARCH} (${PLATFORM})" "${OUTPUT}"
CGO_ENABLED=0 GOOS="${GOOS}" GOARCH="${GOARCH}" \
go build -trimpath -ldflags "${LDFLAGS}" -o "${OUTPUT}" "${MAIN_PKG}"
done
echo "────────────────────────────────────────"
# ── 权限位兜底 + 自检 ─────────────────────────────────────────────────────────
# 确保所有非 .exe 产物均为 0755防止 umask 异常、后处理步骤或发布环境导致 x 位丢失,
# 进而在用户侧(尤其是 Agent 沙箱等无法 chmod 的场景)无法执行。
for f in "${OUTPUT_DIR}"/*; do
case "$f" in
*.exe) continue ;;
esac
chmod 0755 "$f"
if [ ! -x "$f" ]; then
echo "$f 缺少可执行权限,构建失败"
exit 1
fi
done
echo "✅ 权限自检通过(所有非 .exe 产物均为 0755"
echo "✅ 全部编译完成,产物位于 ./${OUTPUT_DIR}/"
ls -lh "${OUTPUT_DIR}/"
# ── 同步版本号到 package.json ─────────────────────────────────────────────────
echo ""
echo "🔖 同步版本号到 package.json: ${VERSION}"
# macOS sed 需要 -i '' 参数Linux 直接 -i
if [[ "$(uname)" == "Darwin" ]]; then
sed -i '' "s/\"version\": \".*\"/\"version\": \"${VERSION}\"/" package.json
else
sed -i "s/\"version\": \".*\"/\"version\": \"${VERSION}\"/" package.json
fi