|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -ueo pipefail |
| 4 | + |
| 5 | +DEFAULT_REPO=Robbepop/modular-bitfield |
| 6 | +DEFAULT_BRANCH=master |
| 7 | + |
| 8 | +usage() { |
| 9 | + echo "Usage: $0 [branch] [version]" |
| 10 | + echo |
| 11 | + echo "Specifying a branch will create a new patch release from that branch." |
| 12 | + echo "Otherwise, a new release will be created from '$DEFAULT_BRANCH'." |
| 13 | + echo |
| 14 | + echo "Specifying a version will create a release with that specific version." |
| 15 | + echo "Otherwise, the version number for the release will be adapted from" |
| 16 | + echo "Cargo.toml." |
| 17 | + echo |
| 18 | + echo "After tagging, the version number in Cargo.toml will automatically be" |
| 19 | + echo "bumped unless a pre-release version number was passed explicitly to" |
| 20 | + echo "this script, in which case the version number in Cargo.toml will be" |
| 21 | + echo "restored." |
| 22 | + echo |
| 23 | + echo "If the version number for the new release ends in .0, a new minor" |
| 24 | + echo "release branch will also be created." |
| 25 | + echo |
| 26 | + echo "Supported environment variables:" |
| 27 | + echo " REPO: The GitHub repository (user/repo) to use for the release." |
| 28 | + echo " Defaults to $DEFAULT_REPO." |
| 29 | + exit 0 |
| 30 | +} |
| 31 | + |
| 32 | +set_package_publish() { |
| 33 | + sed -i'' -e "s/^\\(publish[[:space:]]*=[[:space:]]*\\).*$/\\1$1/" Cargo.toml |
| 34 | +} |
| 35 | + |
| 36 | +set_package_version() { |
| 37 | + sed -i'' -e "s/^\\(version[[:space:]]*=[[:space:]]*\\)\"[^\"]*\"/\\1\"$1\"/" Cargo.toml |
| 38 | + sed -i'' -e "s/^\\(modular-bitfield-impl[[:space:]]*=.*version[[:space:]]*=[[:space:]]*\\)\"[^\"]*\"/\\1\"$1\"/" Cargo.toml |
| 39 | +} |
| 40 | + |
| 41 | +if [ "${1-}" == "--help" ]; then |
| 42 | + usage |
| 43 | +elif [ -n "${1-}" ]; then |
| 44 | + BRANCH=$1 |
| 45 | +else |
| 46 | + BRANCH=$DEFAULT_BRANCH |
| 47 | +fi |
| 48 | + |
| 49 | +if [ -n "${2-}" ]; then |
| 50 | + VERSION=$2 |
| 51 | + if [[ ! "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+(-.*)?$ ]]; then |
| 52 | + echo "Invalid version '$VERSION'; versions must be in the form" |
| 53 | + echo "\`major.minor.patch[-extra]\`." |
| 54 | + exit 1 |
| 55 | + fi |
| 56 | +else |
| 57 | + VERSION= |
| 58 | +fi |
| 59 | + |
| 60 | +REPO=${REPO:-$DEFAULT_REPO} |
| 61 | +ROOT_DIR=$(cd "$(dirname "$0")" && pwd) |
| 62 | +BUILD_DIR="$ROOT_DIR/build-release" |
| 63 | + |
| 64 | +if [ -d "$BUILD_DIR" ]; then |
| 65 | + echo "Existing build directory detected at $BUILD_DIR" |
| 66 | + echo "Aborted." |
| 67 | + exit 1 |
| 68 | +fi |
| 69 | + |
| 70 | +echo "This is an internal modular-bitfield release script!" |
| 71 | +echo -n "Press 'y' to create a new modular-bitfield release from $REPO branch $BRANCH" |
| 72 | +if [ -z "$VERSION" ]; then |
| 73 | + echo "." |
| 74 | +else |
| 75 | + echo -e "\nwith version override $VERSION." |
| 76 | +fi |
| 77 | +echo "(You can abort pushing upstream later on if something goes wrong.)" |
| 78 | +read -r -s -n 1 |
| 79 | + |
| 80 | +if [ "$REPLY" != "y" ]; then |
| 81 | + echo "Aborted." |
| 82 | + exit 0 |
| 83 | +fi |
| 84 | + |
| 85 | +# Using a pristine copy of the repo for the release to avoid local changes |
| 86 | +# making their way into the release process, and to avoid polluting the local |
| 87 | +# repo with wrong changes if the release process is aborted |
| 88 | +cd "$ROOT_DIR" |
| 89 | +mkdir "$BUILD_DIR" |
| 90 | +git clone --recursive "[email protected]:$REPO" "$BUILD_DIR" |
| 91 | + |
| 92 | +cd "$BUILD_DIR" |
| 93 | + |
| 94 | +# Newly created tags and updated branches are stored so they can be pushed all |
| 95 | +# at once at the end after the other work is guaranteed to be successful |
| 96 | +PUSH_BRANCHES="$BRANCH" |
| 97 | + |
| 98 | +echo -e "\nBuilding $BRANCH branch...\n" |
| 99 | + |
| 100 | +git checkout "$BRANCH" |
| 101 | + |
| 102 | +# head is needed in case the version number was manually updated to something |
| 103 | +# which has a tag with a number in it, otherwise there will be multiple matches |
| 104 | +# on the line instead of just the first one |
| 105 | +VERSION_CARGO_NO_TAGS=$(grep -o '^version[[:space:]]*=[[:space:]]*"[^"]*"' Cargo.toml | grep -o "[0-9][0-9.]*" | head -1) |
| 106 | + |
| 107 | +if [ -z "$VERSION" ]; then |
| 108 | + VERSION=$VERSION_CARGO_NO_TAGS |
| 109 | + VERSION_NO_TAGS=$VERSION_CARGO_NO_TAGS |
| 110 | +else |
| 111 | + VERSION_NO_TAGS=$(echo "$VERSION" | grep -o "[0-9][0-9.]*") |
| 112 | +fi |
| 113 | + |
| 114 | +# Converting the version number to an array to auto-generate the next |
| 115 | +# release version number |
| 116 | +IFS="." read -r -a PRE_VERSION_SPLIT <<< "$VERSION_NO_TAGS" |
| 117 | + |
| 118 | +if [[ "$VERSION" =~ ^[0-9][0-9.]*\.0$ ]]; then |
| 119 | + # Minor release gets a branch |
| 120 | + MAKE_BRANCH="${PRE_VERSION_SPLIT[0]}.${PRE_VERSION_SPLIT[1]}" |
| 121 | + BRANCH_VERSION="${PRE_VERSION_SPLIT[0]}.${PRE_VERSION_SPLIT[1]}.$((PRE_VERSION_SPLIT[2] + 1))-pre" |
| 122 | + |
| 123 | + # The next release is usually going to be a minor release; if the next |
| 124 | + # version is to be a major release, the package version in Git will need |
| 125 | + # to be manually updated or someone will have to pass a major version number |
| 126 | + # to the release script at the last second |
| 127 | + PRE_VERSION="${PRE_VERSION_SPLIT[0]}.$((PRE_VERSION_SPLIT[1] + 1)).0-pre" |
| 128 | +else |
| 129 | + # Patch releases do not get branches |
| 130 | + MAKE_BRANCH= |
| 131 | + BRANCH_VERSION= |
| 132 | + |
| 133 | + if [[ "$VERSION" == "$VERSION_NO_TAGS" ]]; then |
| 134 | + # The next release version will always be another patch version |
| 135 | + PRE_VERSION="${PRE_VERSION_SPLIT[0]}.${PRE_VERSION_SPLIT[1]}.$((PRE_VERSION_SPLIT[2] + 1))-pre" |
| 136 | + else |
| 137 | + # The version being released is a pre-release, so do not bump anything |
| 138 | + PRE_VERSION="$VERSION_CARGO_NO_TAGS-pre" |
| 139 | + fi |
| 140 | +fi |
| 141 | + |
| 142 | +TAG_VERSION="v$VERSION" |
| 143 | + |
| 144 | +# At this point: |
| 145 | +# $VERSION is the version of modular-bitfield that is being released |
| 146 | +# $TAG_VERSION is the name that will be used for the Git tag for the release |
| 147 | +# $PRE_VERSION is the next pre-release version of modular-bitfield that will be |
| 148 | +# set on the original branch after tagging |
| 149 | +# $MAKE_BRANCH is the name of the new minor release branch that should be |
| 150 | +# created (if this is not a patch release) |
| 151 | +# $BRANCH_VERSION is the pre-release version of modular-bitfield that will be |
| 152 | +# set on the minor release branch |
| 153 | + |
| 154 | +# Something is messed up and this release has already happened |
| 155 | +if [ "$(git tag | grep -c "^$TAG_VERSION$")" -gt 0 ]; then |
| 156 | + echo -e "\nTag $TAG_VERSION already exists! Please check the branch.\n" |
| 157 | + exit 1 |
| 158 | +fi |
| 159 | + |
| 160 | +set_package_version "$VERSION" |
| 161 | +set_package_publish "true" |
| 162 | + |
| 163 | +git commit -m "Updating metadata for $VERSION" -m "[ci skip]" Cargo.toml |
| 164 | +git tag -s -m "Release $VERSION" "$TAG_VERSION" |
| 165 | + |
| 166 | +set_package_version "$PRE_VERSION" |
| 167 | +set_package_publish "false # Use \`release.sh\`" |
| 168 | + |
| 169 | +git commit -m "Updating source version to $PRE_VERSION" -m "[ci skip]" Cargo.toml |
| 170 | + |
| 171 | +if [ "$MAKE_BRANCH" != "" ]; then |
| 172 | + git checkout -b "$MAKE_BRANCH" "$TAG_VERSION" |
| 173 | + set_package_version "$BRANCH_VERSION" |
| 174 | + set_package_publish "false # Use \`release.sh\`" |
| 175 | + |
| 176 | + git commit -m "Updating source version to $BRANCH_VERSION" -m "[ci skip]" Cargo.toml |
| 177 | + PUSH_BRANCHES="$PUSH_BRANCHES $MAKE_BRANCH" |
| 178 | + PUSH_BRANCHES_MSG=" and branches ${PUSH_BRANCHES}" |
| 179 | +fi |
| 180 | + |
| 181 | +echo -e "\nDone!\n" |
| 182 | +echo "Please confirm packaging success, then press 'y', ENTER to push" |
| 183 | +echo "tags $TAG_VERSION${PUSH_BRANCHES_MSG:-}, or any other key to bail." |
| 184 | +read -r -p "> " |
| 185 | + |
| 186 | +if [ "$REPLY" != "y" ]; then |
| 187 | + echo "Aborted." |
| 188 | + exit 0 |
| 189 | +fi |
| 190 | + |
| 191 | +for BRANCH in $PUSH_BRANCHES; do |
| 192 | + git push origin "$BRANCH" |
| 193 | +done |
| 194 | + |
| 195 | +git push origin --tags |
| 196 | +git checkout "$TAG_VERSION" |
| 197 | +cargo publish -p modular-bitfield-impl |
| 198 | +cargo publish -p modular-bitfield |
| 199 | + |
| 200 | +cd "$ROOT_DIR" |
| 201 | +rm -rf "$BUILD_DIR" |
| 202 | + |
| 203 | +echo -e "\nAll done! Yay!" |
0 commit comments