name: Python package

on: [push, pull_request]

jobs:
  code-qc:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v4
      with:
        python-version: "3.10"
    - name: Lint with flake8 rinterface
      run: |
        python -m pip install flake8
        cd rpy2-rinterface
        ../scripts/run_linting_rinterface.sh
    - name: Lint with flake8 robjects
      run: |
        python -m pip install flake8
        cd rpy2-robjects
        ../scripts/run_linting_robjects.sh
    - name: Set up R ${{ matrix.r-version }}
      uses: r-lib/actions/setup-r@v2
      with:
        r-version: release
    - name: Check type hints rinterface
      run: |
        cd rpy2-rinterface
        python -m pip install '.[types]'
        ../scripts/run_mypy_rinterface.sh
    - name: Check type hints robjects
      run: |
        cd rpy2-rinterface
        python -m pip install .
        cd ..
        cd rpy2-robjects
        python -m pip install --pre '.[types]'
        ../scripts/run_mypy_robjects.sh
  build-sdist:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v4
      with:
        python-version: "3.10"
    - name: Set up R ${{ matrix.r-version }}
      uses: r-lib/actions/setup-r@v2
      with:
        r-version: release
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        python -m pip install setuptools build
    - name: Build sdist rinterface
      run: |
        cd rpy2-rinterface
        python -m build -o dist/ --sdist .
    - name: Upload sdist rinterface
      uses: actions/upload-artifact@v4
      with:
        name: rpy2-rinterface-sdist
        path: rpy2-rinterface/dist/rpy2_rinterface-*.tar.gz
    - name: Build sdist robjects
      run: |
        cd rpy2-robjects
        python -m build -o dist/ --sdist .
    - name: Upload sdist robjects
      uses: actions/upload-artifact@v4
      with:
        name: rpy2-robjects-sdist
        path: rpy2-robjects/dist/rpy2_robjects-*.tar.gz
    - name: Build sdist rpy2
      run: |
        python -m build -o dist/ --sdist .
    - name: Upload sdist rpy2
      uses: actions/upload-artifact@v4
      with:
        name: rpy2-sdist
        path: dist/rpy2-*.tar.gz
  build-api-wheel:
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        python-version: [3.8, 3.9, "3.10", "3.11", "3.12", "3.13"]
        r-version: ["4.4", release]
        os: ["windows-latest", ubuntu-latest, macos-15]
    steps:
    - uses: actions/checkout@v3
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v4
      with:
        python-version: ${{ matrix.python-version }}
    - name: Set up R ${{ matrix.r-version }}
      uses: r-lib/actions/setup-r@v2
      with:
        r-version: ${{ matrix.r-version }}
        use-public-rspm: true
    - name: Print R config (POSIX-only)
      if: "!startsWith(matrix.os, 'windows')"
      run: R CMD config --all
    - name: Set env commands (POSIX)
      if: "!startsWith(matrix.os, 'windows')"
      run: |
        echo "VENV_ACTIVATE=source pyenv_base/bin/activate" >> $GITHUB_ENV
        echo "R_LIBRARY=export LD_LIBRARY_PATH=$(python -m rpy2.situation LD_LIBRARY_PATH):${LD_LIBRARY_PATH}" >> $GITHUB_ENV
    - name: Set virtualenv activation command (Windows)
      if: startsWith(matrix.os, 'windows')
      shell: bash
      run: |
        echo "VENV_ACTIVATE=pyenv_base\Scripts\activate" >> $GITHUB_ENV
        echo "R_LIBRARY=''" >> $GITHUB_ENV
    - name: Patch GHA issue with macos - Missing C library
      if: startsWith(matrix.os, 'macos')
      shell: bash
      run: |
        LIB_ARCHIVES=(\
          libdeflate-1.23-darwin.20-arm64.tar.xz \
          zstd-1.5.5-darwin.20-arm64.tar.xz \
        )
        for name in "${LIB_ARCHIVES[@]}"
        do
            curl -LO https://mac.r-project.org/bin/darwin20/arm64/"${name}"
            sudo tar xJf "${name}" -C /
        done
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        python -m pip install setuptools build
    - name: Build wheel rinterface (Windows)
      shell: bash
      if: startsWith(matrix.os, 'windows')
      run: |
        cd rpy2-rinterface
        echo '[build]' > setup.cfg
        echo 'compiler = mingw32' >> setup.cfg
        export RPY2_API_FORCE=True
        python -m build -C--global-option=--verbose -o dist/ --wheel .
    - name: Build wheel rinterface (non-Windows)
      if: "!startsWith(matrix.os, 'windows')"
      run: |
        cd rpy2-rinterface
        python -m build -o dist/ --wheel .
    - uses: actions/upload-artifact@v4
      with:
        name: rpy2-rinterface-${{ matrix.os }}-py${{ matrix.python-version }}-r${{ matrix.r-version }}
        path: rpy2-rinterface/dist/rpy2_rinterface-*.whl
  build-noabi-wheel:
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        python-version: ["3.10"]
        r-version: [release]
        os: ["windows-latest", ubuntu-latest, macos-15]
        package: [{dir: ".", name: "rpy2"}, {dir: "rpy2-robjects", name: "rpy2-robjects"}]
    steps:
    - uses: actions/checkout@v3
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v4
      with:
        python-version: ${{ matrix.python-version }}
    - name: Set up R ${{ matrix.r-version }}
      uses: r-lib/actions/setup-r@v2
      with:
        r-version: ${{ matrix.r-version }}
        use-public-rspm: true
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        python -m pip install setuptools build
    - name: Build wheel
      run: |
        cd "${{ matrix.package.dir }}"
        python -m build -o dist/ --wheel .
    - uses: actions/upload-artifact@v4
      with:
        name: ${{ matrix.package.name }}-${{ matrix.os }}
        path: ${{ matrix.package.dir }}/dist/*.whl
  test:
    needs: [build-api-wheel, build-noabi-wheel]
    strategy:
      fail-fast: false
      matrix:
        python-version: [3.8, 3.9, "3.10", "3.11", "3.12", "3.13"]
        r-version: ["4.4", release]
        os: ["windows-latest", ubuntu-latest, macos-15]
    uses: ./.github/workflows/test.yml
    with:
      os: ${{ matrix.os }}
      python-version: ${{ matrix.python-version }}
      r-version: ${{ matrix.r-version }}
