name: CI

on:
  push:
    branches:
      - master
  pull_request:

env:
  NODE_VERSION: 12

jobs:
  lint:
    name: Lint
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 1

      - uses: actions/setup-node@v2
        with:
          node-version: '${{ env.NODE_VERSION }}'

      - name: Get package manager's global cache path
        id: global-cache-dir-path
        run: echo "::set-output name=dir::$(npm config get cache)"

      - name: Cache package manager's global cache and node_modules
        id: cache-dependencies
        uses: actions/cache@v2
        with:
          path: |
            ${{ steps.global-cache-dir-path.outputs.dir }}
            node_modules
          key: ${{ runner.os }}-${{ matrix.node-version }}-${{ hashFiles('**/package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-${{ matrix.node-version }}-
      - name: Install Dependencies
        run: npm ci
        if: |
          steps.cache-dependencies.outputs.cache-hit != 'true'
      - name: Lint
        run: npm run-script lint

  test:
    name: Tests
    runs-on: ${{ matrix.os }}
    needs: lint

    strategy:
      matrix:
        os: [ubuntu-latest]

    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 1

      - uses: actions/setup-node@v2
        with:
          node-version: '${{ env.NODE_VERSION }}'

      - name: Get package manager's global cache path
        id: global-cache-dir-path
        run: echo "::set-output name=dir::$(npm config get cache)"

      - name: Cache package manager's global cache and node_modules
        id: cache-dependencies
        uses: actions/cache@v2
        with:
          path: |
            ${{ steps.global-cache-dir-path.outputs.dir }}
            node_modules
          key: ${{ runner.os }}-${{ matrix.node-version }}-${{ hashFiles('**/package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-${{ matrix.node-version }}-
      - name: Install Dependencies
        run: npm ci
        if: |
          steps.cache-dependencies.outputs.cache-hit != 'true'
      - name: Test
        run: npm run-script test

  floating-dependencies:
    name: Floating Dependencies
    runs-on: ${{ matrix.os }}
    needs: lint

    strategy:
      matrix:
        os: [ubuntu-latest]

    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 1

      - uses: actions/setup-node@v2
        with:
          node-version: '${{ env.NODE_VERSION }}'

      - name: Get package manager's global cache path
        id: global-cache-dir-path
        run: echo "::set-output name=dir::$(npm config get cache)"

      - name: Cache package manager's global cache and node_modules
        id: cache-dependencies
        uses: actions/cache@v2
        with:
          path: |
            ${{ steps.global-cache-dir-path.outputs.dir }}
            node_modules
          key: ${{ runner.os }}-${{ matrix.node-version }}-floating-deps
          restore-keys: |
            ${{ runner.os }}-${{ matrix.node-version }}-
      - name: Install Dependencies
        run: npm install --no-package-lock

      - name: Test
        run: npm run-script test

  try-scenarios:
    name: Tests - ${{ matrix.ember-try-scenario }}
    runs-on: ubuntu-latest
    continue-on-error: ${{ matrix.allow-failure }}
    needs: test

    strategy:
      fail-fast: true
      matrix:
        ember-try-scenario: [
            ember-lts-3.20,
            ember-lts-3.24,
            ember-release,
            ember-beta,
            ember-canary,
            ember-default-with-jquery,
            ember-classic,
            embroider-safe
            embroider-optimized,
          ]
        allow-failure: [false]
        include:
          - ember-try-scenario: ember-canary
            allow-failure: true

    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 1

      - uses: actions/setup-node@v2
        with:
          node-version: '${{ env.NODE_VERSION }}'

      - name: Get package manager's global cache path
        id: global-cache-dir-path
        run: echo "::set-output name=dir::$(npm config get cache)"

      - name: Cache package manager's global cache and node_modules
        id: cache-dependencies
        uses: actions/cache@v2
        with:
          path: |
            ${{ steps.global-cache-dir-path.outputs.dir }}
            node_modules
          key: ${{ runner.os }}-${{ matrix.node-version }}-${{ hashFiles('**/package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-${{ matrix.node-version }}-
      - name: Install Dependencies
        run: npm ci
        if: |
          steps.cache-dependencies.outputs.cache-hit != 'true'
      - name: Test
        env:
          EMBER_TRY_SCENARIO: ${{ matrix.ember-try-scenario }}
        run: node_modules/.bin/ember try:one $EMBER_TRY_SCENARIO
