name: Reference Python Package

on:
  [push, pull_request]

jobs:
  build:
      uses: ./.github/workflows/build-ast.yml

  tests:
    name: ANTLR AST tests
    needs: build
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        # Just using minimum and maximum to avoid exploding the matrix.
        python-version: ['3.7', '3.12']
        antlr-version: ['4.7', '4.13']
    defaults:
      run:
        working-directory: source/openqasm

    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.python-version }}

      - uses: actions/download-artifact@v4
        with:
          name: openqasm3-python-wheel
          path: ./source/openqasm/

      - name: Install package
        run: |
          set -e
          pip install --upgrade pip wheel
          pip install -r requirements-dev.txt
          pip install 'antlr4_python3_runtime==${{ matrix.antlr-version }}'
          pip install "$(echo openqasm3-*.whl)[all]"

      - name: Run tests
        run: |
          # Swap into the testing directory so the imported `openqasm3` is the
          # wheel version not the current-directory version.  The
          # `--import-mode=importlib` stops pytest from modifying the path to
          # accidentally put the checked-out version (with no ANTLR) back.
          cd tests
          pytest -vv --color=yes --import-mode=importlib .

  style:
    name: Check AST style
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: source/openqasm
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - uses: actions/setup-python@v5
        with:
          python-version: '3.12'

      - name: Prepare environment
        run: |
          set -e
          python -m pip install --upgrade pip wheel
          python -m pip install -r requirements-dev.txt

      - name: Check formatting
        run: black --check --diff openqasm3 tests

      - name: Check lint
        run: pylint openqasm3 tests

  docs:
    name: AST documentation build
    needs: build
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: source/openqasm

    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.python-version }}

      - uses: actions/download-artifact@v4
        with:
          name: openqasm3-python-wheel
          path: ./source/openqasm/

      - name: Install package
        run: |
          set -e
          pip install --upgrade pip wheel
          pip install -r requirements-dev.txt
          pip install "$(echo openqasm3-*.whl)[all]"

      - name: Build documentation
        working-directory: source/openqasm/docs
        run: make html

      - uses: actions/upload-artifact@v4
        with:
          name: openqasm3-html-documentation
          path: source/openqasm/docs/_build/html/*
          if-no-files-found: error
