VENV := venv

# The live tests take every parameter from the environment. `.env.test` is the
#  git-ignored file that carries them locally; the runner loads it so nothing
#  under `tests/` has to read a file of its own. Absent, the tests skip by name.
ENV_TEST := .env.test
LOAD_ENV_TEST := set -a; if [ -f $(ENV_TEST) ]; then . ./$(ENV_TEST); fi; set +a;
PYTHON := $(VENV)/bin/python
PIP := $(PYTHON) -m pip
TY := $(VENV)/bin/ty
TAG = v$(shell grep -E '__version__ = ".*"' pyrogram/__init__.py | cut -d\" -f2)

RM := rm -rf

GREEN  := \033[0;32m
RED    := \033[0;31m
YELLOW := \033[0;33m
BLUE   := \033[0;34m
BOLD   := \033[1m
RESET  := \033[0m

.PHONY: venv venv-dev venv-docs clean-venv clean-build clean-api clean-docs clean api docs docs-archive build tag dtag lint typecheck test test-unit test-integration

venv:
	@if [ ! -d "$(VENV)" ]; then \
		python3 -m venv $(VENV); \
		$(PIP) install -U pip wheel setuptools; \
	fi

	$(PIP) install -U -e .
	@printf "$(YELLOW)Created venv with %s$(RESET)\n" "$$($(PYTHON) --version)"

venv-dev:
	@if [ ! -d "$(VENV)" ]; then \
		python3 -m venv $(VENV); \
		$(PIP) install -U pip wheel setuptools; \
	fi

	$(PIP) install -U -e .[dev]
	@printf "$(YELLOW)Created dev venv with %s$(RESET)\n" "$$($(PYTHON) --version)"

venv-docs:
	@if [ ! -d "$(VENV)" ]; then \
		python3 -m venv $(VENV); \
		$(PIP) install -U pip wheel setuptools; \
	fi

	$(PIP) install -U -e .[docs]
	@printf "$(YELLOW)Created docs venv with %s$(RESET)\n" "$$($(PYTHON) --version)"

clean-venv:
	$(RM) $(VENV)
	@printf "$(YELLOW)Cleaned venv directory$(RESET)\n"

clean-build:
	$(RM) *.egg-info build dist
	@printf "$(YELLOW)Cleaned build directory$(RESET)\n"

clean-api:
	$(RM) pyrogram/errors/exceptions pyrogram/raw/all.py pyrogram/raw/base pyrogram/raw/functions pyrogram/raw/types
	@printf "$(YELLOW)Cleaned api directory$(RESET)\n"

clean-docs:
	$(RM) docs/build docs/source/api/bound-methods docs/source/api/methods docs/source/api/types docs/source/api/enums docs/source/telegram
	@printf "$(YELLOW)Cleaned docs directory$(RESET)\n"

clean: clean-venv clean-build clean-api clean-docs
	@printf "$(GREEN)Cleaned all directories$(RESET)\n"

api:
	cd compiler/api && ../../$(PYTHON) compiler.py
	cd compiler/errors && ../../$(PYTHON) compiler.py

docs:
	cd compiler/docs && ../../$(PYTHON) compiler.py
	$(VENV)/bin/sphinx-build -b dirhtml "docs/source" "docs/build/html" -j auto

docs-archive:
	cd docs/build/html && zip -r ../docs.zip ./

# `ruff` takes its rule set and its excludes from `pyproject.toml`, so the `lint` job and
#  the `pre-commit` hook both run this recipe instead of spelling the check out again.
lint:
	$(PYTHON) -m ruff check

# The rule set and the excludes live in `pyproject.toml`, same as `lint`. Unlike `lint`,
#  this needs `pyrogram.raw.*` to resolve, so `make api` has to have been run first.
# `ty` only auto-detects a project's virtual environment when it's named `.venv`; ours
#  is plain `venv` (see $(VENV) above), so point it there explicitly or it silently
#  falls back to the system interpreter and can't resolve any installed dependency.
typecheck:
	$(TY) check --python $(VENV)

# `make venv` installs the package alone, so the runner needs `make venv-dev` first.
test:
	@$(LOAD_ENV_TEST) $(PYTHON) -m pytest

test-unit:
	$(PYTHON) -m pytest -m 'not integration'

test-integration:
	@$(LOAD_ENV_TEST) $(PYTHON) -m pytest -m integration

build:
	hatch build

tag:
	git tag $(TAG)
	git push origin $(TAG)

dtag:
	git tag -d $(TAG)
	git push origin -d $(TAG)
