-
Notifications
You must be signed in to change notification settings - Fork 3
/
Makefile
193 lines (154 loc) · 4.55 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
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)
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: swagger.validate swagger.doc generate format lint.fast build
#-------------------------
# Download libraries and tools
#-------------------------
.PHONY: get.tools
## Retrieve tools packages
get.tools:
# swagger
go get -u github.com/go-swagger/go-swagger/cmd/swagger
# linter
go get -u github.com/golangci/golangci-lint/cmd/golangci-lint
# goimports
go get -u golang.org/x/tools/cmd/goimports
## Retrieve tools in GitHub Actions environment
get.ci-tools:
# swagger
go get -u github.com/go-swagger/go-swagger/cmd/swagger
# goimports
go get -u golang.org/x/tools/cmd/goimports
#-------------------------
# Code generation
#-------------------------
.PHONY: generate
## Generate go code
generate:
@echo "==> generating go code"
GOFLAGS=-mod=vendor $(GO) generate $(pkgs)
#-------------------------
# Checks
#-------------------------
.PHONY: format lint.fast lint.full lint.sonar stats.loc
check: format 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)
## 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.r2-d2
## Build all binaries
build:
GO111MODULE=on $(GO) build -o bin/r2-d2 internal/*.go
## Build all binaries for dev env, to avoid "go: inconsistent vendoring in /build:"
build.dev:
GO111MODULE=on $(GO) build -o bin/r2-d2 -mod=mod internal/*.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 -i yousan/swagger-yaml-to-html < pkg/swagger/swagger.yml > doc/index.html