set windows-shell := ["pwsh.exe", "-NoProfile", "-Command"]

core-packages := "-p common -p codeindex -p configsupport -p runtimekit -p managed -p unitysupport -p windowsupport"
code-packages := "-p codeindex -p fileprobe -p outline -p snip -p chunkcat -p hitsnip -p diagpick -p defsnip -p codeshape -p refs -p context"
data-packages := "-p cjson -p ison -p isonl -p zon -p tonl -p jsonlgrep -p jsonshape -p mercury-mhash -p toon -p csvshape -p sqliteshape -p sqlshape -p stringscan"
runtime-packages := "-p runtimekit -p runprobe -p await -p argv -p recent -p pathshadow -p sysshape -p portping -p logshape -p config -p gitshape -p reposhape -p dotnetshape -p binmeta"
windows-packages := "-p windowsupport -p msudo -p envdiff -p proctree -p unlock -p portunlock"
managed-packages := "-p managed -p asmtype -p asmmember -p asmref -p asmapi -p asmflow -p llvmtools -p petools"
unity-packages := "-p unitysupport -p unityasset -p unityprobe -p unitydiag"
clippy-flags := "-- -D warnings -W clippy::pedantic -W clippy::nursery"

default:
    @just --list

fmt:
    cargo fmt --all

fmt-check:
    cargo fmt --all --check

check:
    cargo check --all-targets --all-features

workspace-check:
    just fmt-check
    just check
    just build
    just ai-check
    just powershell

commit-check:
    pwsh -NoProfile -File .\scripts\check-conventional-commits.ps1

release-version-check tag:
    pwsh -NoProfile -File .\scripts\check-release-version.ps1 -Tag {{tag}}

changelog:
    git-cliff --config cliff.toml --output CHANGELOG.md

changelog-unreleased:
    git-cliff --config cliff.toml --unreleased

core-check:
    cargo check {{core-packages}} --all-targets --all-features

code-check:
    cargo check {{code-packages}} --all-targets --all-features

data-check:
    cargo check {{data-packages}} --all-targets --all-features

runtime-check:
    cargo check {{runtime-packages}} --all-targets --all-features

windows-check:
    cargo check {{windows-packages}} --all-targets --all-features

managed-check:
    cargo check {{managed-packages}} --all-targets --all-features

unity-check:
    cargo check {{unity-packages}} --all-targets --all-features

msudo-check:
    cargo check -p msudo --all-targets --all-features

test:
    cargo nextest run --all-features

stable-test:
    $env:CARGO_INCREMENTAL = '0'; cargo nextest run --all-features --run-ignored all

core-test:
    cargo nextest run {{core-packages}} --all-features

code-test:
    cargo nextest run {{code-packages}} --all-features

data-test:
    cargo nextest run {{data-packages}} --all-features

runtime-test:
    cargo nextest run {{runtime-packages}} --all-features

windows-test:
    cargo nextest run {{windows-packages}} --all-features

managed-test:
    cargo nextest run {{managed-packages}} --all-features

unity-test:
    cargo nextest run {{unity-packages}} --all-features

msudo-test:
    cargo nextest run -p msudo --all-features

clippy:
    cargo clippy --all-targets --all-features {{clippy-flags}}

core-clippy:
    cargo clippy {{core-packages}} --all-targets --all-features {{clippy-flags}}

code-clippy:
    cargo clippy {{code-packages}} --all-targets --all-features {{clippy-flags}}

data-clippy:
    cargo clippy {{data-packages}} --all-targets --all-features {{clippy-flags}}

mhash-check:
    cargo check -p mercury-mhash --all-targets --all-features

mhash-test:
    cargo nextest run -p mercury-mhash --all-features

mhash-clippy:
    cargo clippy -p mercury-mhash --all-targets --all-features {{clippy-flags}}

mhash-bench:
    cargo run --profile release-fast -p mercury-mhash --bin mhash -- bench --algorithm sha256,blake3-256 README.md

mhash-benchmark:
    pwsh -NoProfile -ExecutionPolicy Bypass -File ./scripts/benchmark-mhash.ps1

runtime-clippy:
    cargo clippy {{runtime-packages}} --all-targets --all-features {{clippy-flags}}

windows-clippy:
    cargo clippy {{windows-packages}} --all-targets --all-features {{clippy-flags}}

managed-clippy:
    cargo clippy {{managed-packages}} --all-targets --all-features {{clippy-flags}}

unity-clippy:
    cargo clippy {{unity-packages}} --all-targets --all-features {{clippy-flags}}

msudo-clippy:
    cargo clippy -p msudo --all-targets --all-features {{clippy-flags}}

udeps:
    cargo +nightly udeps --all-targets --all-features

deny:
    cargo deny check

build:
    cargo build --profile release-fast --workspace

build-debug:
    cargo build --workspace

msudo-build:
    cargo build -p msudo

