name: Build wheel and publish

on: 
  release:
    types: [published]
  workflow_dispatch:

env:
  CIBW_BUILD: "cp39-* cp310-* cp311-* cp312-*"
  CIBW_ARCHS_LINUX: "x86_64"
  CIBW_SKIP: "*-win32 *musllinux*"
  CIBW_MANYLINUX_X86_64_IMAGE: manylinux2014
  CIBW_BEFORE_BUILD: pip install numpy --config-settings=setup-args="-Dallow-noblas=true"
  CIBW_BUILD_VERBOSITY: "1"

jobs:
  build-wheels-and-dist:
    name: Build ${{ matrix.python-version }} wheels on ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest]
        python-version: [3.9, "3.10", "3.11", "3.12"]

    steps:
    - uses: actions/checkout@v4
      with:
        fetch-depth: 0
    - uses: actions/setup-python@v5
      name: Install Python
      with:
        python-version: "3.10"

    - name: link gfortran and hdf5
      if: runner.os == 'macOS'
      run: |
        sudo ln -s /opt/homebrew/bin/gfortran-12 /usr/local/bin/gfortran
        brew reinstall hdf5
        
    - name: Install numpy
      run: |
        python -m pip install --upgrade pip
        python -m pip install numpy h5py versioneer 

    - name: Install dependencies
      run: |
        python -m pip install setuptools wheel build meson-python ninja meson pytest cibuildwheel

    - name: Build source and wheels distribution on MacOS
      if: runner.os == 'macOS'
      run: |
        python -m build --outdir dist
        find ./dist/*.whl | xargs pip install
        pytest src/cosmic
        
    - name: Build source and wheels distribution on Linux
      if: runner.os == 'Linux'
      run: |
        python -m build --sdist --outdir dist
        python -m cibuildwheel --output-dir dist
        
    - uses: actions/upload-artifact@v4
      with:
        name: dist-${{ matrix.os }}-${{ matrix.python-version }}
        path: ./dist/*

  publish:
    needs: [build-wheels-and-dist]
    name: Publish Python 🐍 distributions 📦 to PyPI and TestPyPI
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest]
        python-version: ["3.10"]

    steps:
    - uses: actions/checkout@v4
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v5
      with:
        python-version: ${{ matrix.python-version }}

    - name: Download artifacts
      uses: actions/download-artifact@v4
      with:
        path: artifacts

    - name: Collect distributions
      run: |
        mkdir -p dist
        find artifacts -type f -name '*.whl' -exec cp {} dist/ \;
        find artifacts -type f -name '*.tar.gz' -exec cp {} dist/ \;

    - name: Publish distribution 📦 to PyPI
      uses: pypa/gh-action-pypi-publish@release/v1
      with:
        password: ${{ secrets.pypi_password }}
