#!/usr/bin/env bash # # Requires: Git, NPM and RubyGems set -eu opt_pre=false # option for bump gem version opt_pkg=false # option for building gem package MAIN_BRANCH="master" RELEASE_BRANCH="production" GEM_SPEC="jekyll-theme-chirpy.gemspec" NODE_SPEC="package.json" CHANGELOG="docs/CHANGELOG.md" CONFIG="_config.yml" CSS_DIST="_sass/dist" JS_DIST="assets/js/dist" FILES=( "$GEM_SPEC" "$NODE_SPEC" "$CHANGELOG" "$CONFIG" ) TOOLS=( "git" "npm" "gem" ) help() { 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" echo echo "Usage:" echo " bash ./tools/release [options]" echo echo "Options:" 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" } _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 } _check_git() { $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 ) $opt_pkg || ( if [[ "$(git branch --show-current)" != "$RELEASE_BRANCH" ]]; then echo "> Abort: Please run the tool in the '$RELEASE_BRANCH' branch." exit 1 fi ) } _check_src() { for i in "${!FILES[@]}"; do _src="${FILES[$i]}" if [[ ! -f $_src && ! -d $_src ]]; then echo -e "> Error: Missing file \"$_src\"!\n" exit 1 fi done } init() { _check_cli _check_git _check_src echo -e "> npm install\n" npm i } ## 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" } _improve_changelog() { # Replace multiple empty lines with a single empty line sed -i '/^$/N;/^\n$/D' "$CHANGELOG" # Escape left angle brackets of HTML tag in the changelog as they break the markdown structure. e.g., '