name: CI

on:
  push:
    branches:
      - main
      - master
  pull_request: {}

concurrency:
  group: ci-${{ github.head_ref || github.ref }}
  cancel-in-progress: true

jobs:
  lint:
    name: "Lints"
    runs-on: ubuntu-latest
    timeout-minutes: 10

    steps:
      - uses: actions/checkout@v4<% if (pnpm) { %>
      - uses: pnpm/action-setup@v4<% } %>
      - uses: actions/setup-node@v4
        with:
          node-version: 22
          cache: <%= pnpm ? 'pnpm' : yarn ? 'yarn' : 'npm' %>
      - name: Install Dependencies
        run: <%= pnpm ? 'pnpm install --frozen-lockfile' : yarn ? 'yarn install --frozen-lockfile' : 'npm ci' %>
      - name: Lint
        run: <%= pnpm ? 'pnpm' : yarn ? 'yarn' : 'npm run' %> lint

  test:
    name: "Tests"
    runs-on: ubuntu-latest
    timeout-minutes: 10
    outputs:
      matrix: ${{ steps.set-matrix.outputs.matrix }}

    steps:
      - uses: actions/checkout@v4<% if (pnpm) { %>
      - uses: pnpm/action-setup@v4<% } %>
      - uses: actions/setup-node@v4
        with:
          node-version: 22
          cache: <%= pnpm ? 'pnpm' : yarn ? 'yarn' : 'npm' %>
      - name: Install Dependencies
        run: <%= pnpm ? 'pnpm install --frozen-lockfile' : yarn ? 'yarn install --frozen-lockfile' : 'npm ci' %>
      - name: Run Tests
        run: <%= pnpm ? 'pnpm' : yarn ? 'yarn' : 'npm run' %> test
      # For the Try Scenarios
      - id: set-matrix
        run: |
          <% if (pnpm) { %> echo "matrix=$(pnpm -s dlx @embroider/try list)" >> $GITHUB_OUTPUT<% } %>
          <% if (npm) { %> echo "matrix=$(npx @embroider/try list)" >> $GITHUB_OUTPUT<% } %>

  floating:
    name: "Floating Dependencies"
    runs-on: ubuntu-latest
    timeout-minutes: 10

    steps:
      - uses: actions/checkout@v4<% if (pnpm) { %>
      - uses: pnpm/action-setup@v4<% } %>
      - uses: actions/setup-node@v4
        with:
          node-version: 22
          cache: <%= pnpm ? 'pnpm' : yarn ? 'yarn' : 'npm' %>
      - name: Install Dependencies
        run: <%= pnpm ? 'pnpm install --no-lockfile' : yarn ? 'yarn install --no-lockfile' : 'npm install --no-package-lock' %>
      - name: Run Tests
        run: <%= pnpm ? 'pnpm' : yarn ? 'yarn' : 'npm run' %> test

  try-scenarios:
    name: ${{ matrix.name }}
    runs-on: ubuntu-latest
    needs: "test"
    timeout-minutes: 10
    strategy:
      fail-fast: false
      matrix: ${{fromJson(needs.test.outputs.matrix)}}

    steps:
      - uses: actions/checkout@v4<% if (pnpm) { %>
      - uses: pnpm/action-setup@v4<% } %>
      - uses: actions/setup-node@v4
        with:
          node-version: 22
          cache: <%= pnpm ? 'pnpm' : yarn ? 'yarn' : 'npm' %>
      - name: Apply Scenario
        run: |
          <% if (pnpm) { %> pnpm dlx @embroider/try apply ${{ matrix.name }}<% } %>
          <% if (npm) { %> npx @embroider/try apply ${{ matrix.name }}<% } %>
      - name: Install Dependencies
        run: <%= pnpm ? 'pnpm install --no-lockfile' : yarn ? 'yarn install --no-lockfile' : 'npm install --no-package-lock' %>
      - name: Run Tests
        run: |
          <% if (pnpm) { %>pnpm test<% } %>
          <% if (npm) { %>npm test<% } %>
        env: ${{ matrix.env }}
