|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Prepare + tag a new SLMCode release: bumps the version everywhere, appends a |
| 3 | +# changelog entry from the commits since the last tag, runs lint + tests, then |
| 4 | +# commits and tags. Push is left to the caller so you can review first. |
| 5 | +# |
| 6 | +# The GitHub release workflow (.github/workflows/release.yml) then builds the |
| 7 | +# binaries, auto-syncs the Homebrew formula checksums and publishes the release. |
| 8 | +# |
| 9 | +# Usage: |
| 10 | +# scripts/prepare-release.sh <x.y.z> [--dry-run] |
| 11 | +# scripts/prepare-release.sh 0.14.0 --dry-run # show changes, commit nothing |
| 12 | +set -euo pipefail |
| 13 | + |
| 14 | +VERSION="${1:-}" |
| 15 | +DRY=0 |
| 16 | +[[ "${2:-}" == "--dry-run" ]] && DRY=1 |
| 17 | + |
| 18 | +if [[ ! "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then |
| 19 | + echo "usage: prepare-release.sh <x.y.z> [--dry-run]" >&2 |
| 20 | + exit 1 |
| 21 | +fi |
| 22 | +if git tag -l "v${VERSION}" | grep -q .; then |
| 23 | + echo "error: tag v${VERSION} already exists" >&2 |
| 24 | + exit 1 |
| 25 | +fi |
| 26 | +for f in cmd/slmcode/version.go Makefile README.md docs/changelog.md; do |
| 27 | + if ! git diff --quiet -- "$f"; then |
| 28 | + echo "error: $f has uncommitted changes — commit or stash first" >&2 |
| 29 | + exit 1 |
| 30 | + fi |
| 31 | +done |
| 32 | + |
| 33 | +PREV="$(git tag --sort=-v:refname | grep -E '^v[0-9]+\.[0-9]+\.[0-9]+$' | head -1 || true)" |
| 34 | +PREV="${PREV:-v0.0.0}" |
| 35 | +echo "Releasing v${VERSION} (previous: ${PREV})" |
| 36 | + |
| 37 | +run() { |
| 38 | + if [[ "$DRY" == 1 ]]; then |
| 39 | + echo " [dry-run] $*" |
| 40 | + else |
| 41 | + "$@" |
| 42 | + fi |
| 43 | +} |
| 44 | + |
| 45 | +# 1. cmd/slmcode/version.go |
| 46 | +perl -0pi -e 's/Version = "[^"]*"/Version = "'"$VERSION"'"/' cmd/slmcode/version.go |
| 47 | + |
| 48 | +# 2. Makefile VERSION |
| 49 | +perl -0pi -e 's/^VERSION \?= .*/VERSION ?= '"$VERSION"'/m' Makefile |
| 50 | + |
| 51 | +# 3. README release badge |
| 52 | +perl -0pi -e 's/label=v[0-9]+\.[0-9]+\.[0-9]+/label=v'"$VERSION"'/' README.md |
| 53 | + |
| 54 | +# 4. Changelog entry from conventional commits since the previous tag. |
| 55 | +{ |
| 56 | + echo "## v${VERSION} — $(date -u +%Y-%m-%d)" |
| 57 | + echo "" |
| 58 | + git --no-pager log --oneline --no-merges "${PREV}..HEAD" | awk '{ |
| 59 | + $1 = "" |
| 60 | + sub(/^ /, "") |
| 61 | + sub(/^(feat|fix|chore|docs|refactor|test|ci|build|perf)(\([^)]*\))?: /, "") |
| 62 | + print "- " toupper(substr($0, 1, 1)) substr($0, 2) |
| 63 | + }' |
| 64 | +} > /tmp/slmcode-changelog-entry.txt |
| 65 | + |
| 66 | +perl -0pi -e 'my $entry = do { local $/; open my $fh, "<", "/tmp/slmcode-changelog-entry.txt" or die $!; <$fh> }; |
| 67 | + s/(# Changelog\n\n)/$1$entry/' docs/changelog.md |
| 68 | + |
| 69 | +changed="$(git diff --name-only -- cmd/slmcode/version.go Makefile README.md docs/changelog.md)" |
| 70 | +if [[ -z "$changed" ]]; then |
| 71 | + echo "error: no changes produced — nothing to release" >&2 |
| 72 | + exit 1 |
| 73 | +fi |
| 74 | + |
| 75 | +echo "Changed files:" |
| 76 | +git --no-pager diff --stat -- cmd/slmcode/version.go Makefile README.md docs/changelog.md |
| 77 | + |
| 78 | +run make lint |
| 79 | +run make test |
| 80 | + |
| 81 | +run git add cmd/slmcode/version.go Makefile README.md docs/changelog.md |
| 82 | +run git commit -m "chore: release v${VERSION}" |
| 83 | +run git tag "v${VERSION}" |
| 84 | + |
| 85 | +if [[ "$DRY" == 1 ]]; then |
| 86 | + git checkout -- cmd/slmcode/version.go Makefile README.md docs/changelog.md |
| 87 | + echo " [dry-run] working tree restored — nothing committed" |
| 88 | +fi |
| 89 | + |
| 90 | +echo "" |
| 91 | +echo "Next steps:" |
| 92 | +echo " git push origin main && git push origin v${VERSION}" |
| 93 | +echo "The release workflow builds binaries, syncs the Homebrew formula checksums" |
| 94 | +echo "and publishes the GitHub release automatically." |
0 commit comments