PYTHON_SRC_DIR := pyrsfnfile
VENV_DIR := .venv


# Build

.PHONY: install build init

install:
	maturin develop

build:
	maturin build --release --strip --sdist

init $(VENV_DIR):
	[ -e $(VENV_DIR) ] || python3 -m venv $(VENV_DIR)
	$(VENV_DIR)/bin/pip install -U pip
	$(VENV_DIR)/bin/pip install --editable . --group dev


# Format

.PHONY: fmt fmt-rust fmt-python

fmt: fmt-rust fmt-python

fmt-rust:
	cargo fmt --all

fmt-python: $(VENV_DIR)
	$(VENV_DIR)/bin/ruff check --select I001 --fix $(PYTHON_SRC_DIR)
	$(VENV_DIR)/bin/ruff format $(PYTHON_SRC_DIR)


# Lint

.PHONY: lint lint-rust lint-cargo-fmt lint-cargo-check lint-clippy lint-python lint-pyproject lint-ruff-format lint-ruff-check lint-mypy

lint: lint-rust lint-python

lint-rust: lint-cargo-fmt lint-cargo-check lint-clippy

lint-cargo-fmt:
	cargo fmt --all --check

lint-cargo-check:
	cargo check --locked --all-targets

lint-clippy:
	cargo clippy --locked --all-targets -- -Dwarnings

lint-python: lint-pyproject lint-ruff-format lint-ruff-check lint-mypy

lint-pyproject: $(VENV_DIR)
	$(VENV_DIR)/bin/validate-pyproject pyproject.toml

lint-ruff-format: $(VENV_DIR)
	$(VENV_DIR)/bin/ruff format --diff $(PYTHON_SRC_DIR)

lint-ruff-check: $(VENV_DIR)
	$(VENV_DIR)/bin/ruff check $(PYTHON_SRC_DIR)

lint-mypy: $(VENV_DIR)
	$(VENV_DIR)/bin/mypy --show-error-context --pretty $(PYTHON_SRC_DIR)


# Clean

.PHONY: clean clean-rust clean-cargo clean-python clean-pycache clean-compilated-package clean-python-tools clean-lock dist-clean

clean: clean-rust clean-python

clean-rust: clean-cargo

clean-cargo:
	cargo clean

clean-python: clean-pycache clean-compilated-package clean-python-tools

clean-pycache:
	find $(PYTHON_SRC_DIR) -name '__pycache__' -exec rm -rf {} +
	find $(PYTHON_SRC_DIR) -type d -empty -delete

clean-compilated-package:
	find $(PYTHON_SRC_DIR) -name '*.so' -delete

clean-python-tools:
	rm -rf .ruff_cache .mypy_cache

clean-lock:
	rm -rf Cargo.lock pylock.toml

dist-clean: clean-lock
	rm -rf $(VENV_DIR)
