PROJECT=craft_providers
# Define when more than the main package tree requires coverage
# like is the case for snapcraft (snapcraft and snapcraft_legacy):
# COVERAGE_SOURCE="starcraft"
UV_TEST_GROUPS := "--group=dev"
UV_DOCS_GROUPS := "--group=docs"
UV_LINT_GROUPS := "--group=lint" "--group=types" $(UV_DOCS_GROUPS)
UV_TICS_GROUPS := "--group=tics"

# If you have dev dependencies that depend on your distro version, uncomment these:
# ifneq ($(wildcard /etc/os-release),)
# include /etc/os-release
# endif
# ifdef VERSION_CODENAME
# UV_TEST_GROUPS += "--group=dev-$(VERSION_CODENAME)"
# UV_DOCS_GROUPS += "--group=dev-$(VERSION_CODENAME)"
# UV_LINT_GROUPS += "--group=dev-$(VERSION_CODENAME)"
# UV_TICS_GROUPS += "--group=dev-$(VERSION_CODENAME)"
# endif

include common.mk

# instructions and skills are imported from canonical/copilot-collections
PRETTIER_IGNORE_DIRS := .github/instructions .github/skills

# this extends PRETTIER_FILES from common .mk
PRETTIER_FILES += $(foreach dir,$(PRETTIER_IGNORE_DIRS),"!$(dir)/**")

ROOT_DIR := $(dir $(realpath $(lastword $(MAKEFILE_LIST))))
ifeq ($(CI)_$(OS),true_Linux)
SHELL:=$(ROOT_DIR)tools/ci-shell.sh
endif

.PHONY: format
format: format-ruff format-codespell format-prettier format-shfmt format-tombi format-pre-commit  ## Run all automatic formatters

.PHONY: lint
lint: lint-code lint-docs lint-twine lint-uv-lockfile lint-actions  ## Run all linters

.PHONY: lint-code
lint-code: lint-ruff lint-ty lint-codespell lint-prettier lint-shfmt lint-shellcheck lint-tombi  ## Run code-specific linters

.PHONY: pack
pack: pack-pip  ## Build all packages

# Find dependencies that need installing
APT_PACKAGES :=
ifeq ($(wildcard /usr/include/libxml2/libxml/xpath.h),)
APT_PACKAGES += libxml2-dev
endif
ifeq ($(wildcard /usr/include/libxslt/xslt.h),)
APT_PACKAGES += libxslt1-dev
endif
ifeq ($(wildcard /usr/share/doc/python3-venv/copyright),)
APT_PACKAGES += python3-venv
endif

# Used for installing build dependencies in CI.
.PHONY: install-build-deps
install-build-deps: install-lint-build-deps
ifeq ($(APT_PACKAGES),)
else ifeq ($(shell which apt-get),)
	$(warning Cannot install build dependencies without apt.)
	$(warning Please ensure the equivalents to these packages are installed: $(APT_PACKAGES))
else
	sudo $(APT) install $(APT_PACKAGES)
endif
ifeq ($(CI)_$(OS),true_Linux)  # Only do this in CI on Linux
	# In CI, delete the android SDK if it's installed. It's kinda huge!
	sudo rm -rf /usr/local/lib/android/
	# Likewise, configure LXD in CI
	echo "::group::Configure LXD"
	sudo groupadd --force --system lxd
	sudo usermod --append --groups lxd $(USER)
	echo "::endgroup::"
else ifeq ($(CI)_$(OS),true_Darwin)  # Only do this in CI on macOS
	brew install multipass
	# Disable spotlight because it tries to index the multipass images, crashing
	# macOS 14+ runners. Thapple.
	sudo mdutil -a -i off
endif


# If additional build dependencies need installing in order to build the linting env.
.PHONY: install-lint-build-deps
install-lint-build-deps: install-ty

.PHONY: lint-ty
lint-ty: install-ty
	ty check

.PHONY: install-ty
install-ty:
ifneq ($(shell which ty),)
else ifneq ($(shell which snap),)
	sudo snap install --beta astral-ty
	sudo snap alias astral-ty.ty ty
else ifneq ($(shell which uv),)
	uv tool install ty
endif

# TICS runs the full test suite (including Multipass integration tests) via
# `make test-coverage`, unlike the qa.yaml jobs, which filter those out with
# PYTEST_ADDOPTS. Install Multipass on Linux so those tests can run there too.
.PHONY: install-multipass
install-multipass:
ifeq ($(shell which multipass),)
ifeq ($(OS),Linux)
	sudo snap install multipass
else ifeq ($(OS),Darwin)
	brew install multipass
endif
endif

.PHONY: setup-tics
setup-tics: install-uv install-build-deps install-multipass ##- Set up a testing environment for Tiobe TICS
	uv venv
	uv sync $(UV_TEST_GROUPS) $(UV_LINT_GROUPS) $(UV_TICS_GROUPS)
ifneq ($(CI),)
	echo $(PWD)/.venv/bin >> $(GITHUB_PATH)
endif

# tests/integration/multipass/test_multipass_instance.py is unreliable in this
# environment: its push_file_io/exec cases consistently fail and exhaust their
# reruns, which multiplies out over many hours across its parametrized cases.
# Other multipass_instance tests (e.g. test_launch.py) pass fine, but until
# test_multipass_instance.py is fixed, exclude the marker so coverage finishes
# in a reasonable time, matching how qa.yaml already filters it out elsewhere.
test-coverage: export PYTEST_ADDOPTS ?= -m 'not multipass_instance'
