
# SHELL ensures more consistent behavior between macOS and Linux.
SHELL=/bin/bash

test_reports := build

.PHONY: *

clean:
	rm -rf build dist .mypy_cache .coverage .pytest_cache
	find . -type d -name "__pycache__" -exec rm -rf {} +
	find . -type f -name "*.pyc" -delete

# Install the package locally in development mode
install:
	pip3 install -e .

# Install development dependencies
dev-install:
	pip3 install -r dev-requirements.txt

# Uninstall the package
uninstall:
	pip3 uninstall gcloud2-wrap

# Run the unit tests with coverage
test:
	rm -rf $(test_reports) .coverage
	mkdir -p $(test_reports)
	python3 -m pytest --cov=gcloud2wrap --cov-report=xml:$(test_reports)/coverage.xml --cov-report=html:$(test_reports)/html --junit-xml=$(test_reports)/pytest.xml gcloud2wrap/test/
	@echo "HTML code coverage report was generated in $(test_reports)/html"
	@echo "Open it with:"
	@echo "  open $(test_reports)/html/index.html"

# Run quick tests without coverage reports
test-quick:
	python3 -m pytest gcloud2wrap/test/ -v

# Run pylint to check code quality
pylint:
	python3 -m pylint --errors-only setup.py gcloud2wrap

# Run mypy for type checking
mypy:
	python3 -m mypy gcloud2wrap

# Format code with black
format:
	python3 -m black gcloud2wrap setup.py
	python3 -m isort gcloud2wrap setup.py

# Check code formatting
format-check:
	python3 -m black --check gcloud2wrap setup.py
	python3 -m isort --check-only gcloud2wrap setup.py

# Run all quality checks
checks: format-check pylint mypy test

# Build distribution packages
build: clean
	python3 -m pip install --upgrade build
	python3 -m build

# Upload to PyPI (requires proper credentials)
upload-pypi: build
	python3 -m pip install --upgrade twine
	python3 -m twine upload dist/*

# Upload to Test PyPI (requires proper credentials)
upload-test-pypi: build
	python3 -m pip install --upgrade twine
	python3 -m twine upload --repository testpypi dist/*

# Install from Test PyPI for testing
install-test-pypi:
	pip3 install --index-url https://test.pypi.org/simple/ gcloud2-wrap

# Install and setup pre-commit hooks
pre-commit-install:
	python3 -m pip install pre-commit
	pre-commit install

# Run pre-commit hooks on all files
pre-commit-run:
	pre-commit run --all-files

# Update pre-commit hooks to latest versions
pre-commit-update:
	pre-commit autoupdate

# Run CI checks locally (same as GitHub Actions)
ci-checks: format-check pre-commit-run test

# Show help
help:
	@echo "Available targets:"
	@echo "  clean           - Remove build artifacts and cache files"
	@echo "  install         - Install package in development mode"
	@echo "  dev-install     - Install development dependencies"
	@echo "  uninstall       - Uninstall the package"
	@echo "  test            - Run tests with coverage reports"
	@echo "  test-quick      - Run tests without coverage reports"
	@echo "  pylint          - Run pylint code quality checks"
	@echo "  mypy            - Run mypy type checking"
	@echo "  format          - Format code with black and isort"
	@echo "  format-check    - Check code formatting"
	@echo "  checks          - Run all quality checks"
	@echo "  build           - Build distribution packages"
	@echo "  upload-pypi     - Upload to PyPI"
	@echo "  upload-test-pypi - Upload to Test PyPI"
	@echo "  install-test-pypi - Install from Test PyPI"
	@echo "  pre-commit-install - Install and setup pre-commit hooks"
	@echo "  pre-commit-run  - Run pre-commit hooks on all files"
	@echo "  pre-commit-update - Update pre-commit hooks to latest versions"
	@echo "  ci-checks       - Run CI checks locally (same as GitHub Actions)"
	@echo "  help            - Show this help message"
