refactor(proto-structure): update shell

This commit is contained in:
bandl 2021-09-21 19:37:53 +08:00
parent 6eec180306
commit e3acfcd0fe
2 changed files with 21 additions and 19 deletions

View File

@ -3,7 +3,9 @@ STORAGE_PATH = $(BASE_PATH)/storage
BASE_OUT = $(BASE_PATH)/bin BASE_OUT = $(BASE_PATH)/bin
dcgen: dcgen:
@make gen-protobuf
@python3 ./shell/proto.py @python3 ./shell/proto.py
@make gen-struct
.PHONY : build .PHONY : build
build: build:

View File

@ -1,6 +1,5 @@
import os import os
from typing import Dict, List
from jinja2 import Template
import yaml import yaml
sysPath = os.getcwd() sysPath = os.getcwd()
@ -8,9 +7,11 @@ tempPath = f"{sysPath}/pkg/structure/generate"
structurePath = f"{sysPath}/pkg/structure" structurePath = f"{sysPath}/pkg/structure"
protobufPath = f"{sysPath}/protobuf" protobufPath = f"{sysPath}/protobuf"
def to_camel(val: str) -> str: def to_camel(val: str) -> str:
return "".join([k.capitalize() for k in val.split('_')]) return "".join([k.capitalize() for k in val.split('_')])
def load_conf(): def load_conf():
conf_path = f"{tempPath}/tem.yaml" conf_path = f"{tempPath}/tem.yaml"
with open(conf_path, 'r', encoding='utf-8') as f: with open(conf_path, 'r', encoding='utf-8') as f:
@ -28,34 +29,36 @@ def load_conf():
'''生成对应的数据结构proto文件''' '''生成对应的数据结构proto文件'''
def mk_structure(cfg_camel): def mk_structure(cfg_camel):
path = "protobuf" path = "protobuf"
storagePath = f"{protobufPath}/storage.proto" storagePath = f"{protobufPath}/storage.proto"
for key, value in cfg_camel.items(): for key, value in cfg_camel.items():
proto_path = path + '/' + key + '.proto' proto_path = path + '/' + key + '.proto'
if not os.path.exists(proto_path): # 如果这个文件不存在 if not os.path.exists(proto_path): # 如果这个文件不存在
'''生成对应的数据结构proto文件''' '''生成对应的数据结构proto文件'''
file = open(proto_path,'w') file = open(proto_path, 'w')
file.write('syntax = "proto3";\nimport "base.proto";\noption go_package = "pkg/proto";\n') file.write('syntax = "proto3";\nimport "base.proto";\noption go_package = "pkg/proto";\n')
for v in value: for v in value:
file.write('\nmessage ' + v +'Request '+'{\n BaseKey key = 1;\n}') file.write('\nmessage ' + v + 'Request ' + '{\n BaseKey key = 1;\n}\n')
file.close() file.close()
else: # 如果这个文件存在 else: # 如果这个文件存在
'''更新数据结构proto文件''' '''更新数据结构proto文件'''
with open(proto_path) as f: with open(proto_path) as f:
line = f.readlines() line = f.readlines()
f.close() f.close()
for v in value: for v in value:
function = v + 'Request' function = v + 'Request'
flag = 0 flag = 0
for l in line: for l in line:
if function in l: if function in l:
flag=1 flag = 1
break break
if flag == 0: if flag == 0:
file = open(proto_path,'a') file = open(proto_path, 'a')
file.write('\nmessage ' + v +'Request '+'{\n BaseKey key = 1;\n}') file.write('\nmessage ' + v + 'Request ' + '{\n BaseKey key = 1;\n}\n')
file.close() file.close()
flag = 0 flag = 0
@ -64,14 +67,15 @@ def mk_structure(cfg_camel):
def mk_storage(cfg_camel): def mk_storage(cfg_camel):
storagePath = f"{protobufPath}/storage.proto" storagePath = f"{protobufPath}/storage.proto"
file = open(storagePath,'w') file = open(storagePath, 'w')
file.write('// Code generated by gen-struct. DO NOT EDIT.\n// make gen-protobuf generated\n\nsyntax = "proto3";\n\noption go_package = "pkg/proto";\n\n') file.write(
for key,value in cfg_camel.items(): '// Code generated by gen-struct. DO NOT EDIT.\n// make gen-protobuf generated\n\nsyntax = "proto3";\n\noption go_package = "pkg/proto";\n\n')
file.write('import "'+ key +'.proto";\n') for key, value in cfg_camel.items():
file.write('import "' + key + '.proto";\n')
file.write('\nmessage CommendResponse {\n repeated string result = 1;\n}\n\nservice CommServer {\n') file.write('\nmessage CommendResponse {\n repeated string result = 1;\n}\n\nservice CommServer {\n')
for key,value in cfg_camel.items(): for key, value in cfg_camel.items():
for v in value: for v in value:
file.write(' rpc '+ v +' ('+ v +'Request) returns (CommendResponse);\n') file.write(' rpc ' + v + ' (' + v + 'Request) returns (CommendResponse);\n')
file.write('}') file.write('}')
file.close() file.close()
print("storage.proto", "-> success") print("storage.proto", "-> success")
@ -81,7 +85,3 @@ if __name__ == "__main__":
conf, cfg_camel = load_conf() conf, cfg_camel = load_conf()
mk_structure(cfg_camel) # 生成对应的数据结构proto文件 mk_structure(cfg_camel) # 生成对应的数据结构proto文件
mk_storage(cfg_camel) mk_storage(cfg_camel)