Search in sources :

Example 1 with BallerinaFileBuilder

use of org.ballerinalang.net.grpc.builder.BallerinaFileBuilder in project ballerina by ballerina-lang.

the class ServiceProtoUtils method writeServiceFiles.

/**
 * Write protobuf file definition content to the filename.
 *
 * @param protoFileDefinition protobuf file definition.
 * @param filename            filename
 * @throws GrpcServerException when error occur while writing content to file.
 */
static void writeServiceFiles(File protoFileDefinition, String filename, boolean generateClientConnector) throws GrpcServerException {
    try {
        // write the proto string to the file in protobuf contract directory
        Path protoFilePath = Paths.get(filename + ServiceProtoConstants.PROTO_FILE_EXTENSION);
        Files.write(protoFilePath, protoFileDefinition.getFileDefinition().getBytes(ServiceProtoConstants.UTF_8_CHARSET));
        // write the proto descriptor byte array to the file in protobuf contract directory
        byte[] fileDescriptor = protoFileDefinition.getFileDescriptorProto().toByteArray();
        Path descFilePath = Paths.get(filename + ServiceProtoConstants.DESC_FILE_EXTENSION);
        Files.write(descFilePath, fileDescriptor);
        if (generateClientConnector) {
            List<byte[]> dependentDescriptorsList = new ArrayList<>();
            dependentDescriptorsList.add(com.google.protobuf.WrappersProto.getDescriptor().toProto().toByteArray());
            // Path path = Paths.get("");
            BallerinaFileBuilder ballerinaFileBuilder = new BallerinaFileBuilder(dependentDescriptorsList);
            ballerinaFileBuilder.setRootDescriptor(fileDescriptor);
            ballerinaFileBuilder.build();
        }
    } catch (IOException e) {
        throw new GrpcServerException("Error while writing file descriptor to file.", e);
    }
}
Also used : Path(java.nio.file.Path) BallerinaFileBuilder(org.ballerinalang.net.grpc.builder.BallerinaFileBuilder) ArrayList(java.util.ArrayList) IOException(java.io.IOException) GrpcServerException(org.ballerinalang.net.grpc.exception.GrpcServerException)

Example 2 with BallerinaFileBuilder

use of org.ballerinalang.net.grpc.builder.BallerinaFileBuilder 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());
}
Also used : BallerinaFileBuilder(org.ballerinalang.net.grpc.builder.BallerinaFileBuilder) ArrayList(java.util.ArrayList) BalGenerationException(org.ballerinalang.net.grpc.exception.BalGenerationException) File(java.io.File) BalFileGenerationUtils.saveFile(org.ballerinalang.protobuf.utils.BalFileGenerationUtils.saveFile) BalGenerationException(org.ballerinalang.net.grpc.exception.BalGenerationException) BalGenToolException(org.ballerinalang.protobuf.exception.BalGenToolException) IOException(java.io.IOException) BalGenToolException(org.ballerinalang.protobuf.exception.BalGenToolException)

Aggregations

IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 BallerinaFileBuilder (org.ballerinalang.net.grpc.builder.BallerinaFileBuilder)2 File (java.io.File)1 Path (java.nio.file.Path)1 BalGenerationException (org.ballerinalang.net.grpc.exception.BalGenerationException)1 GrpcServerException (org.ballerinalang.net.grpc.exception.GrpcServerException)1 BalGenToolException (org.ballerinalang.protobuf.exception.BalGenToolException)1 BalFileGenerationUtils.saveFile (org.ballerinalang.protobuf.utils.BalFileGenerationUtils.saveFile)1