-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMakefile
More file actions
155 lines (141 loc) · 5.86 KB
/
Copy pathMakefile
File metadata and controls
155 lines (141 loc) · 5.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
# Load .env file if it exists
ifneq (,$(wildcard ./.env))
include .env
export
endif
# Build variables
VERSION ?= $(shell git describe --tags --always --dirty 2>/dev/null || echo "dev")
GIT_COMMIT ?= $(shell git rev-parse HEAD 2>/dev/null || echo "unknown")
BUILD_DATE ?= $(shell date -u +"%Y-%m-%dT%H:%M:%SZ")
LDFLAGS := -w -s \
-X github.com/ethpandaops/lab-backend/internal/version.Version=$(VERSION) \
-X github.com/ethpandaops/lab-backend/internal/version.GitCommit=$(GIT_COMMIT) \
-X github.com/ethpandaops/lab-backend/internal/version.BuildDate=$(BUILD_DATE)
# Colors for output (use printf for cross-platform compatibility)
CYAN := \033[0;36m
GREEN := \033[0;32m
YELLOW := \033[0;33m
RED := \033[0;31m
RESET := \033[0m
.PHONY: all build setup-frontend clean run redis stop-redis test generate help
all: build
# Frontend configuration
FRONTEND_SOURCE ?=
FRONTEND_TAG ?=
FRONTEND_BRANCH ?=
FRONTEND_TARGET ?= web/frontend
FRONTEND_VERSION_FILE ?= .tmp/frontend-version.txt
GITHUB_REPO ?= ethpandaops/lab
## build: Setup frontend and build the lab-backend binary
build: setup-frontend
@printf "$(CYAN)==> Building lab-backend...$(RESET)\n"
@mkdir -p bin
@go build -ldflags "$(LDFLAGS)" -o bin/lab-backend ./cmd/server
@printf "$(GREEN)✓ Build complete!$(RESET)\n"
@printf "$(GREEN) Backend binary: bin/lab-backend$(RESET)\n"
## setup-frontend: Setup frontend from source/release
.PHONY: setup-frontend
setup-frontend:
@printf "$(CYAN)==> Setting up frontend...$(RESET)\n"
@mkdir -p .tmp
@# Check if FRONTEND_SOURCE is set (local copy mode)
@if [ -n "$(FRONTEND_SOURCE)" ]; then \
printf "$(YELLOW)Using local frontend source: $(FRONTEND_SOURCE)$(RESET)\n"; \
if [ ! -d "$(FRONTEND_SOURCE)/dist" ]; then \
printf "$(RED)Error: $(FRONTEND_SOURCE)/dist does not exist$(RESET)\n"; \
exit 1; \
fi; \
rm -rf $(FRONTEND_TARGET); \
mkdir -p $(FRONTEND_TARGET); \
cp -r $(FRONTEND_SOURCE)/dist/* $(FRONTEND_TARGET)/; \
echo "dev" > $(FRONTEND_VERSION_FILE); \
printf "$(GREEN)✓ Copied $(FRONTEND_SOURCE)/dist -> $(FRONTEND_TARGET)$(RESET)\n"; \
else \
if [ -n "$(FRONTEND_TAG)" ]; then \
printf "$(YELLOW)Frontend tag: $(FRONTEND_TAG)$(RESET)\n"; \
RELEASE_TAG="$(FRONTEND_TAG)"; \
elif [ -n "$(FRONTEND_BRANCH)" ]; then \
printf "$(YELLOW)Frontend branch: $(FRONTEND_BRANCH)$(RESET)\n"; \
RELEASE_TAG=$$(curl -s "https://api.github.com/repos/$(GITHUB_REPO)/releases" | \
grep -o '"tag_name": *"[^"]*"' | \
grep '"$(FRONTEND_BRANCH)-v' | \
sed 's/"tag_name": *"\([^"]*\)"/\1/' | \
sort -V -r | \
head -1); \
else \
printf "$(YELLOW)Using latest stable release$(RESET)\n"; \
RELEASE_TAG=$$(curl -s "https://api.github.com/repos/$(GITHUB_REPO)/releases/latest" | \
grep '"tag_name":' | \
sed -E 's/.*"tag_name": *"([^"]+)".*/\1/'); \
fi; \
if [ -z "$$RELEASE_TAG" ]; then \
printf "$(RED)Error: No release found$(RESET)\n"; \
exit 1; \
fi; \
printf "$(CYAN)Found release: $$RELEASE_TAG$(RESET)\n"; \
CURRENT_VERSION=$$(cat $(FRONTEND_VERSION_FILE) 2>/dev/null || echo ""); \
if [ "$$RELEASE_TAG" = "$$CURRENT_VERSION" ] && [ -d "$(FRONTEND_TARGET)" ] && [ -f "$(FRONTEND_TARGET)/index.html" ]; then \
printf "$(GREEN)✓ Frontend already up to date ($$RELEASE_TAG)$(RESET)\n"; \
else \
printf "$(CYAN)==> Downloading frontend $$RELEASE_TAG...$(RESET)\n"; \
rm -rf .tmp/frontend-download .tmp/frontend-extract; \
mkdir -p .tmp/frontend-download .tmp/frontend-extract; \
ASSET_NAME="lab-$$RELEASE_TAG.tar.gz"; \
ASSET_URL=$$(curl -s "https://api.github.com/repos/$(GITHUB_REPO)/releases/tags/$$RELEASE_TAG" | \
grep "browser_download_url.*$$ASSET_NAME" | \
cut -d '"' -f 4); \
if [ -z "$$ASSET_URL" ]; then \
printf "$(RED)Error: Release asset $$ASSET_NAME not found$(RESET)\n"; \
exit 1; \
fi; \
curl -L "$$ASSET_URL" -o .tmp/frontend-download/release.tar.gz; \
printf "$(CYAN)==> Extracting frontend...$(RESET)\n"; \
tar -xzf .tmp/frontend-download/release.tar.gz -C .tmp/frontend-extract; \
if [ ! -f ".tmp/frontend-extract/index.html" ]; then \
printf "$(RED)Error: index.html not found in release asset$(RESET)\n"; \
exit 1; \
fi; \
rm -rf $(FRONTEND_TARGET); \
mkdir -p $(FRONTEND_TARGET); \
cp -r .tmp/frontend-extract/* $(FRONTEND_TARGET)/; \
echo "$$RELEASE_TAG" > $(FRONTEND_VERSION_FILE); \
printf "$(GREEN)✓ Frontend updated to $$RELEASE_TAG$(RESET)\n"; \
rm -rf .tmp/frontend-download .tmp/frontend-extract; \
fi; \
fi
## redis: Start Redis container for local development
redis:
@docker rm -f lab-redis 2>/dev/null || true
@docker run --name lab-redis -p 6379:6379 -d redis:7-alpine
@printf "$(GREEN)✓ Redis started on localhost:6379$(RESET)\n"
## stop-redis: Stop and remove Redis container
stop-redis:
@docker rm -f lab-redis 2>/dev/null || true
@printf "$(GREEN)✓ Redis stopped$(RESET)\n"
## clean: Clean build artifacts and frontend
clean: stop-redis
@printf "$(CYAN)==> Cleaning artifacts...$(RESET)\n"
@rm -rf bin/ dist/ .tmp/ $(FRONTEND_TARGET)
@go clean
@printf "$(GREEN)✓ Clean complete$(RESET)\n"
## test: Run all tests
test:
@printf "$(CYAN)==> Running unit tests...$(RESET)\n"
@go install gotest.tools/gotestsum@latest
@gotestsum --raw-command go test -v -race -failfast -coverprofile=coverage.out -covermode=atomic -json $$(go list ./...) && \
printf "$(GREEN)✓ Unit tests passed$(RESET)\n"
## generate: Generates mocks
generate:
@printf "$(CYAN)==> Generating mocks...$(RESET)\n"
@go generate ./... && \
printf "$(GREEN)✓ Mocks generated successfully$(RESET)\n"
## run: Build and run the server locally
run: redis build
@printf "$(CYAN)==> Starting server...$(RESET)\n"
@./bin/lab-backend -config config.yaml
## help: Display this help message
help:
@printf "$(CYAN)lab-backend Makefile$(RESET)\n"
@printf "\n"
@sed -n 's/^##//p' $(MAKEFILE_LIST) | column -t -s ':' | sed -e 's/^/ /'
@printf "\n"