# justfile - Build automation for asdf-acceleration-middleware
# https://just.systems/

# Default recipe (runs when you type `just`)
default:
    @just --list

# Build all crates in release mode
build:
    cargo build --release --all

# Build all crates in debug mode
build-debug:
    cargo build --all

# Run all tests
test:
    cargo test --all

# Run tests with output
test-verbose:
    cargo test --all -- --nocapture

# Run specific crate tests
test-crate crate:
    cargo test -p {{crate}}

# Run benchmarks
bench:
    cargo bench --all

# Run benchmarks for specific crate
bench-crate crate:
    cargo bench -p {{crate}}

# Format code
fmt:
    cargo fmt --all

# Check formatting without applying
fmt-check:
    cargo fmt --all -- --check

# Run clippy linter
clippy:
    cargo clippy --all -- -D warnings

# Run clippy with pedantic lints
clippy-pedantic:
    cargo clippy --all -- -D warnings -W clippy::pedantic

# Full lint (fmt + clippy)
lint: fmt clippy

# Check code without building
check:
    cargo check --all

# Clean build artifacts
clean:
    cargo clean

# Build documentation
doc:
    cargo doc --all --no-deps

# Build and open documentation
doc-open:
    cargo doc --all --no-deps --open

# Run security audit
audit:
    cargo audit

# Check for unsafe code
geiger:
    cargo geiger --all

# Full security check
security-check: audit geiger

# Check dependency licenses
license:
    cargo license

# Update dependencies
update:
    cargo update

# Install all CLI tools
install:
    cargo install --path crates/asdf-accelerate
    cargo install --path crates/asdf-bench
    cargo install --path crates/asdf-discover
    cargo install --path crates/asdf-monitor

# Uninstall all CLI tools
uninstall:
    cargo uninstall asdf-accelerate
    cargo uninstall asdf-bench
    cargo uninstall asdf-discover
    cargo uninstall asdf-monitor

# RSR compliance verification
rsr-verify:
    @echo "🔍 Verifying RSR compliance..."
    @echo ""
    @echo "✅ Type Safety: Rust compile-time guarantees"
    @cargo check --all 2>&1 | grep -q "Finished" && echo "✅ Memory Safety: Compiles without errors" || echo "❌ Compilation errors found"
    @echo "✅ Offline-First: No mandatory network dependencies"
    @test -f README.md && echo "✅ README.md exists" || echo "❌ README.md missing"
    @test -f LICENSE.txt && echo "✅ LICENSE.txt exists" || echo "❌ LICENSE.txt missing"
    @test -f SECURITY.md && echo "✅ SECURITY.md exists" || echo "❌ SECURITY.md missing"
    @test -f CONTRIBUTING.md && echo "✅ CONTRIBUTING.md exists" || echo "❌ CONTRIBUTING.md missing"
    @test -f CODE_OF_CONDUCT.md && echo "✅ CODE_OF_CONDUCT.md exists" || echo "❌ CODE_OF_CONDUCT.md missing"
    @test -f MAINTAINERS.md && echo "✅ MAINTAINERS.md exists" || echo "❌ MAINTAINERS.md missing"
    @test -f CHANGELOG.md && echo "✅ CHANGELOG.md exists" || echo "❌ CHANGELOG.md missing"
    @test -f .well-known/security.txt && echo "✅ .well-known/security.txt exists" || echo "❌ .well-known/security.txt missing"
    @test -f .well-known/ai.txt && echo "✅ .well-known/ai.txt exists" || echo "❌ .well-known/ai.txt missing"
    @test -f .well-known/humans.txt && echo "✅ .well-known/humans.txt exists" || echo "❌ .well-known/humans.txt missing"
    @test -f justfile && echo "✅ justfile exists" || echo "❌ justfile missing"
    @echo ""
    @echo "🎯 RSR Compliance: Silver (targeting Gold)"

# Run all validation checks
validate: fmt-check clippy test rsr-verify
    @echo ""
    @echo "✅ All validation checks passed!"

# CI simulation (what runs in CI/CD)
ci: fmt-check clippy test audit
    @echo ""
    @echo "✅ CI checks completed!"

# Development workflow (format, lint, test)
dev: fmt clippy test

# Full pre-commit check
pre-commit: validate

# Watch tests (requires cargo-watch)
watch:
    cargo watch -x test

# Watch tests for specific crate
watch-crate crate:
    cargo watch -x "test -p {{crate}}"

# Measure code coverage (requires cargo-tarpaulin)
coverage:
    cargo tarpaulin --all --out Html

# Profile with perf (Linux only)
profile binary:
    cargo build --release
    perf record -g ./target/release/{{binary}}
    perf report

# Generate flamegraph (requires cargo-flamegraph)
flamegraph binary:
    cargo flamegraph --bin {{binary}}

# Check binary size
size binary:
    cargo build --release
    ls -lh target/release/{{binary}}
    strip target/release/{{binary}}
    ls -lh target/release/{{binary}}

# Run all CLI tools (for testing)
run-all:
    cargo run -p asdf-accelerate -- --help
    cargo run -p asdf-bench -- --help
    cargo run -p asdf-discover -- --help
    cargo run -p asdf-monitor -- --help

# Create new release
release version:
    @echo "Creating release {{version}}"
    @echo "{{version}}" > VERSION
    @sed -i 's/^version = .*/version = "{{version}}"/' Cargo.toml
    git add Cargo.toml VERSION CHANGELOG.md
    git commit -m "chore: bump version to {{version}}"
    git tag -a v{{version}} -m "Release v{{version}}"
    @echo "Now run: git push && git push --tags"

# Show project stats
stats:
    @echo "📊 Project Statistics"
    @echo ""
    @echo "Lines of code:"
    @find crates -name "*.rs" | xargs wc -l | tail -1
    @echo ""
    @echo "Number of crates:"
    @ls -1 crates | wc -l
    @echo ""
    @echo "Dependencies:"
    @cargo tree --depth 1 | wc -l
    @echo ""
    @echo "Test count:"
    @rg "#\[test\]" crates --count-matches | awk -F: '{sum+=$2} END {print sum}'

# Help for specific recipe
help recipe:
    @just --show {{recipe}}

secret-scan-trufflehog:
    @command -v trufflehog >/dev/null && trufflehog filesystem . --only-verified || true
