name: Deploy

on:
  push:
    tags:
      - 'v*'

env:
  PROJECT_NAME: itaxotools-hapsolutely
  ARTIFACT_DISTRIBUTIONS: itaxotools-hapsolutely-distributions
  ARTIFACT_EXECUTABLE: itaxotools-hapsolutely-windows
  ARTIFACT_BUNDLE: itaxotools-hapsolutely-macos


jobs:

  build:
    name: Build distributions
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Build distributions
        run: pipx run build

      - uses: actions/upload-artifact@v3
        with:
          name: ${{ env.ARTIFACT_DISTRIBUTIONS }}
          path: dist/*


  windows:
    name: Build Windows executable
    runs-on: windows-2019

    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: 3.x

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -r requirements.txt
          pip install ".[dev]"

      - name: Test with pytest
        run: |
          pip install pytest pytest-qt pytest-xvfb
          pytest

      - name: Make executable
        uses: iTaxoTools/TaxIGui/.github/actions/make-executable@main

      - uses: actions/upload-artifact@v3
        with:
          name: ${{ env.ARTIFACT_EXECUTABLE }}
          path: dist/*.exe


  macos:
    name: Build macOS bundle
    runs-on: macos-11

    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: 3.x

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -r requirements.txt
          pip install ".[dev]"

      - name: Install macOS wheels
        uses: iTaxoTools/TaxIGui/.github/actions/install-macos-wheels@main

      - name: Build bundle
        uses: iTaxoTools/TaxIGui/.github/actions/make-bundle@main

      - uses: actions/upload-artifact@v3
        with:
          name: ${{ env.ARTIFACT_BUNDLE }}
          path: dist/*.zip


  publish-to-testpypi:
    name: Publish to TestPyPI
    runs-on: ubuntu-latest
    needs:
      - build
      - windows
      - macos

    environment:
      name: testpypi
      url: https://test.pypi.org/p/${{ env.PROJECT_NAME }}

    permissions:
      id-token: write

    steps:
      - name: Download all the dists
        uses: actions/download-artifact@v3
        with:
          name: ${{ env.ARTIFACT_DISTRIBUTIONS }}
          path: dist

      - uses: pypa/gh-action-pypi-publish@release/v1
        with:
          repository-url: https://test.pypi.org/legacy/
          skip-existing: true


  publish-to-pypi:
    name: Publish to PyPI
    runs-on: ubuntu-latest
    needs:
      - publish-to-testpypi

    environment:
      name: pypi
      url: https://pypi.org/p/${{ env.PROJECT_NAME }}

    permissions:
      id-token: write

    steps:
      - name: Download all the dists
        uses: actions/download-artifact@v3
        with:
          name: ${{ env.ARTIFACT_DISTRIBUTIONS }}
          path: dist

      - uses: pypa/gh-action-pypi-publish@release/v1
        with:
          skip-existing: true


  draft-github-release:
    name: Draft a GitHub Release
    runs-on: ubuntu-latest
    needs:
      - publish-to-testpypi

    environment:
      name: github
      url: ${{ github.server_url }}/${{ github.repository }}/releases

    permissions:
      contents: write
      id-token: write

    steps:
      - name: Download all the dists
        uses: actions/download-artifact@v3
        with:
          name: ${{ env.ARTIFACT_DISTRIBUTIONS }}
          path: dist

      - name: Download Windows executable
        uses: actions/download-artifact@v3
        with:
          name: ${{ env.ARTIFACT_EXECUTABLE }}
          path: dist

      - name: Download macOS bundle
        uses: actions/download-artifact@v3
        with:
          name: ${{ env.ARTIFACT_BUNDLE }}
          path: dist

      - name: Create GitHub Release
        env:
          GITHUB_TOKEN: ${{ github.token }}
        run: >-
          gh release create
          '${{ github.ref_name }}'
          --repo '${{ github.repository }}'
          --generate-notes
          --draft

      - name: Upload artifacts to GitHub Release
        env:
          GITHUB_TOKEN: ${{ github.token }}
        run: >-
          gh release upload
          '${{ github.ref_name }}' dist/**
          --repo '${{ github.repository }}'
