|
| 1 | +#!/bin/bash |
| 2 | +##===----------------------------------------------------------------------===## |
| 3 | +## |
| 4 | +## This source file is part of the Swift.org open source project |
| 5 | +## |
| 6 | +## Copyright (c) 2024-2026 Apple Inc. and the Swift.org project authors |
| 7 | +## Licensed under Apache License v2.0 |
| 8 | +## |
| 9 | +## See LICENSE.txt for license information |
| 10 | +## See CONTRIBUTORS.txt for the list of Swift.org project authors |
| 11 | +## |
| 12 | +## SPDX-License-Identifier: Apache-2.0 |
| 13 | +## |
| 14 | +##===----------------------------------------------------------------------===## |
| 15 | + |
| 16 | +# Release script for swift-java |
| 17 | +# |
| 18 | +# This script automates the release process: |
| 19 | +# 1. Pins swift-java-jni-core to a specific released version |
| 20 | +# 2. Verifies the build |
| 21 | +# 3. Creates a release branch and commit |
| 22 | +# 4. After merge, prepares the next development snapshot |
| 23 | +# |
| 24 | +# Usage: ./scripts/release.sh |
| 25 | + |
| 26 | +set -euo pipefail |
| 27 | + |
| 28 | +REPO_ROOT="$(cd "$(dirname "$0")/.." && pwd)" |
| 29 | +PACKAGE_SWIFT="$REPO_ROOT/Package.swift" |
| 30 | +MAIN_BRANCH="main" |
| 31 | + |
| 32 | +# Colors for output |
| 33 | +RED='\033[0;31m' |
| 34 | +GREEN='\033[0;32m' |
| 35 | +YELLOW='\033[1;33m' |
| 36 | +BOLD='\033[1m' |
| 37 | +NC='\033[0m' # No Color |
| 38 | + |
| 39 | +info() { echo -e "${GREEN}[INFO]${NC} $*"; } |
| 40 | +warn() { echo -e "${YELLOW}[WARN]${NC} $*"; } |
| 41 | +error() { echo -e "${RED}[ERROR]${NC} $*"; exit 1; } |
| 42 | +confirm() { |
| 43 | + echo -e "${YELLOW}$*${NC}" |
| 44 | + read -r -p "Press Enter to continue (or Ctrl+C to abort)... " |
| 45 | +} |
| 46 | + |
| 47 | +# ==== ----------------------------------------------------------------------- |
| 48 | +# MARK: Gather release version |
| 49 | + |
| 50 | +echo "" |
| 51 | +echo -e "${BOLD}swift-java release script${NC}" |
| 52 | +echo "=========================" |
| 53 | +echo "" |
| 54 | + |
| 55 | +CURRENT_TAG="$(git -C "$REPO_ROOT" describe --tags --abbrev=0 2>/dev/null || echo "none")" |
| 56 | +read -r -p "Enter the version to release (current tag: $CURRENT_TAG): " RELEASE_VERSION |
| 57 | + |
| 58 | +# Validate version format |
| 59 | +if [[ ! "$RELEASE_VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then |
| 60 | + error "Invalid version format: '$RELEASE_VERSION'. Expected format: N.N.N" |
| 61 | +fi |
| 62 | + |
| 63 | +info "Preparing release ${BOLD}$RELEASE_VERSION${NC}" |
| 64 | + |
| 65 | +# ==== ----------------------------------------------------------------------- |
| 66 | +# MARK: Check preconditions |
| 67 | + |
| 68 | +# Ensure we're on a clean working tree |
| 69 | +if [[ -n "$(git -C "$REPO_ROOT" status --porcelain)" ]]; then |
| 70 | + error "Working tree is not clean. Please commit or stash your changes first." |
| 71 | +fi |
| 72 | + |
| 73 | +# Ensure we're on the main branch |
| 74 | +CURRENT_BRANCH="$(git -C "$REPO_ROOT" branch --show-current)" |
| 75 | +if [[ "$CURRENT_BRANCH" != "$MAIN_BRANCH" ]]; then |
| 76 | + error "Must be on '$MAIN_BRANCH' branch to start a release. Currently on '$CURRENT_BRANCH'." |
| 77 | +fi |
| 78 | + |
| 79 | +# Ensure main is up to date |
| 80 | +info "Fetching latest changes..." |
| 81 | +git -C "$REPO_ROOT" fetch origin |
| 82 | + |
| 83 | +LOCAL_SHA="$(git -C "$REPO_ROOT" rev-parse HEAD)" |
| 84 | +REMOTE_SHA="$(git -C "$REPO_ROOT" rev-parse origin/$MAIN_BRANCH)" |
| 85 | +if [[ "$LOCAL_SHA" != "$REMOTE_SHA" ]]; then |
| 86 | + error "Local '$MAIN_BRANCH' is not up to date with origin. Please pull first." |
| 87 | +fi |
| 88 | + |
| 89 | +# ==== ----------------------------------------------------------------------- |
| 90 | +# MARK: Determine latest swift-java-jni-core release |
| 91 | + |
| 92 | +info "Fetching latest swift-java-jni-core release tag..." |
| 93 | +JNI_CORE_LATEST=$(gh api repos/swiftlang/swift-java-jni-core/tags --jq '.[0].name') |
| 94 | + |
| 95 | +if [[ -z "$JNI_CORE_LATEST" ]]; then |
| 96 | + error "Could not determine latest swift-java-jni-core release." |
| 97 | +fi |
| 98 | + |
| 99 | +info "Latest swift-java-jni-core release: ${BOLD}$JNI_CORE_LATEST${NC}" |
| 100 | + |
| 101 | +# ==== ----------------------------------------------------------------------- |
| 102 | +# MARK: Update Package.swift for release |
| 103 | + |
| 104 | +RELEASE_BRANCH="release/$RELEASE_VERSION" |
| 105 | +info "Creating branch '${RELEASE_BRANCH}'..." |
| 106 | +git -C "$REPO_ROOT" checkout -b "$RELEASE_BRANCH" |
| 107 | + |
| 108 | +info "Pinning swift-java-jni-core to version ${BOLD}$JNI_CORE_LATEST${NC} in Package.swift..." |
| 109 | + |
| 110 | +# Replace any existing jni-core version specification with the pinned version. |
| 111 | +# This handles both `from: "X.Y.Z"` and `branch: "main"` forms. |
| 112 | +sed -i '' -E \ |
| 113 | + 's|(swiftJavaJNICoreDep = \.package\(url: "https://github\.com/swiftlang/swift-java-jni-core"), .*\)|swiftJavaJNICoreDep = .package(url: "https://github.com/swiftlang/swift-java-jni-core", from: "'"$JNI_CORE_LATEST"'")|' \ |
| 114 | + "$PACKAGE_SWIFT" |
| 115 | + |
| 116 | +# Verify the change was applied |
| 117 | +if ! grep -q "from: \"$JNI_CORE_LATEST\"" "$PACKAGE_SWIFT"; then |
| 118 | + error "Failed to update swift-java-jni-core version in Package.swift" |
| 119 | +fi |
| 120 | + |
| 121 | +info "Package.swift updated." |
| 122 | + |
| 123 | +# ==== ----------------------------------------------------------------------- |
| 124 | +# MARK: Verify build |
| 125 | + |
| 126 | +info "Verifying build..." |
| 127 | +if ! xcrun swift build --package-path "$REPO_ROOT" 2>&1; then |
| 128 | + error "Build failed! Please fix the issues before releasing." |
| 129 | +fi |
| 130 | + |
| 131 | +info "Build succeeded." |
| 132 | + |
| 133 | +# ==== ----------------------------------------------------------------------- |
| 134 | +# MARK: Create release commit |
| 135 | + |
| 136 | +info "Creating release commit..." |
| 137 | +git -C "$REPO_ROOT" add "$PACKAGE_SWIFT" |
| 138 | +git -C "$REPO_ROOT" commit -m "Preparing release $RELEASE_VERSION." |
| 139 | + |
| 140 | +info "Pushing branch '${RELEASE_BRANCH}' to origin..." |
| 141 | +git -C "$REPO_ROOT" push -u origin "$RELEASE_BRANCH" |
| 142 | + |
| 143 | +echo "" |
| 144 | +echo "============================================================" |
| 145 | +info "Release branch '${RELEASE_BRANCH}' has been pushed." |
| 146 | +echo "" |
| 147 | +echo -e " Next steps:" |
| 148 | +echo -e " 1. Create a pull request for '${BOLD}${RELEASE_BRANCH}${NC}'" |
| 149 | +echo -e " 2. Get it reviewed and merged" |
| 150 | +echo -e " 3. Tag the merge commit on main: ${BOLD}git tag $RELEASE_VERSION && git push origin $RELEASE_VERSION${NC}" |
| 151 | +echo -e " 4. Create a GitHub release for the tag" |
| 152 | +echo -e " 5. Come back here and press Enter to prepare the next development snapshot" |
| 153 | +echo "============================================================" |
| 154 | +echo "" |
| 155 | + |
| 156 | +confirm "Once the release PR is merged AND the tag is created, press Enter to continue..." |
| 157 | + |
| 158 | +# ==== ----------------------------------------------------------------------- |
| 159 | +# MARK: Prepare next development snapshot |
| 160 | + |
| 161 | +info "Switching back to '$MAIN_BRANCH' and pulling latest..." |
| 162 | +git -C "$REPO_ROOT" checkout "$MAIN_BRANCH" |
| 163 | +git -C "$REPO_ROOT" pull --rebase origin "$MAIN_BRANCH" |
| 164 | + |
| 165 | +NEXT_DEV_BRANCH="prepare-next-development-from-$RELEASE_VERSION" |
| 166 | +info "Creating branch '${NEXT_DEV_BRANCH}'..." |
| 167 | +git -C "$REPO_ROOT" checkout -b "$NEXT_DEV_BRANCH" |
| 168 | + |
| 169 | +info "Updating Package.swift to use swift-java-jni-core from main branch..." |
| 170 | +sed -i '' -E \ |
| 171 | + 's|(swiftJavaJNICoreDep = \.package\(url: "https://github\.com/swiftlang/swift-java-jni-core"), .*\)|swiftJavaJNICoreDep = .package(url: "https://github.com/swiftlang/swift-java-jni-core", branch: "main")|' \ |
| 172 | + "$PACKAGE_SWIFT" |
| 173 | + |
| 174 | +# Verify the change was applied |
| 175 | +if ! grep -q 'branch: "main"' "$PACKAGE_SWIFT"; then |
| 176 | + error "Failed to update swift-java-jni-core to branch: \"main\" in Package.swift" |
| 177 | +fi |
| 178 | + |
| 179 | +info "Package.swift updated for development." |
| 180 | + |
| 181 | +git -C "$REPO_ROOT" add "$PACKAGE_SWIFT" |
| 182 | +git -C "$REPO_ROOT" commit -m "Prepare next development cycle after $RELEASE_VERSION release." |
| 183 | + |
| 184 | +info "Pushing branch '${NEXT_DEV_BRANCH}' to origin..." |
| 185 | +git -C "$REPO_ROOT" push -u origin "$NEXT_DEV_BRANCH" |
| 186 | + |
| 187 | +echo "" |
| 188 | +echo "============================================================" |
| 189 | +info "Post-release branch '${NEXT_DEV_BRANCH}' has been pushed." |
| 190 | +echo "" |
| 191 | +echo -e " Next steps:" |
| 192 | +echo -e " 1. Create a pull request for '${BOLD}${NEXT_DEV_BRANCH}${NC}'" |
| 193 | +echo -e " 2. Get it reviewed and merged" |
| 194 | +echo "" |
| 195 | +echo -e "${GREEN}Release $RELEASE_VERSION is complete!${NC}" |
| 196 | +echo "============================================================" |
0 commit comments