|
| 1 | +# Makefile for managing a project with multiple Docker Compose services |
| 2 | + |
| 3 | +# Variables |
| 4 | +DOCKER_COMPOSE_FILE := docker-compose.yml |
| 5 | +COMPOSE := docker compose -f $(DOCKER_COMPOSE_FILE) |
| 6 | +COMPOSE_DEV := docker compose -f $(DOCKER_COMPOSE_FILE) -f docker-compose.dev.yml |
| 7 | +BACKEND_SERVICE := backend |
| 8 | +FRONTEND_SERVICE := frontend |
| 9 | + |
| 10 | +# Default target: show help |
| 11 | +.PHONY: help |
| 12 | +help: |
| 13 | + @echo "Available targets:" |
| 14 | + @echo " up - Start all services in the background (detached mode)" |
| 15 | + @echo " down - Stop services and remove containers" |
| 16 | + @echo " ps - List running containers" |
| 17 | + @echo " dev - Start backend and frontend in the background (dev mode)" |
| 18 | + @echo " build-backend - Build the backend image" |
| 19 | + @echo " build-frontend- Build the frontend image" |
| 20 | + @echo " build - Build both backend and frontend services" |
| 21 | + @echo " logs - Tail logs for all services" |
| 22 | + @echo " logs-backend - Tail logs for backend" |
| 23 | + @echo " restart - Restart services" |
| 24 | + @echo " test-backend - Run backend test suite" |
| 25 | + @echo " test-frontend - Run frontend test suite" |
| 26 | + @echo " lint-backend - Lint the backend codebase" |
| 27 | + @echo " lint-frontend - Lint the frontend codebase" |
| 28 | + @echo " clean - Clean dangling Docker images and volumes" |
| 29 | + |
| 30 | +# Dev setup |
| 31 | +.PHONY: dev |
| 32 | +dev: |
| 33 | + $(COMPOSE_DEV) up -d |
| 34 | + @echo "Services running in dev mode" |
| 35 | + |
| 36 | +# List running containers |
| 37 | +.PHONY: ps |
| 38 | +ps: |
| 39 | + $(COMPOSE) ps |
| 40 | + @echo "Services running" |
| 41 | + |
| 42 | +# Start all services in background (detached mode) |
| 43 | +.PHONY: up |
| 44 | +up: |
| 45 | + $(COMPOSE) up -d |
| 46 | + @echo "Services have been started in detached mode." |
| 47 | + |
| 48 | +# Stop and remove all services and containers |
| 49 | +.PHONY: down |
| 50 | +down: |
| 51 | + $(COMPOSE) down |
| 52 | + @echo "Services have been stopped and removed." |
| 53 | + |
| 54 | +# Create folders and fix permissions: doctrine, assets, private-files, |
| 55 | +.PHONY: init |
| 56 | +init: |
| 57 | + cp .env.sample .env |
| 58 | + # $(COMPOSE) exec backend mkdir var/logs/ |
| 59 | + # $(COMPOSE) exec backend mkdir var/private-files/ |
| 60 | + @echo "Folders created." |
| 61 | + $(COMPOSE) exec backend chmod 777 var/logs/ |
| 62 | + $(COMPOSE) exec backend chmod 777 var/private-files/ |
| 63 | + $(COMPOSE) exec backend chmod 777 var/sessions/ |
| 64 | + $(COMPOSE) exec backend chmod 777 var/DoctrineProxies/ |
| 65 | + $(COMPOSE) exec backend chmod 777 public/assets/ |
| 66 | + @echo "Permissions granted." |
| 67 | + |
| 68 | + |
| 69 | +.PHONY: init_dev |
| 70 | +init_dev: |
| 71 | + $(COMPOSE) exec backend composer install |
| 72 | + @echo "Permissions granted." |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | +# Run Migrations |
| 77 | +.PHONY: db-migrations |
| 78 | +db-migrations: |
| 79 | + $(COMPOSE) exec backend php src/tools/apply-updates.php |
| 80 | + @echo "db updates applied." |
| 81 | + |
| 82 | +# Restore dump database |
| 83 | +.PHONY: db-restore |
| 84 | +db-restore: |
| 85 | + $(COMPOSE) exec database bash -c "psql -h localhost -U mapas -d mapas < /data/dump.sql" |
| 86 | + @echo "dump.sql default database dump was restored." |
| 87 | + |
| 88 | +# Build the backend service |
| 89 | +.PHONY: build-backend |
| 90 | +build-backend: |
| 91 | + $(COMPOSE) build $(BACKEND_SERVICE) |
| 92 | + @echo "Backend service has been built." |
| 93 | + |
| 94 | +# # Build the frontend service |
| 95 | +# .PHONY: build-frontend |
| 96 | +# build-frontend: |
| 97 | +# $(COMPOSE_DEV) run --rm frontend "/bin/sh -c 'npm install -g pnpm && pnpm install -s && pnpm run watch'" |
| 98 | +# @echo "Frontend service has been built." |
| 99 | + |
| 100 | +# Build all images (both backend and frontend) |
| 101 | +.PHONY: build |
| 102 | +build: build-backend build-frontend |
| 103 | + @echo "All services have been built." |
| 104 | + |
| 105 | +# Tail logs for all services |
| 106 | +.PHONY: logs |
| 107 | +logs: |
| 108 | + $(COMPOSE) logs |
| 109 | + @echo "Displaying logs for all services." |
| 110 | + |
| 111 | +# Tail logs for backend |
| 112 | +.PHONY: logs-backend |
| 113 | +logs-backend: |
| 114 | + $(COMPOSE) exec $(BACKEND_SERVICE) tail -f var/logs/app.log |
| 115 | + @echo "Displaying logs for backend." |
| 116 | + |
| 117 | +# Restart all services (useful after code changes) |
| 118 | +.PHONY: restart |
| 119 | +restart: |
| 120 | + $(COMPOSE) restart |
| 121 | + @echo "All services have been restarted." |
| 122 | + |
| 123 | +# Run the backend tests |
| 124 | +.PHONY: test-backend |
| 125 | +test-backend: |
| 126 | + $(COMPOSE) exec $(BACKEND_SERVICE) make test # assuming you have a Makefile for testing inside backend |
| 127 | + @echo "Backend tests have been run." |
| 128 | + |
| 129 | +# Run the frontend tests |
| 130 | +.PHONY: test-frontend |
| 131 | +test-frontend: |
| 132 | + $(COMPOSE) exec $(FRONTEND_SERVICE) npm test # or whatever command you use |
| 133 | + @echo "Frontend tests have been run." |
| 134 | + |
| 135 | +# Lint the backend code |
| 136 | +.PHONY: lint-backend |
| 137 | +lint-backend: |
| 138 | + $(COMPOSE) exec $(BACKEND_SERVICE) make lint # assuming you have a Makefile target for this |
| 139 | + @echo "Backend code has been linted." |
| 140 | + |
| 141 | +# Lint the frontend code |
| 142 | +.PHONY: lint-frontend |
| 143 | +lint-frontend: |
| 144 | + $(COMPOSE) exec $(FRONTEND_SERVICE) npm run lint # or command appropriate for your frontend framework |
| 145 | + @echo "Frontend code has been linted." |
| 146 | + |
| 147 | +# Clean Docker: remove any stopped container, dangling images, volumes, etc. |
| 148 | +.PHONY: clean |
| 149 | +clean: |
| 150 | + $(COMPOSE) down -v --remove-orphans |
| 151 | + @sudo find . -name 'node_modules' -type d -prune -exec rm -rf '{}' + |
| 152 | + @sudo rm -rf vendor |
| 153 | + @echo "Cleaned up Docker containers, images, volumes." |
0 commit comments