-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
75 lines (64 loc) · 3.12 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
# 父目录的Makefile
# 定义变量
PROTOC = protoc
PYTHON = python3
TS_PROTO_PLUGIN = $(shell which protoc-gen-ts_proto)
# 默认目标文件夹和参数
PROTO_DIR ?= .
OUTPUT_DIR ?= ../api/v1
COMMON_OUT_DIR ?= ..
COMMON_PROTOS_DIR ?= ./common
GOOGLE_PROTOS_DIR ?= ./common
PARENT_DIR_NAME ?= $(notdir $(patsubst %/,%,$(dir $(CURDIR))))
# 文件列表
GO_PROTO_FILES = $(wildcard $(PROTO_DIR)/*.proto)
PY_PROTO_FILES = $(wildcard $(PROTO_DIR)/*.proto)
TS_PROTO_FILES = $(wildcard $(PROTO_DIR)/*.proto)
COMMON_FILES = $(wildcard $(COMMON_PROTOS_DIR)/begonia/api/v1/*.proto)
COMMON_PROTO_FILES = $(notdir $(COMMON_FILES))
# 通用参数
GO_ARGS = --go_out=$(OUTPUT_DIR) --go_opt=paths=source_relative \
--go-grpc_out=$(OUTPUT_DIR) --grpc-gateway_out=$(OUTPUT_DIR) \
--grpc-gateway_opt=paths=source_relative --go-grpc_opt=paths=source_relative
PY_ARGS = --python_out=$(OUTPUT_DIR) \
--pyi_out=$(OUTPUT_DIR) --grpc_python_out=$(OUTPUT_DIR)
TS_ARGS = --plugin="protoc-gen-ts=$(TS_PROTO_PLUGIN)" \
--ts_proto_opt=esModuleInterop=true --ts_proto_opt=paths=source_relative \
--ts_proto_opt=snakeToCamel=false --ts_proto_opt=oneof=unions \
--ts_proto_opt=Mgoogle/protobuf/field_mask.proto=../../google/protobuf/field_mask \
--ts_proto_opt=Mgoogle/protobuf/timestamp.proto=../../google/protobuf/timestamp \
--ts_proto_opt=Mgoogle/protobuf/any.proto=../../google/protobuf/any \
--ts_proto_opt=Mgoogle/protobuf/descriptor.proto=../../google/protobuf/descriptor \
--ts_proto_opt=Mgoogle/protobuf/struct.proto=../../google/protobuf/struct \
--ts_proto_opt=Mgoogle/protobuf/empty.proto=../../google/protobuf/empty \
--ts_proto_opt=Mgoogle/api/annotations.proto=../../google/api/annotations \
--ts_proto_opt=Mgoogle/api/http.proto=../../google/api/http \
--ts_proto_opt=Mgoogle/api/httpbody.proto=../../google/api/httpbody \
--ts_proto_out=$(OUTPUT_DIR)
common:
@mkdir -p $(OUTPUT_DIR)
.PHONY: make_dir generate go python ts common clean desc all
clean:
@echo $(COMMON_PROTO_FILES) $(COMMON_FILES) “公共文件”
@rm -rf $(COMMON_PROTO_FILES)
# 生成所有代码
generate: make_dir go python ts common clean desc all
# 生成 Go 代码
go: $(GO_PROTO_FILES) | common
$(PROTOC) -I=$(PROTO_DIR) -I=$(COMMON_PROTOS_DIR) -I=$(GOOGLE_PROTOS_DIR) $(GO_ARGS) $?
protoc-go-inject-tag -input="$(OUTPUT_DIR)/*.pb.go"
ts: $(TS_PROTO_FILES) | common
$(PROTOC) -I=$(PROTO_DIR) -I=./common $(TS_ARGS) $?
py: $(PY_PROTO_FILES) | common
$(PYTHON) -m grpc_tools.protoc -I=$(PROTO_DIR) -I=$(GOOGLE_PROTOS_DIR) $(PY_ARGS) $?
touch $(OUTPUT_DIR)/__init__.py
echo "#!/usr/bin/env python" > $(OUTPUT_DIR)/__init__.py
echo "Current path: $(shell pwd)"
sed -i '/from/!s/import \(.*\) as/from . import \1 as/g' $(OUTPUT_DIR)/$\*.py*
echo "Parent path: $(PARENT_DIR_NAME)"
sed -i 's/from begonia import \(.*\) as/from $(PARENT_DIR_NAME).api.begonia.v1 import \1 as/' $(OUTPUT_DIR)/$\*.py*
desc:
protoc --descriptor_set_out="api.bin" --include_imports --include_source_info --proto_path=./ --proto_path=./common ./*.proto
all: go ts py desc
@echo "All done"
@echo "Go to the api directory and run make all"