name: Verify Changed Examples

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

jobs:
  setup:
    name: Get Changes
    runs-on: ubuntu-latest
    outputs:
      matrix: ${{ steps.set-matrix.outputs.matrix }}
    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - name: Get all example files that changed
        id: changed-files
        uses: tj-actions/changed-files@v36
        with:
          files: |
            examples

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

      - name: Get example project directories that changed
        id: changed-dirs
        uses: tj-actions/changed-files@v36
        with:
          dir_names: true
          dir_names_max_depth: "2"
          files: |
            examples
            !examples/cargo-make
            !examples/gtk
            !examples/Makefile.toml
            !examples/README.md
          json: true
          quotepath: false

      - name: List example project directories that changed
        run: echo '${{ steps.changed-dirs.outputs.all_changed_files }}'

      - name: Set Matrix
        id: set-matrix
        run: |
          if [ ${{ steps.changed-files.outputs.any_changed }} == 'true' ]; then
            # Create matrix with changed directories
            echo "matrix={\"directory\":${{ steps.changed-dirs.outputs.all_changed_files }}}" >> "$GITHUB_OUTPUT"
          else
            # Create matrix with one item to prevent an empty vector error
            echo "matrix={\"directory\":[\"INTERNAL\"]}" >> "$GITHUB_OUTPUT"
          fi

  matrix-job:
    name: Verify
    needs: [setup]
    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: "verify-flow"
