|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Helper script to prepare a release for the Python SDK. |
| 4 | + |
| 5 | +output_version_file="src/release/version.py" |
| 6 | +version_template_file="src/release/templates/version.tpl.py" |
| 7 | + |
| 8 | +# Extracts the current build/version number for comparison and backup |
| 9 | +current_build=$(awk -F "['\"]" '/SDK_BUILD_NUMBER =/{print $2}' "$output_version_file") |
| 10 | +current_version=$(awk -F "['\"]" '/SDK_VERSION =/{print $2}' "$output_version_file") |
| 11 | + |
| 12 | +# Function to execute upon exit |
| 13 | +cleanup() { |
| 14 | + echo "Performing cleanup tasks..." |
| 15 | + # Revert changes to file if any |
| 16 | + sed -e "s/{{ build }}/$current_build/" -e "s/{{ version }}/$current_version/" "$version_template_file" > "$output_version_file" |
| 17 | + exit 1 |
| 18 | +} |
| 19 | + |
| 20 | +# Set the trap to call the cleanup function on exit |
| 21 | +trap cleanup SIGINT |
| 22 | + |
| 23 | +enforce_latest_code() { |
| 24 | + if [[ -n "$(git status --porcelain=v1)" ]]; then |
| 25 | + echo "ERROR: working directory is not clean." |
| 26 | + echo "Please stash your changes and try again." |
| 27 | + exit 1 |
| 28 | + fi |
| 29 | +} |
| 30 | + |
| 31 | +# Function to validate the version number format x.y.z(-beta.w) |
| 32 | +update_and_validate_version() { |
| 33 | + while true; do |
| 34 | + # Prompt the user to input the version number |
| 35 | + read -p "Enter the version number (format: x.y.z(-beta.w)): " version |
| 36 | + |
| 37 | + # Validate the version number format |
| 38 | + if [[ "${version}" =~ ^[0-9]+\.[0-9]+\.[0-9]+(-beta\.[0-9]+)?$ ]]; then |
| 39 | + if [[ "${current_version}" != "${version}" ]]; then |
| 40 | + # TODO: Check the less than case as well. |
| 41 | + echo "New version number is: ${version}" |
| 42 | + return 0 |
| 43 | + else |
| 44 | + echo "Version hasn't changed." |
| 45 | + fi |
| 46 | + else |
| 47 | + echo "Invalid version number format: ${version}" |
| 48 | + echo "Please enter a version number in the 'x.y.z(-beta.w)' format." |
| 49 | + fi |
| 50 | + done |
| 51 | +} |
| 52 | + |
| 53 | +# Function to validate the build number format. |
| 54 | +# SEMVER Format: Mmmppbb - 7 Digits |
| 55 | +update_and_validate_build() { |
| 56 | + while true; do |
| 57 | + # Prompt the user to input the build number |
| 58 | + read -p "Enter the build number (format: Mmmppbb): " build |
| 59 | + |
| 60 | + # Validate the build number format |
| 61 | + if [[ "${build}" =~ ^[0-9]{7}$ ]]; then |
| 62 | + if (( 10#$current_build < 10#$build )); then |
| 63 | + # Write the valid build number to the file |
| 64 | + echo "New build number is: ${build}" |
| 65 | + return 0 |
| 66 | + else |
| 67 | + echo "New build version should be higher than current build version." |
| 68 | + fi |
| 69 | + else |
| 70 | + echo "Invalid build number format: ${build}" |
| 71 | + echo "Please enter a build number in the 'Mmmppbb' format." |
| 72 | + fi |
| 73 | + done |
| 74 | +} |
| 75 | + |
| 76 | +# Ensure working directory is clean |
| 77 | +enforce_latest_code |
| 78 | + |
| 79 | +# Update and validate the version number |
| 80 | +update_and_validate_version |
| 81 | + |
| 82 | +# Update and validate the build number |
| 83 | +update_and_validate_build |
| 84 | + |
| 85 | +# Update version & build number in version.py |
| 86 | +sed -e "s/{{ build }}/$build/" -e "s/{{ version }}/$version/" "$version_template_file" > "$output_version_file" |
| 87 | + |
| 88 | +printf "Press ENTER to edit the RELEASE-NOTES in your default editor...\n" |
| 89 | +read -r _ignore |
| 90 | +${EDITOR:-nano} "src/release/RELEASE-NOTES" |
| 91 | + |
| 92 | +# Get Current Branch Name |
| 93 | +branch="$(git rev-parse --abbrev-ref HEAD)" |
| 94 | + |
| 95 | +# if on main, then stash changes and create RC branch |
| 96 | +if [[ "${branch}" = "main" ]]; then |
| 97 | + branch=rc/"${version}" |
| 98 | + git stash |
| 99 | + git fetch origin |
| 100 | + git checkout -b "${branch}" |
| 101 | + git stash apply |
| 102 | +fi |
| 103 | + |
| 104 | +# Add changes and commit/push to branch |
| 105 | +git add . |
| 106 | +git commit -S -m "Release v${version}" |
| 107 | +git push --set-upstream origin "${branch}" |
| 108 | + |
| 109 | +echo "Release has been prepared.. |
| 110 | +Make sure to double check version/build numbers in their appropriate files and |
| 111 | +changelog is correctly filled out. |
| 112 | +Once confirmed, run 'make release' to release the SDK!" |
| 113 | + |
0 commit comments