use of org.ballerinalang.net.grpc.exception.BalGenerationException in project ballerina by ballerina-lang.
the class BallerinaFileBuilder method build.
public void build() {
try {
InputStream targetStream = new ByteArrayInputStream(rootDescriptor);
DescriptorProtos.FileDescriptorProto fileDescriptorSet = DescriptorProtos.FileDescriptorProto.parseFrom(targetStream);
List<DescriptorProtos.DescriptorProto> messageTypeList = fileDescriptorSet.getMessageTypeList();
List<DescriptorProtos.MethodDescriptorProto> methodList = fileDescriptorSet.getService(SERVICE_INDEX).getMethodList();
String methodName;
String reqMessageName;
String resMessageName;
String methodID;
String packageName = EMPTY_STRING.equals(fileDescriptorSet.getPackage()) ? BalGenConstants.DEFAULT_PACKAGE : fileDescriptorSet.getPackage() + PACKAGE_SEPARATOR + BalGenConstants.DEFAULT_PACKAGE;
ClientBuilder clientStubBal = new ClientBuilder(packageName, fileDescriptorSet.getService(SERVICE_INDEX).getName());
DescriptorBuilder descriptorBuilder;
String protoPackage = fileDescriptorSet.getPackage();
if (EMPTY_STRING.equals(protoPackage)) {
descriptorBuilder = new DescriptorBuilder(dependentDescriptors, fileDescriptorSet.getName(), clientStubBal);
} else {
descriptorBuilder = new DescriptorBuilder(dependentDescriptors, fileDescriptorSet.getPackage() + "." + fileDescriptorSet.getName(), clientStubBal);
}
descriptorBuilder.setRootDescriptor(rootDescriptor);
descriptorBuilder.buildMap();
descriptorBuilder.buildKey();
for (DescriptorProtos.MethodDescriptorProto methodDescriptorProto : methodList) {
MethodDescriptor.MethodType methodType = MessageUtils.getMethodType(methodDescriptorProto);
String[] outputTypes = methodDescriptorProto.getOutputType().split(PACKAGE_SEPARATOR_REGEX);
String typeOut = outputTypes[outputTypes.length - 1];
String[] inputTypes = methodDescriptorProto.getInputType().split(PACKAGE_SEPARATOR_REGEX);
String typeIn = inputTypes[inputTypes.length - 1];
methodName = methodDescriptorProto.getName();
if (!EMPTY_STRING.equals(fileDescriptorSet.getPackage())) {
methodID = fileDescriptorSet.getPackage() + PACKAGE_SEPARATOR + fileDescriptorSet.getService(SERVICE_INDEX).getName() + FILE_SEPARATOR + methodName;
} else {
methodID = fileDescriptorSet.getService(SERVICE_INDEX).getName() + FILE_SEPARATOR + methodName;
}
reqMessageName = getMappingBalType(typeIn);
resMessageName = getMappingBalType(typeOut);
ActionBuilder.build(methodName, reqMessageName, resMessageName, methodID, methodType, clientStubBal);
}
for (DescriptorProtos.DescriptorProto descriptorProto : messageTypeList) {
String[] attributesNameArr = new String[descriptorProto.getFieldCount()];
String[] attributesTypeArr = new String[descriptorProto.getFieldCount()];
int j = 0;
for (DescriptorProtos.FieldDescriptorProto fieldDescriptorProto : descriptorProto.getFieldList()) {
attributesNameArr[j] = fieldDescriptorProto.getName();
attributesTypeArr[j] = !fieldDescriptorProto.getTypeName().equals("") ? fieldDescriptorProto.getTypeName().split(PACKAGE_SEPARATOR_REGEX)[fieldDescriptorProto.getTypeName().split(PACKAGE_SEPARATOR_REGEX).length - 1] : getTypeName(fieldDescriptorProto.getType().getNumber());
j++;
}
clientStubBal.addStruct(descriptorProto.getName(), attributesNameArr, attributesTypeArr);
}
StubBuilder.build(clientStubBal, clientStubBal.isFunctionsUnaryNotEmpty());
ClientStruct sampleClient = new ClientStruct(clientStubBal.isFunctionsStremingNotEmpty(), clientStubBal.isFunctionsUnaryNotEmpty(), fileDescriptorSet.getService(SERVICE_INDEX).getName(), packageName);
if (this.balOutPath == null) {
String path = balOutPathGenerator(packageName + PACKAGE_SEPARATOR + fileDescriptorSet.getService(SERVICE_INDEX).getName());
writeBallerina(clientStubBal, DEFAULT_SKELETON_DIR, SKELETON_TEMPLATE_NAME, path + STUB_FILE_PRIFIX);
writeBallerina(sampleClient, DEFAULT_SAMPLE_DIR, SAMPLE_TEMPLATE_NAME, path + SAMPLE_FILE_PRIFIX);
} else {
String path = this.balOutPath + FILE_SEPARATOR + fileDescriptorSet.getService(SERVICE_INDEX).getName();
writeBallerina(clientStubBal, DEFAULT_SKELETON_DIR, SKELETON_TEMPLATE_NAME, path + STUB_FILE_PRIFIX);
File sampleFile = new File(path + SAMPLE_FILE_PRIFIX);
if (!sampleFile.isFile()) {
Files.createFile(Paths.get(sampleFile.getAbsolutePath()));
}
writeBallerina(sampleClient, DEFAULT_SAMPLE_DIR, SAMPLE_TEMPLATE_NAME, path + SAMPLE_FILE_PRIFIX);
}
} catch (IOException e) {
throw new BalGenerationException("Error while generating .bal file.", e);
}
}
use of org.ballerinalang.net.grpc.exception.BalGenerationException in project ballerina by ballerina-lang.
the class GrpcCmd method execute.
@Override
public void execute() {
File protoFile = new File(protoPath);
if (!protoFile.isFile() || !protoFile.exists() || EMPTY_STRING.equals(protoPath) || !protoPath.contains(".proto")) {
String errorMessage = "Invalid proto file location. Please input valid profile location.";
errStream.println(errorMessage);
throw new BalGenToolException(errorMessage);
}
if (helpFlag) {
String commandUsageInfo = BLauncherCmd.getCommandUsageInfo(parentCmdParser, "build");
outStream.println(commandUsageInfo);
return;
}
try {
downloadProtocexe();
} catch (BalGenToolException e) {
LOG.error("Error while generating protoc executable. ", e);
throw new BalGenToolException("Error while generating protoc executable. ", e);
}
ClassLoader classLoader = this.getClass().getClassLoader();
String descriptorPath = BalGenerationConstants.META_LOCATION + getProtoFileName() + "-descriptor.desc";
File descFile = generateDescTempFolder(descriptorPath);
StringBuilder msg = new StringBuilder();
LOG.debug("Initializing the ballerina code generation.");
List<String> protoFiles = readProperties(classLoader);
for (String file : protoFiles) {
try {
exportResource(file, classLoader);
} catch (Exception e) {
msg.append("Error extracting resource file ").append(file).append(NEW_LINE_CHARACTER);
outStream.println(msg.toString());
LOG.error("Error exacting resource file " + file, e);
}
}
msg.append("Successfully generated initial files.").append(NEW_LINE_CHARACTER);
byte[] root = BalFileGenerationUtils.getProtoByteArray(this.exePath, this.protoPath, descFile.getAbsolutePath());
LOG.debug("Successfully generated root descriptor.");
List<byte[]> dependant;
dependant = org.ballerinalang.protobuf.cmd.DescriptorsGenerator.generatedependentDescriptor(descriptorPath, this.protoPath, new ArrayList<>(), exePath, classLoader);
LOG.debug("Successfully generated dependent descriptor.");
// Path balPath = Paths.get(balOutPath);
try {
BallerinaFileBuilder ballerinaFileBuilder;
// By this user can generate stub at different location
if (EMPTY_STRING.equals(balOutPath)) {
ballerinaFileBuilder = new BallerinaFileBuilder(dependant);
} else {
ballerinaFileBuilder = new BallerinaFileBuilder(dependant, balOutPath);
}
ballerinaFileBuilder.setRootDescriptor(root);
ballerinaFileBuilder.build();
} catch (BalGenerationException e) {
LOG.error("Error generating ballerina file.", e);
msg.append("Error generating ballerina file.").append(NEW_LINE_CHARACTER);
outStream.println(msg.toString());
}
msg.append("Successfully generated ballerina file.").append(NEW_LINE_CHARACTER);
// delete temporary meta files
delete(new File("desc_gen"));
delete(new File("google"));
LOG.debug("Successfully deleted temporary files.");
outStream.println(msg.toString());
}
Aggregations