use of org.ballerinalang.protobuf.exception.BalGenToolException in project ballerina by ballerina-lang.
the class BalFileGenerationUtils method generateDescriptor.
/**
* Execute command and generate file descriptor.
*
* @param command protoc executor command.
*/
public static void generateDescriptor(String command) {
boolean isWindows = System.getProperty("os.name").toLowerCase(Locale.ENGLISH).startsWith("windows");
ProcessBuilder builder = new ProcessBuilder();
if (isWindows) {
builder.command("cmd.exe", "/c", "dir");
} else {
builder.command("sh", "-c", command);
}
builder.directory(new File(System.getProperty("user.home")));
Process process;
try {
process = builder.start();
} catch (IOException e) {
throw new BalGenToolException("Error in executing protoc command '" + command + "'.", e);
}
try {
process.waitFor();
} catch (InterruptedException e) {
throw new BalGenToolException("Process not successfully completed. Process is interrupted while" + " running the protoC executor.", e);
}
}
use of org.ballerinalang.protobuf.exception.BalGenToolException in project ballerina by ballerina-lang.
the class BalFileGenerationUtils method createMetaFolder.
/**
* Create meta folder for storing meta files which is used in intermediate processing.
*
* @param folderPath folder path which is needed to be created.
*/
public static void createMetaFolder(String folderPath) {
boolean isFileCreated = new File(folderPath).getParentFile().mkdirs();
if (!isFileCreated) {
LOG.debug("Meta folder did not create successfully '" + folderPath + "'");
}
byte[] dataBytes = new byte[0];
try {
Path file = Paths.get(folderPath);
Files.write(file, dataBytes);
} catch (IOException e) {
throw new BalGenToolException("Error creating .desc meta files.", e);
}
}
use of org.ballerinalang.protobuf.exception.BalGenToolException in project ballerina by ballerina-lang.
the class GrpcCmd method generateDescTempFolder.
/**
* Generate the meta folder which needed for intermediate processing.
*
* @param descriptorPath proto descriptor path
* @return Temporary Created meta file.
*/
private File generateDescTempFolder(String descriptorPath) {
File descFile = new File(descriptorPath);
String path = descFile.getAbsolutePath().substring(0, descFile.getAbsolutePath().lastIndexOf(BalGenerationConstants.FILE_SEPARATOR));
File folderPath = new File(path);
try {
if (!folderPath.exists() && !folderPath.mkdirs()) {
throw new IllegalStateException("Couldn't create dir: " + descFile);
}
descriptorPath = descFile.getAbsolutePath();
byte[] data = new byte[0];
Path file = Paths.get(descriptorPath);
Files.write(file, data);
} catch (IOException e) {
throw new BalGenToolException("Error creating " + descriptorPath + " file.", e);
}
File targetFile = new File(PLUGIN_PROTO_FILEPATH);
File parent1 = targetFile.getParentFile();
File parent2 = targetFile.getParentFile().getParentFile();
File parent3 = targetFile.getParentFile().getParentFile().getParentFile();
if (!parent1.exists() && !parent1.mkdirs()) {
throw new IllegalStateException("Couldn't create dir: " + parent1);
}
if (!parent2.exists() && !parent2.mkdirs()) {
throw new IllegalStateException("Couldn't create dir: " + parent2);
}
if (!parent3.exists() && !parent3.mkdirs()) {
throw new IllegalStateException("Couldn't create dir: " + parent3);
}
return descFile;
}
use of org.ballerinalang.protobuf.exception.BalGenToolException in project ballerina by ballerina-lang.
the class GrpcCmd method readProperties.
private List<String> readProperties(ClassLoader classLoader) {
String fileName;
List<String> protoFilesList = new ArrayList<>();
try (InputStream initialStream = classLoader.getResourceAsStream("standardProtos.properties");
BufferedReader reader = new BufferedReader(new InputStreamReader(initialStream, StandardCharsets.UTF_8))) {
while ((fileName = reader.readLine()) != null) {
protoFilesList.add(fileName);
}
} catch (IOException e) {
throw new BalGenToolException("Error in reading standardProtos.properties.", e);
}
return protoFilesList;
}
use of org.ballerinalang.protobuf.exception.BalGenToolException in project ballerina by ballerina-lang.
the class DescriptorsGenerator method generatedependentDescriptor.
public static List<byte[]> generatedependentDescriptor(String parentDescPath, String parentProtoPath, List<byte[]> list, String exePath, ClassLoader classLoader) {
File initialFile = new File(parentDescPath);
try (InputStream targetStream = new FileInputStream(initialFile)) {
DescriptorProtos.FileDescriptorSet descSet = DescriptorProtos.FileDescriptorSet.parseFrom(targetStream);
for (String depPath : descSet.getFile(0).getDependencyList()) {
String path = BalGenerationConstants.META_DEPENDENCY_LOCATION + depPath.substring(depPath.lastIndexOf(BalGenerationConstants.FILE_SEPARATOR), depPath.length()).replace(".proto", "") + ".desc";
createMetaFolder(path);
String protoPath;
if (!depPath.contains(GOOGLE_STANDARD_LIB)) {
protoPath = new File(new File(resolveProtoFloderPath(parentProtoPath)).toURI().getPath() + depPath).toURI().getPath();
} else {
// Get file from resources folder
File dependentDesc = new File(META_LOCATION + depPath);
File parentFile = dependentDesc.getParentFile();
if (!parentFile.exists() && !parentFile.mkdirs()) {
throw new IllegalStateException("Couldn't create directory '" + META_LOCATION + depPath + "'");
}
try (InputStream initialStream = classLoader.getResourceAsStream(depPath);
OutputStream outStream = new FileOutputStream(dependentDesc)) {
byte[] buffer = new byte[initialStream.available()];
int read = initialStream.read(buffer);
if (read == -1) {
throw new IllegalStateException("Couldn't read input stream of 'google/protobuf'" + " resource: ");
}
outStream.write(buffer);
outStream.close();
protoPath = dependentDesc.getAbsolutePath();
} catch (IOException e) {
throw new BalGenToolException("Error reading resource file '" + depPath + "'", e);
}
}
String command = new ProtocCommandBuilder(exePath, protoPath, resolveProtoFloderPath(protoPath), new File(getDescriptorPath(depPath)).getAbsolutePath()).build();
generateDescriptor(command);
File childFile = new File(path);
try (InputStream childStream = new FileInputStream(childFile)) {
DescriptorProtos.FileDescriptorSet childDescSet = DescriptorProtos.FileDescriptorSet.parseFrom(childStream);
if (childDescSet.getFile(0).getDependencyCount() != 0) {
List<byte[]> newList = new ArrayList<>();
generatedependentDescriptor(path, protoPath, newList, exePath, classLoader);
} else {
initialFile = new File(path);
try (InputStream dependentStream = new FileInputStream(initialFile)) {
DescriptorProtos.FileDescriptorSet set = DescriptorProtos.FileDescriptorSet.parseFrom(dependentStream);
list.add(set.getFile(0).toByteArray());
} catch (IOException e) {
throw new BalGenToolException("Error reading dependent descriptor.", e);
}
}
} catch (IOException e) {
throw new BalGenToolException("Error extracting dependent bal.", e);
}
}
} catch (IOException e) {
throw new BalGenToolException("Error parsing descriptor file " + initialFile, e);
}
return list;
}
Aggregations