name: CI
on:
  push:
    branches:
      - master
  pull_request:
    types:
      - opened
      - synchronize
jobs:
  ci:
    strategy:
      fail-fast: false
      matrix:
        ghc: [ghc984, ghc9101, ghc9121]
    name: Build and test on ${{ matrix.ghc }}
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: cachix/install-nix-action@v30
        with:
          extra_nix_config: |
            accept-flake-config = true
      - uses: cachix/cachix-action@v15
        with:
          name: tweag-ormolu
          authToken: '${{ secrets.CACHIX_TWEAG_ORMOLU_AUTH_TOKEN }}'
      - name: Build and run tests
        run: |
          nix build -L --keep-going .#${{ matrix.ghc }}.ci
  lint:
    needs: ci
    name: Formatting and linting
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: cachix/install-nix-action@v30
        with:
          extra_nix_config: |
            accept-flake-config = true
      - name: Format via Ormolu
        run: |
          nix run .#format
          git diff --exit-code --color=always
      - name: pre-commit-check
        run: |
          nix build -L .#pre-commit-check
  live:
    needs: lint
    name: Build and deploy Ormolu Live
    runs-on: ubuntu-latest
    env:
      NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
      NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }}
    steps:
      - uses: actions/checkout@v4
      - uses: cachix/install-nix-action@v30
        with:
          extra_nix_config: |
            accept-flake-config = true
      - uses: actions/cache@v4
        with:
          path: |
            ~/.ghc-wasm/.cabal/store
            ormolu-live/dist-newstyle
          key: wasm-${{ github.run_id }}
          restore-keys: |
            wasm-${{ github.run_id }}
            wasm-
      - name: Build Ormolu Live
        run: |
          cd ormolu-live
          nix develop .#ormoluLive -c sh -c \
            'npm ci && wasm32-wasi-cabal update && ./build.sh prod'
      - name: Deploy to Netlify, preview
        if: env.NETLIFY_AUTH_TOKEN != ''
        uses: nwtgck/actions-netlify@v3
        with:
          publish-dir: ./ormolu-live/dist
          github-token: ${{ secrets.GITHUB_TOKEN }}
          alias: ${{ github.event.pull_request.head.sha || github.sha }}
          enable-pull-request-comment: true
          enable-commit-comment: false
          enable-commit-status: true
      - name: Deploy to Netlify, production
        if: env.NETLIFY_AUTH_TOKEN != '' && github.ref == 'refs/heads/master'
        run: |
          nix run --inputs-from . nixpkgs#netlify-cli -- \
            deploy --prod -d ./ormolu-live/dist

  # prevent stack.yaml from becoming outdated
  stack:
    name: Build and test via Stack
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: haskell-actions/setup@v2
        with:
          enable-stack: true
          stack-no-global: true
      - uses: actions/cache@v4
        with:
          path: ~/.stack
          key: stack-${{ hashFiles('stack.yaml', 'ormolu.cabal') }}
      - name: Build
        run: stack build
      - name: Test
        run: stack test