.PHONY: help install dev test test-e2e lint format typecheck check clean build publish refresh-policy vendor-tool-template

help:
	@echo "SweatStack CLI Development Commands"
	@echo ""
	@echo "  make install     Install the package"
	@echo "  make dev         Install with development dependencies"
	@echo "  make test        Run tests"
	@echo "  make test-e2e    Run the page template in a browser (downloads Chromium once)"
	@echo "  make lint        Run linter"
	@echo "  make format      Format code"
	@echo "  make typecheck   Run type checker"
	@echo "  make check       Run all checks (lint, format, typecheck, test)"
	@echo "  make refresh-policy  Copy the live SweatStack Tools policy into the package"
	@echo "  make vendor-tool-template  Rebuild the tool template's files and download pins (needs Node)"
	@echo "  make clean       Remove build artifacts"
	@echo "  make build       Build distribution packages"
	@echo "  make publish     Publish to PyPI"

install:
	uv pip install -e .

dev:
	uv pip install -e ".[dev]"

test:
	uv run pytest

test-e2e:
	uv run --extra dev --extra e2e playwright install chromium
	uv run --extra dev --extra e2e pytest tests/e2e -m e2e -v

lint:
	uv run ruff check .

format:
	uv run ruff format .

typecheck:
	uv run mypy src/sweatstack_cli

check: lint typecheck test
	@echo "All checks passed!"

clean:
	rm -rf dist/ build/ *.egg-info src/*.egg-info
	find . -type d -name __pycache__ -exec rm -rf {} + 2>/dev/null || true
	find . -type d -name .pytest_cache -exec rm -rf {} + 2>/dev/null || true
	find . -type d -name .mypy_cache -exec rm -rf {} + 2>/dev/null || true
	find . -type d -name .ruff_cache -exec rm -rf {} + 2>/dev/null || true

# The copy of the SweatStack Tools policy this release falls back to when sweatstack.tools is
# unreachable and nothing is cached. Run before a release, and commit the result.
refresh-policy:
	curl -fsS https://sweatstack.tools/v1/policy -o src/sweatstack_cli/tools/policy.json
	uv run python -c "import json; from sweatstack_cli.tools.policy import parse_policy; parse_policy(json.load(open('src/sweatstack_cli/tools/policy.json')), 'built-in')"

# The files the `tool create` template ships, and the SHA-256 pins of the files it downloads.
# Run after changing a version in scripts/vendor_tool_template.sh, and commit the result.
vendor-tool-template:
	./scripts/vendor_tool_template.sh

build:
	rm -rf dist
	uv build

publish: build
	uvx uv-publish
