-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMakefile
More file actions
158 lines (130 loc) · 4.6 KB
/
Makefile
File metadata and controls
158 lines (130 loc) · 4.6 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
# Project configuration
MOD_NAME := github.com/opensraph/template.go
BINARY_NAME := $(shell basename $(MOD_NAME))
# Build variables
VERSION := $(shell git describe --tags --always --match='v*' 2>/dev/null || echo "dev")
GIT_COMMIT := $(shell git rev-parse --short HEAD 2>/dev/null || echo "unknown")
BUILD_TIME := $(shell date -u +'%Y-%m-%d_%H:%M:%S')
# Go configuration
GO := go
LDFLAGS := -s -w -X $(MOD_NAME)/version.Version=$(VERSION)
# Colors
BLUE := \033[34m
GREEN := \033[32m
YELLOW := \033[33m
RED := \033[31m
RESET := \033[0m
.DEFAULT_GOAL := help
###############
##@ Initial
.PHONY: init
init: ## Initialize development environment
@echo "$(BLUE)Installing development tools...$(RESET)"
$(GO) install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@latest
$(GO) install golang.org/x/tools/cmd/goimports@latest
$(GO) install github.com/goreleaser/goreleaser/v2@latest
@echo "$(GREEN)Development environment initialized!$(RESET)"
.PHONY: deps
deps: ## Download and verify dependencies
@$(GO) mod download && $(GO) mod tidy && $(GO) mod verify
.PHONY: rename
rename: ## Rename Go module (interactive)
@echo "$(YELLOW)Current module: $(MOD_NAME)$(RESET)" \
&& echo "Enter new go module name:" \
&& read new_module_name \
&& echo "New module name: '$$new_module_name'" \
&& echo -n "Are you sure? [y/N] " \
&& read ans && [ $${ans:-N} = y ] \
&& echo "$(BLUE)Updating module name...$(RESET)" \
&& find . -type f -not -path '*/\.git*' -exec sed -i "s|${MOD_NAME}|$$new_module_name|g" {} \; \
&& echo "$(GREEN)Module renamed successfully!$(RESET)" \
&& git add . && git commit -m "rename module to '$$new_module_name'"
###############
##@ Development
.PHONY: run
run: ## Run the application
@CGO_ENABLED=0 $(GO) run -ldflags="$(LDFLAGS)" ./...
fmt: ## Format code
@$(GO) fmt ./...
@if command -v goimports >/dev/null 2>&1; then goimports -w .; fi
.PHONY: lint
lint: ## Run linter
@if command -v golangci-lint >/dev/null 2>&1; then \
golangci-lint run --timeout=3m; \
else \
echo "$(RED)golangci-lint not found. Run 'make init'$(RESET)"; \
exit 1; \
fi
##@ Testing
.PHONY: test
test: ## Run tests with coverage report
@$(GO) test -race -coverprofile=coverage.out ./...
@$(GO) tool cover -func=coverage.out | tail -1
.PHONY: test-verbose
test-verbose: ## Run tests with verbose output
@$(GO) test -v -race ./...
.PHONY: coverage
coverage: test ## Generate and open HTML coverage report
@$(GO) tool cover -html=coverage.out -o coverage.html
@echo "$(GREEN)Coverage report generated: coverage.html$(RESET)"
@echo "$(BLUE)Open coverage.html in your browser to view detailed coverage$(RESET)"
.PHONY: bench
bench: ## Run benchmarks
@$(GO) test -bench=. -benchmem ./...
#########
##@ Build
.PHONY: build
build: ## Build binary
@echo "$(BLUE)Building $(BINARY_NAME)...$(RESET)"
@mkdir -p bin
@CGO_ENABLED=0 $(GO) build -ldflags="$(LDFLAGS)" -o bin/$(BINARY_NAME) ./...
@echo "$(GREEN)Built: bin/$(BINARY_NAME)$(RESET)"
.PHONY: build-all
build-all: ## Build for multiple platforms
@echo "$(BLUE)Building for multiple platforms...$(RESET)"
@mkdir -p bin
@for os in linux darwin windows; do \
for arch in amd64 arm64; do \
echo "Building $$os/$$arch..."; \
GOOS=$$os GOARCH=$$arch CGO_ENABLED=0 $(GO) build \
-ldflags="$(LDFLAGS)" \
-o bin/$(BINARY_NAME)-$$os-$$arch ./...; \
done; \
done
@echo "$(GREEN)Multi-platform build completed!$(RESET)"
.PHONY: install
install: ## Install binary to GOPATH/bin
@CGO_ENABLED=0 $(GO) install -ldflags="$(LDFLAGS)" ./...
##@ Release
.PHONY: release
release: ## Create release with goreleaser
@if command -v goreleaser >/dev/null 2>&1; then \
goreleaser release --clean; \
else \
echo "$(RED)goreleaser not found. Run 'make init'$(RESET)"; \
exit 1; \
fi
##@ Maintenance
.PHONY: clean
clean: ## Clean build artifacts and caches
@$(GO) clean -cache -testcache -modcache
@rm -rf bin/ dist/ coverage.out coverage.html
@echo "$(GREEN)Cleaned!$(RESET)"
.PHONY: check
check: deps test lint ## Run all checks
@echo "$(GREEN)All checks passed!$(RESET)"
##@ Information
.PHONY: info
info: ## Show build information
@echo "$(BLUE)Project Information:$(RESET)"
@echo "Module: $(MOD_NAME)"
@echo "Binary: $(BINARY_NAME)"
@echo "Version: $(VERSION)"
@echo "Commit: $(GIT_COMMIT)"
@echo "Build Time: $(BUILD_TIME)"
@echo "Go Version: $$($(GO) version | cut -d' ' -f3)"
.PHONY: help
help: ## Show this help message
@awk 'BEGIN {FS = ":.*##"; printf "Usage:\n make $(BLUE)<target>$(RESET)\n"} \
/^[a-zA-Z_-]+:.*?##/ { printf " $(BLUE)%-12s$(RESET) %s\n", $$1, $$2 } \
/^##@/ { printf "\n$(YELLOW)%s$(RESET)\n", substr($$0, 5) }' $(MAKEFILE_LIST)