diff options
Diffstat (limited to 'Makefile')
| -rw-r--r-- | Makefile | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..bec4ef6 --- /dev/null +++ b/Makefile @@ -0,0 +1,77 @@ +# Development tasks. +# +# make list the targets +# make test run the suite with PHPUnit +# make lint syntax and style checks, no files touched +# make build validate the package and its PSR-4 autoloading +# +# The library itself has no Composer dependencies; PHPUnit is needed only to +# run the suite, and `make test` installs it for you. + +PHP ?= php +COMPOSER ?= composer + +SOURCES = $(shell find src tests -name '*.php') + +# php-cs-fixer and phpcs are not dependencies of this package. Either one is +# used when it happens to be installed, from vendor/bin or from PATH. +FIXER = $(shell if [ -x vendor/bin/php-cs-fixer ]; then echo vendor/bin/php-cs-fixer; \ + else command -v php-cs-fixer 2>/dev/null; fi) +PHPCS = $(shell if [ -x vendor/bin/phpcs ]; then echo vendor/bin/phpcs; \ + else command -v phpcs 2>/dev/null; fi) +PHPCBF = $(shell if [ -x vendor/bin/phpcbf ]; then echo vendor/bin/phpcbf; \ + else command -v phpcbf 2>/dev/null; fi) + +.DEFAULT_GOAL := help +.PHONY: help test lint format build check clean + +help: + @echo 'Targets:' + @echo ' test run the test suite' + @echo ' lint php -l over the tree, plus php-cs-fixer or phpcs when installed' + @echo ' format apply php-cs-fixer or phpcbf, when installed (writes)' + @echo ' build validate composer.json and the PSR-4 autoloading' + @echo ' check lint + test + build, in that order' + @echo ' clean remove installed dependencies and test artefacts' + +vendor: composer.json + $(COMPOSER) install --no-interaction + @touch vendor + +test: vendor + $(PHP) vendor/bin/phpunit + +# Read-only by design, so it is safe in CI and in a pre-commit hook: it reports +# what `make format` would change without changing anything. +lint: + @set -e; for f in $(SOURCES); do $(PHP) -l $$f >/dev/null; done + @echo 'syntax OK: $(words $(SOURCES)) files' +ifneq ($(FIXER),) + $(FIXER) fix --dry-run --diff +else ifneq ($(PHPCS),) + $(PHPCS) --standard=PSR12 src tests +else + @echo 'php-cs-fixer and phpcs not installed: syntax checks only' +endif + +format: +ifneq ($(FIXER),) + $(FIXER) fix +else ifneq ($(PHPCBF),) + $(PHPCBF) --standard=PSR12 src tests +else + @echo 'php-cs-fixer and phpcbf not installed: nothing to format with' +endif + +# PHP compiles nothing, so the equivalent of a build is proving the package +# metadata is valid and that every class is where PSR-4 says it is. +build: + $(COMPOSER) validate --strict + $(COMPOSER) dump-autoload --optimize --strict-psr + +# The order matters: lint is the fastest to fail, so a broken run reports the +# cheapest problem first. +check: lint test build + +clean: + rm -rf vendor .phpunit.cache .php-cs-fixer.cache |