in extensions/kotlin-dsl/deployment/src/main/java/org/apache/camel/quarkus/dsl/kotlin/deployment/KotlinDslProcessor.java [83:172]
void compileScriptsAOT(BuildProducer<GeneratedClassBuildItem> generatedClass,
BuildProducer<GeneratedResourceBuildItem> generatedResource,
BuildProducer<ReflectiveClassBuildItem> reflectiveClass,
BuildProducer<DslGeneratedClassBuildItem> generatedKotlinClass,
BuildSystemTargetBuildItem buildSystemTargetBuildItem,
CurateOutcomeBuildItem curateOutcomeBuildItem) throws Exception {
LOG.debug("Loading .kts resources");
final Path projectDir = Paths.get(".").toAbsolutePath().normalize();
Path outputDirectory = buildSystemTargetBuildItem.getOutputDirectory();
final Path generatedSourceDir = outputDirectory.resolve("kotlin-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(".kts")) {
return;
}
String name = determineName(resource);
try (InputStream is = resource.getInputStream()) {
String content = toKotlinClass(name, IOHelper.loadText(is));
LOG.debug("Generated Kotlin source content:\n {}", content);
final Path sourceFile = generatedSourceHomeDir.resolve(String.format("%s.kt", 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("kotlin-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(),
KotlinConstantsKt.JVM_TARGET,
KotlinConstantsKt.JVM_TARGET,
KotlinConstantsKt.JVM_TARGET,
List.of(),
List.of());
try (KotlinCompilationProvider compiler = new KotlinCompilationProvider()) {
compiler.compile(filesToCompile, context);
}
try (Stream<Path> classFiles = Files.walk(classesDir)) {
classFiles
.filter(Files::isRegularFile)
.forEach(p -> {
String fileName = p.getFileName().toString();
String relativePath = classesDir.relativize(p).toString();
try {
if (fileName.endsWith(CLASS_EXT)) {
String className = relativePath.replace(File.separatorChar, '.').substring(0,
relativePath.length() - CLASS_EXT.length());
generatedClass.produce(new GeneratedClassBuildItem(true, className, Files.readAllBytes(p)));
if (nameToResource.containsKey(className)) {
reflectiveClass.produce(
ReflectiveClassBuildItem.builder(className).build());
generatedKotlinClass
.produce(new DslGeneratedClassBuildItem(className,
nameToResource.get(className).getLocation()));
}
} else {
generatedResource.produce(new GeneratedResourceBuildItem(
relativePath.replace(File.separatorChar, '/'),
Files.readAllBytes(p)));
}
} catch (IOException e) {
throw new RuntimeException("Could not read " + p);
}
});
}
}