-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
270 lines (231 loc) · 10.5 KB
/
Makefile
File metadata and controls
270 lines (231 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# ---------------------------------------
# 🐰 Project: Alice
# ---------------------------------------
# Define the environment file path, defaulting to .env
DOTENV ?= .env
# Load environment variables. The '-' prefix prevents 'make' from
# erroring out if the file hasn't been created yet, allowing
# our 'check-env' target to provide a helpful error message.
-include $(DOTENV)
PROJECT_NAME := alice
BIN_DIR := bin
# --------------------------------------------------------------------
# 🛠️ Shell Configuration
# --------------------------------------------------------------------
SHELL := bash
.ONESHELL:
.SHELLFLAGS := -eu -o pipefail -c
# --------------------------------------------------------------------
# ⚙️ Tools / Config
# --------------------------------------------------------------------
GO ?= go
GOFLAGS ?=
RACE ?= -race
COVERAGE_OUTPUT ?= cover.out
GOTEST_FLAGS ?= -vet=off -covermode=atomic -coverprofile=$(COVERAGE_OUTPUT)
MIGRATION_FOLDER := migrations
# Use the DSN from your .env (ALICE_DATABASE.DSN)
DATABASE_URL := $(ALICE_DATABASE.DSN)
COMPOSE_FILE ?= compose-dev.yml
DOCKER_COMPOSE ?= docker compose -f $(COMPOSE_FILE)
COMPOSE_ENV = PORT="$(ALICE_SERVER.PORT)" \
MINIO_ROOT_USER="$(ALICE_S3.ACCESS_KEY_ID)" \
MINIO_ROOT_PASSWORD="$(ALICE_S3.SECRET_ACCESS_KEY)"
# Colors for flair
CLR_ORANGE = \033[0;33m
CLR_BLUE = \033[0;34m
CLR_GREEN = \033[0;32m
CLR_RESET = \033[0m
# --------------------------------------------------------------------
# ⚡ Phony Targets
# --------------------------------------------------------------------
.PHONY: help check-env \
deps fmt vet build local \
test test-race test-integration \
start-dev-all start stop restart prune logs \
migration-create migration-up migration-down \
migration-status migration-force \
commit clean
# --------------------------------------------------------------------
# 📖 Help
# --------------------------------------------------------------------
## help: 📋 Display this help message
help:
@printf "$(CLR_BLUE)🐇 Alice Project Commands:$(CLR_RESET)\n"
@printf "Usage: make <target>\n\n"
@printf "Available targets:\n"
@sed -n 's/^##//p' $(MAKEFILE_LIST) | while IFS=':' read -r target help; do \
printf " $(CLR_GREEN)%-18s$(CLR_RESET) %s\n" "$$target" "$$help"; \
done
@printf "\n"
# --------------------------------------------------------------------
# 🏗️ Go Development Tasks
# --------------------------------------------------------------------
## deps: 📦 Tidy and download Go modules
deps:
@echo -e "$(CLR_ORANGE)→ 📦 Tidying and downloading modules...$(CLR_RESET)"
@$(GO) mod tidy
@$(GO) mod download
## fmt: 🎨 Format source code
fmt:
@echo -e "$(CLR_ORANGE)→ 🎨 Formatting code...$(CLR_RESET)"
@$(GO) fmt ./...
## vet: 🔍 Run static analysis
vet:
@echo -e "$(CLR_ORANGE)→ 🔍 Vetting code...$(CLR_RESET)"
@$(GO) vet ./...
# Internal helper target to ensure .env exists
check-env:
@if [ ! -f .env ]; then \
printf "$(CLR_ORANGE)❌ Error: .env file missing!$(CLR_RESET)\n"; \
printf "Please run: $(CLR_GREEN)cp .env.template .env$(CLR_RESET) and configure it.\n"; \
exit 1; \
fi
## build: 🏗️ Build the Go server binary
build: check-env deps fmt vet
@printf "$(CLR_ORANGE)→ 🏗️ Building $(PROJECT_NAME)...$(CLR_RESET)\n"
@mkdir -p $(BIN_DIR)
@$(GO) build $(GOFLAGS) -o $(BIN_DIR)/$(PROJECT_NAME) ./cmd/server
@printf "$(CLR_GREEN)✅ Build complete: $(BIN_DIR)/$(PROJECT_NAME)$(CLR_RESET)\n"
## local: 🚀 Run the app locally
local: build
@printf "$(CLR_GREEN)🚀 Starting $(PROJECT_NAME)...$(CLR_RESET)\n"
@./$(BIN_DIR)/$(PROJECT_NAME)
# --------------------------------------------------------------------
# 🧪 Testing Tasks
# --------------------------------------------------------------------
## test: 🧪 Run unit tests with coverage
test: deps fmt vet
@printf "$(CLR_ORANGE)→ 🧪 Running unit tests...$(CLR_RESET)\n"
@$(GO) test $(GOFLAGS) $(GOTEST_FLAGS) -short ./...
@printf "$(CLR_GREEN)✅ Unit tests passed!$(CLR_RESET)\n"
## test-race: 🏎️ Run tests with the race detector
test-race: deps fmt vet
@printf "$(CLR_ORANGE)→ 🏎️ Checking for data races...$(CLR_RESET)\n"
@$(GO) test $(GOFLAGS) $(RACE) $(GOTEST_FLAGS) ./...
@printf "$(CLR_GREEN)✅ Race detection passed!$(CLR_RESET)\n"
## test-integration: 🔗 Run integration tests (serial)
test-integration: deps fmt vet
@printf "$(CLR_ORANGE)→ 🔗 Running integration tests (serial)...$(CLR_RESET)\n"
@$(GO) test $(GOFLAGS) -p 1 $(GOTEST_FLAGS) ./...
@printf "$(CLR_GREEN)✅ Integration tests passed!$(CLR_RESET)\n"
# --------------------------------------------------------------------
# 🐳 Docker (Services)
# --------------------------------------------------------------------
## start-dev-all: 🛠️ Build and launch the complete development environment
start-dev-all: check-env
@printf "$(CLR_ORANGE)→ 🐳 Starting full stack (App + Infra)...$(CLR_RESET)\n"
@printf "📃 $(CLR_ORANGE)Compose File:$(CLR_RESET) compose-dev-all.yml\n"
@$(COMPOSE_ENV) docker compose -f compose-dev-all.yml up -d --build
@printf "$(CLR_GREEN)✅ Alice is running on http://localhost:$(ALICE_SERVER.PORT)$(CLR_RESET)\n"
## start: 🆙 Bring up all containers defined in compose.yml, compose-dev.yml, or compose-dev-all.yml
start: check-env
@printf "$(CLR_ORANGE)→ 🐳 Starting $(ALICE_PRIMARY.ENV) services...$(CLR_RESET)\n"
@printf "📃 $(CLR_ORANGE)Compose File:$(CLR_RESET) $(COMPOSE_FILE)\n"
@$(COMPOSE_ENV) $(DOCKER_COMPOSE) up -d --build
@printf "$(CLR_GREEN)✅ Services are up and running!$(CLR_RESET)\n"
## stop: 🛑 Gracefully shut down all containers
stop:
@printf "$(CLR_ORANGE)→ 🛑 Stopping $(ALICE_PRIMARY.ENV) services...$(CLR_RESET)\n"
@printf "📃 $(CLR_ORANGE)Compose File:$(CLR_RESET) $(COMPOSE_FILE)\n"
@$(COMPOSE_ENV) $(DOCKER_COMPOSE) down
@printf "$(CLR_GREEN)✅ Services stopped.$(CLR_RESET)\n"
## restart: 🔄 Cycle all services (Stop then Start)
restart: stop start
## prune: 🧼 Hard reset: Wipe containers, volumes, and project images
prune:
@printf "$(CLR_ORANGE)→ 🧼 Deep cleaning $(ALICE_PRIMARY.ENV) environment...$(CLR_RESET)\n"
@printf "📃 $(CLR_ORANGE)Compose File:$(CLR_RESET) $(COMPOSE_FILE)\n"
@$(COMPOSE_ENV) $(DOCKER_COMPOSE) down -v --remove-orphans
@docker image prune -f --filter "label=com.docker.compose.project=$(PROJECT_NAME)"
@printf "$(CLR_GREEN)✨ Docker environment scrubbed.$(CLR_RESET)\n"
## logs: 📋 Aggregate stream of all service logs
logs:
@printf "$(CLR_ORANGE)→ 📋 Tailing logs...$(CLR_RESET)\n"
@printf "📃 $(CLR_ORANGE)Compose File:$(CLR_RESET) $(COMPOSE_FILE)\n"
@$(COMPOSE_ENV) $(DOCKER_COMPOSE) logs -f
# --------------------------------------------------------------------
# 🐍 Database Migrations (golang-migrate)
# --------------------------------------------------------------------
## migration-create: 🆕 Create a new SQL migration
migration-create:
@if [ -z "$(NAME)" ]; then \
printf "$(CLR_ORANGE)❌ Error: NAME is required. Usage: make migration-create NAME=your_migration_name$(CLR_RESET)\n"; \
exit 1; \
fi
@echo "→ Creating new migration: $(NAME)"
@migrate create -ext sql -dir $(MIGRATION_FOLDER) -seq $(NAME)
@echo "→ Migration $(NAME) created successfully."
## migration-up: ⬆️ Apply all pending migrations (with confirmation)
migration-up:
@printf "$(CLR_ORANGE)⚠️ WARNING: About to apply all pending migrations to: $(DATABASE_URL)$(CLR_RESET)\n"
@read -p "Are you sure you want to proceed? [y/N]: " CONFIRM; \
if [[ "$$CONFIRM" =~ ^[yY](es)?$$ ]]; then \
echo "→ Applying migrations..."; \
migrate -path $(MIGRATION_FOLDER) -database "$(DATABASE_URL)" up && \
echo "✅ All migrations applied successfully." || \
(echo "❌ Migration failed." && exit 1); \
else \
echo "❌ Migration cancelled."; \
fi
## migration-down: ⬇️ Rollback migrations (with confirmation)
migration-down:
@printf "$(CLR_ORANGE)⚠️ WARNING: About to ROLLBACK migrations on: $(DATABASE_URL)$(CLR_RESET)\n"
@read -p "Number of migrations to rollback (default: 1): " NUM; \
NUM=$${NUM:-1}; \
read -p "Confirm rollback of $$NUM migration(s)? [y/N]: " CONFIRM; \
if [[ "$$CONFIRM" =~ ^[yY](es)?$$ ]]; then \
migrate -path $(MIGRATION_FOLDER) -database "$(DATABASE_URL)" down $$NUM; \
echo "✅ Rolled back successfully."; \
else \
echo "❌ Rollback cancelled."; \
fi
## migration-status: 📊 Show current migration version
migration-status:
@echo "→ Showing migration status..."
@migrate -path $(MIGRATION_FOLDER) -database "$(DATABASE_URL)" version
## migration-force: 🔨 Force the database to a specific migration version
migration-force:
@echo "→ Forcing migration version..."
@read -p "Enter the version to force: " VERSION; \
if [ -z "$${VERSION}" ]; then \
echo "ERROR: Version is required"; exit 1; \
fi; \
migrate -path $(MIGRATION_FOLDER) -database "$(DATABASE_URL)" force $${VERSION}
# --------------------------------------------------------------------
# 🌿 Git / Workflow
# --------------------------------------------------------------------
## commit: 💾 Stage all changes and commit (usage: make commit MSG="your message")
commit: fmt vet
@if [ -z "$(MSG)" ]; then \
printf "$(CLR_ORANGE)❌ Error: MSG variable is required.$(CLR_RESET)\n"; \
printf "Usage: make commit MSG=\"your message\"\n"; \
exit 1; \
fi
@git add .
@git commit -m "$(MSG)"
@printf "$(CLR_GREEN)✅ Changes committed with message: $(MSG)$(CLR_RESET)\n"
# Meta-programming for Conventional Commits
# We prefix these with 'c-' to avoid collisions with targets like 'test'
define commit_template
c-$1:
@$(MAKE) commit MSG="$(1): $$(MSG)"
endef
$(eval $(call commit_template,feat))
$(eval $(call commit_template,fix))
$(eval $(call commit_template,chore))
$(eval $(call commit_template,refactor))
$(eval $(call commit_template,test))
$(eval $(call commit_template,docs))
$(eval $(call commit_template,style))
$(eval $(call commit_template,perf))
# --------------------------------------------------------------------
# 🧹 Cleanup Tasks
# --------------------------------------------------------------------
## clean: 🧹 Wipe build artifacts, coverage, and test cache
clean:
@printf "$(CLR_ORANGE)→ 🧹 Cleaning $(BIN_DIR)/ and $(COVERAGE_OUTPUT)...$(CLR_RESET)\n"
@rm -rf $(BIN_DIR) $(COVERAGE_OUTPUT)
@printf "$(CLR_ORANGE)→ 🧼 Clearing Go (test) cache...$(CLR_RESET)\n"
@$(GO) clean -testcache
@printf "$(CLR_GREEN)✨ Clean complete!$(CLR_RESET)\n"