-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
189 lines (151 loc) · 4.37 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
GO := go
DIRS_TO_CLEAN:=
FILES_TO_CLEAN:=
ifeq ($(origin GO), undefined)
GO:=$(shell which go)
endif
ifeq ($(GO),)
$(error Could not find 'go' in path. Please install go, or if already installed either add it to your path or set GO to point to its directory)
endif
pkgs = $(shell GOFLAGS=-mod=vendor $(GO) list ./... | grep -vE -e /vendor/ -e /pkg/swagger/)
pkgDirs = $(shell GOFLAGS=-mod=vendor $(GO) list -f {{.Dir}} ./... | grep -vE -e /vendor/ -e /pkg/swagger/)
DIR_OUT:=/tmp
GOLANGCI:=$(shell command -v golangci-lint 2> /dev/null)
WWHRD:=$(shell command -v wwhrd 2> /dev/null)
GO_EXCLUDE := /vendor/|.pb.go|.gen.go
GO_FILES_CMD := find . -name '*.go' | grep -v -E '$(GO_EXCLUDE)'
#-------------------------
# Final targets
#-------------------------
.PHONY: dev
## Execute development pipeline
dev: license generate format lint.fast build
#-------------------------
# Download libraries and tools
#-------------------------
.PHONY: get.tools
## Retrieve tools packages
get.tools:
# License checker
go get -u github.com/frapposelli/wwhrd
# linter
go get -u github.com/golangci/golangci-lint/cmd/golangci-lint
#-------------------------
# Code generation
#-------------------------
.PHONY: generate
## Generate go code
generate:
@echo "==> generating go code"
GOFLAGS=-mod=vendor $(GO) generate $(pkgs)
#-------------------------
# Checks
#-------------------------
.PHONY: format license license.csv lint.fast lint.full lint.sonar stats.loc
check: format license lint.full
## Apply code format, import reorganization and code simplification on source code
format:
@echo "==> formatting code"
@$(GO) fmt $(pkgs)
@echo "==> clean imports"
@goimports -w $(pkgDirs)
@echo "==> simplify code"
@gofmt -s -w $(pkgDirs)
## Check external license usage
license:
ifndef WWHRD
$(error "Please install wwhrd! make get-tools")
endif
@echo "==> license check"
wwhrd check
## Launch linter
lint.fast:
ifndef GOLANGCI
$(error "Please install golangci! make get-tools")
endif
@echo "==> linters (fast)"
@golangci-lint run -v --fast $(pkgDirs)
## Validate code
lint.full:
ifndef GOLANGCI
$(error "Please install golangci! make get-tools")
endif
@echo "==> linters (slow)"
@golangci-lint run -v $(pkgDirs)
#-------------------------
# Build artefacts
#-------------------------
.PHONY: build build.http-server-go
## Build all binaries
build:
$(GO) build -o bin/http-go-server internal/main.go
## Compress all binaries
pack:
@echo ">> packing all binaries"
@upx -7 -qq bin/*
#-------------------------
# Target: depend
#-------------------------
.PHONY: depend vendor.check depend.status depend.update depend.cleanlock depend.update.full
## Use go modules
depend: depend.tidy depend.verify depend.vendor
depend.tidy:
@echo "==> Running dependency cleanup"
$(GO) mod tidy -v
depend.verify:
@echo "==> Verifying dependencies"
$(GO) mod verify
depend.vendor:
@echo "==> Freezing dependencies"
$(GO) mod vendor
depend.update:
@echo "==> Update go modules"
$(GO) get -u -v
depend.update.full: depend.cleanlock depend.update
#-------------------------
# Target: clean
#-------------------------
## Clean build files
clean: clean.go
rm -rf $(DIRS_TO_CLEAN)
rm -f $(FILES_TO_CLEAN)
clean.go: ; $(info cleaning...)
$(eval GO_CLEAN_FLAGS := -i -r)
$(GO) clean $(GO_CLEAN_FLAGS)
#-------------------------
# Target: help
#-------------------------
# COLORS
GREEN := $(shell tput -Txterm setaf 2)
YELLOW := $(shell tput -Txterm setaf 3)
WHITE := $(shell tput -Txterm setaf 7)
RESET := $(shell tput -Txterm sgr0)
TARGET_MAX_CHAR_NUM=20
## Show help
help:
@echo ''
@echo 'Usage:'
@echo ' ${YELLOW}make${RESET} ${GREEN}<target>${RESET}'
@echo ''
@echo 'Targets:'
@awk '/^[a-zA-Z\-\_0-9]+:/ { \
helpMessage = match(lastLine, /^## (.*)/); \
if (helpMessage) { \
helpCommand = substr($$1, 0, index($$1, ":")-1); \
helpMessage = substr(lastLine, RSTART + 3, RLENGTH); \
printf " ${YELLOW}%-$(TARGET_MAX_CHAR_NUM)s${RESET} ${GREEN}%s${RESET}\n", helpCommand, helpMessage; \
} \
} \
{ lastLine = $$0 }' $(MAKEFILE_LIST)
#-------------------------
# Target: swagger.validate
#-------------------------
.PHONY: swagger.validate
swagger.validate:
swagger validate pkg/swagger/swagger.yml
#-------------------------
# Target: swagger.doc
#-------------------------
.PHONY: swagger.doc
swagger.doc:
docker run --platform linux/amd64 -i yousan/swagger-yaml-to-html < pkg/swagger/swagger.yml > doc/index.html