|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -e |
| 3 | +# Usage: |
| 4 | +# check_version.sh |
| 5 | +# |
| 6 | +# Reads version from Cargo.toml and checks it against tags |
| 7 | +# |
| 8 | +# Credit to @richfitz for this from the dust package: |
| 9 | +# https://github.com/mrc-ide/dust/blob/master/scripts/version_check |
| 10 | +VERSION=${1:-$(grep '^version' Cargo.toml | sed 's/.*= *//' | sed 's/"//g')} |
| 11 | +TAG="v${VERSION}" |
| 12 | + |
| 13 | +echo "Proposed version number '$VERSION'" |
| 14 | + |
| 15 | +if echo "$VERSION" | grep -Eq "[0-9]+[.][0-9]+[.][0-9]+"; then |
| 16 | + echo "[OK] Version number in correct format" |
| 17 | +else |
| 18 | + echo "[ERROR] Invalid format version number '$VERSION' must be in format 'x.y.z'" |
| 19 | + exit 1 |
| 20 | +fi |
| 21 | + |
| 22 | +EXIT_CODE=0 |
| 23 | + |
| 24 | +echo "Updating remote git data" |
| 25 | +git fetch --quiet |
| 26 | + |
| 27 | +BRANCH_DEFAULT=$(git remote show origin | awk '/HEAD branch/ {print $NF}') |
| 28 | +LAST_TAG=$(git describe --tags --abbrev=0 "origin/${BRANCH_DEFAULT}") |
| 29 | + |
| 30 | +echo "Last tag was $LAST_TAG" |
| 31 | + |
| 32 | +if git rev-parse "$TAG" >/dev/null 2>&1; then |
| 33 | + echo "[ERROR] Tag $TAG already exists - update version number in Cargo.toml" |
| 34 | + exit 1 |
| 35 | +else |
| 36 | + echo "[OK] Version number not yet present as git tag" |
| 37 | +fi |
| 38 | + |
| 39 | +MAJOR=$(echo $VERSION | cut -d. -f1) |
| 40 | +MINOR=$(echo $VERSION | cut -d. -f2) |
| 41 | +PATCH=$(echo $VERSION | cut -d. -f3) |
| 42 | + |
| 43 | +LAST_VERSION=$(echo "$LAST_TAG" | sed 's/^v//') |
| 44 | +LAST_MAJOR=$(echo $LAST_VERSION | cut -d. -f1) |
| 45 | +LAST_MINOR=$(echo $LAST_VERSION | cut -d. -f2) |
| 46 | +LAST_PATCH=$(echo $LAST_VERSION | cut -d. -f3) |
| 47 | + |
| 48 | +if (( $MAJOR > $LAST_MAJOR )); then |
| 49 | + echo "[OK] Increasing MAJOR version" |
| 50 | + exit $EXIT_CODE |
| 51 | +elif (( $MINOR > $LAST_MINOR )); then |
| 52 | + echo "[OK] Increasing MINOR version" |
| 53 | + exit $EXIT_CODE |
| 54 | +elif (( $PATCH > $LAST_PATCH )); then |
| 55 | + echo "[OK] Increasing PATCH version" |
| 56 | + exit $EXIT_CODE |
| 57 | +else |
| 58 | + echo "[ERROR] Version number has not increased relative to $LAST_VERSION" |
| 59 | + exit 1 |
| 60 | +fi |
0 commit comments