-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
298 lines (254 loc) · 13.4 KB
/
Copy pathMakefile
File metadata and controls
298 lines (254 loc) · 13.4 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# Makefile for ling-base — a multi-module Go library.
#
# Usage:
# make build Build all modules in the workspace.
# make build-pkg PKG=scheduler Build a single module (by directory name).
# make test Run tests for all modules.
# make test-pkg PKG=scheduler Test a single module.
# make test-cover Run tests with coverage for all modules.
# make vet Run go vet for all modules.
# make fmt Format all Go source files.
# make fmt-check Check formatting without modifying files.
# make lint Run golangci-lint (if installed).
# make check Run fmt-check + vet + build + test (CI equivalent).
# make cover-html Generate HTML coverage report (aggregate).
# make clean Remove generated coverage files and demo binaries.
# make tags List all git tags grouped by module.
# make release-patch Bump patch version for a module and create a tag.
# make release-minor Bump minor version for a module and create a tag.
# make release-major Bump major version for a module and create a tag.
# make release-all Auto-detect changed modules and bump patch versions.
# make push-tags Push all tags to the remote.
#
# Release examples:
# make release-patch PKG=scheduler # scheduler/v0.1.0 → scheduler/v0.1.1
# make release-minor PKG=common/jwtutil # common/jwtutil/v0.1.0 → common/jwtutil/v0.2.0
# make release-major PKG=common # common/v0.3.1 → common/v1.0.0
# make release-all # Auto-bump all modules with changes since last tag.
# make release-all LEVEL=minor # Auto-bump all changed modules by minor version.
#
# Root module release:
# make release-patch PKG=. # v0.2.1 → v0.2.2
SHELL := /usr/bin/env bash
.DEFAULT_GOAL := help
# ──────────────────────────────────────────────
# Variables
# ──────────────────────────────────────────────
GO := go
ROOT := $(shell pwd)
GOFLAGS := -trimpath
TAG_PREFIX := github.com/LingByte/ling-base
RELEASE_LEVEL ?= patch
# Find all go.mod directories (excluding vendor).
GO_MOD_DIRS := $(shell find . -name go.mod -not -path './vendor/*' -print0 | xargs -0 -n1 dirname | sed 's|^\./||' | sort)
# ──────────────────────────────────────────────
# Help
# ──────────────────────────────────────────────
.PHONY: help
help: ## Show this help message
@echo "ling-base Makefile — multi-module Go library"
@echo ""
@echo "Build:"
@echo " make build Build all modules"
@echo " make build-pkg PKG=dir Build a single module"
@echo ""
@echo "Test:"
@echo " make test Test all modules"
@echo " make test-pkg PKG=dir Test a single module"
@echo " make test-cover Test with coverage"
@echo " make vet Run go vet"
@echo " make check fmt-check + vet + build + test"
@echo ""
@echo "Code quality:"
@echo " make fmt Format all files"
@echo " make fmt-check Check formatting"
@echo " make lint Run golangci-lint (if installed)"
@echo " make vuln Run govulncheck (if installed)"
@echo " make check fmt-check + vet + build + test"
@echo " make check-all check + lint + vuln"
@echo ""
@echo "Release:"
@echo " make release-patch PKG=dir Bump patch version (v0.1.0 → v0.1.1)"
@echo " make release-minor PKG=dir Bump minor version (v0.1.0 → v0.2.0)"
@echo " make release-major PKG=dir Bump major version (v0.1.0 → v1.0.0)"
@echo " make release-all Auto-bump all changed modules (patch)"
@echo " make release-all LEVEL=minor Auto-bump all changed modules (minor)"
@echo " make tags List all tags"
@echo " make push-tags Push all tags to remote"
@echo ""
@echo "Misc:"
@echo " make clean Remove coverage files and demo binaries"
@echo " make modules List all Go modules in the workspace"
# ──────────────────────────────────────────────
# Build
# ──────────────────────────────────────────────
.PHONY: build build-pkg
build: ## Build all modules
@echo "==> Building all modules..."
@$(GO) build $(GOFLAGS) ./...
build-pkg: ## Build a single module (PKG=dir)
@test -n "$(PKG)" || (echo "Usage: make build-pkg PKG=<directory>" && exit 1)
@echo "==> Building $(PKG)..."
@cd $(PKG) && $(GO) build $(GOFLAGS) ./...
# ──────────────────────────────────────────────
# Test
# ──────────────────────────────────────────────
.PHONY: test test-pkg test-cover
test: ## Run tests for all modules
@echo "==> Testing all modules..."
@$(GO) test -count=1 ./...
test-pkg: ## Test a single module (PKG=dir)
@test -n "$(PKG)" || (echo "Usage: make test-pkg PKG=<directory>" && exit 1)
@echo "==> Testing $(PKG)..."
@cd $(PKG) && $(GO) test -count=1 -v ./...
test-cover: ## Run tests with coverage for all modules
@echo "==> Testing with coverage..."
@for dir in $(GO_MOD_DIRS); do \
echo " → $$dir"; \
(cd "$$dir" && $(GO) test -count=1 -cover ./... 2>&1) || true; \
done
# ──────────────────────────────────────────────
# Code quality
# ──────────────────────────────────────────────
.PHONY: vet fmt fmt-check lint check
vet: ## Run go vet for all modules
@echo "==> Running go vet..."
@$(GO) vet ./...
fmt: ## Format all Go source files
@echo "==> Formatting..."
@gofmt -s -w $(shell find . -name '*.go' -not -path './vendor/*')
fmt-check: ## Check formatting without modifying files
@echo "==> Checking formatting..."
@unformatted=$$(gofmt -s -l $(shell find . -name '*.go' -not -path './vendor/*')); \
if [ -n "$$unformatted" ]; then \
echo "The following files are not formatted:"; \
echo "$$unformatted"; \
exit 1; \
fi; \
echo "All files are properly formatted."
lint: ## Run golangci-lint (if installed)
@echo "==> Running golangci-lint..."
@command -v golangci-lint >/dev/null 2>&1 || { echo "golangci-lint is not installed"; exit 1; }
@golangci-lint run --timeout 10m ./...
vuln: ## Run govulncheck on all modules (if installed)
@echo "==> Running govulncheck..."
@command -v govulncheck >/dev/null 2>&1 || { \
echo "govulncheck is not installed. Install with:"; \
echo " go install golang.org/x/vuln/cmd/govulncheck@latest"; \
exit 1; \
}
@for dir in $(GO_MOD_DIRS); do \
echo " → $$dir"; \
(cd "$$dir" && govulncheck ./... 2>&1) || true; \
done
check: fmt-check vet build test ## Run fmt-check + vet + build + test (CI equivalent)
@echo "==> All checks passed!"
check-all: fmt-check vet build test lint vuln ## Run all checks including lint + vuln
@echo "==> All checks (including lint + vuln) passed!"
# ──────────────────────────────────────────────
# Coverage (HTML)
# ──────────────────────────────────────────────
.PHONY: cover-html
cover-html: ## Generate HTML coverage report for a single module (PKG=dir)
@test -n "$(PKG)" || (echo "Usage: make cover-html PKG=<directory>" && exit 1)
@echo "==> Generating HTML coverage for $(PKG)..."
@cd $(PKG) && $(GO) test -count=1 -coverprofile=coverage.out ./... && \
$(GO) tool cover -html=coverage.out -o coverage.html && \
echo "Coverage report: $(PKG)/coverage.html"
# ──────────────────────────────────────────────
# Module listing
# ──────────────────────────────────────────────
.PHONY: modules
modules: ## List all Go modules in the workspace
@echo "Go modules in workspace:"
@for dir in $(GO_MOD_DIRS); do \
mod=$$(head -1 "$$dir/go.mod" | awk '{print $$2}'); \
echo " $$dir ($$mod)"; \
done
# ──────────────────────────────────────────────
# Tag management
# ──────────────────────────────────────────────
.PHONY: tags tags-pkg
tags: ## List all git tags grouped by module
@echo "All git tags:"
@git tag --sort=v:refname
tags-pkg: ## List tags for a specific module (PKG=dir)
@test -n "$(PKG)" || (echo "Usage: make tags-pkg PKG=<directory>" && exit 1)
@if [ "$(PKG)" = "." ]; then \
prefix=""; \
else \
prefix="$(PKG)/"; \
fi; \
git tag --list "$${prefix}v*" --sort=v:refname
# ──────────────────────────────────────────────
# Release — version bump + tag
# ──────────────────────────────────────────────
.PHONY: release-patch release-minor release-major release-all
release-patch: ## Bump patch version for a module (PKG=dir)
@$(MAKE) _release PKG=$(PKG) LEVEL=patch
release-minor: ## Bump minor version for a module (PKG=dir)
@$(MAKE) _release PKG=$(PKG) LEVEL=minor
release-major: ## Bump major version for a module (PKG=dir)
@$(MAKE) _release PKG=$(PKG) LEVEL=major
release-all: ## Auto-bump all changed modules (LEVEL=patch|minor|major)
@echo "==> Detecting changed modules since their last tag..."
@bash $(ROOT)/scripts/release.sh --all --level $(RELEASE_LEVEL)
# Internal release target.
.PHONY: _release
_release:
@test -n "$(PKG)" || (echo "Usage: make release-{patch,minor,major} PKG=<directory>" && exit 1)
@test -d "$(PKG)" || (echo "Directory not found: $(PKG)" && exit 1)
@bash $(ROOT)/scripts/release.sh --pkg "$(PKG)" --level $(LEVEL)
# ──────────────────────────────────────────────
# Push tags
# ──────────────────────────────────────────────
.PHONY: push-tags push-tag
push-tags: ## Push all tags to remote
@echo "==> Pushing all tags to remote..."
@git push origin --tags
push-tag: ## Push a specific tag (TAG=name)
@test -n "$(TAG)" || (echo "Usage: make push-tag TAG=<tag-name>" && exit 1)
@echo "==> Pushing tag $(TAG)..."
@git push origin "$(TAG)"
# ──────────────────────────────────────────────
# Clean
# ──────────────────────────────────────────────
.PHONY: clean
clean: ## Remove generated coverage files and demo binaries
@echo "==> Cleaning..."
@find . -name 'coverage.out' -delete 2>/dev/null || true
@find . -name 'coverage.html' -delete 2>/dev/null || true
@find ./example/cmd -type f -perm +111 ! -name '*.go' -delete 2>/dev/null || true
@echo "Done."
# ── lingcli 构建 ──
# full 模式需要嵌入 ling-base 源码。
# 构建前运行此 target 将源码同步到 lingcli/embed_source/。
# CI/发布构建自动调用此 target。
EMBED_SOURCE_DIRS := middleware common/response common/response/gin \
common/jwtutil common/jwtutil/gin common/limiter common/limiter/count \
common/limiter/tokenbucket common/limiter/keycount common/circuitbreaker \
common/crypto common/logger common/logger/gin common/constants bootstrap common/eventbus version \
apidocs apidocs/humax apidocs/assets common/i18n common/i18n/gin common/metrics common/tracing \
common/validate
.PHONY: prepare-cli-embed clean-cli-embed build-cli
prepare-cli-embed: ## Sync ling-base source to lingcli/embed_source/ for full mode
@echo "==> Preparing lingcli embedded source..."
@rm -rf lingcli/embed_source/*
@touch lingcli/embed_source/.gitkeep
@for dir in $(EMBED_SOURCE_DIRS); do \
if [ -d "$$dir" ]; then \
mkdir -p "lingcli/embed_source/$$dir"; \
find "$$dir" -maxdepth 1 -type f \( -name '*.go' ! -name '*_test.go' -o -name '*.css' -o -name '*.svg' -o -name '*.png' -o -name '*.font' \) -exec cp {} "lingcli/embed_source/$$dir/" \; 2>/dev/null; \
fi; \
done
@echo " Embedded source: $$(find lingcli/embed_source -type f ! -name '.gitkeep' | wc -l | tr -d ' ') files"
@echo "Done."
clean-cli-embed: ## Clean lingcli embedded source
@rm -rf lingcli/embed_source/*
@touch lingcli/embed_source/.gitkeep
@echo "==> Cleaned lingcli embedded source."
build-cli: prepare-cli-embed ## Build lingcli binary with embedded source
@echo "==> Building lingcli..."
@cd lingcli && go build -o ../bin/lingcli .
@echo " Binary: bin/lingcli"
@echo "Done."