name: Build master

on:
  push:
    paths-ignore:
      - 'doc/**'
      - README.md
      # do not run CI if we are writing a paper
      # https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#example-ignoring-paths
    branches:
      - 'master'
  #schedule:
  #  - cron: "0 13 * * 1"
    # https://jasonet.co/posts/scheduled-actions/

jobs:
  build:
    env:
      #OCANREN_STATS: yes
      # enabling stats required extra link library
      OCANREN_DOCS: yes
      OPAMROOT: /home/user/.opam

    runs-on: ubuntu-22.04
    container:
      image: kakadu18/ocaml:pltlab
      options: --user user

    steps:
      - run: opam --version
      - run: opam exec -- ocamlopt --version

      - name: Cancel Previous Runs
        uses: styfle/cancel-workflow-action@0.11.0
        with:
          access_token: ${{ github.token }}

      - name: Checkout code
        uses: actions/checkout@v3

      - run: opam update
      - name: Install dev version of GT
        run: |
          opam source GT --dev-repo
          opam pin add GT ./GT -y

      - name: Install dependencies for documentation
        run: opam install odoc pa_ppx --yes

      - run: opam install . --yes --deps-only --with-test --with-doc

      - name: List installed packages
        run: opam list

      - run: opam exec -- make #dune build
      - run: opam exec -- make test # dune runtest


      #### API documentation
      - name: Build API documentation
        run: opam exec -- dune build @doc

      - name: Deploy API documentation
        if: ${{ github.event.pull_request.head.repo.full_name == 'PLTools/OCanren' }}
        # We do not deploy in forks
        uses: peaceiris/actions-gh-pages@v3
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          publish_dir: ./_build/default/_doc/_html
          destination_dir: api
