[tool_alias]
railpack = "github:railwayapp/railpack"
buildkit = "github:moby/buildkit"

[tools]
minikube = "1.38.1"
pack = "0.40.6"
railpack = "0.15.1"
buildkit = "0.28.0"
"cargo:sqlx-cli" = "latest"
"cargo:cargo-all-features" = "latest"
helm = "latest"
rust = { version = "1.95.0", profile = "default" }
kubectl = "latest"
k9s = "latest"
vault = "latest"

[tasks.setup]
description = "One-stop dev environment setup (hosts, docker, cluster). Cross-platform (Linux + macOS). Run interactively."
run = "./scripts/dev-setup.sh"

[tasks.down]
description = "Tear down what 'mise setup' brought up (cluster, .env block, daemon.json registries, /etc/hosts block). Interactive."
run = "./scripts/dev-setup.sh down"

[tasks.dev]
run = """
issues=""
for h in rise-registry rise-jfrog rise.local; do
  if ! awk -v h="$h" '{for(i=1;i<=NF;i++) if($i==h){found=1;exit}} END{exit !found}' /etc/hosts 2>/dev/null; then
    issues="${issues}\n  - /etc/hosts missing '$h'  →  mise setup hosts"
  fi
done
if ! docker info > /dev/null 2>&1; then
  issues="${issues}\n  - Docker is not running"
fi
if ! kubectl cluster-info > /dev/null 2>&1; then
  issues="${issues}\n  - No Kubernetes cluster reachable  →  mise setup minikube"
fi
if [ ! -f .env ] || ! grep -q '^# BEGIN RISE K8S DEV$' .env 2>/dev/null; then
  issues="${issues}\n  - .env missing rise-managed block (RISE_K8S_HOST_IP, RISE_AUTH_BACKEND_URL, ...)  →  mise setup"
fi
if [ -n "$issues" ]; then
  printf "Preflight issues:\n%b\n\n" "$issues"
  printf "Continue anyway? [y/N] "
  read -r answer
  case "$answer" in
    [yY]*) ;;
    *) exit 1 ;;
  esac
fi

docker compose up -d
mise run db:migrate
set -m
mise run frontend:dev &
FE_PID=$!
trap 'kill -TERM -$FE_PID 2>/dev/null; wait $FE_PID 2>/dev/null' EXIT INT TERM
mise run backend:run
"""

[tasks."sqlx:prepare"]
description = "Prepare the SQLX query cache that needs to be committed for building with SQLX_OFFLINE=true."
run = "cargo sqlx prepare -- --package rise-deploy --features cli,backend --all-targets"

[tasks."sqlx:check"]
description = "Check that all SQLX queries are valid."
run = "cargo sqlx prepare --check -- --package rise-deploy --features cli,backend --all-targets"

[tasks."resource-store:sqlx:prepare"]
description = "Prepare the SQLX query cache for rise-resource-store (requires running DB)."
run = "cargo sqlx prepare -- --package rise-resource-store --all-targets"

[tasks."resource-store:sqlx:check"]
description = "Verify the SQLX query cache for rise-resource-store is up to date."
run = "cargo sqlx prepare --check -- --package rise-resource-store --all-targets"

[tasks.lint]
description = "Run all linting checks (clippy, formatting, sqlx, helm)."
run = """
cargo all-features check --all-targets
cargo check --package rise-resource-api --all-targets
cargo check --package rise-resource-store --all-targets
cargo all-features clippy --all-targets -- -D warnings
cargo clippy --package rise-resource-api --all-targets -- -D warnings
cargo clippy --package rise-resource-store --all-targets -- -D warnings
cargo fmt --all -- --check
mise sqlx:check
mise resource:schema:check
helm lint helm/rise
"""
env.SQLX_OFFLINE = true

[tasks."helm:deps"]
description = "Refresh vendored Helm chart dependencies under helm/rise/charts."
run = """
helm_repo_state=$(mktemp -d)
trap 'rm -rf "$helm_repo_state"' EXIT
mkdir -p "$helm_repo_state/cache"
printf 'repositories: []\n' > "$helm_repo_state/repositories.yaml"
helm repo add rise-grafana https://grafana.github.io/helm-charts \
  --repository-config "$helm_repo_state/repositories.yaml" \
  --repository-cache "$helm_repo_state/cache"
helm dependency build helm/rise \
  --repository-config "$helm_repo_state/repositories.yaml" \
  --repository-cache "$helm_repo_state/cache"
"""

[tasks."config:schema:generate"]
description = "Generate the backend settings JSON schema."
run = "cargo run --package rise-deploy --features cli,backend -- backend config-schema > docs/engineering/public/schemas/backend-settings.schema.json"
env.SQLX_OFFLINE = true

[tasks."config:schema:check"]
description = "Verify the committed backend settings JSON schema is up to date."
run = "mise config:schema:generate && git --no-pager diff --exit-code -- docs/engineering/public/schemas/backend-settings.schema.json"
env.SQLX_OFFLINE = true

[tasks."resource:schema:generate"]
description = "Generate JSON schemas for the generic resource API."
run = "cargo run --package rise-deploy --features cli,backend -- backend schemas generate --out-dir docs/engineering/public/schemas"
env.SQLX_OFFLINE = true

[tasks."resource:schema:check"]
description = "Verify the committed resource API JSON schemas are up to date."
run = """
mise resource:schema:generate
git --no-pager diff --exit-code -- \
  docs/engineering/public/schemas/resource-envelope.schema.json \
  docs/engineering/public/schemas/resource-metadata.schema.json \
  docs/engineering/public/schemas/controller-status-map.schema.json \
  docs/engineering/public/schemas/organization.schema.json \
  docs/engineering/public/schemas/resource-definition.schema.json
"""
env.SQLX_OFFLINE = true

