# justfile for libtmux
# https://just.systems/

set shell := ["bash", "-uc"]

# File patterns
py_files := "find . -type f \\( -path './.github/scripts/*' -o -not -path '*/\\.*' \\) | grep -i '.*[.]py$' 2> /dev/null"
doc_files := "find . -type f -not -path '*/\\.*' | grep -i '.*[.]rst$\\|.*[.]md$\\|.*[.]css$\\|.*[.]py$\\|mkdocs\\.yml\\|CHANGES\\|TODO\\|.*conf\\.py' 2> /dev/null"
all_files := "find . -type f -not -path '*/\\.*' | grep -i '.*[.]py$\\|.*[.]rst$\\|.*[.]md$\\|.*[.]css$\\|.*[.]py$\\|mkdocs\\.yml\\|CHANGES\\|TODO\\|.*conf\\.py' 2> /dev/null"

# List all available commands
default:
    @just --list

# Run tests with pytest
[group: 'test']
test *args:
    uv run py.test {{ args }}

# Run tests then start continuous testing with pytest-watcher
[group: 'test']
start:
    just test
    uv run ptw .

# Watch files and run tests on change (requires entr)
[group: 'test']
watch-test:
    #!/usr/bin/env bash
    set -euo pipefail
    if command -v entr > /dev/null; then
        {{ all_files }} | entr -c just test
    else
        just test
        just _entr-warn
    fi

# Build documentation
[group: 'docs']
build-docs:
    just -f docs/justfile html

# Watch files and rebuild docs on change
[group: 'docs']
watch-docs:
    #!/usr/bin/env bash
    set -euo pipefail
    if command -v entr > /dev/null; then
        {{ doc_files }} | entr -c just build-docs
    else
        just build-docs
        just _entr-warn
    fi

# Serve documentation
[group: 'docs']
serve-docs:
    just -f docs/justfile serve

# Watch and serve docs simultaneously
[group: 'docs']
dev-docs:
    #!/usr/bin/env bash
    set -euo pipefail
    just watch-docs &
    just serve-docs

# Start documentation server with auto-reload
[group: 'docs']
start-docs:
    just -f docs/justfile start

# Start documentation design mode (watches static files)
[group: 'docs']
design-docs:
    just -f docs/justfile design

# Regenerate capabilities.json from the capability table
[group: 'lint']
build-capabilities:
    uv run python -c 'from libtmux._internal.support import capabilities as c; print(c.render(), end="")' > capabilities.json

# Format code with ruff
[group: 'lint']
ruff-format:
    uv run ruff format . .github/scripts

# Run ruff linter
[group: 'lint']
ruff:
    uv run ruff check . .github/scripts

# Watch files and run ruff on change
[group: 'lint']
watch-ruff:
    #!/usr/bin/env bash
    set -euo pipefail
    if command -v entr > /dev/null; then
        {{ py_files }} | entr -c just ruff
    else
        just ruff
        just _entr-warn
    fi

# Run ty type checker
[group: 'lint']
ty:
    uv run ty check

# Watch files and run ty on change
[group: 'lint']
watch-ty:
    #!/usr/bin/env bash
    set -euo pipefail
    if command -v entr > /dev/null; then
        {{ py_files }} | entr -c just ty
    else
        just ty
        just _entr-warn
    fi

# Format markdown files with prettier
[group: 'format']
format-markdown:
    prettier --parser=markdown -w *.md docs/*.md docs/**/*.md CHANGES

# Run monkeytype to collect runtime types
[group: 'typing']
monkeytype-create:
    uv run monkeytype run $(uv run which py.test)

# Apply collected monkeytype annotations
[group: 'typing']
monkeytype-apply:
    uv run monkeytype list-modules | xargs -n1 -I{} sh -c 'uv run monkeytype apply {}'

[private]
_entr-warn:
    @echo "----------------------------------------------------------"
    @echo "     ! File watching functionality non-operational !      "
    @echo "                                                          "
    @echo "Install entr(1) to automatically run tasks on file change."
    @echo "See https://eradman.com/entrproject/                      "
    @echo "----------------------------------------------------------"


# Remove ignored build artifacts and caches. Untracked files and .env stay.
[group: 'clean']
clean:
    git clean -fdX -e '!.env' -e '!.env.*'

# Preview what clean will delete.
[group: 'clean']
clean-dry:
    git clean -ndX -e '!.env' -e '!.env.*'

# Remove ignored and untracked files. .env and .env.* stay.
# git clean -x ignores .gitignore and only preserves -e patterns, so .env is named here rather than negated.
[group: 'clean']
[confirm("This will delete all untracked and ignored files. Continue?")]
nuke:
    git clean -fdx -e '.env' -e '.env.*'
