|
| 1 | +#!/bin/zsh |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# --------------------------------------------------------- |
| 5 | +# Config |
| 6 | +# --------------------------------------------------------- |
| 7 | +DIST_DIR="dist" |
| 8 | + |
| 9 | +STEP=${1:?usage: release.sh [major|minor|patch|X.Y.Z]} |
| 10 | + |
| 11 | +# --------------------------------------------------------- |
| 12 | +# Pre-flight checks |
| 13 | +# --------------------------------------------------------- |
| 14 | +if ! command -v task >/dev/null 2>&1; then |
| 15 | + echo "Error: 'task' command not found. Install Taskfile runner first." |
| 16 | + exit 1 |
| 17 | +fi |
| 18 | + |
| 19 | +if ! command -v gh >/dev/null 2>&1; then |
| 20 | + echo "Error: 'gh' CLI not found. Install GitHub CLI (gh) first." |
| 21 | + exit 1 |
| 22 | +fi |
| 23 | + |
| 24 | +BRANCH=$(git rev-parse --abbrev-ref HEAD) |
| 25 | +if [ "$BRANCH" != "main" ]; then |
| 26 | + echo "Error: releases may only be created from 'main' (current: $BRANCH)." |
| 27 | + exit 1 |
| 28 | +fi |
| 29 | + |
| 30 | +# Ensure working tree is clean |
| 31 | +if ! git diff --quiet || ! git diff --cached --quiet; then |
| 32 | + echo "Error: working tree is not clean. Commit or stash changes before releasing." |
| 33 | + exit 1 |
| 34 | +fi |
| 35 | + |
| 36 | +# --------------------------------------------------------- |
| 37 | +# Read current version from Cargo.toml |
| 38 | +# --------------------------------------------------------- |
| 39 | +CURRENT_VERSION=$( |
| 40 | + grep -E '^version = "[0-9]+\.[0-9]+\.[0-9]+"' Cargo.toml \ |
| 41 | + | head -n1 \ |
| 42 | + | sed -E 's/version = "([^"]+)"/\1/' |
| 43 | +) |
| 44 | + |
| 45 | +if [ -z "$CURRENT_VERSION" ]; then |
| 46 | + echo "Error: could not determine current version from Cargo.toml" |
| 47 | + exit 1 |
| 48 | +fi |
| 49 | + |
| 50 | +MAJOR=$(echo "$CURRENT_VERSION" | cut -d. -f1) |
| 51 | +MINOR=$(echo "$CURRENT_VERSION" | cut -d. -f2) |
| 52 | +PATCH=$(echo "$CURRENT_VERSION" | cut -d. -f3) |
| 53 | +LATEST=true |
| 54 | + |
| 55 | +# --------------------------------------------------------- |
| 56 | +# Compute new version |
| 57 | +# --------------------------------------------------------- |
| 58 | +case "$STEP" in |
| 59 | + major) |
| 60 | + MAJOR=$((MAJOR+1)) |
| 61 | + MINOR=0 |
| 62 | + PATCH=0 |
| 63 | + ;; |
| 64 | + minor) |
| 65 | + MINOR=$((MINOR+1)) |
| 66 | + PATCH=0 |
| 67 | + ;; |
| 68 | + patch) |
| 69 | + PATCH=$((PATCH+1)) |
| 70 | + ;; |
| 71 | + *) |
| 72 | + if [[ "$STEP" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then |
| 73 | + MAJOR=$(echo "$STEP" | cut -d. -f1) |
| 74 | + MINOR=$(echo "$STEP" | cut -d. -f2) |
| 75 | + PATCH=$(echo "$STEP" | cut -d. -f3) |
| 76 | + LATEST=false |
| 77 | + else |
| 78 | + echo "Invalid step: $STEP" |
| 79 | + echo "Usage: release.sh [major|minor|patch|X.Y.Z]" |
| 80 | + exit 1 |
| 81 | + fi |
| 82 | + ;; |
| 83 | +esac |
| 84 | + |
| 85 | +NEW_VERSION="$MAJOR.$MINOR.$PATCH" |
| 86 | + |
| 87 | +# Check that tag does not already exist |
| 88 | +if git rev-parse -q --verify "refs/tags/$NEW_VERSION" >/dev/null 2>&1; then |
| 89 | + echo "Error: git tag $NEW_VERSION already exists." |
| 90 | + exit 1 |
| 91 | +fi |
| 92 | + |
| 93 | +echo "Current version: $CURRENT_VERSION" |
| 94 | +echo "New version: $NEW_VERSION" |
| 95 | +echo "Branch: $BRANCH" |
| 96 | +echo |
| 97 | + |
| 98 | +read -r -p "Proceed with release? (y/N): " CONFIRM |
| 99 | +if [[ ! "$CONFIRM" =~ ^[Yy]$ ]]; then |
| 100 | + echo "Aborted." |
| 101 | + exit 1 |
| 102 | +fi |
| 103 | + |
| 104 | +# --------------------------------------------------------- |
| 105 | +# Bump Cargo.toml with rollback on failure |
| 106 | +# --------------------------------------------------------- |
| 107 | +CARGO_BAK="Cargo.toml.bak.$$" |
| 108 | +cp Cargo.toml "$CARGO_BAK" |
| 109 | + |
| 110 | +cleanup_on_error() { |
| 111 | + rc=$? |
| 112 | + echo "Error during release (exit code $rc). Restoring Cargo.toml..." |
| 113 | + if [ -f "$CARGO_BAK" ]; then |
| 114 | + mv "$CARGO_BAK" Cargo.toml |
| 115 | + fi |
| 116 | + exit $rc |
| 117 | +} |
| 118 | +trap cleanup_on_error INT TERM ERR |
| 119 | + |
| 120 | +echo "Updating Cargo.toml to version $NEW_VERSION..." |
| 121 | +perl -pi -e 's/^version = "[0-9]+\.[0-9]+\.[0-9]+"/version = "'"$NEW_VERSION"'"/' Cargo.toml |
| 122 | + |
| 123 | +# --------------------------------------------------------- |
| 124 | +# Run tasks: setup, test, build |
| 125 | +# --------------------------------------------------------- |
| 126 | +echo "Running task setup..." |
| 127 | +task setup |
| 128 | + |
| 129 | +echo "Running task test..." |
| 130 | +task test |
| 131 | + |
| 132 | +echo "Running task build:release:all..." |
| 133 | +task "build:release:${NEW_VERSION}" |
| 134 | + |
| 135 | +# If we got here, tasks succeeded – stop rollback trap |
| 136 | +trap - INT TERM ERR |
| 137 | +rm -f "$CARGO_BAK" |
| 138 | + |
| 139 | +# --------------------------------------------------------- |
| 140 | +# Commit, tag, push |
| 141 | +# --------------------------------------------------------- |
| 142 | +if git diff --quiet -- Cargo.toml; then |
| 143 | + echo "Warning: Cargo.toml did not change; nothing to commit." |
| 144 | +else |
| 145 | + git commit Cargo.toml -m "$NEW_VERSION" |
| 146 | +fi |
| 147 | + |
| 148 | +HASH=$(git rev-parse HEAD) |
| 149 | + |
| 150 | +echo "Tagging $NEW_VERSION..." |
| 151 | +git tag "$NEW_VERSION" "$HASH" |
| 152 | + |
| 153 | +echo "Pushing main and tag..." |
| 154 | +git push origin main |
| 155 | +git push origin "refs/tags/$NEW_VERSION" |
| 156 | + |
| 157 | +# --------------------------------------------------------- |
| 158 | +# Create GitHub release from artifacts in DIST_DIR |
| 159 | +# --------------------------------------------------------- |
| 160 | +if [ ! -d "$DIST_DIR" ]; then |
| 161 | + echo "Error: build artifacts directory '$DIST_DIR' not found." |
| 162 | + echo "Check task build:release:all" |
| 163 | + exit 1 |
| 164 | +fi |
| 165 | + |
| 166 | +ASSETS=("$DIST_DIR"/*) |
| 167 | +if [ ${#ASSETS[@]} -eq 0 ]; then |
| 168 | + echo "Error: no build artifacts found in '$DIST_DIR'." |
| 169 | + exit 1 |
| 170 | +fi |
| 171 | + |
| 172 | +LATEST_FLAG="" |
| 173 | +if [ "$LATEST" = true ]; then |
| 174 | + LATEST_FLAG="--latest" |
| 175 | +fi |
| 176 | + |
| 177 | +echo "Creating GitHub release $NEW_VERSION with assets:" |
| 178 | +for a in "${ASSETS[@]}"; do |
| 179 | + echo " - $a" |
| 180 | +done |
| 181 | + |
| 182 | +gh release create "$NEW_VERSION" \ |
| 183 | + --fail-on-no-commits \ |
| 184 | + --generate-notes \ |
| 185 | + $LATEST_FLAG \ |
| 186 | + --target "$HASH" \ |
| 187 | + "${ASSETS[@]}" |
| 188 | + |
| 189 | +echo "Release $NEW_VERSION created successfully." |
| 190 | +exit 0 |
0 commit comments