From 4a7f33f7bb78e6df377e222b20925671520adb85 Mon Sep 17 00:00:00 2001 From: Cotes Chung <11371340+cotes2020@users.noreply.github.com> Date: Tue, 7 Nov 2023 03:38:02 +0800 Subject: [PATCH] build(tools): improve the release script --- tools/release | 61 +++++++++++++++++++++++++++------------------------ 1 file changed, 32 insertions(+), 29 deletions(-) diff --git a/tools/release b/tools/release index 59bc917..6b4cc37 100755 --- a/tools/release +++ b/tools/release @@ -8,7 +8,7 @@ # and production branch. # # -# Usage: run on main branch or the patch branch +# Usage: run on the default, release or the patch branch # # Requires: Git, NPM and RubyGems @@ -18,8 +18,7 @@ opt_pre=false # preview mode option working_branch="$(git branch --show-current)" -# AKA the default branch, main/master branch -STAGING_BRANCH="$(git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@')" +DEFAULT_BRANCH="$(git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@')" PROD_BRANCH="production" @@ -65,15 +64,16 @@ _check_cli() { } _check_git() { - # ensure nothing is uncommitted + # ensure that changes have been committed if [[ -n $(git status . -s) ]]; then echo "> Abort: Commit the staged files first, and then run this tool again." exit 1 fi - # ensure the working branch is the default/patch branch - if [[ $working_branch != "$STAGING_BRANCH" && $working_branch != hotfix/* ]]; then - echo "> Abort: Please run on the $STAGING_BRANCH branch or a patch branche." + if [[ $working_branch != "$DEFAULT_BRANCH" && + $working_branch != hotfix/* && + $working_branch != "$PROD_BRANCH" ]]; then + echo "> Abort: Please run on the default, release or patch branch." exit 1 fi } @@ -101,14 +101,16 @@ check() { _check_node_packages } -# auto-generate a new version number to the file 'package.json' and +# Auto-generate a new version number to the file 'package.json' bump_node() { + bump="standard-version -i $CHANGE_LOG" + if $opt_pre; then - standard-version -i "$CHANGE_LOG" -p rc - else - standard-version -i "$CHANGE_LOG" + bump="$bump -p rc" fi + eval "$bump" + # Change heading of Patch version to heading level 2 (a bug from `standard-version`) sed -i "s/^### \[/## \[/g" "$CHANGE_LOG" # Replace multiple empty lines with a single empty line @@ -117,29 +119,34 @@ bump_node() { ## Bump new version to gem config file bump_gem() { - sed -i "s/[[:digit:]]\+\.[[:digit:]]\+\.[[:digit:]]\+/$1/" "$GEM_SPEC" + _ver="$1" + + if $opt_pre; then + _ver="${1/-/.}" + fi + + sed -i "s/[[:digit:]]\+\.[[:digit:]]\+\.[[:digit:]]\+/$_ver/" "$GEM_SPEC" } -# Update the git branches, create a new tag, and then build the gem package. -release() { +# Creates a new tag on the production branch with the given version number. +# Also commits the changes and merges the production branch into the default branch. +branch() { _version="$1" # X.Y.Z - _latest_commit="$(git rev-parse HEAD)" + + git add . + git commit -m "chore(release): $_version" # Create a new tag on production branch echo -e "> Create tag v$_version\n" git tag "v$_version" - git checkout "$STAGING_BRANCH" + git checkout "$DEFAULT_BRANCH" + git merge --no-ff --no-edit "$PROD_BRANCH" if [[ $working_branch == hotfix/* ]]; then - git merge --no-ff --no-edit "$working_branch" # delete the patch branch git branch -D "$working_branch" fi - - # cherry-pick the latest commit from production branch to default branch - git cherry-pick "$_latest_commit" - } ## Build a gem package @@ -164,7 +171,7 @@ build_gem() { main() { check - if [[ $opt_pre = false ]]; then + if [[ $opt_pre = false && $working_branch != "$PROD_BRANCH" ]]; then git checkout "$PROD_BRANCH" git merge --no-ff --no-edit "$working_branch" fi @@ -175,16 +182,12 @@ main() { bump_gem "$_version" - echo -e "> Build the gem package for v$_version\n" - if [[ $opt_pre = false ]]; then - echo -e "> Bumped version number to $_version\n" - git add . - git commit -m "chore(release): $_version" - - release "$_version" + branch "$_version" fi + echo -e "> Build the gem package for v$_version\n" + build_gem }