-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_makefile
203 lines (172 loc) · 6.25 KB
/
app_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
CURRENT_PATH=$(shell pwd)
GOPATH:=$(shell go env GOPATH)
VERSION=$(shell git describe --tags --always)
DATE=$(shell date +%Y%m%d%H%M%S)
APP_RELATIVE_PATH?=$(shell a=`basename $$PWD` && cd .. && b=`basename $$PWD` && echo $$b/$$a)
INTERNAL_PROTO_FILES=$(shell find internal -name *.proto)
API_PROTO_FILES=$(shell cd ../../../api/$(APP_RELATIVE_PATH) && find . -name *.proto)
GLOBAL_HEADER_PROTO_PATH=../../../pkg/proto
GLOBAL_HEADER_PROTO_FILES=$(shell cd $(GLOBAL_HEADER_PROTO_PATH) && find . -name *.proto)
API_PROTO_HTTP_FILES=$(shell cd ../../../api/$(APP_RELATIVE_PATH) && find . -name `dirname $(APP_RELATIVE_PATH)`*.proto)
API_SWAGGER_DIR=$(basename $(shell cd ../../../api/$(APP_RELATIVE_PATH) && pwd))
API_SWAGGER_FILES=$(shell cd $(API_SWAGGER_DIR) && find . -name *.swagger.json)
KRATOS_VERSION=$(shell go mod graph |grep go-kratos/kratos/v2 |head -n 1 |awk -F '@' '{print $$2}')
KRATOS=$(GOPATH)/pkg/mod/github.com/go-kratos/kratos/v2@$(KRATOS_VERSION)
APP_NAME=$(shell echo $(APP_RELATIVE_PATH) | sed -En "s/\//-/p")
DOCKER_IMAGE=$(shell echo $(APP_NAME) |awk -F '@' '{print "go-kratos/beer-" $$0 ":0.1.0"}')
BASENAME=$(shell cd .. && b=`basename $$PWD` && echo $$b )
.PHONY: init
# init env
init:
go get -u google.golang.org/protobuf/cmd/protoc-gen-go
go get -u google.golang.org/grpc/cmd/protoc-gen-go-grpc
go get -u github.com/go-kratos/kratos/cmd/protoc-gen-go-http/v2
go get -u github.com/google/wire/cmd/wire
go get -u github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2
go get -u github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway
go get -u github.com/go-kratos/kratos/cmd/protoc-gen-go-errors/v2
go get -u github.com/envoyproxy/protoc-gen-validate
.PHONY: grpc
# generate grpc code ( eg. api/xxx/service ... v1/audit.proto)
grpc:
cd ../../../api/$(APP_RELATIVE_PATH) && protoc --proto_path=. \
--proto_path=../../../third_party \
--go_out=paths=source_relative:. \
--go-grpc_out=paths=source_relative:. \
$(API_PROTO_FILES)
.PHONY: http
# generate http code
http:
cd ../../../api/$(APP_RELATIVE_PATH) && protoc --proto_path=. \
--proto_path=../../../third_party \
--go_out=paths=source_relative:. \
--grpc-gateway_out=paths=source_relative:. \
--go-http_out=paths=source_relative:. \
$(API_PROTO_FILES)
.PHONY: errors
# generate errors code
errors:
cd ../../../api/$(APP_RELATIVE_PATH) && protoc --proto_path=. \
--proto_path=../../../third_party \
--go_out=paths=source_relative:. \
--go-errors_out=paths=source_relative:. \
$(API_PROTO_FILES)
.PHONY: swagger
# generate swagger
swagger:
cd ../../../api/$(APP_RELATIVE_PATH) && protoc --proto_path=. \
--proto_path=../../../third_party \
--openapiv2_out . \
--openapiv2_opt logtostderr=true \
$(API_PROTO_HTTP_FILES)
.PHONY: validate
# generate validate code
validate:
cd ../../../api/$(APP_RELATIVE_PATH) && protoc --proto_path=. \
--proto_path=../../../third_party \
--go_out=paths=source_relative:. \
--validate_out=paths=source_relative,lang=go:. \
$(API_PROTO_FILES)
.PHONY: proto
# generate internal proto struct ( internal/conf/conf.proto )
proto:
protoc --proto_path=. \
--proto_path=../../../third_party \
--go_out=paths=source_relative:. \
$(INTERNAL_PROTO_FILES)
.PHONY: conf
# generate internal conf struct
conf:
protoc --proto_path=. \
--proto_path=../../../third_party \
--go_out=paths=source_relative:. \
$(INTERNAL_PROTO_FILES)
.PHONY: generate
# generate client code
generate:
go generate ./...
.PHONY: build
# build
build: build_service build_worker
.PHONY: build_service
# build_service
build_service:
if [ -d ./cmd/service ]; \
then \
mkdir -p bin/ && go build -ldflags "-X main.Version=$(VERSION)-$(DATE)" -o ./bin/ ./cmd/service && \
cd ./bin/ && ls | grep "$(BASENAME)-service" | xargs -I {} rm -rf {} && \
ls | grep 'service' | xargs -I {} mv {} $(BASENAME)-service;\
fi; \
if [ -d ./cmd/interface ]; \
then \
mkdir -p bin/ && go build -ldflags "-X main.Version=$(VERSION)-$(DATE)" -o ./bin/ ./cmd/interface && \
cd ./bin/ && ls | grep "$(BASENAME)-interface" | xargs -I {} rm -rf {} && \
ls | grep 'interface' | xargs -I {} mv {} $(BASENAME)-interface;\
fi
.PHONY: build_worker
# build_worker
build_worker:
if [ -d ./cmd/worker ]; \
then \
mkdir -p bin/ && go build -ldflags "-X main.Version=$(VERSION)-$(DATE)" -o ./bin/ ./cmd/worker && \
cd ./bin/ && ls | grep "$(BASENAME)-worker" | xargs -I {} rm -rf {} && \
ls | grep 'worker' | xargs -I {} mv {} $(BASENAME)-worker;\
fi
.PHONY: test
# test
test:
go test -v ./... -cover
.PHONY: run
run:
cd cmd/* && go run .
.PHONY: ent
ent:
cd internal/data/ && ent generate ./ent/schema
.PHONY: docker
docker:
cd ../../.. && docker build -f deploy/build/Dockerfile --build-arg APP_RELATIVE_PATH=$(APP_RELATIVE_PATH) -t $(DOCKER_IMAGE) .
.PHONY: wire
# generate wire
wire: wire_service wire_worker
.PHONY: wire_service
# generate wire_service
wire_service:
if [[ -d cmd/service ]];then cd cmd/service && wire;fi
if [[ -d cmd/interface ]];then cd cmd/interface && wire;fi
.PHONY: wire_worker
# generate wire_worker
wire_worker:
if [[ -d cmd/worker ]];then cd cmd/worker && wire;fi
.PHONY: api
# generate api proto
api: grpc http swagger errors validate
.PHONY: all
# generate all
all: grpc http proto generate build
# 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 := help
.PHONY: swag
# Swagger UI
swag:
@echo $(API_SWAGGER_FILES)
@echo $(API_SWAGGER_DIR)/v1
@echo 'docker run -dp 80:8080 --name swagger-ui -e SWAGGER_JSON=/$(API_SWAGGER_FILES) -v $(API_SWAGGER_DIR)/v1:/v1 swaggerapi/swagger-ui'
.PHONY: service
# service
service:
cd ../../../api/$(APP_RELATIVE_PATH) && kratos proto server $(API_PROTO_FILES) -t $(CURRENT_PATH)/internal/service/