forum/docs/makefile.md

95 lines
3.8 KiB
Markdown
Raw Normal View History

2021-10-07 13:48:05 +02:00
# Makefile
Here is a Makefile template. It provides some shortcuts for the most common tasks.
To use it, create a new `Makefile` file at the root of your project. Copy/paste
the content in the template section. To view all the available commands, run `make`.
For example, in the [getting started section](/README.md#getting-started), the
2022-07-28 19:01:17 +02:00
`docker compose` commands could be replaced by:
2021-10-07 13:48:05 +02:00
1. Run `make build` to build fresh images
2. Run `make up` (detached mode without logs)
3. Run `make down` to stop the Docker containers
Of course, this template is basic for now. But, as your application is growing,
you will probably want to add some targets like running your tests as described
in [the Symfony book](https://symfony.com/doc/current/the-fast-track/en/17-tests.html#automating-your-workflow-with-a-makefile).
You can also find a more complete example in this [snippet](https://www.strangebuzz.com/en/snippets/the-perfect-makefile-for-symfony).
2021-10-28 12:27:40 +02:00
If you want to run make from within the `php` container, in the [Dockerfile](/Dockerfile),
add:
```diff
2022-10-13 14:13:10 +02:00
gettext \
git \
2021-10-28 12:27:40 +02:00
+make \
```
And rebuild the PHP image.
2023-12-03 21:51:35 +01:00
> [!NOTE]
> If you are using Windows, you have to install [chocolatey.org](https://chocolatey.org/) or [Cygwin](http://cygwin.com) to use the `make` command. Check out this [StackOverflow question](https://stackoverflow.com/q/2532234/633864) for more explanations.
2021-10-07 13:48:05 +02:00
## The template
```Makefile
# Executables (local)
2022-07-28 19:01:17 +02:00
DOCKER_COMP = docker compose
2021-10-07 13:48:05 +02:00
# Docker containers
PHP_CONT = $(DOCKER_COMP) exec php
# Executables
PHP = $(PHP_CONT) php
COMPOSER = $(PHP_CONT) composer
SYMFONY = $(PHP) bin/console
2021-10-07 13:48:05 +02:00
# Misc
.DEFAULT_GOAL = help
.PHONY : help build up start down logs sh composer vendor sf cc test
2021-10-07 13:48:05 +02:00
## —— 🎵 🐳 The Symfony Docker Makefile 🐳 🎵 ——————————————————————————————————
2021-10-07 13:48:05 +02:00
help: ## Outputs this help screen
2022-12-21 14:57:24 +01:00
@grep -E '(^[a-zA-Z0-9\./_-]+:.*?##.*$$)|(^##)' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}{printf "\033[32m%-30s\033[0m %s\n", $$1, $$2}' | sed -e 's/\[32m##/[33m/'
2021-10-07 13:48:05 +02:00
## —— Docker 🐳 ————————————————————————————————————————————————————————————————
build: ## Builds the Docker images
@$(DOCKER_COMP) build --pull --no-cache
up: ## Start the docker hub in detached mode (no logs)
@$(DOCKER_COMP) up --detach
2021-10-28 12:27:40 +02:00
start: build up ## Build and start the containers
2021-10-07 13:48:05 +02:00
down: ## Stop the docker hub
@$(DOCKER_COMP) down --remove-orphans
logs: ## Show live logs
@$(DOCKER_COMP) logs --tail=0 --follow
2023-10-16 09:59:50 +02:00
sh: ## Connect to the FrankenPHP container
2021-10-07 13:48:05 +02:00
@$(PHP_CONT) sh
test: ## Start tests with phpunit, pass the parameter "c=" to add options to phpunit, example: make test c="--group e2e --stop-on-failure"
@$(eval c ?=)
@$(DOCKER_COMP) exec -e APP_ENV=test php bin/phpunit $(c)
2021-10-07 13:48:05 +02:00
## —— Composer 🧙 ——————————————————————————————————————————————————————————————
composer: ## Run composer, pass the parameter "c=" to run a given command, example: make composer c='req symfony/orm-pack'
@$(eval c ?=)
@$(COMPOSER) $(c)
vendor: ## Install vendors according to the current composer.lock file
vendor: c=install --prefer-dist --no-dev --no-progress --no-scripts --no-interaction
vendor: composer
## —— Symfony 🎵 ———————————————————————————————————————————————————————————————
sf: ## List all Symfony commands or pass the parameter "c=" to run a given command, example: make sf c=about
@$(eval c ?=)
@$(SYMFONY) $(c)
cc: c=c:c ## Clear the cache
cc: sf
```