2021-09-28 18:17:39 +03:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
#
|
2022-03-13 11:53:13 +03:00
|
|
|
# Requires: Git, NPM and RubyGems
|
2021-09-28 18:17:39 +03:00
|
|
|
|
|
|
|
set -eu
|
|
|
|
|
2024-04-14 00:15:27 +03:00
|
|
|
opt_pre=false # option for bump gem version
|
|
|
|
opt_pkg=false # option for building gem package
|
2022-03-01 18:24:51 +03:00
|
|
|
|
2024-04-14 00:15:27 +03:00
|
|
|
MAIN_BRANCH="master"
|
|
|
|
RELEASE_BRANCH="production"
|
2022-03-01 18:24:51 +03:00
|
|
|
|
2021-09-28 18:17:39 +03:00
|
|
|
GEM_SPEC="jekyll-theme-chirpy.gemspec"
|
2024-04-14 00:15:27 +03:00
|
|
|
NODE_SPEC="package.json"
|
|
|
|
CHANGELOG="docs/CHANGELOG.md"
|
|
|
|
CONFIG="_config.yml"
|
2022-03-01 18:24:51 +03:00
|
|
|
|
2024-05-11 06:07:48 +03:00
|
|
|
CSS_DIST="_sass/dist"
|
2023-03-17 20:33:44 +03:00
|
|
|
JS_DIST="assets/js/dist"
|
2024-06-05 18:51:10 +03:00
|
|
|
PWA_DIST="_app"
|
2023-03-17 20:33:44 +03:00
|
|
|
|
2022-03-01 18:24:51 +03:00
|
|
|
FILES=(
|
|
|
|
"$GEM_SPEC"
|
2024-04-14 00:15:27 +03:00
|
|
|
"$NODE_SPEC"
|
|
|
|
"$CHANGELOG"
|
|
|
|
"$CONFIG"
|
2022-03-01 18:24:51 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
TOOLS=(
|
|
|
|
"git"
|
|
|
|
"npm"
|
|
|
|
"gem"
|
|
|
|
)
|
2022-02-14 18:59:17 +03:00
|
|
|
|
|
|
|
help() {
|
2024-04-14 00:15:27 +03:00
|
|
|
echo -e "A tool to release new version Chirpy gem.\nThis tool will:"
|
|
|
|
echo " 1. Build a new gem and publish it to RubyGems.org"
|
|
|
|
echo " 2. Merge the release branch into the default branch"
|
2022-02-14 18:59:17 +03:00
|
|
|
echo
|
|
|
|
echo "Usage:"
|
2024-05-31 23:11:01 +03:00
|
|
|
echo " bash $0 [options]"
|
2022-02-14 18:59:17 +03:00
|
|
|
echo
|
|
|
|
echo "Options:"
|
2024-04-14 00:15:27 +03:00
|
|
|
echo " --prepare Preparation for release"
|
|
|
|
echo " -p, --package Build a gem package only, for local packaging in case of auto-publishing failure"
|
|
|
|
echo " -h, --help Display this help message"
|
2022-02-14 18:59:17 +03:00
|
|
|
}
|
|
|
|
|
2023-09-10 12:25:41 +03:00
|
|
|
_check_cli() {
|
|
|
|
for i in "${!TOOLS[@]}"; do
|
|
|
|
cli="${TOOLS[$i]}"
|
|
|
|
if ! command -v "$cli" &>/dev/null; then
|
|
|
|
echo "> Command '$cli' not found!"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2022-03-01 18:24:51 +03:00
|
|
|
_check_git() {
|
2024-05-11 09:58:23 +03:00
|
|
|
$opt_pre || (
|
|
|
|
# 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
|
|
|
|
)
|
2021-09-28 18:17:39 +03:00
|
|
|
|
2024-04-14 00:15:27 +03:00
|
|
|
$opt_pkg || (
|
|
|
|
if [[ "$(git branch --show-current)" != "$RELEASE_BRANCH" ]]; then
|
|
|
|
echo "> Abort: Please run the tool in the '$RELEASE_BRANCH' branch."
|
2024-01-27 21:22:33 +03:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
)
|
2022-03-01 18:24:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
_check_src() {
|
2023-03-13 20:25:42 +03:00
|
|
|
for i in "${!FILES[@]}"; do
|
|
|
|
_src="${FILES[$i]}"
|
|
|
|
if [[ ! -f $_src && ! -d $_src ]]; then
|
2023-09-10 12:25:41 +03:00
|
|
|
echo -e "> Error: Missing file \"$_src\"!\n"
|
2023-03-13 20:25:42 +03:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
done
|
2022-03-01 18:24:51 +03:00
|
|
|
}
|
|
|
|
|
2024-05-11 06:07:48 +03:00
|
|
|
init() {
|
2023-09-10 12:25:41 +03:00
|
|
|
_check_cli
|
2022-03-01 18:24:51 +03:00
|
|
|
_check_git
|
2023-03-13 20:25:42 +03:00
|
|
|
_check_src
|
2024-05-11 06:07:48 +03:00
|
|
|
echo -e "> npm install\n"
|
|
|
|
npm i
|
2022-03-01 18:24:51 +03:00
|
|
|
}
|
|
|
|
|
2024-04-14 00:15:27 +03:00
|
|
|
## Bump new version to gem-spec file
|
|
|
|
_bump_version() {
|
|
|
|
_version="$(grep '"version":' "$NODE_SPEC" | sed 's/.*: "//;s/".*//')"
|
|
|
|
sed -i "s/[[:digit:]]\+\.[[:digit:]]\+\.[[:digit:]]\+/$_version/" "$GEM_SPEC"
|
|
|
|
echo "> Bump gem version to $_version"
|
|
|
|
}
|
2023-11-06 22:38:02 +03:00
|
|
|
|
2024-04-14 00:15:27 +03:00
|
|
|
_improve_changelog() {
|
2023-09-10 12:25:41 +03:00
|
|
|
# Replace multiple empty lines with a single empty line
|
2024-04-14 00:15:27 +03:00
|
|
|
sed -i '/^$/N;/^\n$/D' "$CHANGELOG"
|
|
|
|
# Escape left angle brackets of HTML tag in the changelog as they break the markdown structure. e.g., '<hr>'
|
|
|
|
sed -i -E 's/\s(<[a-z])/ \\\1/g' "$CHANGELOG"
|
2023-09-10 12:25:41 +03:00
|
|
|
}
|
|
|
|
|
2024-04-14 00:15:27 +03:00
|
|
|
prepare() {
|
|
|
|
_bump_version
|
|
|
|
_improve_changelog
|
2021-09-28 18:17:39 +03:00
|
|
|
}
|
|
|
|
|
2024-02-29 22:11:17 +03:00
|
|
|
## Build a Gem package
|
2022-03-01 18:24:51 +03:00
|
|
|
build_gem() {
|
2023-09-10 07:39:18 +03:00
|
|
|
# Remove unnecessary theme settings
|
2024-05-11 06:07:48 +03:00
|
|
|
sed -i -E "s/(^timezone:).*/\1/;s/(^cdn:).*/\1/;s/(^avatar:).*/\1/" $CONFIG
|
2021-09-28 18:17:39 +03:00
|
|
|
rm -f ./*.gem
|
2023-09-10 07:39:18 +03:00
|
|
|
|
|
|
|
npm run build
|
2024-05-11 06:07:48 +03:00
|
|
|
# add CSS/JS distribution files to gem package
|
2024-06-05 18:51:10 +03:00
|
|
|
git add "$CSS_DIST" "$JS_DIST" "$PWA_DIST" -f
|
2024-05-11 06:07:48 +03:00
|
|
|
|
|
|
|
echo -e "\n> gem build $GEM_SPEC\n"
|
2021-09-28 18:17:39 +03:00
|
|
|
gem build "$GEM_SPEC"
|
2023-09-10 07:39:18 +03:00
|
|
|
|
2024-05-11 06:07:48 +03:00
|
|
|
echo -e "\n> Resume file changes ...\n"
|
2023-09-10 07:39:18 +03:00
|
|
|
git reset
|
|
|
|
git checkout .
|
2022-03-01 18:24:51 +03:00
|
|
|
}
|
|
|
|
|
2024-04-14 00:15:27 +03:00
|
|
|
# Push the gem to RubyGems.org (using $GEM_HOST_API_KEY)
|
|
|
|
push_gem() {
|
|
|
|
gem push ./*.gem
|
|
|
|
}
|
2023-09-10 12:25:41 +03:00
|
|
|
|
2024-04-14 00:15:27 +03:00
|
|
|
## Merge the release branch into the default branch
|
|
|
|
merge() {
|
|
|
|
git fetch origin "$MAIN_BRANCH"
|
|
|
|
git checkout -b "$MAIN_BRANCH" origin/"$MAIN_BRANCH"
|
2022-03-01 18:24:51 +03:00
|
|
|
|
2024-04-14 00:15:27 +03:00
|
|
|
git merge --no-ff --no-edit "$RELEASE_BRANCH" || (
|
|
|
|
git merge --abort
|
|
|
|
echo -e "\n> Conflict detected. Aborting merge.\n"
|
|
|
|
exit 0
|
|
|
|
)
|
2022-11-22 16:08:37 +03:00
|
|
|
|
2024-04-14 00:15:27 +03:00
|
|
|
git push origin "$MAIN_BRANCH"
|
|
|
|
}
|
2022-03-01 18:24:51 +03:00
|
|
|
|
2024-04-14 00:15:27 +03:00
|
|
|
main() {
|
2024-05-11 06:07:48 +03:00
|
|
|
init
|
2022-03-01 18:24:51 +03:00
|
|
|
|
2024-04-14 00:15:27 +03:00
|
|
|
if $opt_pre; then
|
|
|
|
prepare
|
|
|
|
exit 0
|
2022-06-09 10:01:27 +03:00
|
|
|
fi
|
2023-03-17 20:33:44 +03:00
|
|
|
|
2023-09-10 12:25:41 +03:00
|
|
|
build_gem
|
2024-04-14 00:15:27 +03:00
|
|
|
$opt_pkg && exit 0
|
|
|
|
push_gem
|
|
|
|
merge
|
2021-09-28 18:17:39 +03:00
|
|
|
}
|
|
|
|
|
2022-02-14 18:59:17 +03:00
|
|
|
while (($#)); do
|
|
|
|
opt="$1"
|
|
|
|
case $opt in
|
2024-04-14 00:15:27 +03:00
|
|
|
--prepare)
|
2022-02-14 18:59:17 +03:00
|
|
|
opt_pre=true
|
|
|
|
shift
|
|
|
|
;;
|
2024-04-14 00:15:27 +03:00
|
|
|
-p | --package)
|
|
|
|
opt_pkg=true
|
|
|
|
shift
|
|
|
|
;;
|
2022-02-14 18:59:17 +03:00
|
|
|
-h | --help)
|
|
|
|
help
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
# unknown option
|
|
|
|
help
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2021-09-28 18:17:39 +03:00
|
|
|
main
|