-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile-common.mk
234 lines (192 loc) · 5.28 KB
/
Makefile-common.mk
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
.DEFAULT_GOAL=all
.DELETE_ON_ERROR:
NULL:=
SPACE:=$(NULL) $(NULL)
.PHONY: all
all: build test lint
CI?=false
ifeq ($(CI),true)
VERBOSE?=true
TEST_FULLPATH?=true
TEST_COVER?=true
endif
VERBOSE?=false
ifeq ($(VERBOSE),true)
VERBOSE_FLAG=$(SPACE)-v
else
VERBOSE_FLAG=
endif
VERSION?=$(shell (git describe --tags --exact-match 2> /dev/null || git rev-parse HEAD) | sed "s/^v//")
.PHONY: version
version:
@echo $(VERSION)
GO?=go
GO_RUN=$(GO) run$(VERBOSE_FLAG)
GO_GET=$(GO) get$(VERBOSE_FLAG)
GO_LIST=$(GO) list$(VERBOSE_FLAG)
GO_MOD=$(GO) mod
GO_TOOL=$(GO) tool
GO_TOOL_COVER=$(GO_TOOL) cover
GO_MODULE=$(shell $(GO_LIST) -m)
GO_TAGS?=
ifneq ($(GO_TAGS),)
GO_TAGS_FLAG=$(SPACE)-tags=$(GO_TAGS)
else
GO_TAGS_FLAG=
endif
BUILD_DIR=build
.PHONY: build
build:
ifneq ($(wildcard ./cmd/*/*.go),)
mkdir -p $(BUILD_DIR)
$(GO) build$(VERBOSE_FLAG)$(GO_TAGS_FLAG) -ldflags="-s -w -X main.version=$(VERSION)" -o $(BUILD_DIR) ./cmd/...
endif
TEST_FULLPATH?=false
ifeq ($(TEST_FULLPATH),true)
TEST_FULLPATH_FLAG=$(SPACE)-fullpath
else
TEST_FULLPATH_FLAG=
endif
TEST_COVER?=false
ifeq ($(TEST_COVER),true)
TEST_COVER_FLAGS=$(SPACE)-cover -coverprofile=coverage.out
else
TEST_COVER_FLAGS=
endif
TEST_COUNT?=
ifneq ($(TEST_COUNT),)
TEST_COUNT_FLAG=$(SPACE)-count=$(TEST_COUNT)
else
TEST_COUNT_FLAG=
endif
.PHONY: test
test:
$(GO) test$(VERBOSE_FLAG)$(TEST_FULLPATH_FLAG)$(GO_TAGS_FLAG)$(TEST_COVER_FLAGS)$(TEST_COUNT_FLAG) ./...
ifeq ($(TEST_COVER),true)
$(GO_TOOL_COVER) -func=coverage.out -o=coverage.txt
ifeq ($(VERBOSE),true)
cat coverage.txt
endif
$(GO_TOOL_COVER) -html=coverage.out -o=coverage.html
endif
.PHONY: generate
generate::
$(GO) generate$(VERBOSE_FLAG) ./...
.PHONY: lint
lint:
$(MAKE) golangci-lint
$(MAKE) lint-rules
$(MAKE) mod-tidy
# version:
# - tag: vX.Y.Z
# - branch: master
# - latest
GOLANGCI_LINT_VERSION?=v1.61.0
# Installation type:
# - binary
# - source
GOLANGCI_LINT_TYPE?=binary
ifeq ($(GOLANGCI_LINT_TYPE),binary)
GOLANGCI_LINT_DIR=$(shell $(GO) env GOPATH)/pkg/golangci-lint/$(GOLANGCI_LINT_VERSION)
GOLANGCI_LINT_BIN=$(GOLANGCI_LINT_DIR)/golangci-lint
$(GOLANGCI_LINT_BIN):
curl$(VERBOSE_FLAG) -fL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOLANGCI_LINT_DIR) $(GOLANGCI_LINT_VERSION)
.PHONY: install-golangci-lint
install-golangci-lint: $(GOLANGCI_LINT_BIN)
else ifeq ($(GOLANGCI_LINT_TYPE),source)
GOLANGCI_LINT_BIN=$(GO_RUN) github.com/golangci/golangci-lint/cmd/golangci-lint@$(GOLANGCI_LINT_VERSION)
install-golangci-lint:
endif
GOLANGCI_LINT_RUN=$(GOLANGCI_LINT_BIN)$(VERBOSE_FLAG) run
.PHONY: golangci-lint
golangci-lint: install-golangci-lint
ifeq ($(CI),true)
$(GOLANGCI_LINT_RUN)
else
# Fix errors if possible.
$(GOLANGCI_LINT_RUN) --fix
endif
.PHONY: golangci-lint-cache-clean
golangci-lint-cache-clean: install-golangci-lint
$(GOLANGCI_LINT_BIN) cache clean
.PHONY: lint-rules
CHECK_MISSING_FILE=@[ -e $(1) ] || (echo "$(1) file is missing" && false)
lint-rules:
# Disallowed files.
@! find . -name ".DS_Store" | (grep "." && echo "Disallowed files")
# Mandatory files.
$(call CHECK_MISSING_FILE,.gitignore)
$(call CHECK_MISSING_FILE,README.md)
$(call CHECK_MISSING_FILE,LICENSE)
$(call CHECK_MISSING_FILE,CODEOWNERS)
$(call CHECK_MISSING_FILE,.github/dependabot.yml)
$(call CHECK_MISSING_FILE,.github/workflows/ci.yml)
$(call CHECK_MISSING_FILE,.github/workflows/dependabot_auto_merge.yml)
$(call CHECK_MISSING_FILE,go.mod)
$(call CHECK_MISSING_FILE,go.sum)
$(call CHECK_MISSING_FILE,.golangci.yml)
$(call CHECK_MISSING_FILE,Makefile)
$(call CHECK_MISSING_FILE,Makefile-common.mk)
# Don't use upper case letter in file and directory name.
# The convention for separator in name is:
# - file: "_"
# - directory in "/cmd": "-"
# - other directory: shouldn't be separated
@! find . -name "*.go" | (grep "[[:upper:]]" && echo "Incorrect file name case")
.PHONY: mod-update
mod-update:
$(GO_GET) -u all
$(MAKE) mod-tidy
.PHONY: mod-update-pierrre
mod-update-pierrre:
GOWORK=off $(GO_LIST) -m -u -json all | jq -r 'select(.Main==null and (.Path | startswith("github.com/pierrre/")) and .Update!=null) | .Path' | xargs -I {} -t $(GO_GET) -u {}
$(MAKE) mod-tidy
MOD_TIDY=$(GO_MOD) tidy$(VERBOSE_FLAG)
.PHONY: mod-tidy
mod-tidy:
ifeq ($(CI),true)
$(MOD_TIDY) -diff
else
$(MOD_TIDY)
endif
.PHONY: git-latest-release
git-latest-release:
@git tag --list --sort=v:refname --format="%(refname:short) => %(creatordate:short)" | tail -n 1
.PHONY: clean
clean:
git clean -fdX
go clean -cache -testcache
$(MAKE) golangci-lint-cache-clean
ifeq ($(CI),true)
GITHUB_REF?=$(error missing GITHUB_REF)
GITHUB_BRANCH=$(shell echo $(GITHUB_REF) | grep -Po "^refs\/heads/\K.+")
GITHUB_TAG=$(shell echo $(GITHUB_REF) | grep -Po "^refs\/tags/\K.+")
CI_LOG_GROUP_START=@echo "::group::$(1)"
CI_LOG_GROUP_END=@echo "::endgroup::"
.PHONY: ci
ci::
$(call CI_LOG_GROUP_START,env)
$(MAKE) ci-env
$(call CI_LOG_GROUP_END)
$(call CI_LOG_GROUP_START,build)
$(MAKE) build
$(call CI_LOG_GROUP_END)
$(call CI_LOG_GROUP_START,test)
$(MAKE) test
$(call CI_LOG_GROUP_END)
$(call CI_LOG_GROUP_START,lint)
$(MAKE) lint
$(call CI_LOG_GROUP_END)
.PHONY: ci-env
ci-env:
env
ifneq ($(GITHUB_TAG),)
ci::
$(call CI_LOG_GROUP_START,tag)
$(MAKE) ci-tag
$(call CI_LOG_GROUP_END)
.PHONY: ci-tag
ci-tag:
GOPROXY=proxy.golang.org $(GO_LIST) -x -m $(GO_MODULE)@$(GITHUB_TAG)
endif
endif # CI end