.PHONY: build check coverage clean publish set-release-version tag-release release

build: clean
	python3 -m build
	twine check dist/*

check:
	flake8 tests ppa scripts
	python3-coverage run --source=ppa -m pytest -v tests

coverage: check
	python3-coverage report
	python3-coverage html

clean:
	rm -rf *.egg-info .eggs dist build .pytest_cache */__pycache__ .coverage htmlcov

publish: build
	twine upload dist/*

set-release-version:
ifndef VERSION
	$(error VERSION environment variable is undefined)
endif
	sed -Ei "s/^(__version__ = ')[0-9]+\.[0-9]+\.[0-9]+.*'$$/\1${VERSION}\'/" ppa/_version.py
	sed -Ei "s/^(version = \")[0-9]+\.[0-9]+\.[0-9]+.*\"$$/\1${VERSION}\"/" pyproject.toml
	sed -Ei "s/^(version: )[0-9]+\.[0-9]+\.[0-9]+.*\$$/\1${VERSION}/" snap/snapcraft.yaml
	sed -Ei "s/^(version: )git\$$/\1${VERSION}/" snap/snapcraft.yaml
	git add ppa/_version.py pyproject.toml snap/snapcraft.yaml

tag-release:
	VERSION=$$(sed -En "s/__version__ = '([0-9]+\.[0-9]+\.[0-9]+)'$$/\1/p" ppa/_version.py); \
	if [ -z "$$VERSION" ]; then \
		echo 'Version is not in the expected format. Is this a release version?'; \
		exit 1; \
	fi; \
	echo "tagging $$VERSION as v$$VERSION"; \
	git commit -s -m "Prepare $$VERSION release"; \
	git tag -a -m "PPA Dev Tools $$VERSION" v$$VERSION

release: clean set-release-version tag-release publish

snap: clean
	export SNAPCRAFT_BUILD_ENVIRONMENT_MEMORY=4G
	export SNAPCRAFT_BUILD_ENVIRONMENT_CPU=4
	export SNAPCRAFT_MAX_PARALLEL_BUILD_COUNT=4
	snapcraft
