# tls-ktls integration workflows (#78).
#
# Linux only. Enter the root devshell with `nix develop ../..`, or let direnv
# do it via .envrc.

[private]
default:
    @just --list

[doc("Remove local build artifacts")]
[group("check")]
clean:
    rm -rf zig-out .zig-cache

[doc("Build the package")]
[group("check")]
build:
    zig build

[doc("Compile every example")]
[group("check")]
build-examples:
    zig build build-examples

[doc("Run unit and live kernel tests")]
[group("check")]
test:
    zig build test --summary all

[doc("Format Zig and Just sources")]
[group("check")]
fmt: fmt-zig fmt-just

[doc("Lint Zig and Just sources")]
[group("check")]
lint: lint-zig lint-just

[doc("Format Zig code")]
[group("check")]
fmt-zig:
    zig fmt build.zig src/ examples/

[doc("Lint Zig code (fmt, ziglint, and workspace std-alias rules)")]
[group("check")]
lint-zig:
    zig fmt --check build.zig src/ examples/
    ziglint build.zig src/ examples/
    rc=0; for rule in ../../rules/std-alias-*.yml; do ast-grep scan --rule "$rule" src examples --globs '*.zig' --report-style short || rc=1; done; exit $rc

[doc("Format Justfile")]
[group("check")]
fmt-just:
    just --fmt --unstable --justfile justfile

[doc("Lint Justfile")]
[group("check")]
lint-just:
    just --check --fmt --unstable --justfile justfile

[doc("Rerun kernel tests N times to shake out timing-dependent flakes")]
[group("check")]
flake-check N="20":
    #!/usr/bin/env bash
    set -euo pipefail
    for i in $(seq 1 {{ N }}); do
        if ! zig build test > /dev/null 2>&1; then
            echo "flaked on run $i of {{ N }}" >&2
            zig build test >&2
            exit 1
        fi
    done
    echo "{{ N }} consecutive green runs"

[doc("Run all CI gates for this integration")]
[group("check")]
ci: lint test build-examples
