---
version: '3'

tasks:
  build:
    deps:
      - :install:python:requirements
    desc: Build the project
    log:
      error: Encountered error while building the project with `poetry build`
      start: Building the project with `poetry build`
      success: Successfully built the project
    cmds:
      - task: clean
      - poetry build

  build:binary:
    deps:
      - :install:python:requirements
    desc: Build standalone binary
    log:
      error: Encountered error while building standalone Python binary with `PyInstaller`
      start: Building standalone Python binary with `PyInstaller`
      success: Successfully build standalone Python binary with `PyInstaller`
    cmds:
      - task: clean
      - |
        {{.PYTHON_HANDLE}}python -OO -m PyInstaller -F run.py

  clean:
    log:
      error: Encountered error while removing `build/` and `dist/`
      start: Removing `build/` and `dist/`
      success: Ensured `build/` and `dist/` are removed
    cmds:
      - rm -rf build
      - rm -rf dist

  global:reset:
    log:
      error: Error removing pip packages
      start: Removing pip packages
      success: Removed pip packages
    cmds:
      - cmd: if type pip &> /dev/null; then pip uninstall -y -r <(pip freeze); fi
        ignore_error: true
      - cmd: if type pip3 &> /dev/null; then pip3 uninstall -y -r <(pip3 freeze); fi
        ignore_error: true

  prepare:
    cmds:
      - .config/log info 'TODO Build the project'

  publish:
    deps:
      - :install:python:requirements
    desc: Publish the project to PyPi.org
    log:
      error: Error running `poetry publish`
      start: Running `poetry publish`
      success: '`poetry publish` executed successfully'
    cmds:
      - poetry publish

  requirementstxt:
    deps:
      - :install:python:requirements
    desc: Generate the requirements.txt fallback file
    log:
      error: Failed to generate `local/requirements.txt`
      start: Generating `local/requirements.txt`
      success: Generated `local/requirements.txt`
    cmds:
      - mkdir -p local
      - poetry export -f requirements.txt --output local/requirements.txt --without-hashes
    status:
      - '[ ! -f pyproject.toml ] || ! type poetry > /dev/null'

  run:
    deps:
      - :install:python:requirements
    desc: Runs the project by calling `run.py`
    log:
      error: Error running `{{.PYTHON_HANDLE}}python run.py`
      start: Running `{{.PYTHON_HANDLE}}python run.py`
      success: Successfully ran `{{.PYTHON_HANDLE}}python run.py`
    cmds:
      - |
        {{.PYTHON_HANDLE}}python run.py

  test:
    deps:
      - :install:python:requirements
    desc: Runs tests with `pytest` after fixing and linting
    log:
      start: Fixing, linting, and testing Python code
      success: Successfully fixed, linted, and tested Python code
    cmds:
      - task: :fix:python
      - task: :lint:python
      - task: :python:test:pytest

  verify:
    cmds:
      - .config/log info 'TODO Verify API key works with pypi.org'
