name: CI

on:
  push:
    paths-ignore:
      - '**/*.md'
      - 'examples/**'
  pull_request:
  workflow_dispatch:

defaults:
  run:
    shell: bash

permissions:
    actions: write

jobs:
  fmt:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
      
      - name: Install nix
        uses: nixbuild/nix-quick-install-action@v29
      
      - name: Restore and cache Nix store
        uses: nix-community/cache-nix-action@v5
        with:
          primary-key: nix-${{ runner.os }}-${{ hashFiles('**/*.nix') }}
          restore-prefixes-first-match: nix-${{ runner.os }}-
          gc-max-store-size-linux: 5368709000 
          purge: true
          purge-prefixes: cache-${{ runner.os }}-
          purge-created: 0
          purge-primary-key: never

      - name: Cache pesde data 
        uses: actions/cache@v4
        with:
          path: ~/.pesde
          key: pesde-${{ runner.os }}-${{ hashFiles('pesde.toml') }}
      
      - name: Install dependencies
        run: nix develop -c pesde install --locked

      - name: Check formatting
        run: nix develop -c lune run fmt -- --check

  typecheck:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
      
      - name: Install nix
        uses: nixbuild/nix-quick-install-action@v29
      
      - name: Restore and cache Nix store
        uses: nix-community/cache-nix-action@v5
        with:
          primary-key: nix-${{ runner.os }}-${{ hashFiles('**/*.nix') }}
          restore-prefixes-first-match: nix-${{ runner.os }}-
          gc-max-store-size-linux: 5368709000 
          purge: true
          purge-prefixes: cache-${{ runner.os }}-
          purge-created: 0
          purge-primary-key: never

      - name: Cache pesde data 
        uses: actions/cache@v4
        with:
          path: ~/.pesde
          key: pesde-${{ runner.os }}-${{ hashFiles('pesde.toml') }}

      - name: Install dependencies
        run: nix develop -c pesde install --locked

      - name: Setup lune typedefs
        run: nix develop -c lune setup

      - name: Typecheck
        run: nix develop -c lune run typecheck

  test:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Install nix
        uses: nixbuild/nix-quick-install-action@v29
      
      - name: Restore and cache Nix store
        uses: nix-community/cache-nix-action@v5
        with:
          primary-key: nix-${{ runner.os }}-${{ hashFiles('**/*.nix') }}
          restore-prefixes-first-match: nix-${{ runner.os }}-
          gc-max-store-size-linux: 5368709000 
          purge: true
          purge-prefixes: cache-${{ runner.os }}-
          purge-created: 0
          purge-primary-key: never

      - name: Cache pesde data 
        uses: actions/cache@v4
        with:
          path: ~/.pesde
          key: pesde-${{ runner.os }}-${{ hashFiles('pesde.toml') }}

      - name: Install dependencies
        run: nix develop -c pesde install --locked

      - name: Run tests
        run: nix develop -c lune run tests
