in extensions/jsh-dsl/deployment/src/main/java/org/apache/camel/quarkus/dsl/jsh/deployment/JshDslProcessor.java [82:164]
void compileScriptsAOT(BuildProducer<GeneratedClassBuildItem> generatedClass,
BuildProducer<GeneratedResourceBuildItem> generatedResource,
BuildProducer<ReflectiveClassBuildItem> reflectiveClass,
BuildProducer<DslGeneratedClassBuildItem> generatedJavaClass,
BuildSystemTargetBuildItem buildSystemTargetBuildItem,
CurateOutcomeBuildItem curateOutcomeBuildItem) throws Exception {
LOG.debug("Loading .jsh resources");
final Path projectDir = Paths.get(".").toAbsolutePath().normalize();
Path outputDirectory = buildSystemTargetBuildItem.getOutputDirectory();
final Path generatedSourceDir = outputDirectory.resolve("jsh-dsl/generated-sources");
Files.createDirectories(generatedSourceDir);
final Path generatedSourceHomeDir = generatedSourceDir.resolve(PACKAGE_NAME.replace('.', File.separatorChar));
Files.createDirectories(generatedSourceHomeDir);
Map<String, Resource> nameToResource = new HashMap<>();
Set<File> filesToCompile = new HashSet<>();
CamelMainHelper.forEachMatchingResource(
resource -> {
if (!resource.getLocation().endsWith(".jsh")) {
return;
}
String name = determineName(resource);
try (InputStream is = resource.getInputStream()) {
String content = toJavaClass(name, IOHelper.loadText(is));
LOG.debug("Generated Java source content:\n {}", content);
final Path sourceFile = generatedSourceHomeDir.resolve(String.format("%s.java", name));
Files.write(sourceFile, content.getBytes(StandardCharsets.UTF_8));
filesToCompile.add(sourceFile.toFile());
nameToResource.put(String.format("%s.%s", PACKAGE_NAME, name), resource);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
if (filesToCompile.isEmpty()) {
return;
}
final Path classesDir = outputDirectory.resolve("jsh-dsl/generated-classes");
Files.createDirectories(classesDir);
CompilationProvider.Context context = new CompilationProvider.Context(
FEATURE,
curateOutcomeBuildItem.getApplicationModel().getDependencies().stream()
.map(ResolvedDependency::getResolvedPaths)
.flatMap(PathCollection::stream)
.map(Path::toFile)
.filter(f -> f.getName().endsWith(".jar"))
.collect(Collectors.toSet()),
Set.of(),
projectDir.toFile(),
generatedSourceDir.toFile(),
classesDir.toFile(),
StandardCharsets.UTF_8.name(),
Map.of(),
null,
null,
null,
List.of(),
List.of());
try (JavaCompilationProvider compiler = new JavaCompilationProvider()) {
compiler.compile(filesToCompile, context);
}
try (Stream<Path> classFiles = Files.walk(classesDir)) {
classFiles
.filter(Files::isRegularFile)
.forEach(p -> {
String relativePath = classesDir.relativize(p).toString();
String className = relativePath.replace(File.separatorChar, '.').substring(0,
relativePath.length() - CLASS_EXT.length());
try {
generatedClass.produce(new GeneratedClassBuildItem(true, className, Files.readAllBytes(p)));
if (nameToResource.containsKey(className)) {
reflectiveClass.produce(
ReflectiveClassBuildItem.builder(className).build());
generatedJavaClass
.produce(new DslGeneratedClassBuildItem(className,
nameToResource.get(className).getLocation(), true));
}
} catch (IOException e) {
throw new RuntimeException("Could not read " + p);
}
});
}
}