name: Main workflow

on:
  - pull_request
  - push

jobs:
  build:
    strategy:
      fail-fast: false
      matrix:
        os:
          - macos-latest
          - ubuntu-latest
          - windows-latest
        ocaml-compiler:
          - 4.14.x

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

    steps:
      - if: ${{ contains(matrix.os, 'ubuntu') }}
        run: sudo apt-get update -y

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

      - name: Use OCaml ${{ matrix.ocaml-compiler }}
        uses: ocaml/setup-ocaml@v2
        with:
          ocaml-compiler: ${{ matrix.ocaml-compiler }}

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

      - run: opam exec -- dune build

      - run: opam exec -- dune runtest

      - run: opam install .

      - run: cd $(opam var prefix); tar -cvf ubase.tar bin/ubase

      - run: mv $(opam var prefix)/ubase.tar .

      - name: Upload the build artifact
        uses: actions/upload-artifact@v3
        with:
          name: ubase-binary-${{ matrix.os }}-${{ matrix.ocaml-compiler }}
          path: ubase.tar

  lint-fmt:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Use OCaml 4.14.x
        uses: ocaml/setup-ocaml@v2
        with:
          ocaml-compiler: 4.14.x
          dune-cache: true

      - name: Lint fmt
        uses: ocaml/setup-ocaml/lint-fmt@v2
