name: Build, test and create release packages.

on:
  workflow_dispatch:
  pull_request:
  push:
    tags:
      - "[0-9]+.[0-9]+.[0-9]+"
      - "[0-9]+.[0-9]+.[0-9]+rc[0-9]+"
      - "[0-9]+.[0-9]+.[0-9]+dev[0-9]+"

jobs:

  test_linux:
    name: Test (${{ matrix.os }}, ${{ matrix.python-version }})
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os:
          - ubuntu-latest
        python-version:
          - 3.8
          - 3.9
          - "3.10"
          - 3.11
          - 3.12
          - 3.13
          - 3.14-dev
          - pypy-3.8
          - pypy-3.9
          - pypy-3.10
    steps:
    - name: Checkout code
      uses: actions/checkout@v3
    - name: Setup Python ${{ matrix.python-version }}
      uses: actions/setup-python@v4
      with:
        python-version: ${{ matrix.python-version }}
    - name: Update pip
      run: python -m pip install -U pip wheel setuptools
    - name: Install tox
      run: python -m pip install "tox<4.0.0" "tox-gh-actions<3.0.0"
    - name: Test with tox
      run: python -m tox
    - name: Store partial coverage reports
      uses: actions/upload-artifact@v4
      with:
        name: coverage
        path: .coverage.*

  test_linux_no_gil:
    name: Test - NO GIL (${{ matrix.os }}, ${{ matrix.python-version }})
    runs-on: ubuntu-latest
    strategy:
      matrix:
        os:
          - ubuntu-latest
        python-version:
          - 3.13
          - 3.14-dev
    steps:
    - name: Checkout code
      uses: actions/checkout@v2
    - name: Setup Python ${{ matrix.python-version }}
      uses: deadsnakes/action@v3.2.0
      with:
        python-version: ${{ matrix.python-version }}
        nogil: true
    - name: Update pip
      run: python -m pip install -U pip wheel setuptools
    - name: Install tox
      run: python -m pip install "tox<4.0.0" "tox-gh-actions<3.0.0"
    - name: Test with tox
      run: python -m tox
    - name: Store partial coverage reports
      uses: actions/upload-artifact@v4
      with:
        name: coverage
        path: .coverage.*

  # test_aarch64_linux:
  #   name: Test (${{ matrix.python.os }}, ${{ matrix.python.python-version }}, aarch64)
  #   runs-on: ${{ matrix.python.os }}
  #   strategy:
  #     matrix:
  #       python:
  #         #- {os: ubuntu-latest,  python-version: 3.7, pyver: py37}
  #         #- {os: ubuntu-latest,  python-version: 3.8, pyver: py38}
  #         #- {os: ubuntu-latest,  python-version: 3.9, pyver: py39}
  #         - {os: ubuntu-latest,  python-version: "3.10", pyver: py310}
  #   env:
  #     py: python${{ matrix.python.python-version }}
  #   steps:
  #   - name: Checkout
  #     uses: actions/checkout@v2
  #   - name: Set up QEMU
  #     id: qemu
  #     uses: docker/setup-qemu-action@v1
  #   - name: Test with tox
  #     run: |
  #           docker run --rm -v ${{ github.workspace }}:/io:rw --workdir=/io \
  #           arm64v8/ubuntu \
  #           bash -exc 'apt-get update && \
  #           apt install software-properties-common -y && \
  #           add-apt-repository ppa:deadsnakes/ppa -y && \
  #           apt install -y ${{ env.py }} && \
  #           apt install -y ${{ env.py }}-venv && \
  #           ${{ env.py }} -m venv .env && \
  #           source .env/bin/activate && \
  #           pip install -U pip wheel setuptools && \
  #           pip install "tox<4.0.0" "tox-gh-actions<3.0.0" && \
  #           tox -e ${{ matrix.python.pyver }} && \
  #           deactivate'

  test_macos:
    name: Test (${{ matrix.os }}, ${{ matrix.python-version }})
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os:
          - macos-latest
        python-version:
          - 3.8
          - 3.9
          - "3.10"
          - 3.11
          - 3.12
          - 3.13
          - 3.14-dev
          - pypy-3.8
          - pypy-3.9
          - pypy-3.10
    steps:
    - name: Checkout code
      uses: actions/checkout@v3
    - name: Setup Python ${{ matrix.python-version }}
      uses: actions/setup-python@v4
      with:
        python-version: ${{ matrix.python-version }}
    - name: Update pip
      run: python -m pip install -U pip wheel setuptools
    - name: Install tox
      run: python -m pip install "tox<4.0.0" "tox-gh-actions<3.0.0"
    - name: Test with tox
      run: python -m tox
    - name: Store partial coverage reports
      uses: actions/upload-artifact@v4
      with:
        name: coverage
        path: .coverage.*

  test_windows:
    name: Test (${{ matrix.os }}, ${{ matrix.python-version }})
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os:
          - windows-latest
          - windows-11-arm
        python-version:
          - 3.8
          - 3.9
          - "3.10"
          - 3.11
          - 3.12
          - 3.13
          - 3.14-dev
          - pypy-3.8
          - pypy-3.9
          - pypy-3.10
        exclude:
          # setup-python only support 3.11+ for windows arm
          - os: windows-11-arm
            python-version: 3.8
          - os: windows-11-arm
            python-version: 3.9
          - os: windows-11-arm
            python-version: "3.10"
          - os: windows-11-arm
            python-version: pypy-3.8
          - os: windows-11-arm
            python-version: pypy-3.9
          - os: windows-11-arm
            python-version: pypy-3.10
    steps:
    - name: Checkout code
      uses: actions/checkout@v3
    - name: Setup Python ${{ matrix.python-version }}
      uses: actions/setup-python@v4
      with:
        python-version: ${{ matrix.python-version }}
    - name: Update pip
      run: python -m pip install -U pip wheel setuptools
    - name: Install tox
      run: python -m pip install "tox<4.0.0" "tox-gh-actions<3.0.0"
    - name: Test with tox
      run: python -m tox

  sdist:
    name: Build source distribution
    needs:
    - test_linux
    - test_macos
    - test_windows
    runs-on: ubuntu-latest
    steps:
    - name: Checkout code
      uses: actions/checkout@v3
      with:
        submodules: true
    - name: Set up Python 3.9
      uses: actions/setup-python@v4
      with:
        python-version: 3.9
    - name: Build full source distribution as tar.gz
      run: python setup.py sdist
    - name: Install the wheel package
      run: pip install wheel
    - name: Build pure Python source wheel
      run: WRAPT_INSTALL_EXTENSIONS=false python setup.py bdist_wheel
    - name: Store built wheels
      uses: actions/upload-artifact@v4
      with:
        name: dist
        path: dist/*

  bdist_wheel:
    name: Build wheels (3.8+) on ${{ matrix.os }} for ${{ matrix.arch }}
    needs:
    - test_linux
    - test_linux_no_gil
    #- test_aarch64_linux
    - test_macos
    - test_windows
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest, windows-11-arm, macos-13, macos-14]
        arch: [auto]
        include:
          - os: ubuntu-latest
            arch: aarch64
          - os: macos-13
            arch: universal2
          - os: macos-14
            arch: universal2
    steps:
      - uses: actions/checkout@v3
      - name: Set up QEMU
        if: ${{ matrix.arch == 'aarch64' }}
        uses: docker/setup-qemu-action@v2
      - name: Build wheels
        uses: pypa/cibuildwheel@v3.1.1
        with:
          output-dir: dist
        env:
            WRAPT_INSTALL_EXTENSIONS: true
            CIBW_SKIP: pp*
            CIBW_BUILD_VERBOSITY: 1
            CIBW_ARCHS: ${{ matrix.arch }}
            CIBW_FREE_THREADED_SUPPORT: 1
      - uses: actions/upload-artifact@v4
        with:
          name: dist-${{ matrix.os }}-${{ matrix.arch }}
          path: dist/*.whl

  # coveralls:
  #   name: Generate code coverage report
  #   if: ${{ false }}  # disable for now
  #   needs:
  #   - test_linux
  #   - test_linux_no_gil
  #   - test_macos
  #   - test_windows
  #   runs-on: ubuntu-latest
  #   steps:
  #   - name: Checkout code
  #     uses: actions/checkout@v3
  #   - name: Setup Python 3.9
  #     uses: actions/setup-python@v4
  #     with:
  #       python-version: 3.9
  #   - name: Install coverage package
  #     run: python -m pip install -U coverage
  #   - name: Download partial coverage reports
  #     uses: actions/download-artifact@v3
  #     with:
  #       name: coverage
  #   - name: Combine coverage
  #     run: python -m coverage combine
  #   - name: Report coverage
  #     run: python -m coverage report
  #   - name: Export coverage to XML
  #     run: python -m coverage xml
  #   - name: Upload coverage statistics to Coveralls
  #     uses: AndreMiras/coveralls-python-action@develop