[tasks."rise-toml:schema:generate"]
description = "Generate the rise.toml JSON schema."
run = "cargo run --package rise-deploy --features cli,backend -- backend rise-toml-schema > docs/user/public/schemas/rise-toml-v1.schema.json"
env.SQLX_OFFLINE = true

[tasks."rise-toml:schema:check"]
description = "Verify the committed rise.toml JSON schema is up to date."
run = "mise rise-toml:schema:generate && git --no-pager diff --exit-code -- docs/user/public/schemas/rise-toml-v1.schema.json"
env.SQLX_OFFLINE = true

[tasks."crd:generate"]
description = "Generate the RiseProject CRD YAML for the Helm chart."
run = "cargo run --package rise-deploy --features cli,backend -- backend crd-schema > helm/rise/crds/riseproject-crd.yaml"
env.SQLX_OFFLINE = true

[tasks."crd:check"]
description = "Verify the committed CRD YAML is up to date with the Rust definition."
run = "mise crd:generate && git --no-pager diff --exit-code -- helm/rise/crds/riseproject-crd.yaml"
env.SQLX_OFFLINE = true

[tasks."docs:install"]
description = "Install documentation dependencies (shared workspace)."
dir = "docs"
run = "npm install"

[tasks."docs:serve"]
description = "Serve bundled user documentation with live reload on port 3001."
depends = ["docs:install"]
dir = "docs/user"
run = "npm run dev"

[tasks."docs:build"]
description = "Build the bundled user documentation."
depends = ["docs:install"]
dir = "docs/user"
run = "npm run build"

[tasks."docs:engineering:serve"]
description = "Serve engineering documentation with live reload on port 3002."
depends = ["docs:install"]
dir = "docs/engineering"
run = "npm run dev"

[tasks."docs:engineering:build"]
description = "Build the engineering documentation."
depends = ["docs:install"]
dir = "docs/engineering"
run = "npm run build"

[tasks."db:nuke"]
description = "Drop and recreate the development database. USE WITH CAUTION."
run = """
set -x
docker compose down -v postgres
docker compose up postgres -d
"""

[tasks."db:migrate"]
description = "Run database migrations. Run before building the backend."
run = "docker compose up postgres -d && sleep 2 && sqlx migrate run"

[tasks."backend:deps"]
description = "Start all backend dependencies with docker compose."
run = "docker compose up -d"

[tasks."backend:run"]
description = "Run the backend server."
depends = ["backend:deps", "db:migrate"]
run = """
if [ -f .env ]; then
  . ./.env
else
  printf "Error: .env not found. Run 'mise setup' first — it writes the\n  RISE_K8S_HOST_IP / RISE_AUTH_BACKEND_URL block the backend needs.\n" >&2
  exit 1
fi
if [ -z "${RISE_AUTH_BACKEND_URL:-}" ] || [ -z "${RISE_K8S_HOST_IP:-}" ]; then
  printf "Error: .env is missing the rise-managed block (RISE_K8S_HOST_IP, RISE_AUTH_BACKEND_URL).\n  Run 'mise setup' to (re)write it.\n" >&2
  exit 1
fi
cargo run --features cli,backend -- backend server
"""
alias = "br"
env.https_proxy = ""
env.HTTPS_PROXY = ""

[tasks."backend:run-container"]
description = "Build and run the backend server in its container (host network). Useful to validate the containerized version works as intended."
depends = ["backend:deps", "db:migrate"]
run = """
if [ -f .env ]; then
  . ./.env
else
  printf "Error: .env not found. Run 'mise setup' first — it writes the\n  RISE_K8S_HOST_IP / RISE_AUTH_BACKEND_URL block the backend needs.\n" >&2
  exit 1
fi
if [ -z "${RISE_AUTH_BACKEND_URL:-}" ] || [ -z "${RISE_K8S_HOST_IP:-}" ]; then
  printf "Error: .env is missing the rise-managed block (RISE_K8S_HOST_IP, RISE_AUTH_BACKEND_URL).\n  Run 'mise setup' to (re)write it.\n" >&2
  exit 1
fi
docker build . -t rise --target rise && docker run --rm --network host -v $PWD/config:/etc/rise:ro -v $HOME/.kube:/root/.kube:ro -v $HOME/.minikube:$HOME/.minikube:ro -e DATABASE_URL=$DATABASE_URL -e RISE_CONFIG_DIR=/etc/rise -e RISE_CONFIG_RUN_MODE=development -e RISE_AUTH_BACKEND_URL=$RISE_AUTH_BACKEND_URL -e RISE_K8S_HOST_IP=$RISE_K8S_HOST_IP -e RISE_LOKI_URL=${RISE_LOKI_URL:-http://localhost:3100} rise backend server
"""

[tasks."frontend:dev"]
description = "Run the Vite frontend development server."
run = "cd frontend && npm install && npm run dev"

[tasks."frontend:build"]
description = "Build the frontend bundle for embedding in the backend binary."
run = """
cd frontend
npm install
npm run build
cp dist/index.html ../static/
cp -r dist/assets/* ../static/assets/
"""

[tasks."dist:build"]
description = "Build local release artifacts using cargo-dist."
run = """
cargo install cargo-dist --locked
cargo dist build --target x86_64-unknown-linux-gnu --target x86_64-apple-darwin --target aarch64-apple-darwin
"""

[tasks."test:e2e-build"]
description = "Run end-to-end build backend tests (all or specific backends)."
run = "bash tests/e2e-build/run.sh"

[tasks."dev:oidc-issuer"]
run = """
( sleep 2 && curl http://localhost:9229/token -d '{"sub": "test", "aud": "http://rise.local:3000"}' ) &
npx @oidc.pub/cli dev issuer --url http://localhost:9229
"""
