-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathMakefile
294 lines (258 loc) · 12.3 KB
/
Makefile
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
include go.mk
# Override the variables in the go.mk
APPROOT=karpor
GOSOURCE_PATHS = ./pkg/... ./cmd/...
LICENSE_CHECKER ?= license-eye
LICENSE_CHECKER_VERSION ?= main
# Front-End tools
UIFORMATER ?= prettier
# Default architecture for building binaries.
# Override this variable by setting GOARCH=<your-architecture> before invoking the make command.
# To find this list of possible platforms, run the following:
# go tool dist list
GOARCH ?= amd64
# Default setting for CGO_ENABLED to disable the use of cgo.
# Can be overridden by setting CGO_ENABLED=1 before invoking the make command.
CGO_ENABLED ?= 0
# Check if the SKIP_UI_BUILD flag is set to control the UI building process.
# If the flag is not set, the BUILD_UI variable is assigned the value 'build-ui'.
# If the flag is set, the BUILD_UI variable remains empty.
ifeq ($(SKIP_UI_BUILD),true)
BUILD_UI =
else
BUILD_UI = build-ui
endif
# If you encounter an error like "panic: permission denied" on MacOS,
# please visit https://github.com/eisenxp/macos-golink-wrapper to find the solution.
.PHONY: gen-version
gen-version: ## Generate version file
@echo "🛠️ Updating the version file ..."
@cd pkg/version/scripts && $(GO) run gen/gen.go
.PHONY: test
test: ## Run the tests
@PKG_LIST=$${TARGET_PKG:-$(GOSOURCE_PATHS)}; \
$(GO) test -gcflags=all=-l -timeout=10m `$(GO) list -e $${PKG_LIST} | grep -vE "cmd|internal|internalimport|generated|handler|middleware|registry|openapi|apis|version|gitutil|server|elasticsearch"` ${TEST_FLAGS}
# cover: Generates a coverage report for the specified TARGET_PKG or default GOSOURCE_PATHS.
# Usage:
# make cover TARGET_PKG=<go-package-path>
# Example:
# make cover # use the default GOSOURCE_PATHS
# make cover TARGET_PKG='./pkg/util/...' # specify a custom package path
.PHONY: cover
cover: ## Generates coverage report
@PKG_LIST=$${TARGET_PKG:-$(GOSOURCE_PATHS)}; \
echo "🚀 Executing unit tests for $${PKG_LIST}:"; \
$(GO) test -gcflags=all=-l -timeout=10m `$(GO) list $${PKG_LIST} | grep -vE "cmd|internal|internalimport|generated|handler|middleware|registry|openapi|apis|version|gitutil|server|elasticsearch"` -coverprofile $(COVERAGEOUT) ${TEST_FLAGS} && \
(echo "\n📊 Calculating coverage rate:"; $(GO) tool cover -func=$(COVERAGEOUT)) || (echo "\n💥 Running go test failed!"; exit 1)
.PHONY: format
format: ## Format source code of frontend and backend
@which $(GOFORMATER) > /dev/null || (echo "Installing $(GOFORMATER)@$(GOFORMATER_VERSION) ..."; $(GO) install mvdan.cc/gofumpt@$(GOFORMATER_VERSION) && echo -e "Installation complete!\n")
@for path in $(GOSOURCE_PATHS); do $(GOFORMATER) -l -w -e `echo $${path} | cut -b 3- | rev | cut -b 5- | rev`; done;
@which $(UIFORMATER) > /dev/null || (echo "Installing $(UIFORMATER) ..."; npm install -g prettier && echo -e "Installation complete!\n")
@cd ui && npx prettier --write .
# Target: update-codegen
# Description: Updates the generated code using the 'hack/update-codegen.sh' script.
# Usage: make update-codegen
.PHONY: update-codegen
update-codegen: ## Update generated code
hack/update-codegen.sh
# VERSION file handling targets
# These targets are used to manage the VERSION file during build process.
# save-version: Creates a backup of the current VERSION file
# restore-version: Restores the VERSION file from backup and removes the backup file
VERSION_FILE := pkg/version/VERSION
VERSION_BACKUP := $(VERSION_FILE).bak
.PHONY: save-version
save-version:
@if [ -f $(VERSION_FILE) ]; then \
echo "📦 Backing up VERSION file..."; \
cp $(VERSION_FILE) $(VERSION_BACKUP); \
fi
.PHONY: restore-version
restore-version:
@if [ -f $(VERSION_BACKUP) ]; then \
echo "📦 Restoring VERSION file..."; \
cp $(VERSION_BACKUP) $(VERSION_FILE); \
rm $(VERSION_BACKUP); \
fi
# Build-related targets
# Internal build targets without version handling
# These targets perform the actual build operation for each platform.
# They are prefixed with '_' to indicate they are internal and should not be called directly.
# Each target is responsible for:
# 1. Cleaning the platform-specific build directory
# 2. Building the binary with correct GOOS and GOARCH
.PHONY: _build-darwin
_build-darwin:
@rm -rf ./_build/darwin
@echo "🚀 Building karpor-server for darwin platform ..."
GOOS=darwin GOARCH=$(GOARCH) CGO_ENABLED=$(CGO_ENABLED) \
$(GO) build -o ./_build/darwin/$(APPROOT) \
./cmd/karpor || exit 1
.PHONY: _build-linux
_build-linux:
@rm -rf ./_build/linux
@echo "🚀 Building karpor-server for linux platform ..."
GOOS=linux GOARCH=$(GOARCH) CGO_ENABLED=$(CGO_ENABLED) \
$(GO) build -o ./_build/linux/$(APPROOT) \
./cmd/karpor || exit 1
.PHONY: _build-windows
_build-windows:
@rm -rf ./_build/windows
@echo "🚀 Building karpor-server for windows platform ..."
GOOS=windows GOARCH=$(GOARCH) CGO_ENABLED=$(CGO_ENABLED) \
$(GO) build -o ./_build/windows/$(APPROOT).exe \
./cmd/karpor || exit 1
# Public build targets with version handling
# These targets provide the complete build workflow for each platform:
# 1. Backup the current VERSION file (save-version)
# 2. Generate a new version (gen-version)
# 3. Build the binary (_build-xxx)
# 4. Restore the original VERSION file (restore-version)
# If build fails, the VERSION file will still be restored
# Target: build-darwin
# Description: Builds for macOS platform.
# Usage:
# make build-darwin GOARCH=<your-architecture> SKIP_UI_BUILD=<true,false>
# Example:
# make build-darwin
# make build-darwin GOARCH=arm64
# make build-darwin GOARCH=arm64 SKIP_UI_BUILD=true
.PHONY: build-darwin
build-darwin: save-version gen-version $(BUILD_UI) ## Build for MacOS (Darwin)
@$(MAKE) _build-darwin || ($(MAKE) restore-version && exit 1)
@$(MAKE) restore-version
# Target: build-linux
# Description: Builds for Linux platform.
# Usage:
# make build-linux GOARCH=<your-architecture> SKIP_UI_BUILD=<true,false>
# Example:
# make build-linux
# make build-linux GOARCH=arm64
# make build-linux GOARCH=arm64 SKIP_UI_BUILD=true
.PHONY: build-linux
build-linux: save-version gen-version $(BUILD_UI) ## Build for Linux
@$(MAKE) _build-linux || ($(MAKE) restore-version && exit 1)
@$(MAKE) restore-version
# Target: build-windows
# Description: Builds for Windows platform.
# Usage:
# make build-windows GOARCH=<your-architecture> SKIP_UI_BUILD=<true,false>
# Example:
# make build-windows
# make build-windows GOARCH=arm64
# make build-windows GOARCH=arm64 SKIP_UI_BUILD=true
.PHONY: build-windows
build-windows: save-version gen-version $(BUILD_UI) ## Build for Windows
@$(MAKE) _build-windows || ($(MAKE) restore-version && exit 1)
@$(MAKE) restore-version
# Target: build-ui
# Description: Builds the UI for the dashboard.
# Usage: make build-ui
.PHONY: build-ui
build-ui: gen-version ## Build UI for the dashboard
@echo "🧀 Building UI for the dashboard ..."
cd ui && npm install && npm run build && touch build/.gitkeep
# Target: build-all
# Description: Builds for all supported platforms (Darwin, Linux, Windows).
# Note: Uses recursive make calls to ensure each platform build has its own
# version handling context, preventing interference between builds.
# Usage: make build-all
.PHONY: build-all
build-all: ## Build for all platforms
@echo "🚀 Building for all platforms..."
@$(MAKE) build-darwin
@$(MAKE) build-linux
@$(MAKE) build-windows
# Target: build
# Description: Automatically builds for the current platform.
# Detects the current OS and calls the appropriate platform-specific build target.
# Usage: make build
.PHONY: build
build: ## Build for current platform
@echo "🔍 Detecting current platform..."
@case "$$(uname -s)" in \
Darwin*) \
echo "🚀 Detected macOS platform, building for darwin..." && \
$(MAKE) build-darwin ;; \
Linux*) \
echo "🚀 Detected Linux platform, building for linux..." && \
$(MAKE) build-linux ;; \
MINGW*|MSYS*|CYGWIN*) \
echo "🚀 Detected Windows platform, building for windows..." && \
$(MAKE) build-windows ;; \
*) \
echo "❌ Unsupported platform: $$(uname -s)" && exit 1 ;; \
esac
.PHONY: check-license
check-license: ## Checks if repo files contain valid license header
@which $(LICENSE_CHECKER) > /dev/null || (echo "Installing $(LICENSE_CHECKER)@$(LICENSE_CHECKER_VERSION) ..."; $(GO) install github.com/apache/skywalking-eyes/cmd/$(LICENSE_CHECKER)@$(LICENSE_CHECKER_VERSION) && echo -e "Installation complete!\n")
@${GOPATH}/bin/$(LICENSE_CHECKER) header check
.PHONY: fix-license
fix-license: ## Adds missing license header to repo files
@which $(LICENSE_CHECKER) > /dev/null || (echo "Installing $(LICENSE_CHECKER)@$(LICENSE_CHECKER_VERSION) ..."; $(GO) install github.com/apache/skywalking-eyes/cmd/$(LICENSE_CHECKER)@$(LICENSE_CHECKER_VERSION) && echo -e "Installation complete!\n")
@${GOPATH}/bin/$(LICENSE_CHECKER) header fix
.PHONY: gen-api-spec
gen-api-spec: ## Generate API Specification with OpenAPI format
@which $(GOPATH)/bin/swag > /dev/null || (echo "Installing [email protected] ..."; $(GO) install github.com/swaggo/swag/cmd/[email protected] && echo "Installation complete!\n")
# Generate API documentation with OpenAPI format
@$(GOPATH)/bin/swag init --parseDependency --parseInternal --parseDepth 1 -g cmd/karpor/main.go -o api/openapispec/ && echo "🎉 Done!" || (echo "💥 Fail!"; exit 1)
# Format swagger comments
@$(GOPATH)/bin/swag fmt -g pkg/**/*.go && echo "🎉 Done!" || (echo "💥 Failed!"; exit 1)
.PHONY: gen-api-doc
gen-api-doc: gen-api-spec ## Generate API Documentation by API Specification
@which $(GOPATH)/bin/swagger > /dev/null || (echo "Installing [email protected] ..."; $(GO) install github.com/go-swagger/go-swagger/cmd/[email protected] && echo "Installation complete!\n")
@$(GOPATH)/bin/swagger generate markdown -f ./api/openapispec/swagger.json --output=docs/api.md && echo "🎉 Done!" || (echo "💥 Fail!"; exit 1)
.PHONY: gen-cli-doc
gen-cli-doc: ## Generate CLI Documentation
@$(GO) run ./hack/gen-cli-docs/main.go && echo "🎉 Done!"
# Target: add-contributor
# Description: Adds a new contributor to the project's list of contributors using the all-contributors-cli tool.
# Usage:
# make add-contributor user=<github-username> role=<contributor-roles>
# Example:
# make add-contributor user=mike role=code
# make add-contributor user=john role=code,doc
# Where:
# <github-username> is the GitHub username of the contributor.
# <contributor-roles> is a comma-separated list of roles the contributor has (e.g., code, doc, design, ideas),
# with all values listed in the https://allcontributors.org/docs/en/emoji-key.
.PHONY: add-contributor
add-contributor: ## Add a new contributor
@if [ -z "$(user)" ] || [ -z "$(role)" ]; then \
echo "Error: 'user' and 'role' must be specified."; \
echo "Usage: make add-contributor user=<github-username> role=<contributor-roles>"; \
exit 1; \
fi
@which all-contributors > /dev/null || (echo "Installing all-contributors-cli ..."; npm i -g all-contributors-cli && echo -e "Installation complete!\n")
@all-contributors add $(user) $(role) && echo "🎉 Done!" || (echo "💥 Fail!"; exit 1)
# Target: update-contributors
# Description: Generate the latest list of contributors and update it in README.
# Usage:
# make update-contributors
.PHONY: update-contributors
update-contributors: ## Update the list of contributors
@which all-contributors > /dev/null || (echo "Installing all-contributors-cli ..."; npm i -g all-contributors-cli && echo -e "Installation complete!\n")
-all-contributors generate && echo "🎉 Done!" || (echo "💥 Fail!"; exit 1)
# Target: check
# Description: Run all checks to ensure code quality.
# The checks are run in the following order:
# 1. 🔨 lint: Check code style and potential issues using golangci-lint
# 2. 🧪 cover: Run tests and generate coverage report
# 3. 📦 build: Build the binary for the current platform
# If any check fails, the subsequent checks will not run.
# Usage:
# make check
.PHONY: check
check: ## Check the lint, test, and build
@echo "🔍 Running all checks..."
@echo "🔨 1/3 Running lint check..."
@$(MAKE) lint || (echo "❌ Lint check failed!" && exit 1)
@echo "✅ Lint check passed!"
@echo "🧪 2/3 Running test coverage..."
@$(MAKE) cover || (echo "❌ Test coverage check failed!" && exit 1)
@echo "✅ Test coverage check passed!"
@echo "📦 3/3 Running build check..."
@$(MAKE) build || (echo "❌ Build check failed!" && exit 1)
@echo "✅ Build check passed!"
@echo "🎉 All checks passed successfully!"