version: "3"
internal: true

includes:
  platforms: ../config/platforms.yml

vars:
  RUST_LOG: '{{.RUST_LOG | default "info"}}'
  BUILD_PROFILE: '{{.BUILD_PROFILE | default "release"}}'
  RUST_BACKTRACE: '{{.RUST_BACKTRACE | default "1"}}'
  CARGO_TERM_COLOR: "always"

tasks:
  install:
    desc: "Install Rust toolchain and components"
    cmds:
      - rustup update stable
      - rustup component add rustfmt clippy
      # Windows: install lld-link to avoid PATH conflicts with Git Bash's link.exe
      - cmd: rustup component add llvm-tools
        platforms: [windows]
      - cargo install cargo-llvm-cov --locked
      - cargo install cargo-edit --locked
      - cargo --version
      - rustc --version

  build:
    desc: "Build all Rust crates with {{.BUILD_PROFILE}} profile"
    cmds:
      - task: build:{{.BUILD_PROFILE | default "release"}}

  build:dev:
    desc: "Build all Rust crates in debug mode"
    cmds:
      - cmd: cargo build --workspace --all-features --exclude kreuzberg-php --exclude kreuzberg-node --exclude kreuzberg-wasm
        platforms: [linux, darwin]
      # Note: exclude benchmark-harness on Windows as jemalloc doesn't build with MSVC
      - cmd: cargo build --workspace --all-features --exclude kreuzberg-php --exclude kreuzberg-node --exclude kreuzberg-wasm --exclude benchmark-harness
        platforms: [windows]

  build:release:
    desc: "Build all Rust crates in release mode"
    cmds:
      - cmd: cargo build --release --workspace --all-features --exclude kreuzberg-php --exclude kreuzberg-node --exclude kreuzberg-wasm
        platforms: [linux, darwin]
      - cmd: cargo build --release --workspace --all-features --exclude kreuzberg-php --exclude kreuzberg-node --exclude kreuzberg-wasm --exclude benchmark-harness
        platforms: [windows]

  build:ci:
    desc: "Build for CI with debug info enabled"
    env:
      RUSTFLAGS: "-C debuginfo=2"
    cmds:
      - cmd: cargo build --release --workspace --all-features --exclude kreuzberg-php --exclude kreuzberg-node --exclude kreuzberg-wasm
        platforms: [linux, darwin]
      - cmd: cargo build --release --workspace --all-features --exclude kreuzberg-php --exclude kreuzberg-node --exclude kreuzberg-wasm --exclude benchmark-harness
        platforms: [windows]

  build:profiling:
    desc: "Build all Rust crates with profiling features (for flamegraph generation)"
    env:
      ENABLE_PROFILING: "true"
      RUSTFLAGS: "-g"
    cmds:
      - cargo build --workspace --release --features full,profiling,api,mcp,otel --exclude kreuzberg-php --exclude kreuzberg-node --exclude kreuzberg-wasm
      - cargo build --manifest-path tools/benchmark-harness/Cargo.toml --release --features profiling

  cli:build:
    desc: "Build CLI binary"
    cmds:
      - cargo build --release --package kreuzberg-cli {{if .TARGET}}--target {{.TARGET}}{{end}}

  cli:build:dev:
    desc: "Build CLI binary in debug mode"
    cmds:
      - cargo build --package kreuzberg-cli

  cli:install:
    desc: "Install CLI binary"
    cmds:
      - cargo install --path crates/kreuzberg-cli

  ffi:build:
    desc: "Build FFI library for language bindings"
    cmds:
      - cargo build --release --package kreuzberg-ffi {{.CARGO_ARGS | default ""}}

  ffi:build:dev:
    desc: "Build FFI library in debug mode"
    cmds:
      - cargo build --package kreuzberg-ffi {{.CARGO_ARGS | default ""}}

  ffi:build:ci:
    desc: "Build FFI library in CI mode (with debug info)"
    env:
      RUSTFLAGS: "-C debuginfo=2"
    cmds:
      - cargo build --release --package kreuzberg-ffi {{.CARGO_ARGS | default ""}}

  wasm:build:all:
    desc: "Build all WASM targets (web/bundler/nodejs/deno) plus the TypeScript wrapper"
    dir: crates/kreuzberg-wasm
    cmds:
      - pnpm run build:all

  test:
    desc: "Run all Rust tests"
    cmds:
      - cmd: bash scripts/ci/rust/run-unit-tests.sh
        platforms: [linux, darwin]

  test:ci:
    desc: "Run Rust tests in CI mode with tessdata setup"
    env:
      RUST_BACKTRACE: "{{.RUST_BACKTRACE}}"
      CARGO_TERM_COLOR: "{{.CARGO_TERM_COLOR}}"
    cmds:
      - cmd: bash scripts/ci/rust/run-unit-tests.sh
        platforms: [linux, darwin]

  test:quick:
    desc: "Run fast Rust tests (unit tests only)"
    cmds:
      - cargo test --lib --workspace --exclude kreuzberg-php --exclude kreuzberg-node --exclude kreuzberg-wasm

  lint:
    desc: "Lint Rust code WITH auto-fix (cargo fmt + cargo clippy --fix)"
    cmds:
      - cargo fmt --all
      - cargo clippy --workspace --fix --allow-dirty --allow-staged --exclude kreuzberg-php --exclude kreuzberg-node --exclude kreuzberg-wasm

  lint:check:
    desc: "Lint Rust code WITHOUT auto-fix (check-only)"
    cmds:
      - cargo fmt --all -- --check
      - cargo clippy --workspace --exclude kreuzberg-php --exclude kreuzberg-node --exclude kreuzberg-wasm -- -D warnings

  format:
    desc: "Format Rust code (with modifications)"
    cmds:
      - cargo fmt --all

  format:check:
    desc: "Check Rust formatting without modifications"
    cmds:
      - cargo fmt --all -- --check

  licenses:
    desc: "Check Rust dependency licenses with cargo-deny"
    cmds:
      - cargo deny check licenses

  doc:
    desc: "Generate Rust documentation and open in browser"
    cmds:
      - cargo doc --no-deps --open

  doc:build:
    desc: "Build Rust documentation (without opening)"
    cmds:
      - cargo doc --no-deps

  clean:
    desc: "Clean Rust build artifacts"
    cmds:
      - cargo clean

  cache:cleanup:
    desc: "Clean up large build artifacts to reduce cache size (for CI)"
    cmds:
      - echo "Cleaning up large build artifacts to reduce cache size..."
      - cmd: find target -type f -name "*.rlib" -size +10M -exec rm -f {} \; 2>/dev/null || true
        ignore_error: true
      - cmd: find target -type f -name "*.so" -size +10M -exec rm -f {} \; 2>/dev/null || true
        ignore_error: true
      - cmd: find target -type f -name "*.dylib" -size +10M -exec rm -f {} \; 2>/dev/null || true
        ignore_error: true
      - cmd: find target -type f -name "*.dll" -size +10M -exec rm -f {} \; 2>/dev/null || true
        ignore_error: true
      - cmd: rm -rf target/*/incremental 2>/dev/null || true
        ignore_error: true
      - echo "Cleanup completed successfully"
      - cmd: du -sh target 2>/dev/null || echo "No target directory found"
        ignore_error: true

  update:
    desc: "Update Rust dependencies (compatible only)"
    cmds:
      - cargo update
      - cmd: cargo update --manifest-path packages/ruby/ext/kreuzberg_rb/native/Cargo.toml
        ignore_error: true

  upgrade:
    desc: "Upgrade Rust dependencies aggressively (cargo upgrade --incompatible)"
    cmds:
      - cargo upgrade --incompatible
      - cargo update
      - cmd: cargo upgrade --incompatible --manifest-path packages/ruby/ext/kreuzberg_rb/native/Cargo.toml
        ignore_error: true
      - cmd: cargo update --manifest-path packages/ruby/ext/kreuzberg_rb/native/Cargo.toml
        ignore_error: true

  e2e:format:
    desc: "Format generated Rust E2E sources"
    cmds:
      - cargo fmt --manifest-path e2e/rust/Cargo.toml

  e2e:lint:
    desc: "Lint Rust E2E crate with clippy"
    cmds:
      - cargo clippy --manifest-path e2e/rust/Cargo.toml -- -D warnings

  e2e:test:
    desc: "Run Rust E2E tests"
    env:
      RUST_TEST_THREADS: "1"
    cmds:
      - cargo test --manifest-path e2e/rust/Cargo.toml --release

  check:android:
    desc: "cargo check kreuzberg-dart + kreuzberg-ffi for Android ABIs (requires ANDROID_NDK_HOME and cargo-ndk)"
    preconditions:
      - sh: command -v cargo-ndk
        msg: "cargo-ndk not installed. Run: cargo install cargo-ndk --locked"
      - sh: 'test -n "${ANDROID_NDK_HOME:-}"'
        msg: "ANDROID_NDK_HOME is not set. Install Android NDK and export ANDROID_NDK_HOME=<path-to-ndk>"
    cmds:
      - cargo ndk --target arm64-v8a --platform 21 -- check -p kreuzberg-dart
      - cargo ndk --target x86_64 --platform 21 -- check -p kreuzberg-dart
      - cargo ndk --target arm64-v8a --platform 21 -- check -p kreuzberg-ffi
      - cargo ndk --target x86_64 --platform 21 -- check -p kreuzberg-ffi

  check:ios:
    desc: "cargo check kreuzberg-dart + kreuzberg-swift for iOS targets (macOS only)"
    platforms: [darwin]
    cmds:
      - rustup target add aarch64-apple-ios aarch64-apple-ios-sim
      - cargo check -p kreuzberg-dart --target aarch64-apple-ios
      - cargo check -p kreuzberg-dart --target aarch64-apple-ios-sim
      - cargo check -p kreuzberg-swift --target aarch64-apple-ios
      - cargo check -p kreuzberg-swift --target aarch64-apple-ios-sim

  check:mobile:
    desc: "cargo check Android + iOS mobile targets"
    cmds:
      - task: check:android
      - task: check:ios
