# This file is managed by 'repo_helper'. Don't edit it directly.
---
name: Linux

on:
  push:
    branches-ignore:
      - 'repo-helper-update'
      - 'pre-commit-ci-update-config'
      - 'imgbot'
    tags:
      - '*'
  pull_request:

jobs:
  tests:
    permissions:
      actions: write
      contents: read
    name: "ubuntu-22.04 / Python ${{ matrix.config.python-version }}"
    runs-on: "ubuntu-22.04"
    continue-on-error: ${{ matrix.config.experimental }}
    env:
      USING_COVERAGE: '3.7,3.8,3.9,3.10,3.11,3.12,3.13,pypy-3.7,pypy-3.8,pypy-3.9'

    strategy:
      fail-fast: False
      matrix:
        config:
          - {python-version: "3.7", testenvs: "py37-sphinx{3.2,3.3,3.4,3.5,4.0,4.1,4.2,4.3,4.4,4.5,5.0,5.1,5.2,5.3},build", experimental: False}
          - {python-version: "3.8", testenvs: "py38-sphinx{3.2,3.3,3.4,3.5,4.0,4.1,4.2,4.3,4.4,4.5,5.0,5.1,5.2,5.3,6.0,6.1,6.2,7.0,7.1},build", experimental: False}
          - {python-version: "3.9", testenvs: "py39-sphinx{3.2,3.3,3.4,3.5,4.0,4.1,4.2,4.3,4.4,4.5,5.0,5.1,5.2,5.3,6.0,6.1,6.2,7.0,7.1,7.2,7.3,7.4},build", experimental: False}
          - {python-version: "3.10", testenvs: "py310-sphinx{3.2,3.3,3.4,3.5,4.0,4.1,4.2,4.3,4.4,4.5,5.0,5.1,5.2,5.3,6.0,6.1,6.2,7.0,7.1,7.2,7.3,7.4,8.0,8.1},build", experimental: False}
          - {python-version: "3.11", testenvs: "py311-sphinx{3.2,3.3,3.4,3.5,4.0,4.1,4.2,4.3,4.4,4.5,5.0,5.1,5.2,5.3,6.0,6.1,6.2,7.0,7.1,7.2,7.3,7.4,8.0,8.1,8.2},build", experimental: False}
          - {python-version: "3.12", testenvs: "py312-sphinx{4.4,4.5,5.0,5.1,5.2,5.3,6.0,6.1,6.2,7.0,7.1,7.2,7.3,7.4,8.0,8.1,8.2},build", experimental: False}
          - {python-version: "3.13", testenvs: "py313-sphinx{6.2,7.0,7.1,7.2,7.3,7.4,8.0,8.1,8.2},build", experimental: False}
          - {python-version: "pypy-3.7", testenvs: "pypy37-sphinx{4.3,4.4,4.5,5.0,5.1,5.2,5.3},build", experimental: False}
          - {python-version: "pypy-3.8", testenvs: "pypy38-sphinx{4.3,4.4,4.5,5.0,5.1,5.2,5.3,6.0,6.1,6.2,7.0,7.1},build", experimental: False}
          - {python-version: "pypy-3.9", testenvs: "pypy39-sphinx{4.3,4.4,4.5,5.0,5.1,5.2,5.3,6.0,6.1,6.2,7.0,7.1,7.2,7.3,7.4}", experimental: True}

    steps:
      - name: Checkout 🛎️
        uses: "actions/checkout@v4"

      - name: Check for changed files
        if: startsWith(github.ref, 'refs/tags/') != true
        uses: dorny/paths-filter@v2
        id: changes
        with:
          list-files: "json"
          filters: |
            code:
              - '!(doc-source/**|CONTRIBUTING.rst|.imgbotconfig|.pre-commit-config.yaml|.pylintrc|.readthedocs.yml)'

      - name: Setup Python 🐍
        id: setup-python
        if: ${{ steps.changes.outputs.code == 'true' || steps.changes.outcome == 'skipped' }}
        uses: "actions/setup-python@v5"
        with:
          python-version: "${{ matrix.config.python-version }}"

      - name: Install dependencies 🔧
        if: steps.setup-python.outcome == 'success'
        run: |
          python -VV
          python -m site
          python -m pip install --upgrade pip setuptools wheel
          python -m pip install --upgrade tox~=3.0 virtualenv!=20.16.0
          python -m pip install --upgrade coverage_pyver_pragma

      - name: "Run Tests for Python ${{ matrix.config.python-version }}"
        if: steps.setup-python.outcome == 'success'
        run: python -m tox -e "${{ matrix.config.testenvs }}" -s false

      - name: "Upload Coverage 🚀"
        uses: actions/upload-artifact@v4
        if: ${{ always() && steps.setup-python.outcome == 'success' }}
        with:
          name: "coverage-${{ matrix.config.python-version }}"
          path: .coverage
          include-hidden-files: true


  Coverage:
    needs: tests
    permissions:
      actions: write
      contents: read
    runs-on: "ubuntu-22.04"
    steps:
      - name: Checkout 🛎️
        uses: "actions/checkout@v4"

      - name: Setup Python 🐍
        uses: "actions/setup-python@v5"
        with:
          python-version: 3.8

      - name: Install dependencies 🔧
        run: |
          python -m pip install --upgrade pip setuptools wheel
          python -m pip install --upgrade "coveralls>=3.0.0" coverage_pyver_pragma

      - name: "Download Coverage 🪂"
        uses: actions/download-artifact@v4
        with:
          path: coverage

      - name: Display structure of downloaded files
        id: show
        run: ls -R
        working-directory: coverage
        continue-on-error: true

      - name: Combine Coverage 👷
        if: ${{ steps.show.outcome != 'failure' }}
        run: |
          shopt -s globstar
          python -m coverage combine coverage/**/.coverage

      - name: "Upload Combined Coverage Artefact 🚀"
        if: ${{ steps.show.outcome != 'failure' }}
        uses: actions/upload-artifact@v4
        with:
          name: "combined-coverage"
          path: .coverage
          include-hidden-files: true

      - name: "Upload Combined Coverage to Coveralls"
        if: ${{ steps.show.outcome != 'failure' }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          coveralls --service=github

  Deploy:
    needs: tests
    permissions:
      actions: write
      issues: write
      contents: read
    runs-on: "ubuntu-22.04"
    steps:
      - name: Checkout 🛎️
        uses: "actions/checkout@v4"
        if: startsWith(github.ref, 'refs/tags/')

      - name: Setup Python 🐍
        uses: "actions/setup-python@v5"
        if: startsWith(github.ref, 'refs/tags/')
        with:
          python-version: 3.8

      - name: Install dependencies 🔧
        if: startsWith(github.ref, 'refs/tags/')
        run: |
          python -m pip install --upgrade pip setuptools wheel
          python -m pip install --upgrade tox~=3.0

      - name: Build distributions 📦
        if: startsWith(github.ref, 'refs/tags/')
        run: |
          tox -e build


      - name: Upload distribution to PyPI 🚀
        if: startsWith(github.ref, 'refs/tags/')
        uses: pypa/gh-action-pypi-publish@v1.13.0
        with:
          user: __token__
          password: ${{ secrets.PYPI_TOKEN }}
          skip-existing: true

      - name: Close milestone 🚪
        if: startsWith(github.ref, 'refs/tags/')
        run: |
          python -m pip install --upgrade github3.py packaging
          python .github/milestones.py
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}



  Conda:
    needs: deploy
    permissions:
      contents: read
    runs-on: ubuntu-22.04
    if: startsWith(github.ref, 'refs/tags/') || (startsWith(github.event.head_commit.message, 'Bump version') != true)
    steps:
      - name: Checkout 🛎️
        uses: "actions/checkout@v4"

      - name: Setup Python 🐍
        uses: "actions/setup-python@v5"
        with:
          python-version: 3.11

      - name: Setup Conda
        uses: conda-incubator/setup-miniconda@v2.1.1
        with:
          activate-environment: env
          conda-build-version: 3.28.4
          miniconda-version: py311_24.1.2-0
          python-version: "3.11"
          miniforge-variant: Mambaforge

      - name: Install dependencies 🔧
        run: |
          python -VV
          python -m site
          python -m pip install --upgrade pip setuptools wheel
          python -m pip install --upgrade "mkrecipe" "whey"
          # $CONDA is an environment variable pointing to the root of the miniconda directory
          $CONDA/bin/conda config --set always_yes yes --set changeps1 no
          $CONDA/bin/conda update -n base conda
          $CONDA/bin/conda info -a
          $CONDA/bin/conda install conda-forge::py-lief=0.14.1
          $CONDA/bin/conda config --add channels conda-forge
          $CONDA/bin/conda config --add channels domdfcoding

          $CONDA/bin/conda config --remove channels defaults

      - name: Build Conda Package 📦
        run: |
          python -m mkrecipe --type wheel || exit 1
          $CONDA/bin/conda build conda -c conda-forge -c domdfcoding --output-folder conda/dist

      - name: Deploy Conda Package 🚀
        if: startsWith(github.ref, 'refs/tags/')
        run: |
          $CONDA/bin/conda config --set always_yes yes --set changeps1 no
          $CONDA/bin/conda install anaconda-client
          $CONDA/bin/conda info -a

          for f in conda/dist/noarch/sphinx-toolbox-*.tar.bz2; do
            [ -e "$f" ] || continue
            echo "$f"
            conda install "$f" || exit 1
            echo "Deploying to Anaconda.org..."
            $CONDA/bin/anaconda -t "$ANACONDA_TOKEN" upload "$f" || exit 1
            echo "Successfully deployed to Anaconda.org."
          done
        env:
          ANACONDA_TOKEN: ${{ secrets.ANACONDA_TOKEN }}
