[tools]
rust = "1.93.0"

[tasks.build]
description = "Build, install, and push to running sidecars"
run = """
set -euo pipefail

# Build and install host binary
cargo install --path .

# Build Linux binary via Docker and cache it
docker build --target proxy -t kap-dev -f .devcontainer/Dockerfile .
CONTAINER=$(docker create kap-dev)
mkdir -p ~/.kap/dev
docker cp "$CONTAINER:/usr/local/bin/kap" ~/.kap/dev/kap-linux
docker rm "$CONTAINER"
echo "[build] cached Linux binary at ~/.kap/dev/kap-linux"

# Deploy to running sidecars
DEPLOYED=0
for sidecar in $(docker ps | awk 'NR>1 {print $NF}' | grep -E 'kap-kap|-kap-1$'); do
  echo "[build] deploying to $sidecar..."
  docker cp ~/.kap/dev/kap-linux "$sidecar:/usr/local/bin/kap"
  docker restart "$sidecar"
  DEPLOYED=$((DEPLOYED + 1))
done

echo ""
if [ "$DEPLOYED" -gt 0 ]; then
  echo "  Pushed to $DEPLOYED sidecar(s)"
fi
echo "  Next steps:"
echo "    kap exec ...       running sidecars already have the new binary"
echo "    kap up --reset     only if you changed the overlay/compose template"
"""
