-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
250 lines (208 loc) · 9.29 KB
/
Copy pathMakefile
File metadata and controls
250 lines (208 loc) · 9.29 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
.PHONY: build build-frontend build-backend docker docker-push helm-package helm-lint \
test test-frontend lint clean dev dev-backend dev-frontend migrate \
install-frontend setup-sqlite-web sqlite-web sqlite-web-bg \
check-frontend-build ensure-frontend start prod wait-db setup check-env verify-scenarios \
test-e2e-cli
VERSION ?= $(shell git describe --tags --always --dirty 2>/dev/null || echo "dev")
DOCKER_REPO ?= ccr.ccs.tencentyun.com/roczzhang/schedsim
LDFLAGS := -X main.version=$(VERSION)
SQLITE_WEB_PORT ?= 8081
SQLITE_DB_PATH ?= ./data/schedsim.db
# ── Default ────────────────────────────────────────────────────────────────────
all: start
# ── Environment setup & check ─────────────────────────────────────────────────
# Install every required development dependency (idempotent).
setup:
@bash scripts/setup.sh
# Verify all required tools are present and at a sufficient version.
# If any REQUIRED check fails, run `make setup` automatically then re-check.
check-env:
@if ! bash scripts/check-env.sh; then \
echo ""; \
echo "Running 'make setup' to install missing dependencies..."; \
$(MAKE) setup; \
echo ""; \
echo "Re-running environment check..."; \
bash scripts/check-env.sh; \
fi
# ── Build ─────────────────────────────────────────────────────────────────────
# Full build: verify environment, then build frontend + backend.
build: check-env build-frontend build-backend
# Backend build.
# CGO_ENABLED=1 is required: mattn/go-sqlite3 is a CGO library.
# A C compiler (gcc/clang) must be present — run 'make setup' if unsure.
build-backend:
CGO_ENABLED=1 go build -ldflags "$(LDFLAGS)" -o bin/schedsim ./cmd/schedsim
# Frontend build.
build-frontend:
@if [ -d "web" ]; then \
cd web && npm ci && npm run build; \
fi
# ── Development ───────────────────────────────────────────────────────────────
# One-shot dev environment: environment check, build if needed, then run everything.
# Backend is backgrounded so the frontend Vite dev server can start in foreground.
# Ctrl-C kills the frontend; the trap ensures the backend is also stopped.
start: check-env ensure-frontend sqlite-web-bg
@trap 'kill %1 2>/dev/null; exit 0' INT TERM; \
$(MAKE) dev-backend & \
sleep 2; \
$(MAKE) dev-frontend
# Same as start — kept for familiarity.
dev: start
# Build frontend if the dist bundle is absent (e.g. after fresh clone or clean).
ensure-frontend:
@if [ ! -f "web/dist/index.html" ]; then \
echo "Frontend not built — building now..."; \
$(MAKE) build-frontend; \
fi
# Check if frontend is built and abort with a helpful message if not.
check-frontend-build:
@if [ ! -f "web/dist/index.html" ]; then \
echo ""; \
echo " Frontend not built. Run 'make build-frontend' or 'make build', then retry."; \
echo ""; \
exit 1; \
fi
# Backend hot-reload (requires air; falls back to go run).
# Runs in foreground when called directly; backgrounded by 'dev' target.
dev-backend:
@if command -v air > /dev/null 2>&1; then \
air; \
else \
echo "air not found — falling back to go run (no hot-reload)."; \
echo "Install with: make setup"; \
go run ./cmd/schedsim; \
fi
# Frontend Vite dev server.
dev-frontend:
@if [ -d "web" ]; then \
cd web && npm run dev; \
fi
# ── SQLite Web (optional DB browser) ─────────────────────────────────────────
# Install sqlite_web at the version pinned in .tool-versions.
setup-sqlite-web:
@pip3 install "sqlite-web==$(shell grep '^sqlite-web ' .tool-versions | awk '{print $$2}')"
@echo "sqlite_web installed."
# Launch sqlite_web in the foreground (database must already exist).
sqlite-web:
@if [ ! -f $(SQLITE_DB_PATH) ]; then \
echo "Error: database not found at $(SQLITE_DB_PATH). Run 'make dev' first."; \
exit 1; \
fi
@echo "Starting sqlite_web on http://localhost:$(SQLITE_WEB_PORT)"
sqlite_web $(SQLITE_DB_PATH) --host 0.0.0.0 --port $(SQLITE_WEB_PORT) --no-browser
# Launch sqlite_web in the background; silently skips if not installed.
sqlite-web-bg:
@if ! command -v sqlite_web > /dev/null 2>&1; then \
echo "sqlite_web not installed (optional) — skipping DB browser."; \
echo "Run 'make setup' or 'make setup-sqlite-web' to install."; \
elif lsof -ti:$(SQLITE_WEB_PORT) > /dev/null 2>&1; then \
echo "sqlite_web already running on http://localhost:$(SQLITE_WEB_PORT)"; \
else \
( i=0; \
while [ ! -f $(SQLITE_DB_PATH) ] && [ $$i -lt 30 ]; do \
sleep 1; i=$$((i+1)); \
done; \
if [ -f $(SQLITE_DB_PATH) ]; then \
sqlite_web $(SQLITE_DB_PATH) --host 0.0.0.0 --port $(SQLITE_WEB_PORT) --no-browser & \
echo "sqlite_web started in background on http://localhost:$(SQLITE_WEB_PORT)"; \
else \
echo "Database not ready after 30s — skipping sqlite_web"; \
fi ) & \
fi
# ── Production ────────────────────────────────────────────────────────────────
# Wait for database file, then start sqlite_web in background (reused by prod).
wait-db: sqlite-web-bg
# Build then run in production mode.
prod: build wait-db
./bin/schedsim
# ── Docker & Helm ─────────────────────────────────────────────────────────────
docker:
DOCKER_BUILDKIT=1 \
docker build \
-t $(DOCKER_REPO):$(VERSION) \
-f deploy/docker/Dockerfile \
.
docker-push: docker
docker push $(DOCKER_REPO):$(VERSION)
helm-lint:
helm lint deploy/helm/schedsim
helm-package:
@mkdir -p dist
helm package deploy/helm/schedsim -d dist/
# ── Test & Lint ───────────────────────────────────────────────────────────────
test:
go test ./internal/... ./pkg/... -v -cover
test-frontend:
@if [ -d "web" ]; then \
cd web && npm test -- --watchAll=false; \
fi
# Verify built-in scenario templates against a running backend.
# Prerequisites: Backend server running on localhost:8080, Kind cluster ready.
verify-scenarios:
./scripts/verify-scenarios.sh
# Run CLI-related E2E tests against a running backend (default: http://localhost:8080).
# Usage:
# make test-e2e-cli # uses default server
# make test-e2e-cli SCHEDSIM_BASE_URL=http://localhost:5173
test-e2e-cli: build-backend
SCHEDSIM_BASE_URL=$${SCHEDSIM_BASE_URL:-http://localhost:8080} \
go test ./test/e2e/ -run "TestCLI" -v -timeout 300s
lint:
golangci-lint run ./...
@if [ -d "web" ]; then \
cd web && npm run lint; \
fi
# ── Misc ──────────────────────────────────────────────────────────────────────
migrate:
go run ./cmd/schedsim migrate
install-frontend:
@if [ -d "web" ]; then \
cd web && npm install; \
fi
clean:
rm -rf bin/
rm -rf web/dist/
rm -rf data/
# ── Help ──────────────────────────────────────────────────────────────────────
help:
@echo ""
@echo "Usage: make <target>"
@echo ""
@echo "Setup"
@echo " setup Install all dev dependencies (idempotent)"
@echo " check-env Verify dev environment; auto-fix if possible"
@echo ""
@echo "Build"
@echo " build Full build: check-env + frontend + backend"
@echo " build-frontend Build React frontend only"
@echo " build-backend Build Go backend only (CGO_ENABLED=1)"
@echo ""
@echo "Development"
@echo " start / dev check-env, build if needed, run all services"
@echo " dev-backend Run backend with hot-reload (air) or go run"
@echo " dev-frontend Run Vite dev server"
@echo ""
@echo "Database"
@echo " sqlite-web Launch sqlite_web in foreground"
@echo " sqlite-web-bg Launch sqlite_web in background (used by dev/start)"
@echo " setup-sqlite-web Install sqlite_web at pinned version"
@echo ""
@echo "Production"
@echo " prod build + run binary"
@echo ""
@echo "Docker & Helm"
@echo " docker Build Docker image"
@echo " docker-push Push Docker image"
@echo " helm-lint Validate Helm chart"
@echo " helm-package Package Helm chart"
@echo ""
@echo "Other"
@echo " test Run Go tests"
@echo " test-frontend Run frontend tests"
@echo " test-e2e-cli Run CLI E2E tests (requires running backend)"
@echo " verify-scenarios Verify built-in scenario templates"
@echo " lint Run golangci-lint + eslint"
@echo " migrate Run DB migration"
@echo " clean Remove bin/, web/dist/, data/"
@echo ""