11# Makefile for github.com/abitofhelp/servicelib
2+ #
3+ # This Makefile provides targets for building, testing, and maintaining the ServiceLib library.
4+ # Run 'make help' to see all available targets.
5+
6+ # ==================================================================================
7+ # Variables
8+ # ==================================================================================
9+
10+ # Project metadata
11+ PROJECT_NAME =servicelib
12+ VERSION =$(shell git describe --tags --always --dirty 2>/dev/null || echo "dev")
13+ GO_VERSION =1.24
214
315# Go parameters
416GOCMD =go
@@ -10,6 +22,7 @@ GOMOD=$(GOCMD) mod
1022GOVET =$(GOCMD ) vet
1123GOFMT =$(GOCMD ) fmt
1224GOGENERATE =$(GOCMD ) generate
25+ GOINSTALL =$(GOCMD ) install
1326
1427# Platform-specific parameters
1528# macOS (Darwin)
@@ -32,12 +45,51 @@ PACKAGE_COVERAGE_FLAGS=-race -coverprofile=
3245# Linter parameters
3346GOLANGCI_LINT =golangci-lint
3447
35- .PHONY : all build build-darwin-amd64 build-darwin-arm64 build-windows-amd64 build-linux-amd64 build-linux-arm64 build-all clean test test-integration test-all test-package coverage coverage-integration coverage-package lint fmt vet tidy vendor generate generate-package security bench bench-package help
48+ # Documentation parameters
49+ GODOC =godoc
50+ GODOC_PORT =6060
51+
52+ # Tools
53+ TOOLS_DIR =$(shell pwd) /tools
54+ TOOLS_BIN_DIR =$(TOOLS_DIR ) /bin
55+ GOIMPORTS =$(TOOLS_BIN_DIR ) /goimports
56+ GOFUMPT =$(TOOLS_BIN_DIR ) /gofumpt
3657
37- all : clean lint test build
58+ .PHONY : all build build-darwin-amd64 build-darwin-arm64 build-windows-amd64 build-linux-amd64 build-linux-arm64 build-all clean test test-integration test-all test-package coverage coverage-integration coverage-package lint fmt vet tidy vendor generate generate-package security bench bench-package help check-go-version tools docs serve-docs ci pre-commit update-deps release
59+
60+ # ==================================================================================
61+ # Main targets
62+ # ==================================================================================
63+
64+ all : check-go-version clean lint test build
65+
66+ # Check Go version
67+ check-go-version :
68+ @echo " Checking Go version..."
69+ @if [ " $( shell go version | awk ' {print $$3}' | sed ' s/go//' ) " != " $( GO_VERSION) " ]; then \
70+ echo " ERROR: Required Go version is $( GO_VERSION) , but you have $( shell go version | awk ' {print $$3}' | sed ' s/go//' ) " ; \
71+ echo " Please update your Go version." ; \
72+ exit 1; \
73+ fi
74+ @echo " Go version $( GO_VERSION) confirmed."
75+
76+ # Install required tools
77+ tools :
78+ @echo " Installing required tools..."
79+ @mkdir -p $(TOOLS_BIN_DIR )
80+ @GOBIN=$(TOOLS_BIN_DIR ) $(GOINSTALL ) golang.org/x/tools/cmd/goimports@latest
81+ @GOBIN=$(TOOLS_BIN_DIR ) $(GOINSTALL ) mvdan.cc/gofumpt@latest
82+ @GOBIN=$(TOOLS_BIN_DIR ) $(GOINSTALL ) github.com/golangci/golangci-lint/cmd/golangci-lint@latest
83+ @GOBIN=$(TOOLS_BIN_DIR ) $(GOINSTALL ) golang.org/x/vuln/cmd/govulncheck@latest
84+ @GOBIN=$(TOOLS_BIN_DIR ) $(GOINSTALL ) github.com/wadey/gocovmerge@latest
85+ @echo " Tools installed successfully."
86+
87+ # ==================================================================================
88+ # Build targets
89+ # ==================================================================================
3890
3991# Build the project (compile without producing a binary since this is a library)
40- build :
92+ build : check-go-version
4193 @echo " Building for current platform..."
4294 $(GOBUILD ) -v ./...
4395
@@ -70,12 +122,19 @@ build-linux-arm64:
70122build-all : build-darwin-amd64 build-darwin-arm64 build-windows-amd64 build-linux-amd64 build-linux-arm64
71123 @echo " Building for all platforms completed."
72124
73- # Clean build artifacts
125+ # Clean build artifacts and temporary files
74126clean :
75127 @echo " Cleaning..."
76128 @rm -f coverage.out coverage.html integration_coverage.out integration_coverage.html unit_coverage.out all_coverage.out all_coverage.html * _coverage.out * _coverage.html
129+ @rm -rf $(TOOLS_DIR )
130+ @find . -type f -name " *.tmp" -delete
131+ @find . -type d -name " vendor" -prune -o -type f -name " .DS_Store" -delete
77132 $(GOCLEAN )
78133
134+ # ==================================================================================
135+ # Test targets
136+ # ==================================================================================
137+
79138# Run tests
80139test :
81140 @echo " Running tests..."
@@ -140,6 +199,10 @@ coverage-package:
140199 @echo " Generating coverage report for $( PACKAGE) ..."
141200 $(GOCMD ) tool cover -html=$(OUTPUT ) .out -o $(OUTPUT ) .html
142201
202+ # ==================================================================================
203+ # Code quality targets
204+ # ==================================================================================
205+
143206# Run linter
144207lint :
145208 @echo " Running linter..."
@@ -155,22 +218,49 @@ lint:
155218fmt :
156219 @echo " Formatting code..."
157220 $(GOFMT ) ./...
221+ @if [ -f $( GOIMPORTS) ]; then \
222+ echo " Running goimports..." ; \
223+ $(GOIMPORTS ) -w -local github.com/abitofhelp/servicelib ./; \
224+ fi
225+ @if [ -f $( GOFUMPT) ]; then \
226+ echo " Running gofumpt..." ; \
227+ $(GOFUMPT ) -w ./; \
228+ fi
158229
159230# Run go vet
160231vet :
161232 @echo " Running go vet..."
162233 $(GOVET ) ./...
163234
235+ # Run all code quality checks
236+ quality : fmt vet lint security
237+ @echo " All code quality checks passed!"
238+
239+ # ==================================================================================
240+ # Dependency management targets
241+ # ==================================================================================
242+
164243# Update dependencies
165244tidy :
166245 @echo " Tidying dependencies..."
167246 $(GOMOD ) tidy
168247
248+ # Update all dependencies to their latest versions
249+ update-deps :
250+ @echo " Updating dependencies to latest versions..."
251+ @go get -u ./...
252+ @$(GOMOD ) tidy
253+ @echo " Dependencies updated. Please run tests to verify compatibility."
254+
169255# Vendor dependencies
170- vendor :
256+ vendor : tidy
171257 @echo " Vendoring dependencies..."
172258 $(GOMOD ) vendor
173259
260+ # ==================================================================================
261+ # Code generation targets
262+ # ==================================================================================
263+
174264# Generate mocks
175265generate :
176266 @echo " Generating mocks..."
@@ -186,6 +276,10 @@ generate-package:
186276 @echo " Generating mocks for package $( PACKAGE) ..."
187277 $(GOGENERATE ) $(PACKAGE )
188278
279+ # ==================================================================================
280+ # Security targets
281+ # ==================================================================================
282+
189283# Check for security vulnerabilities
190284security :
191285 @echo " Checking for security vulnerabilities..."
@@ -197,6 +291,10 @@ security:
197291 govulncheck ./...; \
198292 fi
199293
294+ # ==================================================================================
295+ # Benchmarking targets
296+ # ==================================================================================
297+
200298# Run benchmarks
201299bench :
202300 @echo " Running benchmarks..."
@@ -212,18 +310,69 @@ bench-package:
212310 @echo " Running benchmarks for package $( PACKAGE) ..."
213311 $(GOTEST ) -bench=. -benchmem $(PACKAGE )
214312
313+ # ==================================================================================
314+ # Documentation targets
315+ # ==================================================================================
316+
317+ # Generate documentation
318+ docs :
319+ @echo " Generating documentation..."
320+ @if ! command -v $(GODOC ) > /dev/null; then \
321+ echo " godoc not found, installing..." ; \
322+ go install golang.org/x/tools/cmd/godoc@latest; \
323+ fi
324+ @echo " Documentation generated. Run 'make serve-docs' to view."
325+
326+ # Serve documentation locally
327+ serve-docs :
328+ @echo " Serving documentation at http://localhost:$( GODOC_PORT) /pkg/github.com/abitofhelp/servicelib/"
329+ @$(GODOC ) -http=:$(GODOC_PORT )
330+
331+ # ==================================================================================
332+ # CI/CD targets
333+ # ==================================================================================
334+
335+ # CI target for continuous integration
336+ ci : check-go-version tidy lint test-all
337+
338+ # Pre-commit hook
339+ pre-commit : fmt vet lint security test
340+ @echo " Pre-commit checks passed!"
341+
342+ # Release the library
343+ release :
344+ @echo " Preparing release $( VERSION) ..."
345+ @echo " Ensuring all tests pass..."
346+ @make test-all
347+ @echo " Ensuring code quality..."
348+ @make quality
349+ @echo " Checking for security vulnerabilities..."
350+ @make security
351+ @echo " Updating dependencies..."
352+ @make tidy
353+ @echo " Release $( VERSION) is ready!"
354+ @echo " To complete the release, tag the repository:"
355+ @echo " git tag -a v$( VERSION) -m 'Release $( VERSION) '"
356+ @echo " git push origin v$( VERSION) "
357+
358+ # ==================================================================================
359+ # Help
360+ # ==================================================================================
361+
215362# Help target
216363help :
217364 @echo " Available targets:"
218- @echo " all - Run clean, lint, test, and build"
365+ @echo " all - Run check-go-version, clean, lint, test, and build"
366+ @echo " check-go-version - Verify the correct Go version is installed"
367+ @echo " tools - Install required development tools"
219368 @echo " build - Compile the code for current platform (no binary produced)"
220369 @echo " build-darwin-amd64 - Compile for macOS Intel (amd64)"
221370 @echo " build-darwin-arm64 - Compile for macOS Apple Silicon (arm64)"
222371 @echo " build-windows-amd64 - Compile for Windows (amd64)"
223372 @echo " build-linux-amd64 - Compile for Linux (amd64)"
224373 @echo " build-linux-arm64 - Compile for Linux (arm64)"
225374 @echo " build-all - Compile for all platforms"
226- @echo " clean - Remove test artifacts and clean Go cache"
375+ @echo " clean - Remove test artifacts, tools, and clean Go cache"
227376 @echo " test - Run tests"
228377 @echo " test-integration - Run integration tests (with integration build tag)"
229378 @echo " test-all - Run all tests (unit and integration) with combined coverage report"
@@ -232,15 +381,22 @@ help:
232381 @echo " coverage-integration - Generate test coverage report for integration tests"
233382 @echo " coverage-package - Generate test coverage report for a specific package (PACKAGE=./path/to/package OUTPUT=name)"
234383 @echo " lint - Run linter"
235- @echo " fmt - Format code"
384+ @echo " fmt - Format code with gofmt, goimports, and gofumpt "
236385 @echo " vet - Run go vet"
386+ @echo " quality - Run all code quality checks (fmt, vet, lint, security)"
237387 @echo " tidy - Update dependencies"
388+ @echo " update-deps - Update all dependencies to their latest versions"
238389 @echo " vendor - Vendor dependencies"
239390 @echo " generate - Generate mocks using go:generate"
240391 @echo " generate-package - Generate mocks for a specific package (PACKAGE=./path/to/package)"
241392 @echo " security - Check for security vulnerabilities"
242393 @echo " bench - Run benchmarks"
243394 @echo " bench-package - Run benchmarks for a specific package (PACKAGE=./path/to/package)"
395+ @echo " docs - Generate documentation"
396+ @echo " serve-docs - Serve documentation locally"
397+ @echo " ci - Run continuous integration checks"
398+ @echo " pre-commit - Run pre-commit checks"
399+ @echo " release - Prepare a release"
244400 @echo " help - Show this help message"
245401
246402# Default target
0 commit comments