build-release:
    cargo build --release --workspace

build-size:
    cargo build --profile release-size --workspace

slim:
    cargo build --profile release-fast -p codeshape -p refs -p defsnip

coverage:
    cargo llvm-cov nextest --all-features --summary-only --ignore-filename-regex 'cli\.rs$|main\.rs$|flow_opcode_table\.rs$' --fail-under-lines 80

jade:
    pwsh -NoProfile -File .\scripts\check-jade.ps1 -VerificationConfiguration ReleaseFast

jade-skip-coverage:
    pwsh -NoProfile -File .\scripts\check-jade.ps1 -SkipCoverage -VerificationConfiguration ReleaseFast

miri:
    cargo +nightly miri setup
    cargo +nightly miri test -p common --test miri_json_family

fuzz:
    pwsh -NoProfile -File .\scripts\check-jade-hardening.ps1 -Only Fuzz

sanitizer:
    pwsh -NoProfile -File .\scripts\check-jade-hardening.ps1 -Only Sanitizers

no-panic:
    pwsh -NoProfile -File .\scripts\check-no-panic.ps1

loom:
    cargo nextest run -p runtimekit --test loom_capture

ecosystem:
    pwsh -NoProfile -File .\scripts\check-ecosystem.ps1 -Configuration ReleaseFast

ecosystem-fast:
    cargo build --profile release-fast --workspace
    pwsh -NoProfile -File .\scripts\check-ecosystem.ps1 -Configuration ReleaseFast -SkipBuild -SkipPromptGeneration

powershell:
    pwsh -NoProfile -File .\scripts\check-powershell.ps1

gitea-ci-check:
    pwsh -NoProfile -File .\scripts\check-gitea-ci.ps1 -Wait

gitea-ci-dispatch:
    pwsh -NoProfile -File .\scripts\check-gitea-ci.ps1 -DispatchIfMissing -Wait

prompt:
    pwsh -NoProfile -File .\scripts\generate-ai-prompt.ps1

prompt-check:
    pwsh -NoProfile -File .\scripts\check-ai-prompt.ps1 -Configuration ReleaseFast -SkipBuild

skill:
    pwsh -NoProfile -File .\scripts\generate-ai-skill.ps1

skill-check:
    pwsh -NoProfile -File .\scripts\check-ai-skill.ps1 -Configuration ReleaseFast -SkipBuild

ai-check:
    just prompt-check
    just skill-check

periphery-check:
    just --summary
    just powershell
    just ai-check

assets:
    just prompt
    just skill

install:
    pwsh -NoProfile -File .\scripts\install-toolbox.ps1

uninstall:
    pwsh -NoProfile -File .\scripts\uninstall-toolbox.ps1

package:
    pwsh -NoProfile -File .\scripts\package-toolbox.ps1

publish-release tag notes_path:
    pwsh -NoProfile -File .\scripts\publish-gitea-release.ps1 -Tag {{tag}} -NotesPath {{notes_path}}

release-local tag notes_path:
    just jade
    just package
    just publish-release {{tag}} {{notes_path}}

install-package:
    $installer = Get-ChildItem -LiteralPath .\dist -Recurse -File -Filter install-package-toolbox.ps1 | Sort-Object LastWriteTime -Descending | Select-Object -First 1; if ($null -eq $installer) { throw 'No package installer found under .\dist. Run `just package` first.' }; & $installer.FullName

uninstall-package:
    pwsh -NoProfile -File .\scripts\uninstall-package-toolbox.ps1

package-roundtrip:
    just package
    just install-package
    just uninstall-package

probe:
    pwsh -NoProfile -File .\scripts\probe-toolbox.ps1

probe-fast:
    pwsh -NoProfile -File .\scripts\probe-toolbox.ps1 -BuildProfile ReleaseFast -Warmup 1 -Runs 4

probe-size:
    pwsh -NoProfile -File .\scripts\probe-toolbox.ps1 -BuildProfile ReleaseSize -SkipBenchmarks

msudo-help:
    cargo run -p msudo -- --help

msudo-run-help:
    cargo run -p msudo -- run --help

msudo-status:
    cargo run -p msudo -- status --json

msudo-status-fast:
    cargo build --profile release-fast -p msudo
    .\target\release-fast\msudo.exe status --json

msudo-verify:
    just msudo-check
    just msudo-test
    cargo build --profile release-fast --workspace
    just prompt-check
    just skill-check
    just ecosystem-fast

probe-bloat:
    pwsh -NoProfile -File .\scripts\probe-toolbox.ps1 -BuildProfile ReleaseFast -IncludeBloat -IncludeLlvmLines

probe-flamegraph:
    pwsh -NoProfile -File .\scripts\probe-toolbox.ps1 -BuildProfile ReleaseFast -IncludeFlamegraph

jade-tools:
    pwsh -NoProfile -File .\scripts\install-jade-tooling.ps1
