# This workflow will install Python dependencies, run tests and lint with a variety of Python versions
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions

name: pypi

on:
  push:
    branches: [ main ]
  # pull_request:
  #   branches: [ main ]

jobs:

  build_sdist:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false

    steps:
      - uses: actions/checkout@v2
      - name: Set up Python 
        uses: actions/setup-python@v2
        with:
          python-version: 3.7
      
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          make dev
          make requirements
          pip install cibuildwheel
      
      - name: Install Cython code
        run: |
          make build-cython
      
      - name: Test with pytest
        run: |
          make test
      
      # build source distribution for versions wheels arent build. 
      - name: Build source distribution
        run: |
          python -m build
      
      - uses: actions/upload-artifact@v4
        with:
          name: cibw-sdist
          path: ./dist/*.tar.gz
    
  build_wheels:
    name: Build wheels on ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        # macos-13 = intel macos, macos-14 = arm macos
        os: [ubuntu-latest, windows-2019, macos-13, macos-14]     
    
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-python@v3
      
      - name: Install cibuildwheel
        run: python -m pip install cibuildwheel==2.17

      - name: Build wheels
        run: python -m cibuildwheel --output-dir wheelhouse

      # - name: Build wheels
      #   uses: pypa/cibuildwheel@v2.2.2
      
      - uses: actions/upload-artifact@v4
        with:
            name: cibw-wheels-${{ matrix.os }}-${{ strategy.job-index }}
            path: ./wheelhouse/*.whl
  
  upload_pypi:
    needs: [build_sdist, build_wheels]
    runs-on: ubuntu-latest

    steps:
    # https://cibuildwheel.pypa.io/en/stable/deliver-to-pypi/
      - uses: actions/download-artifact@v4
        with:
          pattern: cibw-*
          path: dist
          merge-multiple: true
      
      - uses: pypa/gh-action-pypi-publish@v1.4.2
        with:
          user: __token__
          password: ${{ secrets.CYCPD_PYPI_TOKEN }}
          # COMMENT BELOW OUT - DEFAULT PUSH TO pypi now!
          # repository_url: https://test.pypi.org/legacy/
