name: CI

on:
  push:
  pull_request:
  workflow_dispatch:

jobs:
  build:
    strategy:
      matrix:
        platform: ["ubuntu-22.04", "macos-12", "windows-2022"]
    runs-on: ${{ matrix.platform }}
    steps:
      - uses: actions/checkout@v3
      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: |
            3.11
            3.10
            3.9
            3.8
      - name: Discover python
        run: |
          echo "PYTHONS=$(python .github/workflows/pydiscovery.py --min-version 3.8 --delimiter ' ' --root-dir ${{ runner.tool_cache }}/Python)" >> "$GITHUB_ENV"
      - name: Cache Rust target directory
        uses: actions/cache@v3
        with:
          key: ${{ runner.os }}
          path: |
            ~/.cargo  
            target
      - name: Run Cargo test
        if: "startsWith(matrix.platform, 'ubuntu')"
        run: cargo test --no-default-features --features pyo3/auto-initialize
      - uses: messense/maturin-action@v1
        env:
          MACOSX_DEPLOYMENT_TARGET: 10.14
        with:
          command: build
          args: >
            -o dist -i ${{ env.PYTHONS }}
            --release ${{ startsWith(matrix.platform, 'ubuntu') && '--sdist' || '' }}
            ${{ startsWith(matrix.platform, 'macos') && '--target universal2-apple-darwin' || '' }}
      - name: Run Python test
        if: "!startsWith(matrix.platform, 'windows')"
        run: |
          python -c "import subprocess, glob, os; file = glob.glob(os.path.join('dist', '*cp38*.whl'))[0]; subprocess.check_output(['pip', 'install', file])"
          pip install pytest
          mv rsoup rsoup2
          pytest -xs tests/
      - name: Upload wheels
        uses: actions/upload-artifact@v2
        with:
          name: wheels
          path: dist

  build-manylinux:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Cache Rust target directory
        uses: actions/cache@v3
        with:
          key: ${{ runner.os }}-manylinux
          path: target
      - name: Build wheels
        run: |
          docker run --rm -w /project -v $(pwd):/project \
            -e EXTRA_PATH=/opt/python/cp38-cp38/bin \
            -e PYTHON_HOME=/opt/python \
            -e CARGO_NET_GIT_FETCH_WITH_CLI=false \
            quay.io/pypa/manylinux2014_x86_64:latest \
            bash /project/.github/workflows/build.sh -t x86_64-unknown-linux-gnu
      - name: Upload wheels
        uses: actions/upload-artifact@v2
        with:
          name: wheels
          path: dist

  release:
    name: Release
    runs-on: ubuntu-latest
    if: startsWith(github.ref, 'refs/tags/') || startsWith(github.ref, 'refs/heads/master') || startsWith(github.ref, 'refs/heads/dev-ci')
    needs: [build, build-manylinux]
    steps:
      - uses: actions/download-artifact@v2
        with:
          name: wheels
      - name: Publish to PyPI
        uses: messense/maturin-action@v1
        env:
          MATURIN_PYPI_TOKEN: ${{ secrets.PYPI_API_TOKEN }}
        with:
          command: upload
          args: --skip-existing *
