-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
217 lines (186 loc) · 5.71 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
SHELL := /bin/bash
BASEDIR = $(shell pwd)
# make GIT_TAG=v1.0.0 build
SERVICE_NAME?=relation-service
# build with version infos
versionDir = "github.com/go-eagle/eagle/pkg/version"
gitTag = $(shell if [ "`git describe --tags --abbrev=0 2>/dev/null`" != "" ];then git describe --tags --abbrev=0; else git log --pretty=format:'%h' -n 1; fi)
buildDate = $(shell TZ=Asia/Shanghai date +%FT%T%z)
gitCommit = $(shell git log --pretty=format:'%H' -n 1)
gitTreeState = $(shell if git status|grep -q 'clean';then echo clean; else echo dirty; fi)
ldflags="-w -X ${versionDir}.gitTag=${gitTag} -X ${versionDir}.buildDate=${buildDate} -X ${versionDir}.gitCommit=${gitCommit} -X ${versionDir}.gitTreeState=${gitTreeState}"
PROJECT_NAME := "github.com/go-microservice/relation-service"
PKG := "$(PROJECT_NAME)"
PKG_LIST := $(shell go list ${PKG}/... | grep -v /vendor/)
GO_FILES := $(shell find . -name '*.go' | grep -v /vendor/ | grep -v _test.go)
# proto
APP_RELATIVE_PATH=$(shell a=`basename $$PWD` && echo $$b)
API_PROTO_FILES=$(shell find api$(APP_RELATIVE_PATH) -name *.proto)
# init environment variables
export PATH := $(shell go env GOPATH)/bin:$(PATH)
export GOPATH := $(shell go env GOPATH)
export GO111MODULE := on
# make make all
.PHONY: all
all: lint test build
.PHONY: build
# make build, Build the binary file
build:
GOOS=linux GOARCH=amd64 go build -v -ldflags ${ldflags} -o bin/$(SERVICE_NAME) cmd/server/main.go cmd/server/wire_gen.go
.PHONY: run
# make run, run current project
run: wire
go run cmd/server/main.go cmd/server/wire_gen.go
.PHONY: wire
# make wire, generate wire_gen.go
wire:
cd cmd/server && wire
.PHONY: dep
# make dep Get the dependencies
dep:
@go mod tidy
.PHONY: fmt
# make fmt
fmt:
@gofmt -s -w .
.PHONY: golint
# make golint
golint:
@if ! which golint &>/dev/null; then \
echo "Installing golint"; \
go get -u golang.org/x/lint/golint; \
fi
@golint -set_exit_status ${PKG_LIST}
.PHONY: lint
# make lint
lint:
@if ! which golangci-lint &>/dev/null; then \
echo "Installing golangci-lint"; \
go install github.com/golangci/golangci-lint/cmd/[email protected]; \
fi
${GOPATH}/bin/golangci-lint run ./...
.PHONY: test
# make test
test: vet
@go test -short ${PKG_LIST}
.PHONY: vet
# make vet
vet:
go vet ./... | grep -v vendor;true
.PHONY: cover
# make cover
cover:
@go test -short -coverprofile=coverage.txt -covermode=atomic ${PKG_LIST}
.PHONY: view-cover
# make view-cover preview coverage
view-cover:
go tool cover -html=coverage.txt -o coverage.html
.PHONY: docker
# make docker 生成docker镜像, eg: make GIT_TAG=v1.0.0 docker
docker:
sh deploy/docker_image.sh $(GIT_TAG)
.PHONY: clean
# make clean
clean:
@-rm -vrf eagle
@-rm -vrf cover.out
@-rm -vrf coverage.txt
@go mod tidy
@echo "clean finished"
.PHONY: graph
# make graph 生成交互式的可视化Go程序调用图(会在浏览器自动打开)
graph:
@export GO111MODULE="on"
@if ! which go-callvis &>/dev/null; then \
echo "downloading go-callvis"; \
go get -u github.com/ofabry/go-callvis; \
fi
@echo "generating graph"
@go-callvis github.com/go-eagle/eagle
.PHONY: mockgen
# make mockgen gen mock file
mockgen:
@echo "downloading mockgen"
@go get github.com/golang/mock/mockgen
cd ./internal && for file in `egrep -rnl "type.*?interface" ./dao | grep -v "_test" `; do \
echo $$file ; \
cd .. && mockgen -destination="./internal/mock/$$file" -source="./internal/$$file" && cd ./internal ; \
done
.PHONY: init
# init env
init:
go install google.golang.org/protobuf/cmd/[email protected]
go install google.golang.org/grpc/cmd/[email protected]
go install github.com/google/gnostic@latest
go install github.com/google/gnostic/cmd/protoc-gen-openapi@latest
go install github.com/pseudomuto/protoc-gen-doc/cmd/protoc-gen-doc@latest
go install github.com/golang/mock/mockgen@latest
go install github.com/favadi/protoc-go-inject-tag@latest
.PHONY: proto
# generate proto struct only
proto:
protoc --proto_path=. \
--proto_path=./third_party \
--go_out=. --go_opt=paths=source_relative \
$(API_PROTO_FILES)
.PHONY: grpc
# generate grpc code
grpc:
protoc --proto_path=. \
--proto_path=./third_party \
--go_out=. --go_opt=paths=source_relative \
--go-grpc_out=. --go-grpc_opt=paths=source_relative \
$(API_PROTO_FILES)
.PHONY: http
# generate http code
http:
protoc --proto_path=. \
--proto_path=./third_party \
--go_out=. --go_opt=paths=source_relative \
--go-gin_out=. --go-gin_opt=paths=source_relative \
$(API_PROTO_FILES)
.PHONY: openapi
# generate openapi
openapi:
protoc --proto_path=. \
--proto_path=./third_party \
--openapi_out=. \
$(API_PROTO_FILES)
.PHONY: doc
# generate html or markdown doc for data type
doc:
protoc --proto_path=. \
--proto_path=./third_party \
--doc_out=./api \
--doc_opt=html,index.html \
$(API_PROTO_FILES)
.PHONY: docs
# gen swagger doc
docs:
@if ! which swag &>/dev/null; then \
echo "downloading swag"; \
go get -u github.com/swaggo/swag/cmd/swag; \
fi
@swag init
@mv docs/docs.go api/http
@mv docs/swagger.json api/http
@mv docs/swagger.yaml api/http
@echo "gen-docs done"
@echo "see docs by: http://localhost:8080/swagger/index.html"
# show help
help:
@echo ''
@echo 'Usage:'
@echo ' make [target]'
@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 + 2, RLENGTH); \
printf "\033[36m %-22s\033[0m %s\n", helpCommand,helpMessage; \
} \
} \
{ lastLine = $$0 }' $(MAKEFILE_LIST)
.DEFAULT_GOAL := all