name: CI

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

jobs:
  build_sdist:
    name: Build sdist & documentation
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: '3.12'
          cache: 'pip'
      - run: python -m pip install --upgrade pip build twine
      - run: python -m build --sdist
      - run: python -m twine check dist/*
      # Build documentation
      - run: sudo apt-get install pandoc
      - run: pip install "$(ls ./dist/hdf5plugin-*.tar.gz)[doc]"
        env:
          HDF5PLUGIN_STRIP: all  # Do not build the filters
      - run: sphinx-build --fail-on-warning --keep-going -b html doc/ ./html

  build_test:
    name: ${{ matrix.cibw_build }} / ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: ubuntu-20.04
            cibw_build: "cp38-manylinux_x86_64"
          - os: windows-2019
            cibw_build: "cp38-win_amd64"
          - os: macos-13
            cibw_build: "cp38-macosx_x86_64"
          - os: ubuntu-latest
            cibw_build: "cp310-manylinux_x86_64"
          - os: windows-latest
            cibw_build: "cp311-win_amd64"
          - os: macos-latest
            cibw_build: "cp312-macosx_arm64"

    steps:
      - uses: actions/checkout@v4
      - uses: pypa/cibuildwheel@v2.23.0
        env:
          MACOSX_DEPLOYMENT_TARGET: "10.13"
          CIBW_BUILD_VERBOSITY: 1
          CIBW_BUILD: ${{ matrix.cibw_build }}
          CIBW_TEST_EXTRAS: "test"
          # Test twice: with latest version and with numpy v1 and oldest supported h5py
          CIBW_TEST_COMMAND: >
            python {project}/test/test.py &&
            pip install -r {project}/ci/oldest_h5py.txt &&
            python {project}/test/test.py
