# Generated by `starci compile --target circleci` from pi-gamebook-tools.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-ec8db2e6a4b7d1c4-{{ 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
      - run:
          name: "Tool: bun"
          command: |
            mkdir -p "$HOME/starci-tools/bun"
            curl -fsSL 'https://github.com/oven-sh/bun/releases/download/bun-v1.3.14/bun-linux-x64.zip' -o /tmp/bun.download
            echo "951ee2aee855f08595aeec6225226a298d3fea83a3dcd6465c09cbccdf7e848f  /tmp/bun.download" | sha256sum -c -
            unzip -q "/tmp/bun.download" -d "$HOME/starci-tools/bun"
      - restore_cache:
          name: "Cache: bun"
          keys:
            - starci-cache-bun-
      - run:
          name: "check"
          environment:
            STARCI_RUN: |-
              bun install --frozen-lockfile && bun run check
          command: |
            docker pull -q debian:12-slim
            image_path=$(docker image inspect debian:12-slim --format '{{range .Config.Env}}{{println .}}{{end}}' | sed -n 's/^PATH=//p')
            docker run --rm \
              -v "$PWD:/workspace" \
              -w /workspace \
              -v "$HOME/starci-tools/bun:/opt/starci/tools/bun:ro" \
              -v "$HOME/starci-cache/bun:/root/.bun/install/cache" \
              -e "PATH=/opt/starci/tools/bun/bun-linux-x64:$image_path" \
              debian:12-slim 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-ec8db2e6a4b7d1c4-{{ 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-6b293961e36e63ef-{{ 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
      - run:
          name: "Tool: bun"
          command: |
            mkdir -p "$HOME/starci-tools/bun"
            curl -fsSL 'https://github.com/oven-sh/bun/releases/download/bun-v1.3.14/bun-linux-x64.zip' -o /tmp/bun.download
            echo "951ee2aee855f08595aeec6225226a298d3fea83a3dcd6465c09cbccdf7e848f  /tmp/bun.download" | sha256sum -c -
            unzip -q "/tmp/bun.download" -d "$HOME/starci-tools/bun"
      - restore_cache:
          name: "Cache: bun"
          keys:
            - starci-cache-bun-
      - run:
          name: "test"
          environment:
            STARCI_RUN: |-
              bun install --frozen-lockfile && bun test
          command: |
            docker pull -q debian:12-slim
            image_path=$(docker image inspect debian:12-slim --format '{{range .Config.Env}}{{println .}}{{end}}' | sed -n 's/^PATH=//p')
            docker run --rm \
              -v "$PWD:/workspace" \
              -w /workspace \
              -v "$HOME/starci-tools/bun:/opt/starci/tools/bun:ro" \
              -v "$HOME/starci-cache/bun:/root/.bun/install/cache" \
              -e "PATH=/opt/starci/tools/bun/bun-linux-x64:$image_path" \
              debian:12-slim 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-6b293961e36e63ef-{{ checksum "/tmp/starci-inputs" }}
          paths:
            - ~/starci-passed

workflows:
  ci:
    jobs:
      - check
      - test
