on: 
  pull_request:
    paths:
      - .github/actions/extism/**
      - .github/workflows/ci-ocaml.yml
      - manifest/**
      - runtime/**
      - libextism/**
      - ocaml/**
      - dune-project
      - extism.opam
  workflow_dispatch:

name: OCaml CI

jobs:
  ocaml:
    name: OCaml
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest]
        rust:
          - stable
    steps:
      - name: Checkout sources
        uses: actions/checkout@v3
      - uses: ./.github/actions/extism
      - name: Setup OCaml env
        uses: ocaml/setup-ocaml@v2
        with:
          ocaml-compiler: ocaml-base-compiler.5.0.0
      - name: Cache OCaml
        id: cache-ocaml
        uses: actions/cache@v3
        with:
          path: _build
          key: ${{ runner.os }}-ocaml-${{ hashFiles('ocaml/**.ml') }}-${{ hashFiles('dune-project') }}
      - name: Build OCaml Host SDK
        if: steps.cache-ocaml.outputs.cache-hit != 'true'
        run: |
          opam install -y --deps-only .
          cd ocaml
          LD_LIBRARY_PATH=/usr/local/lib opam exec -- dune build
      - name: Test OCaml Host SDK
        run: |
          opam install -y --deps-only .
          cd ocaml
          LD_LIBRARY_PATH=/usr/local/lib opam exec -- dune exec ./bin/main.exe ../wasm/code.wasm count_vowels -- --input "qwertyuiop"
          LD_LIBRARY_PATH=/usr/local/lib opam exec -- dune runtest
