-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
147 lines (126 loc) · 4.42 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
GOPATH:=$(shell go env GOPATH)
# 从命令行输入的路径变量
PROTO_PATH := $(shell read -p "Enter proto file path: " path; echo $$path)
# 提取目录路径
DIR_PATH := $(dir $(PROTO_PATH))
# 删除第一个路径和最后一个文件名
PATH_WITHOUT_PROJECT = $(subst $(word 1,$(subst /, ,$(DIR_PATH)))/,,$(DIR_PATH))
# 使用basename函数获取文件名
FILE_NAME := $(basename $(notdir $(PROTO_PATH)))
UPPER_FILE_NAME := $(shell echo $(FILE_NAME) | awk '{print toupper(substr($$0, 1, 1)) substr($$0, 2)}')
name:
@echo $(PATH_WITHOUT_PROJECT)
@echo $(DIR_PATH)
@echo $(FILE_NAME)
@echo $(UPPER_FILE_NAME)
API_Path := "api"
APP_PATH := "app"
api: add grpc http swagger errors proto server
gen: add project grpc http errors swagger proto server replace
.PHONY: init
# init env
init:
echo $(GOPATH)
cd $(GOPATH)/bin; \
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
go install github.com/go-kratos/kratos/cmd/kratos/v2@latest
go install github.com/go-kratos/kratos/cmd/protoc-gen-go-http/v2@latest
go install github.com/google/gnostic/cmd/protoc-gen-openapi@latest
go install github.com/google/wire/cmd/wire@latest
go install github.com/envoyproxy/protoc-gen-validate@latest
go install github.com/limes-cloud/kratosx/cmd/protoc-gen-go-errors@latest
.PHONY: add
# add proto
add:
if [ -d $(PROTO_PATH) ]; then exit 0; fi; \
kratos proto add $(PROTO_PATH)
mkdir -p $(DIR_PATH)/gen
.PHONY: project
# 假设您要在命令行中指定路径
project:
#创建项目
if [ ! -d $(APP_PATH) ]; then mkdir $(APP_PATH); fi; \
cd $(APP_PATH); \
if [ -d $(PATH_WITHOUT_PROJECT) ]; then exit 0; fi; \
kratos new $(PATH_WITHOUT_PROJECT) & pid=$$!; \
echo "Waiting for 10 seconds..."; \
sleep 10; \
wait $$pid; \
cd $(PATH_WITHOUT_PROJECT); \
rm -rf api third_party .github LiCENSE README.md go.mod go.sum \
internal/biz/greeter.go internal/data/greeter.go internal/service/greeter.go; \
touch README.md
.PHONY: grpc
# generate grpc code
grpc:
cd $(DIR_PATH) && protoc --proto_path=. \
--proto_path=../../../../third_party \
--go_out=paths=source_relative:./gen \
--go-grpc_out=paths=source_relative:./gen \
./$(FILE_NAME).proto
.PHONY: http
# generate http code
http:
cd $(DIR_PATH) && protoc --proto_path=. \
--proto_path=../../../../third_party \
--go_out=paths=source_relative:./gen \
--go-http_out=paths=source_relative:./gen \
./$(FILE_NAME).proto
.PHONY: errors
# generate errors code
errors:
cd $(DIR_PATH) && protoc --proto_path=. \
--proto_path=../../../../third_party \
--go_out=paths=source_relative:./gen \
--go-errors_out=paths=source_relative:./gen \
./$(FILE_NAME).proto
.PHONY: swagger
# generate swagger
swagger:
-cd $(DIR_PATH) && protoc --proto_path=. \
--proto_path=../../../../third_party \
--openapiv2_out ./gen \
--openapiv2_opt logtostderr=true \
./$(FILE_NAME).proto
.PHONY: proto
# generate internal proto struct
proto:
-cd $(DIR_PATH) && protoc --proto_path=. \
--proto_path=../../../../third_party \
--go_out=paths=source_relative:./gen \
./$(FILE_NAME).proto
.PHONY: server
server:
kratos proto server $(PROTO_PATH) -t $(APP_PATH)/$(PATH_WITHOUT_PROJECT)"internal/service"
UNAME_S := $(shell uname -s)
# 替换文件内容的目标
replace:
@echo "Processing proto file: $(PROTO_PATH)"
@echo "Path without project: $(PATH_WITHOUT_PROJECT)"
@cd $(APP_PATH) && \
LAST_FOLDER=$$(basename $(PATH_WITHOUT_PROJECT)) && \
cd $$(dirname $(PATH_WITHOUT_PROJECT)) && \
echo "Running find in: $$(pwd)/$$LAST_FOLDER" && \
if [ "$(UNAME_S)" = "Darwin" ]; then \
find $$LAST_FOLDER -type f -exec sed -i '' 's/Greeter/$(UPPER_FILE_NAME)/g' {} + && \
find $$LAST_FOLDER -type f -exec sed -i '' 's/greeter/$(FILE_NAME)/g' {} + ; \
else \
find $$LAST_FOLDER -type f -exec sh -c '\
sed -i "" -e "s/Greeter/$(UPPER_FILE_NAME)/g" $$0 \
' {} \; && \
find $$LAST_FOLDER -type f -exec sh -c '\
sed -i "" -e "s/greeter/$(FILE_NAME)/g" $$0 \
' {} \; ; \
fi
.PHONY: build
# build
build:
mkdir -p bin/ && go build -ldflags "-X main.Version=$(VERSION)" -o ./bin/ ./...
.PHONY: config
# generate internal proto
config:
-cd $(DIR_PATH) && protoc --proto_path=. \
--proto_path=../../../../../../third_party \
--go_out=paths=source_relative:. \
$(PROTO_PATH)