# Generated by `starci compile --target circleci` from rust.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:
  fmt:
    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 '/Cargo.lock' '/Cargo.toml' '/src' '/tests'
      - run:
          name: "Identify the inputs"
          command: |
            git ls-files -s -- ':(top,literal)Cargo.lock' ':(top,literal)Cargo.toml' ':(top,literal)src' ':(top,literal)tests' > /tmp/starci-inputs
      - restore_cache:
          name: Has this already passed with these inputs?
          keys:
            - starci-passed-fmt-5ad6145ce9906c86-{{ 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: cargo-registry"
          keys:
            - starci-cache-cargo-registry-
      - restore_cache:
          name: "Cache: cargo-target"
          keys:
            - starci-cache-cargo-target-
      - run:
          name: "fmt"
          environment:
            STARCI_RUN: |-
              rustup component add rustfmt && cargo fmt --check
          command: |
            docker run --rm \
              -v "$PWD:/workspace" \
              -w /workspace \
              -v "$HOME/starci-cache/cargo-registry:/usr/local/cargo/registry" \
              -v "$HOME/starci-cache/cargo-target:/workspace/target" \
              rust:1-slim-bookworm sh -c "$STARCI_RUN"
      - save_cache:
          name: "Save cache: cargo-registry"
          key: starci-cache-cargo-registry-{{ .Revision }}
          paths:
            - ~/starci-cache/cargo-registry
      - save_cache:
          name: "Save cache: cargo-target"
          key: starci-cache-cargo-target-{{ .Revision }}
          paths:
            - ~/starci-cache/cargo-target
      - run:
          name: "Remember this pass"
          command: |
            mkdir -p ~/starci-passed && date > ~/starci-passed/at
      - save_cache:
          key: starci-passed-fmt-5ad6145ce9906c86-{{ checksum "/tmp/starci-inputs" }}
          paths:
            - ~/starci-passed

  clippy:
    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 '/Cargo.lock' '/Cargo.toml' '/src' '/tests'
      - run:
          name: "Identify the inputs"
          command: |
            git ls-files -s -- ':(top,literal)Cargo.lock' ':(top,literal)Cargo.toml' ':(top,literal)src' ':(top,literal)tests' > /tmp/starci-inputs
      - restore_cache:
          name: Has this already passed with these inputs?
          keys:
            - starci-passed-clippy-9f4c64678c823d96-{{ 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: cargo-registry"
          keys:
            - starci-cache-cargo-registry-
      - restore_cache:
          name: "Cache: cargo-target"
          keys:
            - starci-cache-cargo-target-
      - run:
          name: "clippy"
          environment:
            STARCI_RUN: |-
              rustup component add clippy && cargo clippy --all-targets -- -D warnings
          command: |
            docker run --rm \
              -v "$PWD:/workspace" \
              -w /workspace \
              -v "$HOME/starci-cache/cargo-registry:/usr/local/cargo/registry" \
              -v "$HOME/starci-cache/cargo-target:/workspace/target" \
              rust:1-slim-bookworm sh -c "$STARCI_RUN"
      - save_cache:
          name: "Save cache: cargo-registry"
          key: starci-cache-cargo-registry-{{ .Revision }}
          paths:
            - ~/starci-cache/cargo-registry
      - save_cache:
          name: "Save cache: cargo-target"
          key: starci-cache-cargo-target-{{ .Revision }}
          paths:
            - ~/starci-cache/cargo-target
      - run:
          name: "Remember this pass"
          command: |
            mkdir -p ~/starci-passed && date > ~/starci-passed/at
      - save_cache:
          key: starci-passed-clippy-9f4c64678c823d96-{{ 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 '/Cargo.lock' '/Cargo.toml' '/src' '/tests'
      - run:
          name: "Identify the inputs"
          command: |
            git ls-files -s -- ':(top,literal)Cargo.lock' ':(top,literal)Cargo.toml' ':(top,literal)src' ':(top,literal)tests' > /tmp/starci-inputs
      - restore_cache:
          name: Has this already passed with these inputs?
          keys:
            - starci-passed-test-4b5177ea80753245-{{ 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: cargo-registry"
          keys:
            - starci-cache-cargo-registry-
      - restore_cache:
          name: "Cache: cargo-target"
          keys:
            - starci-cache-cargo-target-
      - run:
          name: "test"
          environment:
            STARCI_RUN: |-
              cargo test --locked
          command: |
            docker run --rm \
              -v "$PWD:/workspace" \
              -w /workspace \
              -v "$HOME/starci-cache/cargo-registry:/usr/local/cargo/registry" \
              -v "$HOME/starci-cache/cargo-target:/workspace/target" \
              rust:1-slim-bookworm sh -c "$STARCI_RUN"
      - save_cache:
          name: "Save cache: cargo-registry"
          key: starci-cache-cargo-registry-{{ .Revision }}
          paths:
            - ~/starci-cache/cargo-registry
      - save_cache:
          name: "Save cache: cargo-target"
          key: starci-cache-cargo-target-{{ .Revision }}
          paths:
            - ~/starci-cache/cargo-target
      - run:
          name: "Remember this pass"
          command: |
            mkdir -p ~/starci-passed && date > ~/starci-passed/at
      - save_cache:
          key: starci-passed-test-4b5177ea80753245-{{ checksum "/tmp/starci-inputs" }}
          paths:
            - ~/starci-passed

  release-build:
    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 '/Cargo.lock' '/Cargo.toml' '/src' '/tests'
      - run:
          name: "Identify the inputs"
          command: |
            git ls-files -s -- ':(top,literal)Cargo.lock' ':(top,literal)Cargo.toml' ':(top,literal)src' ':(top,literal)tests' > /tmp/starci-inputs
      - restore_cache:
          name: Has this already passed with these inputs?
          keys:
            - starci-passed-release-build-f4d9509b82d7e2c2-{{ 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: cargo-registry"
          keys:
            - starci-cache-cargo-registry-
      - restore_cache:
          name: "Cache: cargo-target"
          keys:
            - starci-cache-cargo-target-
      - run:
          name: "release build"
          environment:
            STARCI_RUN: |-
              cargo build --release --locked
          command: |
            docker run --rm \
              -v "$PWD:/workspace" \
              -w /workspace \
              -v "$HOME/starci-cache/cargo-registry:/usr/local/cargo/registry" \
              -v "$HOME/starci-cache/cargo-target:/workspace/target" \
              rust:1-slim-bookworm sh -c "$STARCI_RUN"
      - save_cache:
          name: "Save cache: cargo-registry"
          key: starci-cache-cargo-registry-{{ .Revision }}
          paths:
            - ~/starci-cache/cargo-registry
      - save_cache:
          name: "Save cache: cargo-target"
          key: starci-cache-cargo-target-{{ .Revision }}
          paths:
            - ~/starci-cache/cargo-target
      - run:
          name: "Remember this pass"
          command: |
            mkdir -p ~/starci-passed && date > ~/starci-passed/at
      - save_cache:
          key: starci-passed-release-build-f4d9509b82d7e2c2-{{ checksum "/tmp/starci-inputs" }}
          paths:
            - ~/starci-passed

workflows:
  ci:
    jobs:
      - fmt
      - clippy
      - test
      - release-build:
          requires: [fmt, clippy, test]
