name: MetagraphOptimization_CI

on: [push]

env:
  # keep the depot directly in the repository for the cache
  JULIA_DEPOT_PATH: './.julia'

jobs:
  test:
    runs-on: ubuntu-22.04

    steps:
      - name: Checkout repository
        uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - name: Setup Julia environment
        uses: https://github.com/julia-actions/setup-julia@v1.9.2
        with:
          version: '1.9.2'

      - name: Instantiate
        run: |
          julia --project=./ -e 'using Pkg; Pkg.instantiate()'
          julia --project=./ -e 'using Pkg; Pkg.add(url="https://github.com/QEDjl-project/QEDprocesses.jl/")'

      - name: Format check
        run: |
          julia --project=./ -e 'using JuliaFormatter; format(".", verbose=true, ignore=[".julia/*"])'
          julia --project=./ -e '
          out = Cmd(`git diff --name-only`) |> read |> String
          if out == ""
            exit(0)
          else
            @error "Some files have not been formatted!!!"
            write(stdout, out)
            exit(1)
          end'

      - name: Run tests
        run: julia --project=./ -t 4 -e 'using Pkg; Pkg.test()' -O0

      - name: Run examples
        run: |
          julia --project=examples/ -e 'using Pkg; Pkg.develop(Pkg.PackageSpec(path=pwd())); Pkg.instantiate(); Pkg.precompile()'
          julia --project=examples/ -t 4 -e 'include("examples/import_bench.jl")' -O3

  docs:
    runs-on: ubuntu-22.04

    steps:
      - name: Checkout repository
        uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - name: Setup Julia environment
        uses: https://github.com/julia-actions/setup-julia@v1.9.2
        with:
          version: '1.9.2'

      - name: Build docs
        run: |
          julia --project=docs/ -e 'using Pkg; Pkg.develop(Pkg.PackageSpec(path=pwd())); Pkg.instantiate(); Pkg.precompile()'
          julia --project=docs/ docs/make.jl

      - name: Upload artifacts
        uses: actions/upload-artifact@v3
        with:
          name: web-doc
          path: docs/build/

      #- name: Webhook Trigger
      #  uses: https://github.com/zzzze/webhook-trigger@master
      #  continue-on-error: true
      #  with:
      #    data: "{\"event\":\"action_completed\", \"download_url\":\"deckardcain.local:8099/something\"}"
      #    webhook_url: ${{ secrets.WEBHOOK_URL }}
