name: generate docs
permissions:
  contents: write
on:
  push:
    branches: main
    paths:
      - docs/conf.py
      - '**.rst'
      - '**.py'
      - .github/workflows/generate-docs.yml
  pull_request:
    paths:
      - docs/conf.py
      - '**.rst'
      - '**.py'
      - .github/workflows/generate-docs.yml
jobs:
  generate-docs:

    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2
      - name: Set up Python
        uses: actions/setup-python@v2
        with:
          python-version: 3.x

      # Installation instructions are from: https://python-poetry.org/docs/
      - name: Install dependencies
        shell: bash
        run: |
          sudo apt update && sudo apt install -y \
            enchant-2 \
            --no-install-recommends
          pip install \
            poetry

      - name: Build
        shell: bash
        run: |
          poetry build

      - name: Generate the docs
        shell: bash
        run: |
          rm -rf docs/html
          poetry install
          poetry run sphinx-build -b html docs/ docs/html/

      - name: Check for modified files
        id: git-check
        run: echo ::set-output name=modified::$(if git diff-index --ignore-submodules --quiet HEAD --; then echo "false"; else echo "true"; fi)

      - name: Commit changes, if any
        if: steps.git-check.outputs.modified == 'true'
        run: |
          git add .
          git config --local user.email "github-actions[bot]@users.noreply.github.com"
          git config --local user.name "github-actions[bot]"
          git remote set-url origin https://${{ github.actor }}:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git
          git commit -am "I HAVE GENERATED YOUR NEW DOCS!" || true
          git fetch origin
          git rebase origin/${{github.ref_name }} || true
          git push origin ${{github.ref_name }}


