forked from Vonage/gosrvlib
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Makefile
283 lines (237 loc) · 9.94 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
# MAKEFILE
#
# @author Nicola Asuni
# @link https://github.com/Vonage/gosrvlib
# ------------------------------------------------------------------------------
SHELL=/bin/bash
.SHELLFLAGS=-o pipefail -c
# Project owner
OWNER=Vonage
# 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=gosrvlib
# 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.61.0
# Directory containing the source code
SRCDIR=./pkg
# 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
# Set default configuration file to generate a new project from the example service
ifeq ($(CONFIG),)
CONFIG=project.cfg
endif
# Include the configuration file
include $(CONFIG)
# --- 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 example : Build and test the service example"
@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 project : Generate a new project from the example using the data set via CONFIG=project.cfg"
@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 version : Update this library version in the examples"
@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 example"
@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) version format clean mod deps generate qa example
# 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 go.uber.org/mock/mockgen@latest
# 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
# Build and test the example
.PHONY: example
example:
cd examples/service && \
make clean mod deps gendoc generate qa build
# Format the source code
.PHONY: format
format:
@find $(SRCDIR) -type f -name "*.go" -exec $(GOFMT) -s -w {} \;
cd examples/service && make format
# 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
# Create a new project based on the example template
.PHONY: project
project:
cd examples/service && make clean
@mkdir -p ./target/$(gosrvlibexamplecvspath)/$(gosrvlibexample)
@rm -rf ./target/$(gosrvlibexamplecvspath)/$(gosrvlibexample)/*
@cp -rf examples/service/. ./target/$(gosrvlibexamplecvspath)/$(gosrvlibexample)/
sed -i '/^replace /d' ./target/$(gosrvlibexamplecvspath)/$(gosrvlibexample)/go.mod
find ./target/$(gosrvlibexamplecvspath)/$(gosrvlibexample) -depth -regextype sed -regex '.*gosrvlibexample.*' -execdir sh -c 'f="{}"; mv -- "$$f" "$$(echo "$$f" | sed s/gosrvlibexample/$(gosrvlibexample)/)"' \;
find ./target/$(gosrvlibexamplecvspath)/$(gosrvlibexample) -depth -regextype sed -regex '.*GOSRVLIBEXAMPLE.*' -execdir sh -c 'f="{}"; mv -- "$$f" "$$(echo "$$f" | sed s/GOSRVLIBEXAMPLE/$(GOSRVLIBEXAMPLE)/)"' \;
find ./target/$(gosrvlibexamplecvspath)/$(gosrvlibexample) -type f -exec sed -i "s|gosrvlibexampleshortdesc|$(gosrvlibexampleshortdesc)|g" {} \;
find ./target/$(gosrvlibexamplecvspath)/$(gosrvlibexample) -type f -exec sed -i "s|gosrvlibexamplelongdesc|$(gosrvlibexamplelongdesc)|g" {} \;
find ./target/$(gosrvlibexamplecvspath)/$(gosrvlibexample) -type f -exec sed -i "s|gosrvlibexampleauthor|$(gosrvlibexampleauthor)|g" {} \;
find ./target/$(gosrvlibexamplecvspath)/$(gosrvlibexample) -type f -exec sed -i "s|gosrvlibexampleemail|$(gosrvlibexampleemail)|g" {} \;
find ./target/$(gosrvlibexamplecvspath)/$(gosrvlibexample) -type f -exec sed -i "s|gosrvlibexamplecvspath|$(gosrvlibexamplecvspath)|g" {} \;
find ./target/$(gosrvlibexamplecvspath)/$(gosrvlibexample) -type f -exec sed -i "s|gosrvlibexampleprojectlink|$(gosrvlibexampleprojectlink)|g" {} \;
find ./target/$(gosrvlibexamplecvspath)/$(gosrvlibexample) -type f -exec sed -i "s|gosrvlibexampleowner|$(gosrvlibexampleowner)|g" {} \;
find ./target/$(gosrvlibexamplecvspath)/$(gosrvlibexample) -type f -exec sed -i "s|gosrvlibexamplevcsgit|$(gosrvlibexamplevcsgit)|g" {} \;
find ./target/$(gosrvlibexamplecvspath)/$(gosrvlibexample) -type f -exec sed -i "s|gosrvlibexample|$(gosrvlibexample)|g" {} \;
find ./target/$(gosrvlibexamplecvspath)/$(gosrvlibexample) -type f -exec sed -i "s|GOSRVLIBEXAMPLE|$(GOSRVLIBEXAMPLE)|g" {} \;
# 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
cd examples/service && make updatego
# 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
cd examples/service && make updatelint
# Update dependencies
.PHONY: updatemod
updatemod:
$(GO) get -t -u ./... && go mod tidy -compat=$(shell grep -oP 'go \K[0-9]+\.[0-9]+' go.mod)
cd examples/service && make updatemod
# Set the gosrvlib version in the example go.mod
.PHONY: version
version:
sed -i "s|github.com/Vonage/gosrvlib v.*$$|github.com/Vonage/gosrvlib v$(VERSION)|" examples/service/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
$(MAKE) version