-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
215 lines (173 loc) · 6.8 KB
/
Makefile
File metadata and controls
215 lines (173 loc) · 6.8 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
# Go-Graphql Makefile
# Build, test, and development automation
# ============================================================================
# Variables
# ============================================================================
# Go parameters
GOCMD := go
GOBUILD := $(GOCMD) build
GOTEST := $(GOCMD) test
GOVET := $(GOCMD) vet
GOFMT := gofmt
GOMOD := $(GOCMD) mod
GOGENERATE := $(GOCMD) generate
# Binary
BINARY_NAME := go-graphql
BINARY_DIR := bin
# Coverage
COVERAGE_FILE := coverage.out
COVERAGE_HTML := coverage.html
COVERAGE_TARGET := 80
# Packages
PACKAGES := ./...
PACKAGES_NO_MOCKS := $(shell go list ./... | grep -v /mocks)
# Tools
GOLANGCI_LINT := golangci-lint
GINKGO := ginkgo
# ============================================================================
# Default target
# ============================================================================
.PHONY: all
all: fmt vet lint test build
# ============================================================================
# Build targets
# ============================================================================
.PHONY: build
build: ## Build the binary
@echo "Building $(BINARY_NAME)..."
@mkdir -p $(BINARY_DIR)
$(GOBUILD) -o $(BINARY_DIR)/$(BINARY_NAME) ./cmd
.PHONY: clean
clean: ## Clean build artifacts
@echo "Cleaning..."
@rm -rf $(BINARY_DIR)
@rm -f $(COVERAGE_FILE) $(COVERAGE_HTML)
# ============================================================================
# Code quality targets
# ============================================================================
.PHONY: fmt
fmt: ## Format code with gofmt
@echo "Formatting code..."
@$(GOFMT) -s -w .
.PHONY: fmt-check
fmt-check: ## Check if code is formatted
@echo "Checking code format..."
@test -z "$$($(GOFMT) -s -l . | tee /dev/stderr)" || (echo "Code is not formatted. Run 'make fmt'" && exit 1)
.PHONY: vet
vet: ## Run go vet
@echo "Running go vet..."
$(GOVET) $(PACKAGES)
.PHONY: lint
lint: ## Run golangci-lint
@echo "Running linter..."
@which $(GOLANGCI_LINT) > /dev/null || (echo "golangci-lint not found. Install: https://golangci-lint.run/usage/install/" && exit 1)
$(GOLANGCI_LINT) run $(PACKAGES)
# ============================================================================
# Test targets
# ============================================================================
.PHONY: test
test: ## Run all tests
@echo "Running tests..."
$(GOTEST) -v -race $(PACKAGES_NO_MOCKS)
.PHONY: test-short
test-short: ## Run tests without race detector (faster)
@echo "Running tests (short mode)..."
$(GOTEST) -v $(PACKAGES_NO_MOCKS)
.PHONY: test-coverage
test-coverage: ## Run tests with coverage report
@echo "Running tests with coverage..."
$(GOTEST) -v -race -coverprofile=$(COVERAGE_FILE) -covermode=atomic $(PACKAGES_NO_MOCKS)
@echo ""
@echo "Coverage summary:"
@$(GOCMD) tool cover -func=$(COVERAGE_FILE) | tail -1
@echo ""
@echo "Generating HTML report: $(COVERAGE_HTML)"
@$(GOCMD) tool cover -html=$(COVERAGE_FILE) -o $(COVERAGE_HTML)
@echo ""
@echo "Per-package coverage:"
@$(GOCMD) tool cover -func=$(COVERAGE_FILE) | grep -E "^[a-z]" | grep -v "total:" || true
.PHONY: test-coverage-view
test-coverage-view: test-coverage ## Run tests with coverage and open HTML report
@echo "Opening coverage report..."
@xdg-open $(COVERAGE_HTML) 2>/dev/null || open $(COVERAGE_HTML) 2>/dev/null || echo "Open $(COVERAGE_HTML) in your browser"
.PHONY: test-ginkgo
test-ginkgo: ## Run tests with Ginkgo (if installed)
@echo "Running tests with Ginkgo..."
@which $(GINKGO) > /dev/null || (echo "Ginkgo not found. Install: go install github.com/onsi/ginkgo/v2/ginkgo@latest" && exit 1)
$(GINKGO) -v -race -cover -coverprofile=$(COVERAGE_FILE) $(PACKAGES_NO_MOCKS)
# ============================================================================
# Generation targets
# ============================================================================
.PHONY: generate
generate: ## Generate mocks and other code
@echo "Generating code..."
$(GOGENERATE) ./tools/...
.PHONY: generate-check
generate-check: generate ## Check if generated code is up to date
@echo "Checking if generated code is up to date..."
@git diff --exit-code || (echo "Generated code is out of date. Run 'make generate' and commit changes." && exit 1)
# ============================================================================
# Dependency targets
# ============================================================================
.PHONY: deps
deps: ## Download dependencies
@echo "Downloading dependencies..."
$(GOMOD) download
.PHONY: tidy
tidy: ## Tidy go.mod
@echo "Tidying go.mod..."
$(GOMOD) tidy
.PHONY: verify
verify: ## Verify dependencies
@echo "Verifying dependencies..."
$(GOMOD) verify
# ============================================================================
# CI targets
# ============================================================================
.PHONY: ci
ci: fmt-check vet lint test ## Run all CI checks
@echo "CI checks passed!"
.PHONY: ci-coverage
ci-coverage: fmt-check vet lint test-coverage ## Run all CI checks with coverage
@echo "CI checks with coverage passed!"
# ============================================================================
# Development targets
# ============================================================================
.PHONY: run
run: build ## Build and run the application
@echo "Running $(BINARY_NAME)..."
./$(BINARY_DIR)/$(BINARY_NAME)
.PHONY: install-tools
install-tools: ## Install development tools
@echo "Installing development tools..."
go install github.com/onsi/ginkgo/v2/ginkgo@latest
@echo "Note: Install golangci-lint from https://golangci-lint.run/usage/install/"
# ============================================================================
# Container image targets
# ============================================================================
# Image configuration
IMG_TAG ?= dev
VERSION ?= 0.1.0
BUILD_TIME ?= $(shell date -u +"%Y-%m-%dT%H:%M:%SZ")
# Kind cluster configuration
KIND_CLUSTER_NAME ?= kind
KIND_IMAGE ?= kindest/node:v1.31.0
.PHONY: docker-build
docker-build: ## Build container image with ko (local)
@echo "Building container image with ko..."
VERSION=$(VERSION) BUILD_TIME=$(BUILD_TIME) ko build ./cmd --local --bare -t $(IMG_TAG)
.PHONY: docker-push
docker-push: ## Build and push container image with ko
@echo "Building and pushing container image..."
VERSION=$(VERSION) BUILD_TIME=$(BUILD_TIME) ko build ./cmd --bare -t $(IMG_TAG)
# ============================================================================
# Help
# ============================================================================
.PHONY: help
help: ## Show this help message
@echo "Go Graphql Makefile"
@echo ""
@echo "Usage: make [target]"
@echo ""
@echo "Targets:"
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf " \033[36m%-20s\033[0m %s\n", $$1, $$2}'