-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMakefile
236 lines (194 loc) · 6.85 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
# MAKEFILE
#
# @author Nicola Asuni
# @link https://github.com/tecnickcom/statsd
# ------------------------------------------------------------------------------
SHELL=/bin/bash
.SHELLFLAGS=-o pipefail -c
# Project owner
OWNER=tecnickcom
# Project vendor
VENDOR=${OWNER}
# Lowercase VENDOR name for Docker
LCVENDOR=$(shell echo "${VENDOR}" | tr '[:upper:]' '[:lower:]')
# CVS path (path to the parent dir containing the project)
CVSPATH=github.com/${VENDOR}
# Project name
PROJECT=statsd
# Project version
VERSION=$(shell cat VERSION)
# Project release number (packaging build number)
RELEASE=$(shell cat RELEASE)
# Current directory
CURRENTDIR=$(dir $(realpath $(firstword $(MAKEFILE_LIST))))
# Target directory
TARGETDIR=$(CURRENTDIR)target
# Directory where to store binary utility tools
BINUTIL=$(TARGETDIR)/binutil
# GO lang path
ifeq ($(GOPATH),)
# extract the GOPATH
GOPATH=$(firstword $(subst /src/, ,$(CURRENTDIR)))
endif
# Add the GO binary dir in the PATH
export PATH := $(GOPATH)/bin:$(PATH)
# Docker tag
DOCKERTAG=$(VERSION)-$(RELEASE)
# Docker command
ifeq ($(DOCKER),)
DOCKER=$(shell which docker)
endif
# Common commands
GO=GOPATH=$(GOPATH) GOPRIVATE=$(CVSPATH) $(shell which go)
GOVERSION=${shell go version | grep -Po '(go[0-9]+.[0-9]+)'}
GOFMT=$(shell which gofmt)
GOTEST=GOPATH=$(GOPATH) $(shell which gotest)
GODOC=GOPATH=$(GOPATH) $(shell which godoc)
GOLANGCILINT=$(BINUTIL)/golangci-lint
GOLANGCILINTVERSION=v1.63.4
# Directory containing the source code
SRCDIR=./
# List of packages
GOPKGS=$(shell $(GO) list $(SRCDIR)/...)
# Enable junit report when not in LOCAL mode
ifeq ($(strip $(DEVMODE)),LOCAL)
TESTEXTRACMD=&& $(GO) tool cover -func=$(TARGETDIR)/report/coverage.out
else
TESTEXTRACMD=2>&1 | tee >(PATH=$(GOPATH)/bin:$(PATH) go-junit-report > $(TARGETDIR)/test/report.xml); test $${PIPESTATUS[0]} -eq 0
endif
# --- MAKE TARGETS ---
# Display general help about this command
.PHONY: help
help:
@echo ""
@echo "$(PROJECT) Makefile."
@echo "GOPATH=$(GOPATH)"
@echo "The following commands are available:"
@echo ""
@echo " make clean : Remove any build artifact"
@echo " make coverage : Generate the coverage report"
@echo " make dbuild : Build everything inside a Docker container"
@echo " make deps : Get dependencies"
@echo " make format : Format the source code"
@echo " make generate : Generate go code automatically"
@echo " make linter : Check code against multiple linters"
@echo " make mod : Download dependencies"
@echo " make qa : Run all tests and static analysis tools"
@echo " make tag : Tag the Git repository"
@echo " make test : Run unit tests"
@echo " make updateall : Update everything"
@echo " make updatego : Update Go version"
@echo " make updatelint : Update golangci-lint version"
@echo " make updatemod : Update dependencies"
@echo " make versionup : Increase the patch number in the VERSION file"
@echo ""
@echo "Use DEVMODE=LOCAL for human friendly output."
@echo ""
@echo "To test and build everything from scratch:"
@echo " DEVMODE=LOCAL make format clean mod deps generate qa"
@echo "or use the shortcut:"
@echo " make x"
@echo ""
# Alias for help target
all: help
# Alias to test and build everything from scratch
.PHONY: x
x:
DEVMODE=LOCAL $(MAKE) format clean mod deps generate qa
# Remove any build artifact
.PHONY: clean
clean:
rm -rf $(TARGETDIR)
# Generate the coverage report
.PHONY: coverage
coverage: ensuretarget
$(GO) tool cover -html=$(TARGETDIR)/report/coverage.out -o $(TARGETDIR)/report/coverage.html
# Build everything inside a Docker container
.PHONY: dbuild
dbuild: dockerdev
@mkdir -p $(TARGETDIR)
@rm -rf $(TARGETDIR)/*
@echo 0 > $(TARGETDIR)/make.exit
CVSPATH=$(CVSPATH) VENDOR=$(LCVENDOR) PROJECT=$(PROJECT) MAKETARGET='$(MAKETARGET)' DOCKERTAG='$(DOCKERTAG)' $(CURRENTDIR)dockerbuild.sh
@exit `cat $(TARGETDIR)/make.exit`
# Get the test dependencies
.PHONY: deps
deps: ensuretarget
curl --silent --show-error --fail --location "https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh" | sh -s -- -b $(BINUTIL) $(GOLANGCILINTVERSION)
$(GO) install github.com/rakyll/gotest
$(GO) install github.com/jstemmer/go-junit-report/v2@latest
$(GO) install github.com/golang/mock/mockgen
# Build a base development Docker image
.PHONY: dockerdev
dockerdev:
$(DOCKER) build --pull --tag ${LCVENDOR}/dev_${PROJECT} --file ./resources/docker/Dockerfile.dev ./resources/docker/
# Create the trget directories if missing
.PHONY: ensuretarget
ensuretarget:
@mkdir -p $(TARGETDIR)/test
@mkdir -p $(TARGETDIR)/report
@mkdir -p $(TARGETDIR)/binutil
# Format the source code
.PHONY: format
format:
@find $(SRCDIR) -type f -name "*.go" -exec $(GOFMT) -s -w {} \;
# Generate test mocks
.PHONY: generate
generate:
@find $(SRCDIR) -type f -name "*mock_test.go" -exec rm {} \;
$(GO) generate $(GOPKGS)
# Execute multiple linter tools
.PHONY: linter
linter:
@echo -e "\n\n>>> START: Static code analysis <<<\n\n"
$(GOLANGCILINT) run --exclude-use-default=false --max-issues-per-linter 0 --max-same-issues 0 $(SRCDIR)/...
@echo -e "\n\n>>> END: Static code analysis <<<\n\n"
# Download dependencies
.PHONY: mod
mod:
$(GO) mod download all
# Run all tests and static analysis tools
.PHONY: qa
qa: linter test coverage
# Tag the Git repository
.PHONY: tag
tag:
git tag -a "v$(VERSION)" -m "Version $(VERSION)" && \
git push origin --tags
# Run the unit tests
.PHONY: test
test: ensuretarget
@echo -e "\n\n>>> START: Unit Tests <<<\n\n"
$(GOTEST) \
-shuffle=on \
-tags=unit,benchmark \
-covermode=atomic \
-bench=. \
-race \
-failfast \
-coverprofile=$(TARGETDIR)/report/coverage.out \
-v $(GOPKGS) $(TESTEXTRACMD)
@echo -e "\n\n>>> END: Unit Tests <<<\n\n"
# Update everything
.PHONY: updateall
updateall: updatego updatelint updatemod
# Update go version
.PHONY: updatego
updatego:
$(eval LAST_GO_TOOLCHAIN=$(shell curl -s https://go.dev/dl/ | grep -oP 'go[0-9]+\.[0-9]+\.[0-9]+\.linux-amd64\.tar\.gz' | head -n 1 | grep -oP 'go[0-9]+\.[0-9]+\.[0-9]+'))
$(eval LAST_GO_VERSION=$(shell echo ${LAST_GO_TOOLCHAIN} | grep -oP '[0-9]+\.[0-9]+'))
sed -i "s|^go [0-9]*\.[0-9]*$$|go ${LAST_GO_VERSION}|g" go.mod
sed -i "s|^toolchain go[0-9]*\.[0-9]*\.[0-9]*$$|toolchain ${LAST_GO_TOOLCHAIN}|g" go.mod
# Update linter version
.PHONY: updatelint
updatelint:
$(eval LAST_GOLANGCILINT_VERSION=$(shell curl -sL https://github.com/golangci/golangci-lint/releases/latest | grep -oP '<title>Release \Kv[0-9]+\.[0-9]+\.[0-9]+'))
sed -i "s|^GOLANGCILINTVERSION=v[0-9]*\.[0-9]*\.[0-9]*$$|GOLANGCILINTVERSION=${LAST_GOLANGCILINT_VERSION}|g" Makefile
# Update dependencies
.PHONY: updatemod
updatemod:
$(GO) get -t -u ./... && go mod tidy -compat=$(shell grep -oP 'go \K[0-9]+\.[0-9]+' go.mod)
# Increase the patch number in the VERSION file
.PHONY: versionup
versionup:
echo ${VERSION} | gawk -F. '{printf("%d.%d.%d\n",$$1,$$2,(($$3+1)));}' > VERSION