Last active
July 3, 2024 06:38
-
-
Save AlcidesRC/36eea84497c1ebf9735e7d18536e4465 to your computer and use it in GitHub Desktop.
Ejemplo de Makefile para proyectos PHP
This file contains 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
.DEFAULT_GOAL := help | |
### | |
# CONSTANTS | |
### | |
ifneq (,$(findstring xterm,$(TERM))) | |
BLACK := $(shell tput -Txterm setaf 0) | |
RED := $(shell tput -Txterm setaf 1) | |
GREEN := $(shell tput -Txterm setaf 2) | |
YELLOW := $(shell tput -Txterm setaf 3) | |
BLUE := $(shell tput -Txterm setaf 4) | |
MAGENTA := $(shell tput -Txterm setaf 5) | |
CYAN := $(shell tput -Txterm setaf 6) | |
WHITE := $(shell tput -Txterm setaf 7) | |
RESET := $(shell tput -Txterm sgr0) | |
else | |
BLACK := "" | |
RED := "" | |
GREEN := "" | |
YELLOW := "" | |
BLUE := "" | |
MAGENTA := "" | |
CYAN := "" | |
WHITE := "" | |
RESET := "" | |
endif | |
#--- | |
SERVICE_APP = app | |
#--- | |
HOST_USER_ID := $(shell id --user) | |
HOST_USER_NAME := $(shell id --user --name) | |
HOST_GROUP_ID := $(shell id --group) | |
HOST_GROUP_NAME := $(shell id --group --name) | |
#--- | |
RANDOM_SEED := $(shell head -200 /dev/urandom | cksum | cut -f1 -d " ") | |
#--- | |
DOCKER_COMPOSE_COMMAND = docker compose | |
DOCKER_RUN = $(DOCKER_COMPOSE_COMMAND) run --rm $(SERVICE_APP) | |
DOCKER_RUN_AS_USER = $(DOCKER_COMPOSE_COMMAND) run --rm --user $(HOST_USER_ID):$(HOST_GROUP_ID) $(SERVICE_APP) | |
DOCKER_EXEC = $(DOCKER_COMPOSE_COMMAND) exec $(SERVICE_APP) | |
DOCKER_EXEC_AS_USER = $(DOCKER_COMPOSE_COMMAND) exec --user $(HOST_USER_ID):$(HOST_GROUP_ID) $(SERVICE_APP) | |
DOCKER_BUILD_ARGUMENTS = --build-arg="HOST_USER_ID=$(HOST_USER_ID)" --build-arg="HOST_USER_NAME=$(HOST_USER_NAME)" --build-arg="HOST_GROUP_ID=$(HOST_GROUP_ID)" --build-arg="HOST_GROUP_NAME=$(HOST_GROUP_NAME)" | |
### | |
# FUNCTIONS | |
### | |
require-%: | |
@if [ -z "$($(*))" ] ; then \ | |
echo "" ; \ | |
echo " ${RED}⨉${RESET} Parameter [ ${YELLOW}${*}${RESET} ] is required!" ; \ | |
echo "" ; \ | |
echo " ${YELLOW}ℹ${RESET} Usage [ ${YELLOW}make <command>${RESET} ${RED}${*}=${RESET}${YELLOW}xxxxxx${RESET} ]" ; \ | |
echo "" ; \ | |
exit 1 ; \ | |
fi; | |
define taskDone | |
@echo "" | |
@echo " ${GREEN}✓${RESET} ${GREEN}Task done!${RESET}" | |
@echo "" | |
endef | |
# $(1)=TEXT $(2)=EXTRA | |
define showInfo | |
@echo " ${YELLOW}ℹ${RESET} $(1) $(2)" | |
endef | |
# $(1)=TEXT $(2)=EXTRA | |
define showAlert | |
@echo " ${RED}!${RESET} $(1) $(2)" | |
endef | |
# $(1)=NUMBER $(2)=TEXT | |
define orderedList | |
@echo "" | |
@echo " ${CYAN}$(1).${RESET} ${CYAN}$(2)${RESET}" | |
@echo "" | |
endef | |
### | |
# HELP | |
### | |
.PHONY: help | |
help: | |
@clear | |
@echo "╔══════════════════════════════════════════════════════════════════════════════╗" | |
@echo "║ ║" | |
@echo "║ ${YELLOW}.:${RESET} AVAILABLE COMMANDS ${YELLOW}:.${RESET} ║" | |
@echo "║ ║" | |
@echo "╚══════════════════════════════════════════════════════════════════════════════╝" | |
@echo "" | |
@grep -E '^[a-zA-Z_0-9%-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "· ${YELLOW}%-30s${RESET} %s\n", $$1, $$2}' | |
@echo "" | |
### | |
# DOCKER RELATED | |
### | |
.PHONY: build | |
build: ## Docker: builds the service | |
@$(DOCKER_COMPOSE_COMMAND) build $(DOCKER_BUILD_ARGUMENTS) | |
$(call taskDone) | |
.PHONY: up | |
up: ## Docker: starts the service | |
@$(DOCKER_COMPOSE_COMMAND) up --remove-orphans --detach | |
$(call taskDone) | |
.PHONY: restart | |
restart: ## Docker: restarts the service | |
@$(DOCKER_COMPOSE_COMMAND) restart | |
$(call taskDone) | |
.PHONY: down | |
down: ## Docker: stops the service | |
@$(DOCKER_COMPOSE_COMMAND) down --remove-orphans | |
$(call taskDone) | |
.PHONY: logs | |
logs: ## Docker: exposes the service logs | |
@$(DOCKER_COMPOSE_COMMAND) logs | |
$(call taskDone) | |
.PHONY: bash | |
bash: ## Docker: establish a bash session into main container | |
$(DOCKER_RUN_AS_USER) bash | |
### | |
# COMPOSER | |
### | |
.PHONY: composer-dump | |
composer-dump: ## Application: <composer dump-auto> | |
$(DOCKER_RUN_AS_USER) composer dump-auto --ansi --no-plugins --profile --classmap-authoritative --apcu --strict-psr | |
$(call taskDone) | |
.PHONY: composer-install | |
composer-install: ## Application: <composer install> | |
$(DOCKER_RUN_AS_USER) composer install --ansi --no-plugins --classmap-authoritative --audit --apcu-autoloader | |
$(call taskDone) | |
.PHONY: composer-remove | |
composer-remove: require-package ## Application: <composer remove> | |
$(DOCKER_RUN_AS_USER) composer remove --ansi --no-plugins --classmap-authoritative --apcu-autoloader --with-all-dependencies --unused | |
$(call taskDone) | |
.PHONY: composer-require-dev | |
composer-require-dev: ## Application: <composer require --dev> | |
$(DOCKER_RUN_AS_USER) composer require --ansi --no-plugins --classmap-authoritative --apcu-autoloader --with-all-dependencies --prefer-stable --sort-packages --dev | |
$(call taskDone) | |
.PHONY: composer-require | |
composer-require: ## Application: <composer require> | |
$(DOCKER_RUN_AS_USER) composer require --ansi --no-plugins --classmap-authoritative --apcu-autoloader --with-all-dependencies --prefer-stable --sort-packages | |
$(call taskDone) | |
.PHONY: composer-update | |
composer-update: ## Application: <composer update> | |
$(DOCKER_RUN_AS_USER) composer update --ansi --no-plugins --classmap-authoritative --apcu-autoloader --with-all-dependencies | |
$(call taskDone) | |
### | |
# QA | |
### | |
.PHONY: linter | |
linter: ## QA: <composer linter [filter="path/to/file"]> | |
@$(eval filter ?= 'app') | |
@composer linter $(filter) | |
$(call taskDone) | |
.PHONY: phpcs | |
phpcs: ## QA: <composer phpcbs [filter="path/to/file"]> | |
@$(eval filter ?= 'app') | |
@composer phpcs $(filter) | |
$(call taskDone) | |
.PHONY: phpcbf | |
phpcbf: ## QA: <composer phpcbf [filter="path/to/file"]> | |
@$(eval filter ?= 'app') | |
@composer phpcbf $(filter) | |
$(call taskDone) | |
.PHONY: phpstan | |
phpstan: ## QA: <composer phpstan [filter="path/to/file"]> | |
@$(eval filter ?= 'app') | |
@composer phpstan $(filter) | |
$(call taskDone) | |
.PHONY: tests | |
tests: ## QA: <composer tests [testsuite="Unit"] [filter="testMethodName path/to/test/file"]> | |
@$(eval testsuite ?= 'Unit') | |
@$(eval filter ?= '.') | |
@php -d xdebug.mode=off vendor/bin/phpunit --configuration=phpunit.xml --testdox --colors --order-by=random --random-order-seed=$(RANDOM_SEED) --testsuite=$(testsuite) --filter=$(filter) | |
$(call taskDone) | |
.PHONY: tests-unit | |
tests-unit: ## QA: <composer tests-unit [filter="testMethodName path/to/file"]> | |
@$(eval filter ?= '.') | |
@php -d xdebug.mode=off vendor/bin/phpunit --configuration=phpunit.xml --testdox --colors --order-by=random --random-order-seed=$(RANDOM_SEED) --testsuite=Unit --filter=$(filter) | |
$(call taskDone) | |
.PHONY: coverage | |
coverage: ## QA: <composer coverage> | |
@composer coverage | |
$(call taskDone) | |
### | |
# MISCELANEOUS | |
### | |
.PHONY: show-context | |
show-context: ## Setup: show context | |
$(call showInfo,"Showing context") | |
@echo " · Host user : (${YELLOW}${HOST_USER_ID}${RESET}) ${YELLOW}${HOST_USER_NAME}${RESET}" | |
@echo " · Host group : (${YELLOW}${HOST_GROUP_ID}${RESET}) ${YELLOW}${HOST_GROUP_NAME}${RESET}" | |
@echo " · Service(s) : ${YELLOW}${SERVICE_APP}${RESET}" | |
$(call taskDone) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment