Skip to content

Commit

Permalink
Add scripts and workflows to manage version number
Browse files Browse the repository at this point in the history
These were copied almost verbatim from hipercow. The only difference is
that I removed the bits of code related to keeping hipercow.windows in
sync.
  • Loading branch information
plietar committed Jul 10, 2024
1 parent 195ffc5 commit ecc09f1
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 1 deletion.
24 changes: 24 additions & 0 deletions .github/workflows/check-version.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
on:
pull_request:
branches:
- master
- main

name: Check Version

jobs:
all:
runs-on: ubuntu-latest

name: Check Version

env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}

steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Check version format and availability
run: ./scripts/check_version
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: orderly2
Title: Orderly Next Generation
Version: 1.99.20
Version: 1.99.21
Authors@R: c(person("Rich", "FitzJohn", role = c("aut", "cre"),
email = "[email protected]"),
person("Robert", "Ashton", role = "aut"),
Expand Down
55 changes: 55 additions & 0 deletions scripts/bump_version
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/usr/bin/env bash
set -eu

ACTION="${1:-patch}"
CURRENT_VERSION=$(grep '^Version' DESCRIPTION | sed 's/.*: *//')
MAJOR=$(echo $CURRENT_VERSION | cut -d. -f1)
MINOR=$(echo $CURRENT_VERSION | cut -d. -f2)
PATCH=$(echo $CURRENT_VERSION | cut -d. -f3)
echo "Current version: ${CURRENT_VERSION}"

if [ $# -gt 1 ]; then
echo "Invalid args"
exit 1
fi

case $ACTION in
reset)
echo "Resetting version"
LAST_VERSION=$(git show origin/main:DESCRIPTION | grep '^Version' | sed 's/.*: *//')
MAJOR=$(echo $LAST_VERSION | cut -d. -f1)
MINOR=$(echo $LAST_VERSION | cut -d. -f2)
PATCH=$(echo $LAST_VERSION | cut -d. -f3)
;;
major)
echo "Updating major version"
MAJOR=$(($MAJOR + 1))
MINOR=0
PATCH=0
;;
minor)
echo "Updating minor version"
MINOR=$(($MINOR + 1))
PATCH=0
;;
patch)
echo "Updating patch version"
PATCH=$(($PATCH + 1))
;;
*)
if echo "$ACTION" | grep -Eq "[0-9]+[.][0-9]+[.][0-9]+"; then
echo "Updating version directly"
MAJOR=$(echo $VERSION | cut -d. -f1)
MINOR=$(echo $VERSION | cut -d. -f2)
PATCH=$(echo $VERSION | cut -d. -f3)
else
echo "[ERROR] Invalid format version number '$VERSION' must be in format 'x.y.z'"
exit 1
fi
;;
esac

NEXT_VERSION="${MAJOR}.${MINOR}.${PATCH}"
echo "Updating to version: ${NEXT_VERSION}"

sed -i "s/^Version: .*$/Version: ${NEXT_VERSION}/" DESCRIPTION
48 changes: 48 additions & 0 deletions scripts/check_version
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env bash
set -e
# Usage:
# check_version [<version>]
#
# If version is not given as a positional argument, then we'll read it
# from the DESCRIPTION file.
#
# We assume that a version already exists as a tag.
VERSION=${1:-$(grep '^Version' DESCRIPTION | sed 's/.*: *//')}
TAG="v${VERSION}"

echo "Proposed version number '$VERSION'"

if echo "$VERSION" | grep -Eq "[0-9]+[.][0-9]+[.][0-9]+"; then
echo "[OK] Version number in correct format"
else
echo "[ERROR] Invalid format version number '$VERSION' must be in format 'x.y.z'"
exit 1
fi

EXIT_CODE=0

LAST_VERSION=$(git show origin/main:DESCRIPTION | grep '^Version' | sed 's/.*: *//')

echo "Last version was '$LAST_VERSION'"

MAJOR=$(echo $VERSION | cut -d. -f1)
MINOR=$(echo $VERSION | cut -d. -f2)
PATCH=$(echo $VERSION | cut -d. -f3)

LAST_VERSION=$(echo "$LAST_VERSION" | sed 's/^v//')
LAST_MAJOR=$(echo $LAST_VERSION | cut -d. -f1)
LAST_MINOR=$(echo $LAST_VERSION | cut -d. -f2)
LAST_PATCH=$(echo $LAST_VERSION | cut -d. -f3)

if (( $MAJOR > $LAST_MAJOR )); then
echo "[OK] ${VERSION} increases MAJOR version over old version ${LAST_VERSION}"
elif (( $MINOR > $LAST_MINOR )); then
echo "[OK] ${VERSION} increases MINOR version over old version ${LAST_VERSION}"
elif (( $PATCH > $LAST_PATCH )); then
echo "[OK] ${VERSION} increases PATCH version over old version ${LAST_VERSION}"
else
echo "[ERROR] Version number has not increased relative to $LAST_VERSION"
EXIT_CODE=1
fi

exit $EXIT_CODE

0 comments on commit ecc09f1

Please sign in to comment.