scripts/release.sh
#!/usr/bin/env bash
#
# Release to PyPI. There are a few things that have to be done...

set -e

# Get dir containing this script. It will work as long as the last component of
# the path used to find the script is not a symlink (directory links are OK).
# https://stackoverflow.com/questions/59895/get-the-source-directory-of-a-bash-script-from-within-the-script-itself
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"

ROOT=$(realpath "${DIR}/..")


BRANCH=$(git rev-parse --abbrev-ref HEAD)

if [[ "${BRANCH}" != "master" ]]; then
    echo "You must be on master to release."
    exit 1
fi

if [[ -n "$(git diff --stat)" ]]; then
    echo "Working directory must be clean for release."
    exit 1
fi



VERSION=$(sed -n 's/^version = "\(.*\)"/\1/p' "${ROOT}/pyproject.toml")

do_release() {
    VER_FILE="${ROOT}/src/hark_lang/__init__.py"

    echo "Updating __version__..."
    printf '# file auto-generated by release.sh\n' > "${VER_FILE}"
    printf '__version__ = "%s"\n' "${VERSION}" >> "${VER_FILE}"

    echo "Building..."
    "${DIR}/make_lambda_dist.sh"
    poetry build

    echo "Tagging..."
    git add "${VER_FILE}"
    git commit -m "Updated __version__ to ${VERSION}"
    git tag -a -s "v${VERSION}" -m "Published version v${VERSION}."

    echo "Publishing..."
    poetry publish

    echo "DONE :)"
}



printf "\nRELEASE CHECKLIST\n"
printf " - updated CHANGELOG.md?\n"
printf " - make test and make lint?\n"
printf " - updated version number in pyproject.toml?\n"
printf " - updated the Guide (and version reference in introduction.md)?\n"

printf "\nReleasing version: %s (from pyproject.toml)\n\n" "${VERSION}"

read -p "Are you sure (Y/n)? " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Y]$ ]]; then
    echo "Aborting."
    exit 1
fi

read -p "Definitely??? (Y/n)? " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Y]$ ]]; then
    echo "Aborting."
    exit 1
fi

do_release
