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

default:
    @just --list

# Install the virtual environment and install the pre-commit hooks
@install all="false":
    echo "Creating python virtual environment using uv"
    [ {{ all }}="true" ] && uv sync --all-groups || uv sync --dev

# Run code quality tools.
check:
    @echo "Checking lock file consistency with 'pyproject.toml'"
    uv lock --locked
    @echo "Static type checking: Running ty"
    uv run ty check
    @echo "Checking for obsolete dependencies: Running deptry"
    uv run deptry src
    @echo "Checking for dependency: Running tach"
    uv run tach check

# Test the code with pytest
test:
    @echo "Testing code: Running pytest"
    uv run pytest --cov --cov-config=pyproject.toml --cov-report=xml

# Build wheel file
build: clean-build
    @echo "Creating wheel file"
    uvx --from build pyproject-build --installer uv

publish:
    uv publish
# Clean build artifacts
clean-build:
    @echo "Removing build artifacts"
    uv run python -c "import shutil; import os; shutil.rmtree('dist') if os.path.exists('dist') else None"

# py-spy Profiling (CPU) (need sudo)
@prof-cpu output="prof/cpu_prof.svg" depth="2000" iter="100" mode="current":
    sudo PYTHONPATH="$(pwd)/src:$PYTHONPATH" \
    uv run --isolated --no-project --python=3.13 py-spy record \
    -o {{ output }} \
    --format flamegraph \
    --rate 20 \
    -- \
    python -m prof.profile_predicate --depth={{ depth }} --iter={{ iter }} --mode={{mode}}
    sudo chown "$(id -un):$(id -gn)" {{ output }}
    chmod u+rw {{ output }}

# Memory Profiling (Memray)
@prof-mem binoutput="prof/memory.bin" output="prof/mem_flamegraph.html" depeth="2000" iter="100" mode="current":
    uv run memray run \
        --force \
        -o {{ binoutput }} \
        -m prof.profile_predicate --depth {{ depeth }} --iter={{ iter }} --mode={{ mode }}\
    && uv run memray flamegraph \
        --force \
        -o {{ output }} \
        {{ binoutput }}
