# Generated by `starci compile --target circleci` from pi-gamebook.ci.star. Edit that, not this.
#
# Each job is one step of the plan: the same image, command, tools and
# packages, given only the step's inputs. A job skips its work when it has
# already passed with the same inputs.
#
# What does not carry over:
#  - Passes are remembered in this project's CircleCI cache, which expires.
#    starci's own store does not.

version: 2.1

jobs:
  check:
    machine:
      image: ubuntu-2404:current
    resource_class: medium
    steps:
      - checkout
      - run:
          name: "Keep only this step's inputs"
          command: |
            git sparse-checkout set --no-cone '/bun.lock' '/index.ts' '/omp.ts' '/package.json' '/rules' '/src' '/test' '/tsconfig.json'
      - run:
          name: "Identify the inputs"
          command: |
            git ls-files -s -- ':(top,literal)bun.lock' ':(top,literal)index.ts' ':(top,literal)omp.ts' ':(top,literal)package.json' ':(top,literal)rules' ':(top,literal)src' ':(top,literal)test' ':(top,literal)tsconfig.json' > /tmp/starci-inputs
      - restore_cache:
          name: Has this already passed with these inputs?
          keys:
            - starci-passed-check-c6f009914a378669-{{ checksum "/tmp/starci-inputs" }}
      - run:
          name: "Stop here if it has"
          command: |
            if [ -e ~/starci-passed/at ]; then
              echo "Passed before with the same inputs, on $(cat ~/starci-passed/at). Nothing to do."
              circleci-agent step halt
            fi
      - restore_cache:
          name: "Cache: bun"
          keys:
            - starci-cache-bun-
      - run:
          name: "check"
          environment:
            STARCI_RUN: |-
              bun install --frozen-lockfile && bun run check
          command: |
            docker run --rm \
              -v "$PWD:/workspace" \
              -w /workspace \
              -v "$HOME/starci-cache/bun:/root/.bun/install/cache" \
              oven/bun:1 sh -c "$STARCI_RUN"
      - save_cache:
          name: "Save cache: bun"
          key: starci-cache-bun-{{ .Revision }}
          paths:
            - ~/starci-cache/bun
      - run:
          name: "Remember this pass"
          command: |
            mkdir -p ~/starci-passed && date > ~/starci-passed/at
      - save_cache:
          key: starci-passed-check-c6f009914a378669-{{ checksum "/tmp/starci-inputs" }}
          paths:
            - ~/starci-passed

  test:
    machine:
      image: ubuntu-2404:current
    resource_class: medium
    steps:
      - checkout
      - run:
          name: "Keep only this step's inputs"
          command: |
            git sparse-checkout set --no-cone '/bun.lock' '/index.ts' '/omp.ts' '/package.json' '/rules' '/src' '/test' '/tsconfig.json'
      - run:
          name: "Identify the inputs"
          command: |
            git ls-files -s -- ':(top,literal)bun.lock' ':(top,literal)index.ts' ':(top,literal)omp.ts' ':(top,literal)package.json' ':(top,literal)rules' ':(top,literal)src' ':(top,literal)test' ':(top,literal)tsconfig.json' > /tmp/starci-inputs
      - restore_cache:
          name: Has this already passed with these inputs?
          keys:
            - starci-passed-test-bbcb3fc8d6ed8a2d-{{ checksum "/tmp/starci-inputs" }}
      - run:
          name: "Stop here if it has"
          command: |
            if [ -e ~/starci-passed/at ]; then
              echo "Passed before with the same inputs, on $(cat ~/starci-passed/at). Nothing to do."
              circleci-agent step halt
            fi
      - restore_cache:
          name: "Cache: bun"
          keys:
            - starci-cache-bun-
      - run:
          name: "test"
          environment:
            STARCI_RUN: |-
              bun install --frozen-lockfile && bun test
          command: |
            docker run --rm \
              -v "$PWD:/workspace" \
              -w /workspace \
              -v "$HOME/starci-cache/bun:/root/.bun/install/cache" \
              oven/bun:1 sh -c "$STARCI_RUN"
      - save_cache:
          name: "Save cache: bun"
          key: starci-cache-bun-{{ .Revision }}
          paths:
            - ~/starci-cache/bun
      - run:
          name: "Remember this pass"
          command: |
            mkdir -p ~/starci-passed && date > ~/starci-passed/at
      - save_cache:
          key: starci-passed-test-bbcb3fc8d6ed8a2d-{{ checksum "/tmp/starci-inputs" }}
          paths:
            - ~/starci-passed

workflows:
  ci:
    jobs:
      - check
      - test
