#!/bin/bash set -e # 遇到错误时退出脚本 # 检查是否在 Git 仓库中 if ! git rev-parse --is-inside-work-tree > /dev/null 2>&1; then echo "Error: Not inside a Git repository." exit 1 fi # 获取最近的 tag TAG=$(git describe --tags --abbrev=0 2>/dev/null) # 获取最后一次提交的 SHA COMMIT_SHA=$(git rev-parse --short HEAD) # 检查是否有未提交的更改 if [ -n "$(git status --porcelain)" ]; then DIRTY="-dirty" else DIRTY="" fi # 决定版本信息 if [ -n "$TAG" ]; then VERSION="$TAG-$COMMIT_SHA$DIRTY" else VERSION="$COMMIT_SHA$DIRTY" fi # 输出版本信息 echo "Injecting version: $VERSION" # 处理选项 BUILD_ALL=false INSTALL_DIR="" while [[ $# -gt 0 ]]; do case $1 in --all) BUILD_ALL=true shift ;; --install-dir) INSTALL_DIR="$2" shift 2 ;; *) echo "Unknown option: $1" exit 1 ;; esac done # 编译函数 build_binary() { local OS=$1 local ARCH=$2 local OUTPUT="sylixos-uploader-${OS}-${ARCH}-${VERSION}" if [ "$OS" = "windows" ]; then OUTPUT+=".exe" fi GOOS=$OS GOARCH=$ARCH CGO_ENABLED=0 go build -o "$OUTPUT" -ldflags "-s -w -X 'sylixos-uploader/common.Version=$VERSION'" echo "Built: $OUTPUT" } # 默认只编译本地平台 if [ "$BUILD_ALL" = true ]; then echo "Building for all platforms..." PLATFORMS=( "linux amd64" "linux arm64" "windows amd64" "windows arm64" "darwin amd64" "darwin arm64" "sylixos arm64" ) for PLATFORM in "${PLATFORMS[@]}"; do build_binary $PLATFORM done else echo "Building for local platform..." HOSTOS=$(go env GOHOSTOS) HOSTARCH=$(go env GOHOSTARCH) build_binary $HOSTOS $HOSTARCH fi # 安装到指定目录 if [ -n "$INSTALL_DIR" ]; then mkdir -p "$INSTALL_DIR" BIN_NAME="sylixos-uploader-$(go env GOHOSTOS)-$(go env GOHOSTARCH)-${VERSION}" cp "$BIN_NAME" "$INSTALL_DIR/sylixos-uploader" echo "Installed to $INSTALL_DIR" fi exit 0