# Makefile for the by-framework workspace

SHELL := /bin/bash

ROOT_PROJECT := .
LIB_PROJECTS := $(patsubst %/pyproject.toml,%,$(wildcard libs/*/pyproject.toml))
PYTHON_PROJECTS := $(ROOT_PROJECT) $(LIB_PROJECTS)
PROJECTS ?= $(PYTHON_PROJECTS)

.PHONY: all help list-projects install format lint test ci clean

all: format lint test

ci: install lint test

help:
	@echo "Workspace commands:"
	@echo "  make list-projects          # Show managed Python projects"
	@echo "  make install                # Sync dependencies for all projects"
	@echo "  make format                 # Format Python code for all projects"
	@echo "  make lint                   # Lint Python code for all projects"
	@echo "  make test                   # Run tests for all projects"
	@echo "  make ci                     # Install, lint, and test all projects"
	@echo "  make clean                  # Remove caches and build artifacts"
	@echo ""
	@echo "Optional override:"
	@echo "  make test PROJECTS='libs/by-framework-filestore-minio libs/by-framework-filestore-byclaw-qa'"

list-projects:
	@for project in $(PROJECTS); do \
		echo $$project; \
	done

install:
	@for project in $(PROJECTS); do \
		echo "==> Syncing $$project"; \
		(cd $$project && uv sync --all-extras); \
	done

format:
	@for project in $(PROJECTS); do \
		paths=""; \
		if [ -d "$$project/src" ]; then paths="$$paths src"; fi; \
		if [ -d "$$project/tests" ]; then paths="$$paths tests"; fi; \
		if [ -z "$$paths" ]; then continue; fi; \
		echo "==> Formatting $$project"; \
		(cd $$project && uv run --extra dev isort $$paths); \
		(cd $$project && uv run --extra dev ruff check --fix $$paths); \
		(cd $$project && uv run --extra dev pyink --config pyproject.toml $$paths); \
	done

lint:
	@for project in $(PROJECTS); do \
		paths=""; \
		if [ -d "$$project/src" ]; then paths="$$paths src"; fi; \
		if [ -d "$$project/tests" ]; then paths="$$paths tests"; fi; \
		if [ -z "$$paths" ]; then continue; fi; \
		echo "==> Linting $$project"; \
		(cd $$project && uv run --extra dev pylint $$paths); \
		(cd $$project && uv run --extra dev ruff check $$paths); \
	done

test:
	@for project in $(PROJECTS); do \
		echo "==> Testing $$project"; \
		(cd $$project && uv run --extra dev pytest); \
	done

clean:
	find . -type d -name "__pycache__" -exec rm -rf {} +
	rm -rf .pytest_cache .ruff_cache .mypy_cache .coverage
	rm -rf htmlcov
	rm -rf dist build *.egg-info
	rm -rf by-framework.log
	rm -rf libs/*/.pytest_cache libs/*/.ruff_cache libs/*/.mypy_cache
