# 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 ]

jobs:

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

    steps:
      - uses: actions/checkout@v4
      - name: Set up Python 
        uses: actions/setup-python@v5
        with:
          python-version: 3.8
      
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          make dev
          make requirements
          pip install cibuildwheel
      
      # Update to only do push to pypi after succesfully running build-test.yml... instead of doing the tests here?
      - name: Install
        run: |
          make install
      
      - name: Test with pytest
        run: |
          make test
      
      # build source distribution for versions wheels arent build. 
      # https://setuptools.pypa.io/en/latest/userguide/quickstart.html#install-build
      - name: Build source distribution
        run: |
          make build
        # python setup.py sdist -d wheelhouse

      
      - uses: actions/upload-artifact@v4
        with:
          name: cibw-sdist
          path: ./wheelhouse/*.tar.gz

  #BUILD WHEELS ONLY NEEDED FOR NON-PURE PYTHON PACKAGES
  #This is described here: https://packaging.python.org/en/latest/overview/#python-binary-distributions
  build_wheels:
    name: Build wheels
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        # macos-13 = intel mac runner
        # macos-14 = apple silicon mac runner
        os: [ubuntu-latest, windows-2019, macos-13]
        # add macos-14 when apple silicon mac runners are available
        # for all of the python versions. Seems like only 3.10 + is 
        # available now, but is intended to be fixed: 
        # https://github.com/actions/setup-python/issues/808
    
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-python@v5
        name: Install Python
        with:
          python-version: '3.8'
      
      - name: Install cibuildwheel
        run: python -m pip install cibuildwheel==2.7.0

      - 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.PYMSKT_TOKEN }}
          # password: ${{ secrets.PYMSKT_TESTPYPI_TOKEN }}
          # COMMENT BELOW OUT - DEFAULT PUSH TO pypi now!
          # repository_url: https://test.pypi.org/legacy/