# From: https://github.com/rkdarst/sphinx-actions-test/blob/master/.github/workflows/sphinx-build.yml

name: sphinx
on: [push, pull_request]

env:
  DEFAULT_BRANCH: "main"
  #SPHINXOPTS: "-W --keep-going -T"
  # ^-- If these SPHINXOPTS are enabled, then be strict about the builds and fail on any warnings

jobs:
  build-and-deploy:
    name: Build and gh-pages
    runs-on: ubuntu-latest
    steps:
      # https://github.com/marketplace/actions/checkout
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0
          lfs: true
      # https://github.com/marketplace/actions/setup-python
      # ^-- This gives info on matrix testing.
      - name: Install Python
        uses: actions/setup-python@v2
        with:
          python-version: 3.8
      # https://docs.github.com/en/actions/guides/building-and-testing-python#caching-dependencies
      # ^-- How to set up caching for pip on Ubuntu
      - name: Cache pip
        uses: actions/cache@v2
        with:
          path: ~/.cache/pip
          key: ${{ runner.os }}-pip-${{ hashFiles('requirements.txt') }}
          restore-keys: |
            ${{ runner.os }}-pip-
            ${{ runner.os }}-
      - name: Cache poetry
        uses: actions/cache@v2
        with:
          #path: ~/.cache/pip
          path: ~/.cache/pypoetry/virtualenvs
          key: ${{ runner.os }}-pip-${{ hashFiles('pyproject.toml') }}
          #key: ${{ runner.os }}-pip-${{ hashFiles('requirements.txt') }}
          restore-keys: |
            ${{ runner.os }}-poetry-
            ${{ runner.os }}-
      # https://docs.github.com/en/actions/guides/building-and-testing-python#installing-dependencies
      # ^-- This gives info on installing dependencies with pip
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          python -m pip install poetry
          poetry config virtualenvs.create false
          poetry install

      - name: Debugging information
        run: |
          echo "github.ref:" ${{github.ref}}
          echo "github.event_name:" ${{github.event_name}}
          echo "github.head_ref:" ${{github.head_ref}}
          echo "github.base_ref:" ${{github.base_ref}}
          set -x
          git rev-parse --abbrev-ref HEAD
          git branch
          git branch -a
          git remote -v
          python -V
          pip list --not-required
          pip list
          poetry debug

      # Build
      #- uses: ammaraskar/sphinx-problem-matcher@master
      - name: Build Sphinx docs
        working-directory: ./docs
        run: |
          ./build_doc.sh
          # make dirhtml
          # This fixes broken copy button icons, as explained in
          #   https://github.com/coderefinery/sphinx-lesson/issues/50
          #   https://github.com/executablebooks/sphinx-copybutton/issues/110
          # This can be removed once these PRs are accepted (but the
          # fixes also need to propagate to other themes):
          #   https://github.com/sphinx-doc/sphinx/pull/8524
          #   https://github.com/readthedocs/sphinx_rtd_theme/pull/1025
          #sed -i 's/url_root="#"/url_root=""/' _build/html/index.html || true


      # Add the .nojekyll file
      - name: nojekyll
        working-directory: ./docs
        #if: ${{ github.event_name == 'push' }}
        run: |
          touch _build/html/.nojekyll

      # Deploy
      # https://github.com/peaceiris/actions-gh-pages
      - name: Deploy
        uses: peaceiris/actions-gh-pages@v3
        #if: ${{ github.event_name == 'push' }}
        #if: ${{ success() && github.event_name == 'push' && github.ref == 'refs/heads/$defaultBranch' }}
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          publish_dir: ./docs/_build/html
          publish_branch: gh-pages
          force_orphan: true

