diff --git a/.gitattributes b/.gitattributes index c683cc36..a1d61af0 100644 --- a/.gitattributes +++ b/.gitattributes @@ -8,6 +8,8 @@ /*.dist export-ignore /phpbench.json export-ignore /composer.lock export-ignore +/docker-compose.yml export-ignore +/Dockerfile export-ignore /README.md export-ignore /Makefile export-ignore /.roave-backward-compatibility-check.json export-ignore diff --git a/.gitignore b/.gitignore index 51522731..5f660b2d 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ phpunit.xml infection.txt coverage phpcs.xml +/.env /.phpcs.cache /.phpunit.result.cache /.phpunit.cache diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..e2146988 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,20 @@ +FROM php:8.4 + +# git needed for Infection +RUN apt-get update \ + && apt-get -y install --no-install-recommends \ + git + +ADD --chmod=0755 https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/ + +RUN install-php-extensions @composer pcov + +# Mapping the host user to the docker one ensure +# files have the same permissions +ARG USER_ID +ARG GROUP_ID + +RUN groupadd --gid ${GROUP_ID} code \ + && useradd --create-home --shell /bin/bash --uid ${USER_ID} --gid code code + +USER code diff --git a/Makefile b/Makefile index 1fedebfb..3717acea 100644 --- a/Makefile +++ b/Makefile @@ -1,40 +1,45 @@ +ifdef CI + DOCKER_PHP_EXEC := +else + DOCKER_PHP_EXEC := docker compose run --rm php +endif PARALLELISM := $(shell nproc) .PHONY: all -all: install phpcbf phpcs phpstan phpunit infection phpbench +all: phpcbf phpcs phpstan phpunit infection phpbench -.PHONY: install -install: vendor/composer/installed.json +.env: /etc/passwd /etc/group Makefile + printf "USER_ID=%s\nGROUP_ID=%s\n" `id --user "${USER}"` `id --group "${USER}"` > .env -vendor/composer/installed.json: composer.json composer.lock - @composer install $(INSTALL_FLAGS) +vendor/composer/installed.json: composer.json composer.lock .env docker-compose.yml Dockerfile + @$(DOCKER_PHP_EXEC) composer install $(INSTALL_FLAGS) @touch -c composer.json composer.lock vendor/composer/installed.json .PHONY: phpunit -phpunit: - @php -d assert.exception=1 -d zend.assertions=1 vendor/bin/phpunit $(PHPUNIT_FLAGS) +phpunit: vendor/composer/installed.json + @$(DOCKER_PHP_EXEC) php -d assert.exception=1 -d zend.assertions=1 vendor/bin/phpunit $(PHPUNIT_FLAGS) .PHONY: infection -infection: - @php -d assert.exception=1 -d zend.assertions=1 -d xdebug.mode=coverage vendor/bin/phpunit --coverage-xml=build/coverage-xml --log-junit=build/junit.xml $(PHPUNIT_FLAGS) - @php -d assert.exception=1 -d zend.assertions=1 vendor/bin/infection -v -s --threads=$(PARALLELISM) --coverage=build --skip-initial-tests $(INFECTION_FLAGS) +infection: vendor/composer/installed.json + @$(DOCKER_PHP_EXEC) php -d assert.exception=1 -d zend.assertions=1 -d xdebug.mode=coverage vendor/bin/phpunit --coverage-xml=build/coverage-xml --log-junit=build/junit.xml $(PHPUNIT_FLAGS) + @$(DOCKER_PHP_EXEC) php -d assert.exception=1 -d zend.assertions=1 vendor/bin/infection -v -s --threads=$(PARALLELISM) --coverage=build --skip-initial-tests $(INFECTION_FLAGS) .PHONY: phpcbf -phpcbf: - @vendor/bin/phpcbf --parallel=$(PARALLELISM) || true +phpcbf: vendor/composer/installed.json + @$(DOCKER_PHP_EXEC) vendor/bin/phpcbf --parallel=$(PARALLELISM) || true .PHONY: phpcs -phpcs: - @vendor/bin/phpcs --parallel=$(PARALLELISM) $(PHPCS_FLAGS) +phpcs: vendor/composer/installed.json + @$(DOCKER_PHP_EXEC) vendor/bin/phpcs --parallel=$(PARALLELISM) $(PHPCS_FLAGS) .PHONY: phpstan -phpstan: - @php -d xdebug.mode=off vendor/bin/phpstan analyse --memory-limit=-1 +phpstan: vendor/composer/installed.json + @$(DOCKER_PHP_EXEC) php -d xdebug.mode=off vendor/bin/phpstan analyse --memory-limit=-1 ifndef PHPBENCH_REPORT override PHPBENCH_REPORT = aggregate endif .PHONY: phpbench -phpbench: - @vendor/bin/phpbench run -l dots --retry-threshold=5 --report=$(PHPBENCH_REPORT) $(PHPBENCH_FLAGS) +phpbench: vendor/composer/installed.json + @$(DOCKER_PHP_EXEC) vendor/bin/phpbench run -l dots --retry-threshold=5 --report=$(PHPBENCH_REPORT) $(PHPBENCH_FLAGS) diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 00000000..3cdc7764 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,11 @@ +services: + php: + build: + context: . + args: + USER_ID: ${USER_ID} + GROUP_ID: ${GROUP_ID} + # Same paths mean stack traces from docker are usable in host too + volumes: + - .:${PWD} + working_dir: ${PWD}