in extensions/grpc/codegen/src/main/java/org/apache/camel/quarkus/grpc/codegen/CamelQuarkusGrpcCodegenProvider.java [86:178]
public boolean trigger(CodeGenContext context) throws CodeGenException {
final Config config = context.config();
if (!config.getValue("quarkus.camel.grpc.codegen.enabled", Boolean.class)) {
LOG.info("Skipping " + this.getClass() + " invocation on user's request");
return false;
}
Path outDir = context.outDir();
Path workDir = context.workDir();
Set<String> protoDirs = new HashSet<>();
try {
List<String> protoFiles = new ArrayList<>();
if (Files.isDirectory(context.inputDir())) {
try (Stream<Path> protoFilesPaths = Files.walk(context.inputDir())) {
protoFilesPaths
.filter(Files::isRegularFile)
.filter(s -> s.toString().endsWith(PROTO))
.map(Path::normalize)
.map(Path::toAbsolutePath)
.map(Path::toString)
.forEach(protoFiles::add);
protoDirs.add(context.inputDir().normalize().toAbsolutePath().toString());
}
}
Path dirWithProtosFromDependencies = workDir.resolve("protoc-protos-from-dependencies");
Collection<Path> protoFilesFromDependencies = gatherProtosFromDependencies(dirWithProtosFromDependencies, protoDirs,
context);
if (!protoFilesFromDependencies.isEmpty()) {
protoFilesFromDependencies.stream()
.map(Path::normalize)
.map(Path::toAbsolutePath)
.map(Path::toString)
.forEach(protoFiles::add);
}
if (!protoFiles.isEmpty()) {
initExecutables(workDir, context.applicationModel());
Path protocDependenciesDir = workDir.resolve("protoc-dependencies");
Collection<String> protosToImport = gatherDirectoriesWithImports(protocDependenciesDir, context);
if (!protoFilesFromDependencies.isEmpty() && !protosToImport.isEmpty()) {
// Clean up potential duplicates that can exist when scan-for-imports artifacts clash with scan-for-proto configs
protoFiles.forEach(file -> {
if (!file.contains(context.inputDir().toString())) {
String relativeFileName = file.replace(dirWithProtosFromDependencies.toString(), "");
Path path = Paths.get(protocDependenciesDir.toAbsolutePath().toString(), relativeFileName);
if (Files.exists(path)) {
LOG.debugf("Cleaning up duplicate proto file %s", path);
try {
Files.delete(path);
} catch (IOException e) {
LOG.errorf("Failed cleaning up duplicate proto file %s", e);
}
}
}
});
}
List<String> command = new ArrayList<>();
command.add(executables.protoc.toString());
for (String protoImportDir : protosToImport) {
command.add(String.format("-I=%s", escapeWhitespace(protoImportDir)));
}
for (String protoDir : protoDirs) {
command.add(String.format("-I=%s", escapeWhitespace(protoDir)));
}
command.addAll(asList("--plugin=protoc-gen-grpc=" + executables.grpc,
"--grpc_out=" + outDir,
"--java_out=" + outDir));
command.addAll(protoFiles);
ProcessBuilder processBuilder = new ProcessBuilder(command);
final Process process = ProcessUtil.launchProcess(processBuilder, context.shouldRedirectIO());
int resultCode = process.waitFor();
if (resultCode != 0) {
throw new CodeGenException("Failed to generate Java classes from proto files: " + protoFiles +
" to " + outDir.toAbsolutePath() + " with command " + String.join(" ", command));
}
new CamelQuarkusGrpcPostProcessor(outDir).process();
LOG.info("Successfully finished generating and post-processing sources from proto files");
return true;
}
} catch (IOException | InterruptedException e) {
throw new CodeGenException(
"Failed to generate java files from proto file in " + context.inputDir().toAbsolutePath(), e);
}
return false;
}