diff --git a/Makefile b/Makefile index d1d306f9..36a14632 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,4 @@ +# Find an extensive introduction to Makefiles here: https://makefiletutorial.com/ # Use bash as the shell when executing a rule's recipe. For more details: # https://www.gnu.org/software/make/manual/html_node/Choosing-the-Shell.html @@ -7,6 +8,11 @@ SHELL := bash PACKAGE_NAME := package PACKAGE_VERSION := $(shell python -c $$'try: import $(PACKAGE_NAME); print($(PACKAGE_NAME).__version__);\nexcept: print("unknown");') +# Files that this package depends on are in the src/ folder, so collect them +# all into a list. Also collect everything needed for testing this package. +PACKAGE_DEPS := $(shell find src/$(PACKAGE_NAME)/ -type f -not -path "*/__pycache__/*") +PACKAGE_DEPS_TEST := $(shell find tests/ -type f -not -path "*/__pycache__/*") + # This variable contains the first goal that matches any of the listed goals # here, else it contains an empty string. The net effect is to filter out # whether this current run of `make` requires a Python virtual environment @@ -106,7 +112,8 @@ upgrade-quiet: # Generate a Software Bill of Materials (SBOM). .PHONY: sbom -sbom: requirements +sbom: requirements dist/$(PACKAGE_NAME)-$(PACKAGE_VERSION)-sbom.json +dist/$(PACKAGE_NAME)-$(PACKAGE_VERSION)-sbom.json: cyclonedx-py --force --requirements --format json --output dist/$(PACKAGE_NAME)-$(PACKAGE_VERSION)-sbom.json # Generate a requirements.txt file containing version and integrity hashes for all @@ -119,8 +126,8 @@ sbom: requirements # We also want to make sure that this package itself is added to the requirements.txt # file, and if possible even with proper hashes. .PHONY: requirements -requirements: requirements.txt -requirements.txt: pyproject.toml +requirements: requirements.txt dist/$(PACKAGE_NAME)-$(PACKAGE_VERSION)-requirements.txt +requirements.txt dist/$(PACKAGE_NAME)-$(PACKAGE_VERSION)-requirements.txt: .venv/upgraded-on echo -n "" > requirements.txt for pkg in $$(python -m pip freeze --local --disable-pip-version-check --exclude-editable); do \ pkg=$${pkg//[$$'\r\n']}; \ @@ -162,14 +169,18 @@ check-mypy: pre-commit run mypy --all-files check-actionlint: pre-commit run actionlint --all-files -check: +check: .venv/checked-on +.venv/checked-on: $(PACKAGE_DEPS) pre-commit run --all-files + echo "Automatically generated by Python Package Makefile on $$(date '+%Y-%m-%d %H:%M:%S %z')." > .venv/checked-on # Run all unit tests. The --files option avoids stashing but passes files; however, # the hook setup itself does not pass files to pytest (see .pre-commit-config.yaml). .PHONY: test -test: +test: .venv/tested-on +.venv/tested-on: $(PACKAGE_DEPS) $(PACKAGE_DEPS_TEST) pre-commit run pytest --hook-stage push --files tests/ + echo "Automatically generated by Python Package Makefile on $$(date '+%Y-%m-%d %H:%M:%S %z')." > .venv/tested-on # Build a source distribution package and a binary wheel distribution artifact. # When building these artifacts, we need the environment variable SOURCE_DATE_EPOCH @@ -223,6 +234,7 @@ prune: .PHONY: dist-clean clean dist-clean: rm -fr dist/* + rm -f .venv/checked-on .venv/tested-on rm -f requirements.txt clean: dist-clean rm -fr .coverage .hypothesis/ .mypy_cache/ .pytest_cache/