Last active
May 31, 2024 00:51
-
-
Save B-Galati/ff6ad09aab746736bcf497f0fe67bca9 to your computer and use it in GitHub Desktop.
Example of Makefile for php script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.PHONY: help clean build vendor node_modules test php-cs-fixer | |
SYMFONY_ENV ?= dev | |
SYMFONY_CONSOLE_ARGS ?= --env=dev | |
SYMFONY_ASSETS_INSTALL ?= relative | |
COMPOSER_ARGS ?= | |
PHP=php | |
ifneq ($(SYMFONY_ENV), dev) | |
COMPOSER_ARG = --optimize-autoloader --no-dev --no-suggest --no-interaction --classmap-authoritative | |
SYMFONY_CONSOLE_ARGS = --env=$(SYMFONY_ENV) --no-debug | |
SYMFONY_ASSETS_INSTALL = hard | |
endif | |
COLOR_RESET = \033[0m | |
COLOR_INFO = \033[32m | |
COLOR_COMMENT = \033[33m | |
## Help | |
help: | |
@printf "${COLOR_COMMENT}Usage:${COLOR_RESET}\n" | |
@printf " make [target]\n\n" | |
@printf "${COLOR_COMMENT}Available targets:${COLOR_RESET}\n" | |
@awk '/^[a-zA-Z\-\_0-9\.@]+:/ { \ | |
helpMessage = match(lastLine, /^## (.*)/); \ | |
if (helpMessage) { \ | |
helpCommand = substr($$1, 0, index($$1, ":")); \ | |
helpMessage = substr(lastLine, RSTART + 3, RLENGTH); \ | |
printf " ${COLOR_INFO}%-16s${COLOR_RESET} %s\n", helpCommand, helpMessage; \ | |
} \ | |
} \ | |
{ lastLine = $$0 }' $(MAKEFILE_LIST) | |
# SOURCE : http://stackoverflow.com/questions/10858261/abort-makefile-if-variable-not-set | |
# Check that given variables are set and all have non-empty values, | |
# die with an error otherwise. | |
# | |
# Params: | |
# 1. Variable name(s) to test. | |
# 2. (optional) Error message to print. | |
check_defined = \ | |
$(strip $(foreach 1,$1, \ | |
$(call __check_defined,$1,$(strip $(value 2))))) | |
__check_defined = \ | |
$(if $(value $1),, \ | |
$(error Undefined $1$(if $2, ($2)))) | |
####################### | |
# BUILDING TASKS | |
####################### | |
## Install all package dependencies and assets | |
build: vendor node_modules | |
php app/console assetic:dump $(SYMFONY_CONSOLE_ARGS) --forks=4 | |
## Install vendor folder | |
vendor: composer.lock | |
composer config extra.symfony-assets-install $(SYMFONY_ASSETS_INSTALL) | |
composer install $(COMPOSER_ARGS) | |
## Update / create composer.lock file | |
composer.lock: | |
composer update --lock $(COMPOSER_ARGS) | |
## Install node_modules | |
node_modules: package.json | |
npm install | |
## Clean vendors, node_modules, cache, logs, assets, etc. | |
clean: | |
rm -rf vendor/ node_modules/ web/bundles/ web/assets/ app/cache/* app/logs/* | |
####################### | |
# TESTING TASKS | |
####################### | |
## Run all tests (unit tests, code style, linters, etc.) | |
test: | |
test `find ./src -iname "*.php" | xargs -n1 -P6 php -l | grep -Fv "No syntax errors" | wc -l` -eq 0 | |
# vendor/bin/php-cs-fixer fix --diff --dry-run -v | |
php app/console $(SYMFONY_CONSOLE_ARGS) security:check | |
php app/console $(SYMFONY_CONSOLE_ARGS) lint:yaml app | |
php app/console $(SYMFONY_CONSOLE_ARGS) lint:yaml src | |
php app/console $(SYMFONY_CONSOLE_ARGS) lint:twig src app | |
# find ./src ./app -iname "*.less" | xargs -n1 -P6 node_modules/less/bin/lessc --lint | |
php -f ./phpunit | |
####################### | |
# DEPLOYMENT TASKS | |
####################### | |
## Compress and optimize all png and jpg files | |
trimage: pngquant mozjpeg | |
## Optimize png files using pngquant and zopflipng | |
pngquant: | |
find ./src/ ./web/assets/ -regextype posix-extended -type f -regex '.*(png)$$' \ | |
| xargs -I {} -P4 -n1 bash -c 'pngquant -o "{}" --force --quality=70-80 "{}";zopflipng -m -y "{}" "{}";' | |
## Optimize jpeg files using mozjpeg | |
mozjpeg: | |
find ./web/assets/ ./src/ -regextype posix-extended -type f -regex '.*(jpe?g)$$' \ | |
| xargs -I {} -P4 -n1 bash -c 'echo "{}";mozjpeg -copy none -optimize "{}" > "{}.opt";mv "{}.opt" "{}";' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment