version: "3"

vars:
  RUST_LOG: info

tasks:
  setup:
    desc: "Set up the development environment"
    cmds:
      - rustup update stable
      - cargo install prek || true
      - cargo install cargo-deny || true
      - cargo install cargo-machete || true
      - cargo install cargo-sort || true
      - cargo install cargo-edit || true
      - prek install
      - prek install --hook-type commit-msg

  update:
    desc: "Update dependencies and pre-commit hooks"
    cmds:
      - prek autoupdate
      - cargo update
      - gau --update .

  upgrade:
    desc: "Upgrade dependencies to latest (including incompatible) then update"
    cmds:
      - cargo upgrade --incompatible
      - task: update

  build:
    desc: "Build all crates"
    cmds:
      - cargo build --workspace

  build:release:
    desc: "Build release binary"
    cmds:
      - cargo build --release -p alef

  test:
    desc: "Run all tests"
    cmds:
      - cargo test --workspace

  test:release:
    desc: "Run all tests in release mode (catches release-only optimizer/lifetime issues)"
    cmds:
      - cargo test --release --workspace

  bench:
    desc: "Run codegen benchmarks (criterion)"
    cmds:
      - cargo bench

  check:
    desc: "Type-check all crates"
    cmds:
      - cargo check --workspace

  fmt:
    desc: "Format all code"
    cmds:
      - cargo fmt --all

  lint:
    desc: "Run all linters via prek"
    cmds:
      - prek run --all-files

  set-version:
    desc: "Set package version (usage: task set-version -- 0.18.0)"
    requires:
      vars: [CLI_ARGS]
    cmds:
      # Use `sed -i.bak ... && rm -f` for portability between BSD sed (macOS) and GNU sed (Linux CI).
      - |
        version="{{.CLI_ARGS}}"
        version="${version#v}"
        case "$version" in
          [0-9]*.[0-9]*.[0-9]*) ;;
          *) echo "set-version: expected semver like '0.18.0' (with or without leading 'v'); got '{{.CLI_ARGS}}'" >&2; exit 2 ;;
        esac
        sed -i.bak "s/^version = \".*\"/version = \"$version\"/" Cargo.toml
        rm -f Cargo.toml.bak
        sed -i.bak "s/^alef_version = \".*\"/alef_version = \"$version\"/" alef.toml
        rm -f alef.toml.bak
        sed -i.bak -E "s|pub const ALEF_REV: &str = \"v?[0-9]+\.[0-9]+\.[0-9]+\";|pub const ALEF_REV: \&str = \"v$version\";|" src/core/template_versions.rs
        rm -f src/core/template_versions.rs.bak
      - cargo update

  clean:
    desc: "Clean build artifacts"
    cmds:
      - cargo clean
      - rm -rf .alef/
