---
version: '3'

tasks:
  pip:
    deps:
      - :install:software:python
    run: when_changed
    log:
      error: Failed to install pip3 package `{{.PACKAGE}}`
      start: Installing pip3 package `{{.PACKAGE}}`
      success: Installed pip3 package `{{.PACKAGE}}`
    cmds:
      - |
        if [ -n "$CI" ]; then echo "*************** pip3 --> {{.PACKAGE}}"; fi
      - pip3 install {{.PACKAGE}}
    status:
      - type {{.PACKAGE}} > /dev/null

  pipx:
    cmds:
      - task: :install:pipx:install
        vars:
          BIN_NAME: '{{.BIN_NAME}}'
          PACKAGE: '{{.PACKAGE}}'

  pytest:
    cmds:
      - task: :install:pipx:pytest

  pytest-cov:
    cmds:
      - task: :install:pipx:pytest-cov

  requirements:
    run: once
    cmds:
      - task: :install:software:python
      - task: :{{if eq .REPOSITORY_TYPE "ansible"}}install:pipx:ansible{{else}}donothing{{end}}
      - task: requirements:poetry
      - task: :{{if eq .REPOSITORY_TYPE "ansible"}}ansible:galaxy:requirements{{else}}donothing{{end}}

  requirements:poetry:
    cmds:
      - task: requirements:poetry:prereqs
      - task: requirements:poetry:install
    status:
      - '[ ! -f pyproject.toml ] || ! type poetry &> /dev/null'

  requirements:poetry:install:
    summary: |
      # Poetry Requirements

      `poetry publish -r gitlab` will publish to GitLab registry on GitLab CI
    env:
      CPPFLAGS: -I/usr/local/opt/openssl@3/include
      LDFLAGS: -L/usr/local/opt/openssl@3/lib
    run: once
    log:
      error: Failed to configure / install via `poetry`
      start: Configuring Poetry and running `poetry install`
      success: Successfully ran `poetry install`
    cmds:
      - task: :install:software:poetry
      - poetry env use "$(which python3)"
      - poetry config virtualenvs.create {{if .PYTHON_VIRTUALENV}}{{.PYTHON_VIRTUALENV}}{{else}}false{{end}}
      - poetry config virtualenvs.in-project true
      - |
        if [ -n "$PYPI_TOKEN" ]; then
          poetry config pypi-token.pypi "$PYPI_TOKEN" || EXIT_CODE=$?
          if [ -n "$EXIT_CODE" ]; then
            .config/log warn 'Failed to run `poetry config pypi-token.pypi`'
          fi
        fi
      - |
        if [ -n "$GITLAB_CI" ]; then
          poetry config repositories.gitlab "https://gitlab.com/api/v4/projects/${CI_PROJECT_ID}/packages/pypi"
        fi
      - |
        if [ -n "$GITLAB_CI" ]; then
          poetry config http-basic.gitlab gitlab-ci-token "${CI_JOB_TOKEN}" || EXIT_CODE=$?
          if [ -n "$EXIT_CODE" ]; then
            .config/log warn 'Failed to run `poetry config http-basic.gitlab gitlab-ci-token`'
          fi
        fi
      - |
        if [ -n "$AUTO_UPDATE_PIP_PACKAGES" ]; then
          .config/log info 'Updating Poetry dependencies{{if .INSTALL_OPTIONS}} with options `{{.INSTALL_OPTIONS}}`{{end}}'
          poetry update{{if .INSTALL_OPTIONS}} {{.INSTALL_OPTIONS}}{{end}}
        else
          .config/log info 'Installing Poetry dependencies{{if .INSTALL_OPTIONS}} with options `{{.INSTALL_OPTIONS}}`{{end}}'
          export "CFLAGS=-I/usr/local/include -L/usr/local/lib"
          poetry install{{if .INSTALL_OPTIONS}} {{.INSTALL_OPTIONS}}{{end}} || EXIT_CODE=$?
          if [ -n "$EXIT_CODE" ]; then
            .config/log warn 'Running `poetry install{{if .INSTALL_OPTIONS}} {{.INSTALL_OPTIONS}}{{end}}` failed.'
            .config/log info 'Attempting to fix by running `poetry update{{if .INSTALL_OPTIONS}} {{.INSTALL_OPTIONS}}{{end}}`'
            poetry update{{if .INSTALL_OPTIONS}} {{.INSTALL_OPTIONS}}{{end}}
          fi
        fi
    sources:
      - pyproject.toml

  requirements:poetry:prereqs:
    tags:
      - update
    env:
      GCC5_MISSING_MESSAGE: |
        # Sudo Required for gcc-5 Symlink

        It looks like your system is missing gcc-5 but gcc is available. Select "Yes" if you would
        like to symlink your version of gcc to gcc-5 which, for the purposes of this project, can
        prevent issues with our Python runner `poetry`. However, there are some circumstances where
        you may not want to do this. This will generally be harmless - you can always remove the symlink
        by running `sudo rm -rf /usr/local/bin/gcc-5`.
    log:
      error: Error checking `gcc`
      start: Checking if `gcc` should be symlinked to `gcc-5`
      success: Checked `gcc`
    cmds:
      - |
        if which gcc &> /dev/null; then
          TMP="$(mktemp)" && echo "$GCC5_MISSING_MESSAGE" > "$TMP" && .config/log md "$TMP"
          if [[ $- == *i* ]]; then
            if [ -w /usr/local/bin ]; then
              if .config/log confirm 'Symlink `gcc` to `gcc-5`?'; then
                ln -s "$(which gcc)" /usr/local/bin/gcc-5
              fi
            else
              if .config/log confirm 'Symlink `gcc` to `gcc-5` via sudo?'; then
                .config/log info 'Running sudo command `sudo ln -s '"$(which gcc)"' /usr/local/bin/gcc-5`'
                sudo ln -s "$(which gcc)" /usr/local/bin/gcc-5
              else
                .config/log warn 'Continuing with missing `gcc-5` binary'
              fi
            fi
          else
            .config/log info 'Session is non-interactive'
            .config/log warn 'Symlinking `gcc` to `gcc-5` since session is non-interactive'
            .config/log info 'Remove the symlink later on (if required) by running `sudo rm /usr/local/bin/gcc-5`'
            if [ -w /usr/local/bin ]; then
              ln -s "$(which gcc)" /usr/local/bin/gcc-5
            else
              sudo ln -s "$(which gcc)" /usr/local/bin/gcc-5
            fi
          fi
        else
          .config/log warn '`gcc` is missing.'
        fi
    status:
      - which gcc-5

  venv:node:
    deps:
      - :install:software:dasel
      - :install:software:python
    vars:
      POETRY_MANUAL_SETUP_MSG: Adding semantic-release-python plugin Python dependencies by
        running `poetry add pip@latest setuptools@latest twine@latest wheel@latest`
    log:
      error: Encountered error while installing Python dependencies for `semantic-release-python`
      start: Installing `semantic-release-python` Python dependencies
      success: Successfully installed `semantic-release-python` Python dependencies
    cmds:
      - |
        REQUIREMENTS_PATH="$PWD/lib/requirements.txt"
        cd "$INIT_CWD" || exit
        if type poetry > /dev/null && test -f pyproject.toml; then
          .config/log info 'Poetry installation detected. Using Poetry to install NPM package Python dependencies.'
          if dasel select -f pyproject.toml '.tool.poetry.extras.semantic' > /dev/null; then
            .config/log info '`semantic` extras bundle detected.. Installing via `poetry install -E semantic`'
            poetry install -E semantic
          else
            .config/log info '{{.POETRY_MANUAL_SETUP_MSG}}'
            poetry add pip@latest setuptools@latest twine@latest wheel@latest
          fi
        else
          if [ -f venv/bin/activate ]; then
            .config/log info 'Using existing virtualenv found at `venv/`'
            . venv/bin/activate
          elif [ -f .venv/bin/activate ]; then
            .config/log info 'Using existing virtualenv found at `.venv/`'
            . .venv/bin/activate
          else
            .config/log info 'No virtualenv detected.. Creating virtualenv in `.venv/`'
            python3 -m venv .venv
            . .venv/bin/activate
          fi
          .config/log info 'Installing Python dependencies into virtualenv'
          pip3 install -U pip setuptools
          pip3 install -r "$REQUIREMENTS_PATH"
        fi
    sources:
      - lib/requirements.txt
