PYTHON_VERSION ?= 3.10
CARGO_HOME ?= $(HOME)/.cargo
PATH := $(HOME)/.rye/shims:$(HOME)/.local/bin:$(CARGO_HOME)/bin:$(PATH)

SHELL := /bin/bash
PYTHON_FILES := $(shell find src/ -type f -name '*.py' -o -name '*.pyi')

UV_PYTHON_PREFERENCE ?= only-managed
UV ?= uv
ifdef INSIDE_EMACS
UV_RUN ?= uv run --color never
else
UV_RUN ?= uv run
endif
RUN ?= $(UV_RUN)

REQUIRED_UV_VERSION ?= 0.13.10
bootstrap:
	@INSTALLED_UV_VERSION=$$(uv --version 2>/dev/null | awk '{print $$2}' || echo "0.0.0"); \
    DETECTED_UV_VERSION=$$(printf '%s\n' "$(REQUIRED_UV_VERSION)" "$$INSTALLED_UV_VERSION" | sort -V | head -n1); \
	if [ "$$DETECTED_UV_VERSION" != "$(REQUIRED_UV_VERSION)" ]; then \
		uv self update $(REQUIRED_UV_VERSION) || curl -LsSf https://astral.sh/uv/$(REQUIRED_UV_VERSION)/install.sh | sh; \
	fi
	@echo $(PYTHON_VERSION) > .python-version
.PHONY: bootstrap

install: | bootstrap
	@$(UV) sync --python-preference=$(UV_PYTHON_PREFERENCE)
.PHONY: install

sync_extra_args ?=
sync: | bootstrap
	@$(UV) sync --python-preference=$(UV_PYTHON_PREFERENCE) --frozen $(sync_extra_args)
.PHONY: sync

lock: | bootstrap
ifdef update_all
	@$(UV) sync -U $(sync_extra_args)
else
	@$(UV) sync $(sync_extra_args)
endif
.PHONY: lock

update: | bootstrap
	@$(MAKE) lock update_all=1
.PHONY: update

# Local fallback for the CI release jobs ('build distribution' and 'publish_pypi'), for when the tag
# pipeline fails after the tag is pushed and a second tag is not wanted.
#
# The version comes from the git tag (hatch-vcs), so the distributions are built from a temporary
# worktree checked out at RELEASE_TAG, regardless of the current HEAD or uncommitted changes; what
# reaches PyPI is what was tagged.  RELEASE_TAG defaults to the tag pointing at HEAD.
#
# Credentials are uv's: UV_PUBLISH_TOKEN (or UV_PUBLISH_USERNAME and UV_PUBLISH_PASSWORD), the
# keyring, or a prompt.  --check-url skips the files PyPI already has, so this also completes a
# partial upload.
RELEASE_TAG ?= $(shell git describe --tags --exact-match HEAD 2>/dev/null)
RELEASE_WORKTREE = .worktrees/publish-$(RELEASE_TAG)
RELEASE_DIST_DIR = dist/$(RELEASE_TAG)
PYPI_CHECK_URL ?= https://pypi.org/simple/xotless/
publish: | bootstrap
	@if [ -z "$(RELEASE_TAG)" ]; then \
		echo "HEAD is not tagged; pass RELEASE_TAG=<tag> to choose the release to publish" >&2; \
		exit 1; \
	fi
	@if ! [[ "$(RELEASE_TAG)" =~ ^[0-9]+(\.[0-9]+)*(a[0-9]+|b[0-9]+|rc[0-9]+)?(\.post[0-9]+)?$$ ]]; then \
		echo "$(RELEASE_TAG) is not a release tag; refusing to publish" >&2; exit 1; \
	fi
	@if ! git rev-parse -q --verify "refs/tags/$(RELEASE_TAG)" >/dev/null; then \
		echo "There is no tag $(RELEASE_TAG); refusing to publish" >&2; exit 1; \
	fi
	rm -rf $(RELEASE_DIST_DIR)
	git worktree add --detach $(RELEASE_WORKTREE) $(RELEASE_TAG)
	$(UV) build --out-dir $(RELEASE_DIST_DIR) $(RELEASE_WORKTREE); \
	status=$$?; \
	git worktree remove --force $(RELEASE_WORKTREE); \
	exit $$status
	$(UV) publish --check-url $(PYPI_CHECK_URL) $(RELEASE_DIST_DIR)/*
.PHONY: publish

format: | install
	@$(RUN) ruff check --fix src/
	@$(RUN) ruff format src/
.PHONY: format

lint: | install
	@$(RUN) ruff check src/
	@$(RUN) ruff format --check src/
.PHONY: lint

pytest_paths ?= "src/"
PYTEST_PATHS ?= $(pytest_paths)
PYTEST_HYPOTHESIS_ARGS ?=
PYTEST_EXTRA_ARGS ?=
PYTEST_FAILURE_ARGS ?= --ff --maxfail 1
PYTEST_ARGS ?= -l -q $(PYTEST_FAILURE_ARGS) $(PYTEST_EXTRA_ARGS) $(PYTEST_HYPOTHESIS_ARGS)
test: | install
	@$(RUN) pytest $(PYTEST_ARGS) $(PYTEST_PATHS)
.PHONY: test

doctest: | install
	@$(MAKE) SPHINXOPTS=$(SPHINXOPTS) SPHINXBUILD="$(RUN) sphinx-build" -C docs doctest
.PHONY: test

mypy: | install
	@$(RUN) mypy -p xotless --config-file $(PWD)/mypy.ini
.PHONY: mypy

zuban: | install
	@$(RUN) zuban check --mode=mypy --config-file $(PWD)/mypy.ini src/xotless
.PHONY: zuban

typecheck: zuban
.PHONY: typecheck

py shell: install
	@$(RUN) ipython
.PHONY: py shell

SPHINXOPTS ?= -W
docs/build: install
	$(MAKE) SPHINXOPTS=$(SPHINXOPTS) SPHINXBUILD="$(RUN) sphinx-build" -C docs html
.PHONY: docs/build


CADDY_IMAGE ?= caddy:2.10-alpine
CADDY_SERVER_PORT ?= 6942
docs/browse: docs/build
	@docker run --rm --network host \
        -v $(PWD)/docs/build/html/:/var/www \
        -it $(CADDY_IMAGE) \
	    caddy file-server --browse --listen :$(CADDY_SERVER_PORT) --root /var/www
.PHONY: docs/browse
