---

name: CI

on:
  push:
    branches:
      - 'main'
    tags:
      - '*'
  pull_request:

jobs:

  cache-nix-dependencies:
    name: Cache Nix dependencies

    runs-on: ubuntu-latest
    needs: []

    steps:
      - name: Clone Repository
        uses: actions/checkout@v4

      - name: Install Nix
        uses: cachix/install-nix-action@v23
        with:
          extra_nix_config: |
            ## Access token to avoid triggering GitHub's rate limiting.
            access-tokens = github.com=${{ secrets.GITHUB_TOKEN }}

      - name: Setup Nix cache
        uses: cachix/cachix-action@v12
        with:
          name: ppx-deriving-madcast
          authToken: '${{ secrets.CACHIX_AUTH_TOKEN }}'

      - name: Build Nix environment
        run: nix develop --command true

  build-and-test:
    name: Build and test

    runs-on: ubuntu-latest
    needs: cache-nix-dependencies

    steps:
      - name: Clone Repository
        uses: actions/checkout@v4

      - name: Install Nix
        uses: cachix/install-nix-action@v23
        with:
          extra_nix_config: |
            ## Access token to avoid triggering GitHub's rate limiting.
            access-tokens = github.com=${{ secrets.GITHUB_TOKEN }}

      - name: Setup Nix cache
        uses: cachix/cachix-action@v12
        with:
          name: ppx-deriving-madcast

      - name: Build Everything
        run: nix develop --command dune build

      - name: Build Documentation
        run: nix develop --command dune build

      - name: Test Everything
        run: nix develop --command dune test

  flake-checks:
    name: Flake checks

    runs-on: ubuntu-latest
    needs: cache-nix-dependencies

    steps:
      - name: Check out repository code
        uses: actions/checkout@v4

      - name: Install Nix
        uses: cachix/install-nix-action@v23
        with:
          extra_nix_config: |
            ## Access token to avoid triggering GitHub's rate limiting.
            access-tokens = github.com=${{ secrets.GITHUB_TOKEN }}

      - name: Setup Nix caches
        uses: cachix/cachix-action@v12
        with:
          name: ppx-deriving-madcast
          authToken: '${{ secrets.CACHIX_AUTH_TOKEN }}'

      - name: Run flake checks
        run: nix flake check --print-build-logs
