name: Check Examples

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

jobs:
  setup:
    name: Get Examples
    runs-on: ubuntu-latest
    outputs:
      matrix: ${{ steps.set-matrix.outputs.matrix }}
      source_changed: ${{ steps.set-source-changed.outputs.source_changed }}
    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Install JQ Tool
        uses: mbround18/install-jq@v1

      - name: Set Matrix
        id: set-matrix
        run: |
          examples=$(ls examples | 
          awk '{print "examples/" $0}' | 
          grep -v examples/README.md | 
          grep -v examples/Makefile.toml | 
          grep -v examples/cargo-make | 
          grep -v examples/gtk | 
          jq -R -s -c 'split("\n")[:-1]')
          echo "Example Directories: $examples"
          echo "matrix={\"directory\":$examples}" >> "$GITHUB_OUTPUT"

      - name: Get source files that changed
        id: changed-source
        uses: tj-actions/changed-files@v36
        with:
          files: |
            integrations
            leptos
            leptos_config
            leptos_dom
            leptos_hot_reload
            leptos_macro
            leptos_reactive
            leptos_server
            meta
            router
            server_fn
            server_fn_macro

      - name: List source files that changed
        run: echo '${{ steps.changed-source.outputs.all_changed_files }}'

      - name: Set source_changed
        id: set-source-changed
        run: |
          echo "source_changed=${{ steps.changed-source.outputs.any_changed }}" >> "$GITHUB_OUTPUT"

  matrix-job:
    name: Check
    needs: [setup]
    if: needs.setup.outputs.source_changed == 'true'
    strategy:
      matrix: ${{ fromJSON(needs.setup.outputs.matrix) }}
      fail-fast: false
    uses: ./.github/workflows/run-cargo-make-task.yml
    with:
      directory: ${{ matrix.directory }}
      cargo_make_task: "check"
