This repository has been archived by the owner on May 11, 2022. It is now read-only.
generated from octomation/go-tool
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
255 lines (202 loc) · 5.45 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
# sourced by https://github.com/octomation/makefiles
.DEFAULT_GOAL = test-with-coverage
GIT_HOOKS = post-merge pre-commit pre-push
GO_VERSIONS = 1.14 1.15
SHELL := /bin/bash -euo pipefail # `explain set -euo pipefail`
OS = $(shell uname -s | tr '[:upper:]' '[:lower:]')
ARCH = $(shell uname -m | tr '[:upper:]' '[:lower:]')
GO111MODULE = on
GOFLAGS = -mod=vendor
GOPRIVATE = go.octolab.net
GOPROXY = direct
LOCAL = $(MODULE)
MODULE = `GO111MODULE=on go list -m $(GOFLAGS)`
PACKAGES = `GO111MODULE=on go list $(GOFLAGS) ./...`
PATHS = $(shell echo $(PACKAGES) | sed -e "s|$(MODULE)/||g" | sed -e "s|$(MODULE)|$(PWD)/*.go|g")
TIMEOUT = 1s
ifeq (, $(PACKAGES))
PACKAGES = $(MODULE)
endif
ifeq (, $(PATHS))
PATHS = .
endif
export GO111MODULE := $(GO111MODULE)
export GOFLAGS := $(GOFLAGS)
export GOPRIVATE := $(GOPRIVATE)
export GOPROXY := $(GOPROXY)
go-env:
@echo "GO111MODULE: `go env GO111MODULE`"
@echo "GOFLAGS: $(strip `go env GOFLAGS`)"
@echo "GOPRIVATE: $(strip `go env GOPRIVATE`)"
@echo "GOPROXY: $(strip `go env GOPROXY`)"
@echo "LOCAL: $(LOCAL)"
@echo "MODULE: $(MODULE)"
@echo "PACKAGES: $(PACKAGES)"
@echo "PATHS: $(strip $(PATHS))"
@echo "TIMEOUT: $(TIMEOUT)"
.PHONY: go-env
deps-check:
@go mod verify
@if command -v egg > /dev/null; then \
egg deps check license; \
egg deps check version; \
fi
.PHONY: deps-check
deps-clean:
@go clean -modcache
.PHONY: deps-clean
deps-fetch:
@go mod download
@if [[ "`go env GOFLAGS`" =~ -mod=vendor ]]; then go mod vendor; fi
.PHONY: deps-fetch
deps-tidy:
@go mod tidy
@if [[ "`go env GOFLAGS`" =~ -mod=vendor ]]; then go mod vendor; fi
.PHONY: deps-tidy
deps-update: selector = '{{if not (or .Main .Indirect)}}{{.Path}}{{end}}'
deps-update:
@if command -v egg > /dev/null; then \
packages="`egg deps list`"; \
else \
packages="`go list -f $(selector) -m -mod=readonly all`"; \
fi; \
if [[ "`go version`" == *1.1[1-3]* ]]; then \
go get -d -mod= -u $$packages; \
else \
go get -d -u $$packages; \
fi; \
if [[ "`go env GOFLAGS`" =~ -mod=vendor ]]; then go mod vendor; fi
.PHONY: deps-update
deps-update-all:
@if [[ "`go version`" == *1.1[1-3]* ]]; then \
go get -d -mod= -u ./...; \
else \
go get -d -u ./...; \
fi; \
if [[ "`go env GOFLAGS`" =~ -mod=vendor ]]; then go mod vendor; fi
.PHONY: deps-update-all
go-fmt:
@if command -v goimports > /dev/null; then \
goimports -local $(LOCAL) -ungroup -w $(PATHS); \
else \
gofmt -s -w $(PATHS); \
fi
.PHONY: go-fmt
go-generate:
@go generate $(PACKAGES)
.PHONY: go-generate
lint:
@golangci-lint run ./...
@looppointer ./...
.PHONY: lint
test:
@go test -race -timeout $(TIMEOUT) $(PACKAGES)
.PHONY: test
test-clean:
@go clean -testcache
.PHONY: test-clean
test-with-coverage:
@go test -cover -timeout $(TIMEOUT) $(PACKAGES) | column -t | sort -r
.PHONY: test-with-coverage
test-with-coverage-profile:
@go test -cover -covermode count -coverprofile c.out -timeout $(TIMEOUT) $(PACKAGES)
.PHONY: test-with-coverage-profile
test-with-coverage-report: test-with-coverage-profile
@go tool cover -html c.out
.PHONY: test-with-coverage-report
BINARY = $(BINPATH)/$(shell basename $(MAIN))
BINPATH = $(PWD)/bin/$(OS)/$(ARCH)
COMMIT = $(shell git rev-parse --verify HEAD)
DATE = $(shell date +%Y-%m-%dT%T%Z)
LDFLAGS = -ldflags "-s -w -X main.commit=$(COMMIT) -X main.date=$(DATE)"
MAIN = $(MODULE)
export GOBIN := $(BINPATH)
export PATH := $(BINPATH):$(PATH)
build-env:
@echo "BINARY: $(BINARY)"
@echo "BINPATH: $(BINPATH)"
@echo "COMMIT: $(COMMIT)"
@echo "DATE: $(DATE)"
@echo "GOBIN: `go env GOBIN`"
@echo "LDFLAGS: $(LDFLAGS)"
@echo "MAIN: $(MAIN)"
@echo "PATH: $$PATH"
.PHONY: build-env
build:
@go build -o $(BINARY) $(LDFLAGS) $(MAIN)
.PHONY: build
build-clean:
@rm -f $(BINARY)
.PHONY: build-clean
install:
@go install $(LDFLAGS) $(MAIN)
.PHONY: install
install-clean:
@go clean -cache
.PHONY: install-clean
dist-check:
@goreleaser --snapshot --skip-publish --rm-dist
.PHONY: dist-check
dist-dump:
@godownloader .goreleaser.yml > bin/install
.PHONY: dist-dump
TOOLFLAGS = -mod=
tools-env:
@echo "GOBIN: `go env GOBIN`"
@echo "TOOLFLAGS: $(TOOLFLAGS)"
.PHONY: tools-env
toolset:
@( \
GOFLAGS=$(TOOLFLAGS); \
cd tools; \
go mod tidy; \
go mod download; \
if [[ "`go env GOFLAGS`" =~ -mod=vendor ]]; then go mod vendor; fi; \
go generate tools.go; \
)
.PHONY: toolset
ifdef GIT_HOOKS
hooks: unhook
@for hook in $(GIT_HOOKS); do cp githooks/$$hook .git/hooks/; done
.PHONY: hooks
unhook:
@ls .git/hooks | grep -v .sample | sed 's|.*|.git/hooks/&|' | xargs rm -f || true
.PHONY: unhook
define hook_tpl
$(1):
@githooks/$(1)
.PHONY: $(1)
endef
render_hook_tpl = $(eval $(call hook_tpl,$(hook)))
$(foreach hook,$(GIT_HOOKS),$(render_hook_tpl))
endif
ifdef GO_VERSIONS
define go_tpl
go$(1):
@docker run \
--rm -it \
-v $(PWD):/src \
-w /src \
golang:$(1) bash
.PHONY: go$(1)
endef
render_go_tpl = $(eval $(call go_tpl,$(version)))
$(foreach version,$(GO_VERSIONS),$(render_go_tpl))
endif
init: deps test lint hooks
@git config core.autocrlf input
.PHONY: init
clean: build-clean deps-clean install-clean test-clean
.PHONY: clean
deps: deps-fetch toolset
.PHONY: deps
env: go-env build-env tools-env
.PHONY: env
format: go-fmt
.PHONY: format
generate: go-generate format
.PHONY: generate
refresh: deps-tidy update deps generate format test build
.PHONY: refresh
update: deps-update
.PHONY: update