name: Haddock
# Test documentation: haddock, user-manual.
# Since `stack haddock` compiles Agda but `cabal haddock` does
# not, this test is faster using `BUILD=CABAL` [Issue #2188].
on:
  push:
    branches:
    - master
    - ci-*
    - release*
    paths: &trigger_path_list
    - 'src/full/**.hs'
    - 'Agda.cabal'
    - '.github/workflows/haddock.yml'
  pull_request:
    paths: *trigger_path_list

jobs:
  build:
    strategy:
      matrix:
        os: [ubuntu-latest]
        ghc-ver: ['9.2.1']
        cabal-ver: [latest]

    if: |
      !contains(github.event.head_commit.message, '[skip ci]')
      && !contains(github.event.head_commit.message, '[ci skip]')
      && !contains(github.event.head_commit.message, '[github skip]')
      && !contains(github.event.head_commit.message, '[skip github]')

    runs-on: ${{ matrix.os }}

    steps:
    - uses: actions/checkout@v2

    - uses: haskell/actions/setup@v1
      id: setup-haskell
      with:
        ghc-version: ${{ matrix.ghc-ver }}
        cabal-version: ${{ matrix.cabal-ver }}

    - name: Environment settings based on the Haskell setup
      run: |
        export GHC_VER=$(ghc --numeric-version)
        export CABAL_VER=$(cabal --numeric-version)
        echo "GHC_VER    = ${GHC_VER}"
        echo "CABLAL_VER = ${CABAL_VER}"
        echo "GHC_VER=${GHC_VER}"         >> ${GITHUB_ENV}
        echo "CABAL_VER=${CABAL_VER}"     >> ${GITHUB_ENV}
      # From now on, use env.{GHC|CABAL}_VER rather than matrix.{ghc|cabal}-ver!

    - name: Resolve dependencies
      run: |
        cabal update
        cabal configure -fenable-cluster-counting --enable-tests --disable-library-profiling

    - uses: actions/cache@v2
      name: Cache dependencies
      id: cache
      with:
        path: |
          ${{ steps.setup-haskell.outputs.cabal-store }}
        # The file `plan.json` contains the build information.
        key: ${{ runner.os }}-cabal-${{ env.GHC_VER }}-${{ env.CABAL_VER }}-${{ hashFiles('**/plan.json') }}

    - name: Install dependencies
      if: ${{ !steps.cache.outputs.cache-hit }}
      run: |
        cabal build --dependencies-only --force-reinstalls

    # Testing Haddock [Issue 1773]
    - name: Build Haddock
      run: |
        cabal haddock --haddock-html-location='https://hackage.haskell.org/package/$pkg-$version/docs' --haddock-hyperlink-source --haddock-quickjump

    - name: Prepare to upload built htmls
      run: |
        find dist-newstyle -path '*/doc/html/Agda' -type d -exec cp -R {} html \;
        ls -R html

    - name: Deploy haddock
      uses: peaceiris/actions-gh-pages@v3
      if: github.ref == 'refs/heads/master'
      with:
        github_token: ${{ secrets.GITHUB_TOKEN }}
        publish_dir: html
        force_orphan:  true
        destination_dir: docs
