# shellcheck shell=bash # Shared plumbing for the release scripts that run in WSL. Source it; do not # run it. # # Sourcing runs in the caller's shell, so everything defined here ($root, # SETTINGS, setting, require_setting, die, step, run_or_die) is the caller's # afterwards. The Windows half (steps 2 and 3) reads the same release.env with # _common.ps1, which parses the same KEY=VALUE lines. The caller sets # `set -euo pipefail` first. #: The directory of these scripts and the repository root above it, worked out #: from this file rather than from the working directory, so the scripts can be #: run from anywhere. here=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) # shellcheck disable=SC2034 # read by the scripts that source this file root=$(dirname "$here") die() { printf 'error: %s\n' "$*" >&2; exit 1; } step() { printf '\n== %s\n' "$*"; } # Runs a command and, when it fails, stops with the step's name and its exit # code: `run_or_die 'git archive' git archive ...`. run_or_die() { local what=$1 rc=0 shift "$@" || rc=$? ((rc == 0)) || die "$what failed (exit $rc)" } # --------------------------------------------------------------- release.env # Read, not sourced: sourcing would run whatever the file says and would break # on a space in a value. A line is blank, a comment, or KEY=VALUE; anything else # is a typo and is reported here rather than as an empty variable later. Keys # and values are trimmed and surrounding double quotes are dropped. A CRLF file # (saved from a Windows editor) reads the same as an LF one. trim() { local s=$1 s=${s#"${s%%[![:space:]]*}"} s=${s%"${s##*[![:space:]]}"} printf '%s' "$s" } declare -A SETTINGS=() env_file=$here/release.env [[ -f $env_file ]] || die "no deploy/release.env - copy deploy/release.env.example and fill it in" while IFS= read -r line || [[ -n $line ]]; do line=${line%$'\r'} [[ $line =~ ^[[:space:]]*(#|$) ]] && continue [[ $line == *=* ]] || die "release.env: not a KEY=VALUE line: $line" key=$(trim "${line%%=*}") value=$(trim "${line#*=}") while [[ $value == \"* ]]; do value=${value#\"}; done while [[ $value == *\" ]]; do value=${value%\"}; done SETTINGS[$key]=$value done < "$env_file" unset line key value # setting NAME [DEFAULT]: the value, or the default when unset or empty. setting() { local value=${SETTINGS[$1]-} if [[ -n $value ]]; then printf '%s' "$value"; else printf '%s' "${2-}"; fi } # require_setting NAME: the value, or a stop naming the missing setting. require_setting() { local value value=$(setting "$1") [[ -n $value ]] || die "set $1 in deploy/release.env" printf '%s' "$value" } # ------------------------------------------------------------- shared checks need_git() { command -v git >/dev/null 2>&1 || die "git is not on PATH" } # The commit a branch or ref points at, or a stop saying which one did not # resolve. resolve_commit() { local commit commit=$(git rev-parse --verify "$1^{commit}") || die "resolve $2 commit failed" printf '%s' "$commit" }