2021-01-25 01:20:51 +03:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
#
|
2021-04-12 08:15:14 +03:00
|
|
|
# How does it work:
|
2021-01-25 01:20:51 +03:00
|
|
|
#
|
2021-06-24 16:30:05 +03:00
|
|
|
# 1. Cherry pick the latest commit from default branch
|
|
|
|
# to the target release branch if the target release branch already existed.
|
|
|
|
#
|
|
|
|
# 2. Bump latest version number to the following files:
|
|
|
|
#
|
2021-04-12 08:15:14 +03:00
|
|
|
# - _sass/jekyll-theme-chirpy.scss
|
2021-04-19 09:51:45 +03:00
|
|
|
# - _javascript/copyright
|
2021-04-12 08:15:14 +03:00
|
|
|
# - assets/js/dist/*.js (will be built by gulp later)
|
|
|
|
# - jekyll-theme-chirpy.gemspec
|
|
|
|
# - package.json
|
2021-01-28 17:32:54 +03:00
|
|
|
#
|
2021-06-24 16:30:05 +03:00
|
|
|
# 3. Create a git-tag on release branch
|
2021-04-12 08:15:14 +03:00
|
|
|
#
|
2021-06-24 16:30:05 +03:00
|
|
|
# 4. Build a RubyGems package base on the latest git-tag
|
2021-04-12 08:15:14 +03:00
|
|
|
#
|
|
|
|
#
|
|
|
|
# Usage:
|
|
|
|
#
|
2021-06-24 16:30:05 +03:00
|
|
|
# Run on default branch, if run on other branch requires parameter '-m' (manual mode).
|
2021-01-28 17:32:54 +03:00
|
|
|
#
|
2021-02-04 20:41:35 +03:00
|
|
|
#
|
2021-02-10 12:53:25 +03:00
|
|
|
# Requires: Git, Gulp, RubyGems
|
2021-01-25 01:20:51 +03:00
|
|
|
|
|
|
|
set -eu
|
|
|
|
|
2021-06-24 16:30:05 +03:00
|
|
|
opt_manual=false
|
2021-02-04 20:41:35 +03:00
|
|
|
|
2021-01-25 01:20:51 +03:00
|
|
|
ASSETS=(
|
|
|
|
"_sass/jekyll-theme-chirpy.scss"
|
2021-04-19 09:51:45 +03:00
|
|
|
"_javascript/copyright"
|
2021-01-25 01:20:51 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
GEM_SPEC="jekyll-theme-chirpy.gemspec"
|
|
|
|
|
2021-01-26 22:17:00 +03:00
|
|
|
NODE_META="package.json"
|
|
|
|
|
2021-06-24 16:30:05 +03:00
|
|
|
DEFAULT_BRANCH="master"
|
|
|
|
|
|
|
|
_working_branch="$(git branch --show-current)"
|
|
|
|
|
2021-01-28 17:32:54 +03:00
|
|
|
_check_src() {
|
|
|
|
if [[ ! -f $1 && ! -d $1 ]]; then
|
2021-02-02 16:29:31 +03:00
|
|
|
echo -e "Error: Missing file \"$1\"!\n"
|
2021-01-28 17:32:54 +03:00
|
|
|
exit -1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
check() {
|
|
|
|
if [[ -n $(git status . -s) ]]; then
|
2021-02-02 16:29:31 +03:00
|
|
|
echo "Error: Commit unstaged files first, and then run this tool againt."
|
|
|
|
exit -1
|
|
|
|
fi
|
|
|
|
|
2021-06-24 16:30:05 +03:00
|
|
|
# ensure working on default branch or running in 'manual' mode
|
|
|
|
if [[ $_working_branch != $DEFAULT_BRANCH && $opt_manual == "false" ]]; then
|
2021-04-12 08:15:14 +03:00
|
|
|
echo "Error: This operation must be performed on the 'master' branch or '--manual' mode!"
|
2021-01-28 17:32:54 +03:00
|
|
|
exit -1
|
|
|
|
fi
|
|
|
|
|
|
|
|
for i in "${!ASSETS[@]}"; do
|
|
|
|
_check_src "${ASSETS[$i]}"
|
|
|
|
done
|
|
|
|
|
|
|
|
_check_src "$NODE_META"
|
|
|
|
_check_src "$GEM_SPEC"
|
|
|
|
}
|
|
|
|
|
|
|
|
_bump_assets() {
|
2021-01-27 15:06:31 +03:00
|
|
|
for i in "${!ASSETS[@]}"; do
|
2021-02-10 12:53:25 +03:00
|
|
|
sed -i "s/v[[:digit:]]\+\.[[:digit:]]\+\.[[:digit:]]\+/v$1/" "${ASSETS[$i]}"
|
2021-01-25 01:20:51 +03:00
|
|
|
done
|
|
|
|
|
|
|
|
gulp
|
|
|
|
}
|
|
|
|
|
2021-01-28 17:32:54 +03:00
|
|
|
_bump_gemspec() {
|
2021-02-04 20:41:35 +03:00
|
|
|
sed -i "s/[[:digit:]]\+\.[[:digit:]]\+\.[[:digit:]]\+/$1/" "$GEM_SPEC"
|
2021-01-26 22:17:00 +03:00
|
|
|
}
|
|
|
|
|
2021-01-28 17:32:54 +03:00
|
|
|
_bump_node() {
|
2021-01-26 22:17:00 +03:00
|
|
|
sed -i \
|
2021-02-04 20:41:35 +03:00
|
|
|
"s,[\"]version[\"]: [\"][[:digit:]]\+\.[[:digit:]]\+\.[[:digit:]]\+[\"],\"version\": \"$1\"," \
|
2021-01-26 22:17:00 +03:00
|
|
|
$NODE_META
|
2021-01-25 01:20:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bump() {
|
2021-01-28 17:32:54 +03:00
|
|
|
_bump_assets "$1"
|
|
|
|
_bump_gemspec "$1"
|
|
|
|
_bump_node "$1"
|
2021-01-25 01:20:51 +03:00
|
|
|
|
|
|
|
if [[ -n $(git status . -s) ]]; then
|
|
|
|
git add .
|
|
|
|
git commit -m "Bump version to $1"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2021-01-28 17:32:54 +03:00
|
|
|
build_gem() {
|
2021-02-02 16:29:31 +03:00
|
|
|
rm -f ./*.gem
|
2021-01-28 17:32:54 +03:00
|
|
|
gem build "$GEM_SPEC"
|
|
|
|
}
|
|
|
|
|
2021-02-02 16:29:31 +03:00
|
|
|
release() {
|
|
|
|
_version="$1"
|
|
|
|
_major=""
|
|
|
|
_minor=""
|
2021-06-24 16:30:05 +03:00
|
|
|
_new_release_branch=false
|
2021-02-02 16:29:31 +03:00
|
|
|
|
|
|
|
IFS='.' read -r -a array <<< "$_version"
|
|
|
|
|
|
|
|
for elem in "${array[@]}"; do
|
|
|
|
if [[ -z $_major ]]; then
|
|
|
|
_major="$elem"
|
|
|
|
elif [[ -z $_minor ]]; then
|
|
|
|
_minor="$elem"
|
|
|
|
else
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
_release_branch="$_major-$_minor-stable"
|
|
|
|
|
2021-06-24 16:30:05 +03:00
|
|
|
if $opt_manual; then
|
2021-02-10 12:53:25 +03:00
|
|
|
echo -e "Bump version to $_version (manual release)\n"
|
|
|
|
bump "$_version"
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2021-02-02 16:29:31 +03:00
|
|
|
if [[ -z $(git branch -v | grep "$_release_branch") ]]; then
|
|
|
|
git checkout -b "$_release_branch"
|
2021-06-24 16:30:05 +03:00
|
|
|
_new_release_branch=true
|
2021-02-02 16:29:31 +03:00
|
|
|
else
|
|
|
|
git checkout "$_release_branch"
|
2021-02-10 12:53:25 +03:00
|
|
|
# cherry-pick the latest commit from master branch to release branch
|
2021-06-24 16:30:05 +03:00
|
|
|
git cherry-pick "$(git rev-parse $DEFAULT_BRANCH)"
|
2021-02-02 16:29:31 +03:00
|
|
|
fi
|
|
|
|
|
2021-02-10 12:53:25 +03:00
|
|
|
echo -e "Bump version to $_version\n"
|
|
|
|
bump "$_version"
|
|
|
|
|
2021-02-02 16:29:31 +03:00
|
|
|
echo -e "Create tag v$_version\n"
|
|
|
|
git tag "v$_version"
|
|
|
|
|
2021-02-10 12:53:25 +03:00
|
|
|
echo -e "Build the gem pakcage for v$_version\n"
|
2021-02-02 16:29:31 +03:00
|
|
|
build_gem
|
|
|
|
|
2021-06-24 16:30:05 +03:00
|
|
|
# head back to working branch
|
2021-07-11 12:19:24 +03:00
|
|
|
git checkout "$_working_branch"
|
2021-06-24 16:30:05 +03:00
|
|
|
|
|
|
|
if [[ $_working_branch == $DEFAULT_BRANCH ]]; then
|
|
|
|
if $_new_release_branch; then
|
2021-07-11 12:19:24 +03:00
|
|
|
git merge "$_release_branch"
|
2021-06-24 16:30:05 +03:00
|
|
|
fi
|
|
|
|
fi
|
2021-02-02 16:29:31 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-02-26 16:16:19 +03:00
|
|
|
help() {
|
|
|
|
echo "Bump new version to Chirpy project"
|
|
|
|
echo "Usage:"
|
|
|
|
echo
|
|
|
|
echo " bash /path/to/bump.sh [options]"
|
|
|
|
echo
|
|
|
|
echo "Options:"
|
|
|
|
echo " -m, --manual Manual relase, bump version only."
|
|
|
|
echo " -h, --help Print this help information."
|
|
|
|
}
|
|
|
|
|
2021-01-25 01:20:51 +03:00
|
|
|
main() {
|
2021-01-28 17:32:54 +03:00
|
|
|
check
|
2021-01-25 01:20:51 +03:00
|
|
|
|
2021-02-02 16:29:31 +03:00
|
|
|
_latest_tag="$(git describe --tags $(git rev-list --tags --max-count=1))"
|
2021-01-25 01:20:51 +03:00
|
|
|
|
|
|
|
echo "Input a version number (hint: latest version is ${_latest_tag:1})"
|
|
|
|
|
|
|
|
read _version
|
|
|
|
|
|
|
|
if [[ $_version =~ ^[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]$ ]]; then
|
|
|
|
|
|
|
|
if git tag --list | egrep -q "^v$_version$"; then
|
|
|
|
echo "Error: version '$_version' already exists"
|
|
|
|
exit -1
|
|
|
|
fi
|
|
|
|
|
2021-02-10 12:53:25 +03:00
|
|
|
release "$_version"
|
2021-01-28 17:32:54 +03:00
|
|
|
|
2021-01-25 01:20:51 +03:00
|
|
|
else
|
2021-02-10 12:53:25 +03:00
|
|
|
|
2021-01-25 01:20:51 +03:00
|
|
|
echo "Error: Illegal version number: '$_version'"
|
|
|
|
fi
|
2021-02-02 16:29:31 +03:00
|
|
|
|
2021-01-25 01:20:51 +03:00
|
|
|
}
|
|
|
|
|
2021-02-04 20:41:35 +03:00
|
|
|
while (($#)); do
|
|
|
|
opt="$1"
|
|
|
|
case $opt in
|
|
|
|
-m | --manual)
|
2021-06-24 16:30:05 +03:00
|
|
|
opt_manual=true
|
2021-02-04 20:41:35 +03:00
|
|
|
shift
|
|
|
|
;;
|
2021-02-26 16:16:19 +03:00
|
|
|
-h | --help)
|
|
|
|
help
|
|
|
|
exit 0
|
|
|
|
;;
|
2021-02-04 20:41:35 +03:00
|
|
|
*)
|
|
|
|
echo "unknown option '$opt'!"
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2021-01-25 01:20:51 +03:00
|
|
|
main
|