-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathMakefile
More file actions
280 lines (246 loc) · 9.86 KB
/
Copy pathMakefile
File metadata and controls
280 lines (246 loc) · 9.86 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
271
272
273
274
275
276
277
278
279
280
# Binary name
BINARY_NAME=dashbrr
# Go parameters
GOCMD=go
GOBUILD=$(GOCMD) build
GOCLEAN=$(GOCMD) clean
GOGET=$(GOCMD) get
GOMOD=$(GOCMD) mod
# Frontend parameters
PNPM=pnpm
# Docker parameters
DOCKER_COMPOSE=docker compose
# Build directories
BUILD_DIR=web/dist
BIN_DIR=bin
# Main Go file
MAIN_GO=./cmd/dashbrr/main.go
# Version information
VERSION=$(shell git describe --tags --always --dirty)
COMMIT=$(shell git rev-parse --short HEAD)
BUILD_DATE=$(shell date -u +"%Y-%m-%dT%H:%M:%SZ")
# Build flags
LDFLAGS=-s -w \
-X github.com/autobrr/dashbrr/internal/buildinfo.Version=$(VERSION) \
-X github.com/autobrr/dashbrr/internal/buildinfo.Commit=$(COMMIT) \
-X github.com/autobrr/dashbrr/internal/buildinfo.Date=$(BUILD_DATE)
.PHONY: all clean frontend backend deps-go deps-frontend dev docker-dev docker-dev-quick docker-build help docker-clean test-integration test-integration-db test-integration-db-stop run lint lint-backend type-check preview check-air fmt gofix-changed gofix-check-changed precommit
# Default target
all: clean deps-frontend deps-go frontend backend
# Clean build artifacts
clean:
@echo "Cleaning..."
$(GOCLEAN)
find $(BUILD_DIR) -mindepth 1 -not -name '.gitkeep' -delete 2>/dev/null || true
rm -rf $(BIN_DIR)
# Install Go dependencies
deps-go:
@echo "Installing Go dependencies..."
$(GOMOD) tidy
# Install frontend dependencies
deps-frontend:
@echo "Installing frontend dependencies..."
cd web && $(PNPM) install
# Build frontend
frontend: deps-frontend lint
@echo "Building frontend..."
cd web && $(PNPM) build
# Build backend and create final binary
backend: deps-go
@echo "Building backend..."
mkdir -p $(BIN_DIR)
$(GOBUILD) -ldflags="$(LDFLAGS)" -o $(BIN_DIR)/$(BINARY_NAME) $(MAIN_GO)
# Format changed code only (fast, for iteration)
fmt:
@echo "Formatting changed Go code..."
@gofiles=$$({ git diff --name-only --diff-filter=d; git diff --name-only --cached --diff-filter=d; } | sort -u | grep '\.go$$' || true); \
if [ -n "$$gofiles" ]; then echo "$$gofiles" | xargs gofmt -w; fi
@echo "Formatting changed frontend code..."
@webfiles=$$({ git diff --name-only --diff-filter=d -- 'web/'; git diff --name-only --cached --diff-filter=d -- 'web/'; } | sort -u | sed 's|^web/||' | grep -E '\.(ts|tsx|js|jsx)$$' || true); \
if [ -n "$$webfiles" ]; then cd web && echo "$$webfiles" | xargs pnpm eslint --fix; fi
# Lint frontend code
lint:
@echo "Linting frontend code..."
cd web && $(PNPM) lint
# Lint changed backend code
lint-backend:
@echo "Linting changed backend code..."
golangci-lint run --new-from-merge-base=develop --timeout=5m
# Apply go fix to changed Go files only
gofix-changed:
@echo "Running go fix on changed Go files..."
@gofiles=$$({ git diff --name-only --diff-filter=d; git diff --name-only --cached --diff-filter=d; } | sort -u | grep '\.go$$' || true); \
if [ -z "$$gofiles" ]; then \
echo "No changed Go files for go fix."; \
exit 0; \
fi; \
gopkgs=$$(printf '%s\n' "$$gofiles" | xargs -n 1 dirname | sort -u); \
printf '%s\n' "$$gopkgs" | while IFS= read -r pkg; do \
[ -n "$$pkg" ] || continue; \
go fix "./$$pkg" || true; \
done; \
tmp=$$(mktemp); \
printf '%s\n' "$$gopkgs" | while IFS= read -r pkg; do \
[ -n "$$pkg" ] || continue; \
go fix -diff "./$$pkg" >> "$$tmp" || true; \
done; \
if [ -s "$$tmp" ]; then \
echo "go fix left pending changes for changed Go files:"; \
cat "$$tmp"; \
rm -f "$$tmp"; \
echo "Re-run 'make gofix-changed'."; \
exit 1; \
fi; \
rm -f "$$tmp"; \
echo "go fix applied."
# Check go fix drift on changed Go files only (for CI/pre-commit)
gofix-check-changed:
@echo "Checking go fix drift on changed Go files..."
@tmp=$$(mktemp); \
gofiles=$$({ git diff --name-only --diff-filter=d; git diff --name-only --cached --diff-filter=d; } | sort -u | grep '\.go$$' || true); \
if [ -z "$$gofiles" ]; then \
rm -f "$$tmp"; \
echo "No changed Go files for go fix check."; \
exit 0; \
fi; \
gopkgs=$$(printf '%s\n' "$$gofiles" | xargs -n 1 dirname | sort -u); \
printf '%s\n' "$$gopkgs" | while IFS= read -r pkg; do \
[ -n "$$pkg" ] || continue; \
go fix -diff "./$$pkg" >> "$$tmp" || true; \
done; \
if [ -s "$$tmp" ]; then \
echo "go fix changes required for changed Go files:"; \
cat "$$tmp"; \
rm -f "$$tmp"; \
echo "Run 'make gofix-changed'."; \
exit 1; \
fi; \
rm -f "$$tmp"; \
echo "go fix check clean."
# Local pre-commit gate (changed files only)
precommit: fmt gofix-changed lint-backend lint type-check
@echo "Pre-commit checks passed."
# Type check frontend code
type-check:
@echo "Type checking frontend code..."
cd web && $(PNPM) typecheck
# Preview frontend build
preview:
@echo "Starting frontend preview server..."
cd web && $(PNPM) preview
# Ensure air is installed for backend hot reload in dev commands
check-air:
@if ! command -v air > /dev/null 2>&1; then \
echo ""; \
echo "============================================================"; \
echo " ERROR: 'air' is required for make dev"; \
echo "============================================================"; \
echo "Install: go install github.com/air-verse/air@latest"; \
echo ""; \
exit 1; \
fi
# Wait for backend to be ready
wait-backend:
@echo "Waiting for backend to be ready..."
@for i in $$(seq 1 30); do \
if curl -s http://localhost:8080/health > /dev/null; then \
echo "Backend is ready!"; \
exit 0; \
fi; \
echo "Waiting for backend... ($$i/30)"; \
sleep 1; \
done; \
echo "Backend failed to start within 30 seconds"; \
exit 1
# Development mode - run frontend and backend with SQLite and in-memory cache
dev: check-air
@echo "Starting development servers with in-memory cache..."
@echo "Starting backend server with SQLite in debug mode..."
@echo "Using air for backend hot reload..."
@env GIN_MODE=debug DASHBRR__DB_TYPE=sqlite DASHBRR_WEB_DEV_SERVER=http://localhost:3000 DASHBRR_AUTH_BYPASS=true air -c .air.toml & \
backend_pid=$$!; \
echo "Waiting for backend to be ready..."; \
$(MAKE) wait-backend; \
echo "Starting frontend server..."; \
cd web && $(PNPM) dev --host & \
frontend_pid=$$!; \
trap 'kill $$backend_pid $$frontend_pid 2>/dev/null' EXIT; \
wait
# Docker development mode - run with PostgreSQL and memory cache
docker-dev:
@echo "Starting Docker development environment with PostgreSQL and memory cache..."
$(DOCKER_COMPOSE) down
$(DOCKER_COMPOSE) build
$(DOCKER_COMPOSE) up --force-recreate
# Docker development mode - quick start with current cache configuration
docker-dev-quick:
@echo "Starting Docker development environment (quick start)..."
$(DOCKER_COMPOSE) up
# Clean Docker development environment (including volumes)
docker-clean:
@echo "Cleaning Docker development environment (including volumes)..."
$(DOCKER_COMPOSE) down -v
# Docker commands
docker-build:
@echo "Building Docker image..."
docker build -t $(BINARY_NAME):latest .
# Start PostgreSQL for integration tests
test-integration-db:
@echo "Starting PostgreSQL for integration tests..."
$(DOCKER_COMPOSE) -f docker-compose/docker-compose.integration.yml up -d
@echo "Waiting for PostgreSQL to be ready..."
@for i in $$(seq 1 30); do \
if docker compose -f docker-compose/docker-compose.integration.yml exec -T postgres pg_isready -U dashbrr > /dev/null 2>&1; then \
echo "PostgreSQL is ready!"; \
exit 0; \
fi; \
echo "Waiting for PostgreSQL... ($$i/30)"; \
sleep 1; \
done; \
echo "PostgreSQL failed to start within 30 seconds"; \
exit 1
# Stop PostgreSQL for integration tests
test-integration-db-stop:
@echo "Stopping PostgreSQL for integration tests..."
$(DOCKER_COMPOSE) -f docker-compose/docker-compose.integration.yml down -v
# Run integration tests
test-integration: test-integration-db
@echo "Running integration tests..."
DASHBRR__DB_HOST=localhost \
DASHBRR__DB_PORT=5432 \
DASHBRR__DB_USER=dashbrr \
DASHBRR__DB_PASSWORD=dashbrr \
DASHBRR__DB_NAME=dashbrr_test \
$(GOCMD) test -v -tags=integration ./... || (make test-integration-db-stop && exit 1)
@echo "Stopping test database..."
@make test-integration-db-stop
# Run the application
run: all
@echo "Starting $(BINARY_NAME)..."
$(BIN_DIR)/$(BINARY_NAME)
# Help target
help:
@echo "Available targets:"
@echo " all - Build everything (clean, install dependencies, build frontend and backend)"
@echo " clean - Remove build artifacts and clean Go workspace"
@echo " deps-go - Install Go dependencies"
@echo " deps-frontend - Install frontend dependencies using pnpm"
@echo " frontend - Build the frontend application"
@echo " backend - Build the backend Go binary"
@echo " fmt - Format changed files only (fast, for iteration)"
@echo " lint - Run ESLint on frontend code"
@echo " lint-backend - Lint changed backend files only"
@echo " gofix-changed - Apply go fix to changed Go files only"
@echo " gofix-check-changed - Check go fix drift on changed Go files only"
@echo " precommit - Run local pre-commit gate (fmt + gofix + lint)"
@echo " type-check - Run TypeScript type checking"
@echo " preview - Start frontend preview server"
@echo " dev - Start development environment with SQLite and in-memory cache (requires air)"
@echo " docker-dev - Start Docker development environment with memory cache"
@echo " docker-dev-quick - Start Docker development environment without rebuilding"
@echo " docker-clean - Clean Docker environment including volumes"
@echo " docker-build - Build Docker image"
@echo " test-integration - Run integration tests with PostgreSQL"
@echo " test-integration-db - Start PostgreSQL for integration tests"
@echo " test-integration-db-stop - Stop PostgreSQL integration test database"
@echo " run - Build and run the application"