#!/usr/bin/env bash
set -e

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
cd "$SCRIPT_DIR"

echo "=== Activating virtualenv ==="
source .venv/bin/activate

echo "=== Installing from dev ==="
pip install -e .

echo "=== Initialising .pet/ ==="
pet init

echo "=== Running tests ==="
python -m pytest tests/ -v

echo "=== Regenerating docs ==="
for src in $(find . -name "*.md.pet" -not -path "./.venv/*"); do
    out="${src%.pet}"
    echo "  $src -> $out"
    pet "$src" "$out"
done

echo "=== Building distribution ==="
hatch build

echo "=== Uploading to PyPI ==="
VERSION=$(python -c "from importlib.metadata import version; print(version('pet-doc'))")
python -m twine upload dist/pet_doc-"$VERSION"*

echo "=== Released pet-doc $VERSION ==="
