# cTrader API client justfile
set shell := ["bash", "-euo", "pipefail", "-c"]
set script-interpreter := ["bash", "-euo", "pipefail"]
set positional-arguments

# Variables
proto_path := "protos/vendor"
python_out_path := "src/ctrader_api_client/_internal/proto"

# Default recipe. List all available recipes.
[private]
default:
    @just --list

# Verify that the required toolchains are present
[group('build')]
[script]
check-deps:
    if ! command -v uv &> /dev/null; then
        echo "uv not found. Install from https://docs.astral.sh/uv/getting-started/installation/" >&2
        exit 1
    fi
    echo "==> All required toolchains are present"

# Install the pre-commit hooks, then build the project. Extra arguments go to `uv sync`.
[group('build')]
[script]
setup *args: check-deps
    echo "==> Install the pre-commit hooks"
    uv tool install prek
    uv tool run prek install --hook-type pre-commit
    echo "==> Build the project"
    just build "$@"

# Sync the Python dependencies. Extra arguments go to `uv sync`.
[group('build')]
build *args:
    uv sync "$@"

# Format the code. Extra arguments go to `ruff format`.
[group('dev')]
fmt *args:
    uv run ruff format "$@"

# Lint the code. Extra arguments go to `ruff check`.
[group('dev')]
lint *args:
    uv run ruff check --fix "$@"

# Type-check the code. Extra arguments go to `zuban check`.
[group('dev')]
type-check *args:
    uv run zuban check "$@"

# Run the tests. Extra arguments go to pytest.
[group('dev')]
test *args:
    uv run pytest "$@"

# Run the whole CI pipeline
[group('dev')]
@ci:
    just fmt
    just lint
    just type-check
    just test

# Serve the documentation locally. Extra arguments go to `mkdocs serve`.
[group('tools')]
documentation *args:
    uv run mkdocs serve "$@"

# Fetch the .proto files at the given version into the vendor directory
[group('tools')]
update-proto version='main':
    ./protos/update.sh "$1"

# Generate the Python code from the .proto files. Run this after `update-proto`.
[group('tools')]
[script]
compile-proto:
    echo "==> Compile the .proto files from {{ proto_path }}/ into {{ python_out_path }}/"
    uv run protoc -I={{ proto_path }} --python_betterproto_out={{ python_out_path }} {{ proto_path }}/*.proto
    echo "==> Fix the cross-module imports"
    uv run python scripts/fix_proto_imports.py
    echo "==> Format the generated code"
    uv run ruff check --fix {{ python_out_path }}
    uv run ruff format {{ python_out_path }}
