#!/bin/sh

cdir=$(pwd)
app=$(basename $cdir)

function build {
    # build the app for pypi
    source venv/bin/activate
    echo "⚙️ building $app..."
    if [[ $# -eq 0 ]]; then
        if cleanup && tests; then
            python -m build
        else
            echo "❌ build was unsuccessful"
        fi
    elif [ $1="--skip-checks" ]; then
        echo "⚠️ skipping checks"
        python -m build
    fi
}

function check {
    echo "📝 checking long description"
    twine check dist/*
}

function cleanup {
    # lint app using black and flake8
    echo "🧾 linting $app..."
    source venv/bin/activate
    isort src tests
    black src tests
    flake8 src tests
    ruff src tests
    echo "🧾 type checking $app..."
    mypy src tests
}

function tests {
    # test app using pytest
    echo "🧾 testing $app..."
    source venv/bin/activate
    pytest -vv --cov-report term-missing
}

function upload {
    # upload to pypi
    source venv/bin/activate
    # read -p '❓ version? ' version
    echo "⬆️ uploading $app to pypi..."
    #twine upload dist/*$version*
    twine upload dist/*$version*
}

function help {
  printf "%s <task> [args]\n\nTasks:\n" "${0}"

  compgen -A function | grep -v "^_" | cat -n

  printf "\nExtended help:\n  Each task has comments for general usage\n"
}

TIMEFORMAT=$'\nTask completed in %3lR'
time "${@:-help}"
