name: Package for PyPI

on: [push]

defaults:
  run:
    shell: bash

jobs:
  pypi-sdist:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v3
    - uses: actions/setup-python@v4
      with:
        python-version: 3.8
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        python -m pip install --upgrade setuptools
        pip install --upgrade -r requirements.txt
    - name: Run tests
      timeout-minutes: 30
      run: |
        python setup.py test
    - name: Build Python source distribution
      run: |
        python setup.py clean sdist
    - name: Upload artifacts
      uses: actions/upload-artifact@v3
      with:
        name: picard-sdist
        path: dist/*
    - name: Publish Python distribution to PyPI
      if: startsWith(github.ref, 'refs/tags/')
      run: |
        pip install --upgrade twine
        twine upload --non-interactive dist/*
      env:
        TWINE_USERNAME: __token__
        TWINE_PASSWORD: ${{ secrets.PYPI_UPLOAD_TOKEN }}

  pypi-bdist:
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [macos-11, windows-2019]
        python-version: ['3.7', '3.8', '3.9', '3.10', '3.11']
    steps:
    - uses: actions/checkout@v3
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v4
      with:
        python-version: ${{ matrix.python-version }}
    - name: Install gettext (macOS)
      if: runner.os == 'macOS'
      run: |
        brew install gettext
        brew link gettext --force
        echo "/usr/local/opt/gettext/bin" >> $GITHUB_PATH
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        python -m pip install --upgrade setuptools wheel
        pip install --upgrade -r requirements.txt
    - name: Run tests
      timeout-minutes: 30
      run: |
        python setup.py test
    - name: Build Python binary distribution
      run: |
        python setup.py clean bdist_wheel
    - name: Upload artifacts
      uses: actions/upload-artifact@v3
      with:
        name: picard-bdist-${{ runner.os }}
        path: dist/*.whl
    - name: Publish Python distribution to PyPI
      if: startsWith(github.ref, 'refs/tags/')
      run: |
        pip install --upgrade twine>=3.0
        twine upload --non-interactive dist/*
      env:
        TWINE_USERNAME: __token__
        TWINE_PASSWORD: ${{ secrets.PYPI_UPLOAD_TOKEN }}
