From d3813796b6d359134b021625eaf4d937b9b55428 Mon Sep 17 00:00:00 2001 From: caoweiping Date: Sat, 18 Apr 2026 23:17:40 +0800 Subject: [PATCH] =?UTF-8?q?refactor(scripts):=20=E7=BB=9F=E4=B8=80?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E5=8F=B7=E8=8E=B7=E5=8F=96=E9=80=BB=E8=BE=91?= =?UTF-8?q?=E5=B9=B6=E6=B7=BB=E5=8A=A0=E7=89=88=E6=9C=AC=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=E8=BE=93=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 重构版本号获取脚本,统一处理不同CI环境和本地环境的版本号获取 添加版本信息输出功能,便于调试和验证构建版本 修改相关脚本以使用新的版本号获取方式 --- .github/workflows/build-tauri.yml | 15 +++++ .github/workflows/build.yml | 15 +++++ scripts/package/build_app_tauri.sh | 3 +- scripts/package/getversion.sh | 100 ++++++++++++++++++++++++----- scripts/package/package-all.sh | 24 +++++-- scripts/package/package-deb.sh | 10 +-- 6 files changed, 141 insertions(+), 26 deletions(-) diff --git a/.github/workflows/build-tauri.yml b/.github/workflows/build-tauri.yml index 41f9c532f..a0679071f 100644 --- a/.github/workflows/build-tauri.yml +++ b/.github/workflows/build-tauri.yml @@ -55,6 +55,21 @@ jobs: run: | echo "VERSION_TAG=${GITHUB_REF_NAME}" >> $GITHUB_ENV + - name: Determine and output version + run: | + VERSION_WITH_V=$(bash scripts/package/getversion.sh) + VERSION_NO_V=$(bash scripts/package/getversion.sh --strip-v) + echo "VERSION_WITH_V=${VERSION_WITH_V}" >> $GITHUB_ENV + echo "VERSION_NO_V=${VERSION_NO_V}" >> $GITHUB_ENV + echo "========================================" + echo "Build Version Information (Tauri)" + echo "========================================" + echo "GitHub ref: ${{ github.ref }}" + echo "GitHub ref_name: ${{ github.ref_name }}" + echo "Version (with v): ${VERSION_WITH_V}" + echo "Version (no v): ${VERSION_NO_V}" + echo "========================================" + - name: Set up Python uses: actions/setup-python@v6 with: diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 45078c1c3..66418d5cb 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -57,6 +57,21 @@ jobs: run: | echo "VERSION_TAG=${GITHUB_REF_NAME}" >> $GITHUB_ENV + - name: Determine and output version + run: | + VERSION_WITH_V=$(bash scripts/package/getversion.sh) + VERSION_NO_V=$(bash scripts/package/getversion.sh --strip-v) + echo "VERSION_WITH_V=${VERSION_WITH_V}" >> $GITHUB_ENV + echo "VERSION_NO_V=${VERSION_NO_V}" >> $GITHUB_ENV + echo "========================================" + echo "Build Version Information" + echo "========================================" + echo "GitHub ref: ${{ github.ref }}" + echo "GitHub ref_name: ${{ github.ref_name }}" + echo "Version (with v): ${VERSION_WITH_V}" + echo "Version (no v): ${VERSION_NO_V}" + echo "========================================" + - name: Set up Python uses: actions/setup-python@v6 with: diff --git a/scripts/package/build_app_tauri.sh b/scripts/package/build_app_tauri.sh index 9a2b5ae8d..7b1afefbf 100755 --- a/scripts/package/build_app_tauri.sh +++ b/scripts/package/build_app_tauri.sh @@ -6,7 +6,8 @@ set -e APP_NAME="ActivityWatch" BUNDLE_ID="net.activitywatch.ActivityWatch" -VERSION="0.1.0" +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +VERSION="$("$SCRIPT_DIR/getversion.sh" --strip-v)" ICON_PATH="aw-tauri/src-tauri/icons/icon.icns" if [[ "$(uname)" != "Darwin" ]]; then diff --git a/scripts/package/getversion.sh b/scripts/package/getversion.sh index 9c237975e..6a9f1e24f 100755 --- a/scripts/package/getversion.sh +++ b/scripts/package/getversion.sh @@ -1,19 +1,89 @@ #!/bin/bash +set -e -# TODO: Merge with scripts/package/getversion.sh -# set -e - -if [[ $TRAVIS_TAG ]]; then - _version=$TRAVIS_TAG; -elif [[ $APPVEYOR_REPO_TAG_NAME ]]; then - _version=$APPVEYOR_REPO_TAG_NAME; -else - # Exact - _version=$(git describe --tags --abbrev=0 --exact-match 2>/dev/null) - if [[ -z $_version ]]; then - # Latest tag + commit ID - _version="$(git describe --tags --abbrev=0).dev-$(git rev-parse --short HEAD)" +SCRIPT_NAME="$(basename "$0")" +STRIP_V=false + +show_usage() { + cat << EOF +Usage: $SCRIPT_NAME [OPTIONS] + +Get the version of ActivityWatch from git tags or CI environment variables. + +Options: + --strip-v, --no-v Remove the 'v' prefix from the version (if present) + --help, -h Show this help message + +Environment Variables (used in CI, checked in priority order): + GITHUB_REF_NAME GitHub Actions tag/ref (e.g., "v0.14.0") + TRAVIS_TAG Travis CI tag + APPVEYOR_REPO_TAG_NAME AppVeyor CI tag + +Version Format: + - Release tag: v0.14.0 + - Dev version: v0.14.0.dev-abc1234 + - Beta/RC: v0.14.0b1, v0.14.0rc1 + +Examples: + $SCRIPT_NAME # v0.14.0 or v0.14.0.dev-abc1234 + $SCRIPT_NAME --strip-v # 0.14.0 or 0.14.0.dev-abc1234 +EOF +} + +parse_args() { + while [[ $# -gt 0 ]]; do + case "$1" in + --strip-v|--no-v) + STRIP_V=true + shift + ;; + --help|-h) + show_usage + exit 0 + ;; + *) + echo "ERROR: Unknown argument: $1" >&2 + show_usage >&2 + exit 1 + ;; + esac + done +} + +get_version_internal() { + local _version="" + + if [[ -n "$GITHUB_REF_NAME" && "$GITHUB_REF_NAME" == v* ]]; then + _version="$GITHUB_REF_NAME" + elif [[ -n "$TRAVIS_TAG" ]]; then + _version="$TRAVIS_TAG" + elif [[ -n "$APPVEYOR_REPO_TAG_NAME" ]]; then + _version="$APPVEYOR_REPO_TAG_NAME" + else + _version="$(git describe --tags --abbrev=0 --exact-match 2>/dev/null || true)" + if [[ -z "$_version" ]]; then + local _latest_tag + _latest_tag="$(git describe --tags --abbrev=0 2>/dev/null || echo "v0.0.0")" + local _commit_hash + _commit_hash="$(git rev-parse --short HEAD 2>/dev/null || echo "unknown")" + _version="${_latest_tag}.dev-${_commit_hash}" + fi + fi + + echo "$_version" +} + +main() { + parse_args "$@" + + local version + version="$(get_version_internal)" + + if $STRIP_V; then + version="$(echo "$version" | sed -e 's/^v//')" fi -fi + + echo "$version" +} -echo $_version; +main "$@" diff --git a/scripts/package/package-all.sh b/scripts/package/package-all.sh index 52c19f2d1..78777cc1a 100755 --- a/scripts/package/package-all.sh +++ b/scripts/package/package-all.sh @@ -29,8 +29,14 @@ function get_platform() { echo $_platform; } +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + function get_version() { - $(dirname "$0")/getversion.sh; + "$SCRIPT_DIR/getversion.sh"; +} + +function get_version_no_prefix() { + "$SCRIPT_DIR/getversion.sh" --strip-v; } function get_arch() { @@ -40,13 +46,23 @@ function get_arch() { platform=$(get_platform) version=$(get_version) +version_no_prefix=$(get_version_no_prefix) arch=$(get_arch) -# Suffix to distinguish Tauri builds from aw-qt builds in release assets build_suffix="" if [[ $TAURI_BUILD == "true" ]]; then build_suffix="-tauri" fi -echo "Platform: $platform, arch: $arch, version: $version, tauri: ${TAURI_BUILD:-false}" + +echo "========================================" +echo "Build Version Information" +echo "========================================" +echo "Platform: $platform" +echo "Arch: $arch" +echo "Version (with v): $version" +echo "Version (no v): $version_no_prefix" +echo "Tauri build: ${TAURI_BUILD:-false}" +echo "========================================" +echo # For Tauri Linux builds, include helper scripts and README if [[ $platform == "linux" && $TAURI_BUILD == "true" ]]; then @@ -78,8 +94,6 @@ function build_setup() { exit 1 fi - # Windows installer version should not include 'v' prefix, see: https://github.com/microsoft/winget-pkgs/pull/17564 - version_no_prefix="$(echo $version | sed -e 's/^v//')" if [[ $TAURI_BUILD == "true" ]]; then env AW_VERSION=$version_no_prefix "$innosetupdir/iscc.exe" scripts/package/aw-tauri.iss else diff --git a/scripts/package/package-deb.sh b/scripts/package/package-deb.sh index c9f5ecd2c..bc486a761 100755 --- a/scripts/package/package-deb.sh +++ b/scripts/package/package-deb.sh @@ -1,15 +1,15 @@ #!/usr/bin/bash -# Setting the shell is required, as `sh` doesn't support slicing. # Fail fast set -e # Verbose commands for CI verification set -x -VERSION=$(scripts/package/getversion.sh) -# Slice off the "v" from the tag, which is probably guaranteed -VERSION_NUM=${VERSION:1} -echo $VERSION_NUM +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +VERSION="$("$SCRIPT_DIR/getversion.sh")" +VERSION_NUM="$("$SCRIPT_DIR/getversion.sh" --strip-v)" +echo "Version (with v): $VERSION" +echo "Version (without v): $VERSION_NUM" PKGDIR="activitywatch_$VERSION_NUM" # Package tools