|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +# Function to list the 5 most recent remote tags by date |
| 6 | +list_recent_remote_tags() { |
| 7 | + echo "Fetching the 5 most recent remote tags by date:" |
| 8 | + git ls-remote --tags origin | \ |
| 9 | + grep -E 'refs/tags/v[0-9]+\.[0-9]+\.[0-9]+(-[a-z]+\.[0-9]+)?$' | \ |
| 10 | + while read -r hash ref; do |
| 11 | + tag="${ref#refs/tags/}" |
| 12 | + date=$(git log -1 --format='%ci' "$hash" 2>/dev/null || echo "unknown date") |
| 13 | + echo "$tag $date" |
| 14 | + done | sort -k2 -r | head -n 5 |
| 15 | +} |
| 16 | + |
| 17 | +# Function to get the next version in sequence |
| 18 | +guess_next_version() { |
| 19 | + # Get the most recent tag |
| 20 | + local latest_tag |
| 21 | + latest_tag=$(git ls-remote --tags origin | \ |
| 22 | + grep -Eo 'v[0-9]+\.[0-9]+\.[0-9]+(-[a-z]+\.[0-9]+)?$' | \ |
| 23 | + sort -V | tail -n 1) |
| 24 | + |
| 25 | + # Strip 'v' from the tag |
| 26 | + latest_version="${latest_tag#v}" |
| 27 | + |
| 28 | + if [[ "$latest_version" =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)(-(alpha|beta)\.([0-9]+))?$ ]]; then |
| 29 | + major="${BASH_REMATCH[1]}" |
| 30 | + minor="${BASH_REMATCH[2]}" |
| 31 | + patch="${BASH_REMATCH[3]}" |
| 32 | + channel="${BASH_REMATCH[5]:-}" |
| 33 | + channel_num="${BASH_REMATCH[6]:-}" |
| 34 | + |
| 35 | + if [[ -n "$channel" ]]; then |
| 36 | + # Increment the channel number for pre-releases |
| 37 | + channel_num=$((channel_num + 1)) |
| 38 | + echo "$major.$minor.$patch-$channel.$channel_num" |
| 39 | + else |
| 40 | + # Increment the patch version for stable releases |
| 41 | + patch=$((patch + 1)) |
| 42 | + echo "$major.$minor.$patch" |
| 43 | + fi |
| 44 | + else |
| 45 | + echo "1.0.0" # Default to this if no valid tags exist |
| 46 | + fi |
| 47 | +} |
| 48 | + |
| 49 | +# Function to validate version format |
| 50 | +validate_version() { |
| 51 | + if [[ "$1" =~ ^v?([0-9]+\.[0-9]+\.[0-9]+(-[a-z]+\.[0-9]+)?)$ ]]; then |
| 52 | + echo "${BASH_REMATCH[1]}" |
| 53 | + else |
| 54 | + echo "Error: Invalid version format. Expected x.x.x or x.x.x-channel.x" |
| 55 | + exit 1 |
| 56 | + fi |
| 57 | +} |
| 58 | + |
| 59 | +# Step 1: List recent tags |
| 60 | +list_recent_remote_tags |
| 61 | + |
| 62 | +# Step 2: Prompt user for new version with a default guess |
| 63 | +default_version=$(guess_next_version) |
| 64 | +echo "" |
| 65 | +read -p "Enter the new version (default: $default_version): " INPUT_VERSION |
| 66 | +VERSION="${INPUT_VERSION:-$default_version}" |
| 67 | + |
| 68 | +# Step 3: Validate and clean the version |
| 69 | +VERSION=$(validate_version "$VERSION") |
| 70 | +NEW_TAG="v$VERSION" |
| 71 | + |
| 72 | +# Confirm with the user |
| 73 | +echo "" |
| 74 | +echo "This will:" |
| 75 | +echo " - Update package.json to version: $VERSION" |
| 76 | +echo " - Push a new git tag: $NEW_TAG" |
| 77 | +read -p "Do you want to continue? (y/n): " CONFIRM |
| 78 | + |
| 79 | +if [[ "$CONFIRM" != "y" ]]; then |
| 80 | + echo "Aborted." |
| 81 | + exit 0 |
| 82 | +fi |
| 83 | + |
| 84 | +# Step 4: Update package.json if necessary |
| 85 | +PACKAGE_VERSION=$(jq -r '.version' package.json) |
| 86 | +if [[ "$PACKAGE_VERSION" == "$VERSION" ]]; then |
| 87 | + echo "package.json is already updated to version $VERSION." |
| 88 | +else |
| 89 | + echo "Updating package.json..." |
| 90 | + jq ".version = \"$VERSION\"" package.json > package.temp.json |
| 91 | + mv package.temp.json package.json |
| 92 | + |
| 93 | + # Commit changes |
| 94 | + echo "Committing changes..." |
| 95 | + git add package.json |
| 96 | + git commit -m "chore: bump version to $VERSION" |
| 97 | + git push |
| 98 | +fi |
| 99 | + |
| 100 | +# Step 5: Push tag |
| 101 | +if git tag | grep -q "$NEW_TAG\$"; then |
| 102 | + echo "Tag $NEW_TAG already exists." |
| 103 | +else |
| 104 | + echo "Creating and pushing tag $NEW_TAG..." |
| 105 | + git tag "$NEW_TAG" |
| 106 | + git push origin "$NEW_TAG" |
| 107 | +fi |
| 108 | + |
| 109 | +echo "Release process completed successfully." |
0 commit comments