代码拉取完成,页面将自动刷新
同步操作将从 Plato/rpc-repo 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os
import util
import subprocess
import shutil
import platform
import json
from driver import Initializer
from driver import Builder
from plato_template import Template
from xml.dom.minidom import parse
import xml.dom.minidom
class Initializer_java(Initializer):
def __init__(self, options, version, proj_root):
Initializer.__init__(self, options, version, proj_root)
self.base_package=''
def check(self):
print("Start check java rpc idl environment")
if not os.path.samefile(self.proj_root, os.getcwd()):
with open('%s/.gitignore'%(self.proj_root), 'a+') as f:
f.write('/tmp/')
f.write('/pub/')
f.write('/src/')
f.write('/framework/')
if not os.path.exists(self.proj_root+'/framework'):
self.installFramework()
if not os.path.exists("repo.init.java"):
return self.init()
else:
return True
def build_backend(self):
pass
# 初始化java 相关依赖,初始化
def init(self):
if os.path.exists("repo.init.java"):
return True
print("Start init java rpc idl environment")
if not Initializer.checkPrecondition(self):
return False
# 检查git 环境
if not util.check_git_env():
return False
# 检查pb 环境
if not util.check_pb_env():
return False
# 检查java环境
if not util.check_java_env():
return False
# 检查jdk环境
if not util.check_maven_env():
return False
open("repo.init.java", "w+").close()
return True
# 安装对应的service box framework
def installFramework(self):
print("Start call java installFramework")
# 环境检查
def checkEnv(self):
print("Start call checkEnv")
# @title java idl to class file
class Builder_java(Builder):
def __index__(self, options, version, proj_root):
#self.options = options
Builder.__init__(self, options, version, proj_root)
def deleteIdl(self, name):
print("命令已废弃,请使用 -a 命令")
def updateIdl(self, name, service_name=None):
print("命令已废弃,请使用 -a 命令")
def build_idl(self, name, sname=None, build_type="all", make_service_type="all", version=""):
working_path = os.getcwd()
src_path = working_path+'/src/java/'+name
os.chdir(src_path)
print('change dir to '+src_path)
print("executing [mvn clean install]")
subprocess.call(["mvn", "clean", "install"], shell=True)
print("install maven project success")
DOMTree = xml.dom.minidom.parse(src_path+"/pom.xml")
Data = DOMTree.documentElement
groupId = Data.getElementsByTagName("groupId")[0].childNodes[0].data
artifactId = Data.getElementsByTagName("artifactId")[0].childNodes[0].data
version = Data.getElementsByTagName("version")[0].childNodes[0].data
print('''
<dependency>
<groupId>%s</groupId>
<artifactId>%s</artifactId>
<version>%s</version>
</dependency>'''% (groupId, artifactId,version))
os.chdir('../../')
def addIdl2Repo(self, file_name, options=None):
if options['package'] is None:
print('ERROR: java add rule [--package option must set before --add operation.]')
return
self.base_package = options['package']
working_path = os.getcwd()
self.working_path = working_path
src_path = '%s/src'%(self.proj_root)
file_base_name = os.path.basename(file_name).split('.')[0]
self.file_base_name = file_base_name
print(file_name,file_base_name, working_path, self.proj_root)
# 处理idl 路径问题
add = True
if not add:
file_name = "src/idl/" + file_name
file_name = self.check_idl_name(file_name)
if not os.path.exists(file_name):
print("file: " + file_name + " not found")
return False
# 递归处理idl import 依赖文件
idl_list = util.deal_idl_import(file_name)
# 递归拷贝所有idl依赖文件
# 拷贝idl并生成对应的文件和文件夹 src/idl 目录下 保持原有目录一致
if add:
util.copy_dep_idl(os.path.dirname(file_name), idl_list, 'src/idl')
shutil.copyfile(file_name, './src/idl/' + os.path.basename(file_name))
os.chdir('src/idl')
# 生成pb对应的json
cmd = subprocess.Popen(["../../bin/rpc-frontend", "-t", "protobuf", "-f", os.path.basename(file_name)],
stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding='utf-8')
ret = cmd.communicate()
if cmd.returncode != 0:
print("".join(ret))
return False
print("protobuf.json file generated.")
#生成protoc文件
cmd = subprocess.Popen([self.pyexec, working_path+"/javagen_pb_layer.py",
os.path.basename(file_name) + ".protobuf.json" ,self.base_package],
stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding='utf-8')
ret = cmd.communicate()
if cmd.returncode != 0:
print("".join(ret))
return False
print("message.proto file generated.")
# 生成idl对应的maven仓库目录结构
if not os.path.exists('../java'):
os.mkdir('../java')
java_proj_dir= '../java/'+file_base_name
java_code_dir= java_proj_dir+'/src/main/java'
self.java_code_dir = java_code_dir
if not os.path.exists(java_code_dir):
os.makedirs(java_code_dir)
resource_dir= java_proj_dir+'/src/main/resources'
if not os.path.exists(resource_dir):
os.makedirs(resource_dir)
print("java maven project dir created.")
#copy 代码到maven resources目录,idl,json,proto
proto_message_file_name = file_base_name+'.message.proto'
proto_json_file_name = file_base_name+'.idl.protobuf.json'
proto_file_path = os.path.join(resource_dir, proto_message_file_name)
shutil.copyfile(file_base_name+'.idl', os.path.join(resource_dir, file_base_name+'.idl'))
shutil.copyfile(proto_json_file_name, os.path.join(resource_dir, proto_json_file_name))
shutil.copyfile(proto_message_file_name, proto_file_path)
print("copy [idl,proto.json,.proto] to maven resources.")
#生成pom.xml文件
pom_templ = open(working_path+'/template/java/pom.xml.template', encoding='utf-8').read()
t = Template(pom_templ)
pom_xml_file = open(java_proj_dir+'/pom.xml','w', encoding='utf-8')
pom_xml_str = t.render({'base_package':self.base_package,
'idl_name':file_base_name+'-idl',
'version':'1.0.0-SNAPSHOT'})
pom_xml_file.write(pom_xml_str)
pom_xml_file.close()
print("maven pom.xml generated.")
#生成proto对应的java class
cmd = subprocess.Popen(["protoc.exe", "--java_out="+java_code_dir, proto_message_file_name],
stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding='utf-8')
self.messageClsName = file_base_name.capitalize()+ 'Message'
ret = cmd.communicate()
if cmd.returncode != 0:
print("".join(ret))
return False
print("proto java message class generated.")
#生成java intreface
pb_json = json.loads(open(proto_json_file_name).read())
## 1.转换javaType
## 2.generate service
for service in pb_json["services"]:
for method in service['methods']:
self.addJavaType(method)
service['messageClsName'] = self.messageClsName
self.genInterface(service)
print("java interface classes generated.")
os.chdir('../../')
return True
def genInterface(self,service):
java_templ = Template(open(self.working_path+'/template/java/service.java.template', encoding='utf-8').read())
pck = self.base_package+'.'+self.file_base_name
java_file_path = self.java_code_dir+'/'+ pck.replace('.','/')+'/'+service['name']+'.java'
java_str = java_templ.render({'package_name':pck,'service':service})
java_file = open(java_file_path,'w', encoding='utf-8')
java_file.write(java_str)
java_file.close()
def addJavaType(self,methodObj):
methodObj['retType']['javaType'] = self.converType(methodObj['retType'])
for argument in methodObj['arguments']:
argument['javaType'] = self.converType(argument)
def converType(self,typeObj):
messageClsName = self.messageClsName
if typeObj['type']=='':
return 'void'
elif 'isEnum' in typeObj:
return messageClsName +'.' + typeObj['type']
elif 'isStruct' in typeObj:
return messageClsName +'.' + typeObj['type']
elif typeObj['type']=='string':
return 'String'
elif typeObj['type']=='int32' or typeObj['type']=='uint32':
return 'Integer'
elif typeObj['type']=='double':
return 'Double'
elif typeObj['type']=='float':
return 'Float'
elif typeObj['type']=='bool':
return 'Boolean'
elif typeObj['type']=='uint64' or typeObj['type']=='int64':
return 'Long'
elif typeObj['type']=='repeated':
return 'List<'+self.converType(typeObj['key'])+'>'
elif typeObj['type']=='map':
return 'Map<'+self.converType(typeObj['key'])+','+self.converType(typeObj['value'])+'>'
return ''
def updateRoot(self):
print("java no implement")
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。