version: '3'

vars:
  # Per ../Taskfile.yml header: joinPath is evaluated eagerly by go-task
  # templating and yields a native-separator absolute path so {{.DEFT_ROOT}}
  # resolves correctly under node on Windows (#566).
  DEFT_ROOT: '{{joinPath .TASKFILE_DIR ".."}}'

tasks:

  install:
    desc: Install deft (dev convenience -- end users should use the compiled binary from GitHub Releases)
    cmds:
      - go run ./cmd/deft-install/

  uninstall:
    desc: Remove deft entry from AGENTS.md
    dir: '{{.USER_WORKING_DIR}}'
    deps:
      - task: :engine:_ts-build
    cmds:
      - task: :engine:invoke
        vars:
          ENGINE_CMD: 'install-uninstall --project-root "{{.USER_WORKING_DIR}}"'

  # User-facing upgrade entrypoint (#1061). Native deft-ts handler (#2022 Phase 2):
  #   1. Writes / refreshes the bare ``.deft-version`` marker.
  #   2. Writes the canonical YAML provenance manifest at
  #      ``<install>/VERSION`` (#1046 PR-B AC-4) -- including the
  #      ``install_root`` field added in #1062.
  #   3. Delegates to ``agents:refresh`` so the AGENTS.md managed
  #      section is brought to the current rendered template (v3 marker
  #      with sha/refreshed/session attributes, #1046 PR-B AC-5).
  # This wrapper exists so the ``framework:doctor`` failure prose and
  # ``docs/install-manifest.md`` can cite a real Taskfile target rather
  # than a raw ``run`` invocation. Idempotent on re-run: a second
  # invocation against a current manifest + AGENTS.md is a no-op.
  #
  # Per ``conventions/task-caching.md`` (#574): no ``sources:`` /
  # ``generates:`` because the task is mutating + must run on every
  # invocation. CLI_ARGS is explicitly cleared (``vars: { CLI_ARGS: "" }``)
  # so any operator-supplied ``-- <flags>`` are deterministically dropped
  # before reaching the upgrade handler: go-task only forwards ``{{.CLI_ARGS}}``
  # when a ``cmds`` line references it, but pinning the var to empty makes
  # the contract explicit AND defends against a future edit that
  # accidentally appends ``{{.CLI_ARGS}}`` to the dispatch line (SLizard P1
  # on PR #1067 -- the upgrade handler does not currently accept flags; future
  # flag additions land deliberately, not implicitly via Taskfile drift).
  upgrade:
    desc: "Upgrade deft framework: refresh AGENTS.md to current marker, write canonical install manifest, regenerate .deft-version derivative (#1061). Dispatches via deft-ts install-upgrade (vendored bin.js in source checkouts, global deft on npm consumer deposits -- #2054)."
    dir: '{{.USER_WORKING_DIR}}'
    vars:
      CLI_ARGS: ""
    # #2054: route through the consumer-aware engine dispatch (engine:_ts-build is a
    # no-op on vendored deposits; engine:invoke falls back to global `deft`). The
    # previous `_ts-build` dep unconditionally ran `pnpm run build` inside the
    # `@deftai/directive-content` payload -- which has no `build` script -- so
    # `task upgrade` failed on the primary npm consumer workflow. Framework-root is
    # intentionally omitted: install-upgrade auto-detects the consumer's
    # `.deft/core/VERSION` (#2053) when run from the global engine.
    deps:
      - task: :engine:_ts-build
    cmds:
      - task: :engine:invoke
        vars:
          ENGINE_CMD: 'install-upgrade --project-root "{{.USER_WORKING_DIR}}"'
