|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# Usage: ./scripts/release.sh <version> |
| 5 | +# |
| 6 | +# <version> is a Maven-style version without the leading 'v', e.g. 0.2.0 |
| 7 | +# or 0.2.0-beta.1. The git tag will be prefixed with 'v' (vX.Y.Z). |
| 8 | +# |
| 9 | +# This script bumps the jmh-fork version everywhere it's pinned, |
| 10 | +# regenerates CHANGELOG.md from conventional commits (skipped for |
| 11 | +# alpha/beta/rc), commits, tags, and prints the push command. |
| 12 | +# It does NOT push. |
| 13 | +# |
| 14 | +# Based on this: https://github.com/CodSpeedHQ/codspeed-cpp/blob/main/scripts/release.sh |
| 15 | + |
| 16 | +# First and only argument is the version number |
| 17 | +VERSION_NO_V=${1} # The version number without the 'v' prefix |
| 18 | +VERSION=v$1 # The version number, prefixed with 'v' |
| 19 | + |
| 20 | +# Validate version format (X.Y.Z or X.Y.Z-alpha where X, Y, Z are integers) |
| 21 | +if [[ ! "$VERSION_NO_V" =~ ^[0-9]+\.[0-9]+\.[0-9]+(-alpha)?$ ]]; then |
| 22 | + echo "Error: Invalid version format '$VERSION_NO_V'" |
| 23 | + echo "Usage: $0 <version>" |
| 24 | + echo " Version must be in format X.Y.Z or X.Y.Z-alpha (e.g., 1.2.3 or 1.2.3-alpha)" |
| 25 | + exit 1 |
| 26 | +fi |
| 27 | + |
| 28 | +# Check is on main (unless releasing an alpha version) |
| 29 | +if [[ ! "$VERSION_NO_V" =~ -alpha ]]; then |
| 30 | + if [ "$(git rev-parse --abbrev-ref HEAD)" != "main" ]; then |
| 31 | + echo "Not on main branch (only alpha releases can be made from non-main branches)" |
| 32 | + exit 1 |
| 33 | + fi |
| 34 | +fi |
| 35 | + |
| 36 | +# Check that GITHUB_TOKEN is set |
| 37 | +if [ -z "${GITHUB_TOKEN:-}" ]; then |
| 38 | + echo "GITHUB_TOKEN is not set. Trying to fetch it from gh" |
| 39 | + GITHUB_TOKEN=$(gh auth token) |
| 40 | +fi |
| 41 | + |
| 42 | +# Check that the tag doesn't already exist |
| 43 | +if git rev-parse "$VERSION" >/dev/null 2>&1; then |
| 44 | + echo "error: tag $VERSION already exists" >&2 |
| 45 | + exit 1 |
| 46 | +fi |
| 47 | + |
| 48 | + |
| 49 | +## Bump all the versions in the repo |
| 50 | +## |
| 51 | + |
| 52 | +# List of files to update with version numbers. |
| 53 | +VERSION_FILES=( |
| 54 | + "jmh-fork/build.gradle.kts" |
| 55 | + "examples/example-maven/pom.xml" |
| 56 | + "examples/example-gradle/build.gradle.kts" |
| 57 | +) |
| 58 | + |
| 59 | +# Get current version from jmh-fork/build.gradle.kts |
| 60 | +PREVIOUS_VERSION=$(awk -F'"' '/version = / {print $2; exit}' jmh-fork/build.gradle.kts) |
| 61 | + |
| 62 | +# Prompt the release version |
| 63 | +echo "Previous version: ${PREVIOUS_VERSION}" |
| 64 | +echo "New version: ${VERSION_NO_V}" |
| 65 | +read -p "Are you sure you want to release this version? (y/n): " confirm |
| 66 | +if [ "$confirm" != "y" ]; then |
| 67 | + echo "Aborting release" |
| 68 | + exit 1 |
| 69 | +fi |
| 70 | + |
| 71 | +# Update version in all relevant files |
| 72 | +echo "Updating version numbers in source files..." |
| 73 | + |
| 74 | +# Use sed in a cross-platform way (macOS requires empty string after -i) |
| 75 | +sed_inplace() { |
| 76 | + if [[ "$OSTYPE" == "darwin"* ]]; then |
| 77 | + sed -i '' "$@" |
| 78 | + else |
| 79 | + sed -i "$@" |
| 80 | + fi |
| 81 | +} |
| 82 | + |
| 83 | +for file in "${VERSION_FILES[@]}"; do |
| 84 | + sed_inplace "s/${PREVIOUS_VERSION}/${VERSION_NO_V}/g" "$file" |
| 85 | + echo " Updated $file" |
| 86 | +done |
| 87 | + |
| 88 | +# Bump the jmh-fork poms (parent + all child modules) in one shot |
| 89 | +(cd jmh-fork && mvn versions:set -DnewVersion="$VERSION_NO_V" -DgenerateBackupPoms=false -q) |
| 90 | +echo " Updated jmh-fork/**/pom.xml (via mvn versions:set)" |
| 91 | + |
| 92 | +# Commit version changes |
| 93 | +git add \ |
| 94 | + jmh-fork \ |
| 95 | + examples/example-maven/pom.xml \ |
| 96 | + examples/example-gradle/build.gradle.kts |
| 97 | +git cliff -o CHANGELOG.md --tag "$VERSION" --github-token "$GITHUB_TOKEN" |
| 98 | +git add CHANGELOG.md |
| 99 | +git commit -m "chore: Release $VERSION" |
| 100 | +git tag -s "$VERSION" -m "Release $VERSION" |
| 101 | +git push origin HEAD |
| 102 | +git push origin "$VERSION" |
| 103 | + |
| 104 | +# Create GitHub release |
| 105 | +if [[ "$VERSION_NO_V" =~ -alpha ]]; then |
| 106 | + gh release create "$VERSION" -t "$VERSION" --generate-notes --latest=false --draft |
| 107 | +else |
| 108 | + gh release create "$VERSION" -t "$VERSION" --generate-notes --latest --draft |
| 109 | +fi |
0 commit comments