#!/usr/bin/env bash # Cut a release from main: check the tree, bump the workspace version, write # the changelog entry, commit, tag, and build the web bundle. See RELEASING.md. # # Usage: scripts/release.sh [--dry-run | --push] # scripts/release.sh --tag-only # # Semver without the tag's `v`, e.g. 0.2.0. The tag is v0.2.0. # --dry-run Run every check and print the plan without changing anything. # --push Push the release commit and tag after asking. Off by default. # --tag-only Annotate an existing commit with an empty tree change and stop; # for tagging a release that shipped before this script existed. set -euo pipefail cd "$(dirname "$0")/.." REPO_ROOT="$PWD" CHANGELOG="$REPO_ROOT/CHANGELOG.md" die() { printf 'release: %s\n' "$*" >&2 exit 1 } usage() { sed -n '2,12p' "$0" | sed 's/^# \{0,1\}//' } step() { printf '\n==> %s\n' "$1" >&2 } is_semver() { [[ "$1" =~ ^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)$ ]] } # True when the first version is greater than the second. semver_gt() { local -a a b IFS=. read -r -a a <<<"$1" IFS=. read -r -a b <<<"$2" if ((a[0] != b[0])); then ((a[0] > b[0])) return fi if ((a[1] != b[1])); then ((a[1] > b[1])) return fi ((a[2] > b[2])) } tag_only() { local tag="$1" commit="$2" sha [[ "$tag" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]] || die "tag must look like vX.Y.Z, not '$tag'" sha="$(git rev-parse --verify --quiet "$commit^{commit}")" || die "'$commit' is not a commit in this checkout" if git rev-parse --verify --quiet "refs/tags/$tag" >/dev/null; then die "tag $tag already exists" fi git tag -a "$tag" -m "protokill $tag" "$sha" printf 'created annotated tag %s on %s\n' "$tag" "$(git rev-parse --short "$sha")" } set_workspace_version() { local version="$1" file file="$(mktemp)" awk -v version="$version" ' /^\[workspace\.package\]/ { in_package = 1 } /^\[/ && !/^\[workspace\.package\]/ { in_package = 0 } in_package && /^version = / { sub(/version = ".*"/, "version = \"" version "\"") } { print } ' Cargo.toml >"$file" mv "$file" Cargo.toml } # Replaces the Unreleased section with a fresh one followed by the new release # section. The entries are the bullets the Unreleased section held, and the # section records the ruleset the release shipped. write_changelog_entry() { local version="$1" date="$2" ruleset entry file ruleset="$( sed -n 's/^pub const RULESET_VERSION: u32 = \([0-9][0-9]*\);$/\1/p' \ crates/game/mission/src/lib.rs | head -n 1 )" entry="$( awk ' /^## Unreleased$/ { in_section = 1; next } in_section && /^## / { exit } in_section && /^- / && $0 != "- Nothing yet." { print } ' "$CHANGELOG" )" [[ -n "$entry" ]] || entry="- Release v$version." if [[ -n "$ruleset" ]]; then entry="$entry"$'\n'"- Ruleset: \`RULESET_VERSION\` $ruleset." fi file="$(mktemp)" ENTRY="$entry" awk -v version="$version" -v date="$date" ' /^## Unreleased$/ { print "## Unreleased" print "" print "- Nothing yet." print "" print "## [v" version "] - " date print "" print ENVIRON["ENTRY"] print "" skip = 1 next } skip { if (/^## /) skip = 0 else next } { print } ' "$CHANGELOG" >"$file" mv "$file" "$CHANGELOG" } case "${1:-}" in --tag-only) [[ $# -eq 3 ]] || { usage exit 2 } tag_only "$2" "$3" exit 0 ;; -h | --help) usage exit 0 ;; esac mode=release push=0 while [[ $# -gt 0 ]]; do case "$1" in --dry-run) mode=dry shift ;; --push) push=1 shift ;; -h | --help) usage exit 0 ;; -*) die "unknown option $1" ;; *) break ;; esac done [[ $# -eq 1 ]] || { usage exit 2 } version="${1#v}" is_semver "$version" || die "version must be semver without the v, e.g. 0.2.0" tag="v$version" step "checks" git rev-parse --git-dir >/dev/null 2>&1 || die "not a git checkout" branch="$(git symbolic-ref --quiet --short HEAD || true)" [[ "$branch" == main ]] || die "releases are cut from main, not ${branch:-a detached HEAD}" git rev-parse --verify --quiet refs/remotes/origin/main >/dev/null || die "origin/main is missing; fetch it first" [[ "$(git rev-parse HEAD)" == "$(git rev-parse refs/remotes/origin/main)" ]] || die "main is not up to date with origin/main; fetch and merge first" [[ -z "$(git status --porcelain)" ]] || die "the working tree has changes; commit or stash them first" if git rev-parse --verify --quiet "refs/tags/$tag" >/dev/null; then die "tag $tag already exists" fi latest_tag="" latest_version="" while IFS= read -r candidate; do candidate_version="${candidate#v}" is_semver "$candidate_version" || continue if [[ -z "$latest_version" ]]; then latest_tag="$candidate" latest_version="$candidate_version" elif semver_gt "$candidate_version" "$latest_version"; then latest_tag="$candidate" latest_version="$candidate_version" fi done < <(git tag --list 'v*') if [[ -n "$latest_tag" ]]; then semver_gt "$version" "$latest_version" || die "$tag must be greater than the latest tag, $latest_tag" fi current_version="$( awk ' /^\[workspace\.package\]/ { in_package = 1; next } /^\[/ { in_package = 0 } in_package && /^version = / { gsub(/"/, "", $3); print $3; exit } ' Cargo.toml )" [[ -n "$current_version" ]] || die "Cargo.toml has no [workspace.package] version" [[ -f "$CHANGELOG" ]] || die "CHANGELOG.md is missing" grep -q '^## Unreleased$' "$CHANGELOG" || die "CHANGELOG.md has no '## Unreleased' section" today="$(date -u +%Y-%m-%d)" printf 'release: plan for %s\n' "$tag" printf ' main: %s (up to date with origin/main)\n' "$(git rev-parse --short HEAD)" printf ' tree: clean\n' printf ' latest tag: %s\n' "${latest_tag:-none}" printf ' Cargo version: %s -> %s\n' "$current_version" "$version" printf ' changelog: move Unreleased entries under "## [%s] - %s"\n' "$tag" "$today" printf ' commit: release: %s\n' "$tag" printf ' tag: annotated %s\n' "$tag" printf ' bundle: scripts/deploy.sh --production -> %s\n' "$REPO_ROOT/output" if [[ "$push" == 1 ]]; then printf ' push: yes, after asking\n' else printf ' push: no (pass --push to push afterwards)\n' fi step "scripts/ci.sh --full" scripts/ci.sh --full if [[ "$mode" == dry ]]; then printf '\nrelease: dry run for %s passed; nothing was changed\n' "$tag" >&2 exit 0 fi step "workspace version $current_version -> $version" set_workspace_version "$version" grep -q "^version = \"$version\"$" Cargo.toml || die "could not set the workspace version in Cargo.toml" cargo update --workspace --offline step "changelog entry for $tag" write_changelog_entry "$version" "$today" step "commit and tag" git add Cargo.toml Cargo.lock CHANGELOG.md git commit -m "release: $tag" git tag -a "$tag" -m "protokill $tag" step "release web bundle" scripts/deploy.sh --production printf 'bundle: %s\n' "$REPO_ROOT/output" if [[ "$push" == 1 ]]; then printf '\nrelease: will push:\n' printf ' git push origin HEAD:main\n' printf ' git push origin refs/tags/%s\n' "$tag" read -r -p "Push the release commit and tag? [y/N] " answer case "$answer" in [yY] | [yY][eE][sS]) ;; *) die "not pushed" ;; esac git push origin HEAD:main git push origin "refs/tags/$tag" printf 'pushed %s and %s\n' "$(git rev-parse --short HEAD)" "$tag" else printf '\nrelease: %s is committed and tagged locally; push with:\n' "$tag" printf ' git push origin HEAD:main\n' printf ' git push origin refs/tags/%s\n' "$tag" fi