use of io.automatiko.engine.codegen.context.QuarkusApplicationBuildContext in project automatiko-engine by automatiko-io.
the class AutomatikoQuarkusProcessor method buildContext.
private GeneratorContext buildContext(AutomatikoBuildTimeConfig config, AppPaths appPaths, IndexView index) {
GeneratorContext generationContext = QuarkusGeneratorContext.ofResourcePath(appPaths.getResourceFiles()[0], appPaths.getFirstClassesPath().toFile());
generationContext.withBuildContext(new QuarkusApplicationBuildContext(config, className -> {
DotName classDotName = createDotName(className);
return !index.getAnnotations(classDotName).isEmpty() || index.getClassByName(classDotName) != null;
}, className -> {
return index.getAllKnownImplementors(createDotName(className)).stream().map(c -> c.name().toString()).collect(Collectors.toList());
}));
return AutomatikoBuildData.create(config, generationContext).getGenerationContext();
}
use of io.automatiko.engine.codegen.context.QuarkusApplicationBuildContext in project automatiko-engine by automatiko-io.
the class AbstractCodegenTest method generateCode.
protected Application generateCode(List<String> processResources, List<String> rulesResources, List<String> decisionResources, List<String> javaRulesResources, boolean hasRuleUnit) throws Exception {
GeneratorContext context = GeneratorContext.ofResourcePath(new File("src/test/resources"), new File("target/classes"));
// Testing based on Quarkus as Default
context.withBuildContext(new QuarkusApplicationBuildContext(config, (className -> hasClassAvailable(className)), c -> Collections.emptyList()));
ApplicationGenerator appGen = new ApplicationGenerator(this.getClass().getPackage().getName(), new File("target/codegen-tests")).withGeneratorContext(context).withRuleUnits(hasRuleUnit).withDependencyInjection(null);
if (!processResources.isEmpty()) {
appGen.withGenerator(ProcessCodegen.ofFiles(processResources.stream().map(resource -> new File("src/test/resources", resource)).collect(Collectors.toList())));
}
if (!decisionResources.isEmpty()) {
appGen.withGenerator(DecisionCodegen.ofFiles(Paths.get("src/test/resources").toAbsolutePath(), decisionResources.stream().map(resource -> new File("src/test/resources", resource)).collect(Collectors.toList())));
}
Collection<GeneratedFile> generatedFiles = appGen.generate();
List<JavaFileObject> sources = new ArrayList<JavaFileObject>();
for (GeneratedFile entry : generatedFiles) {
String fileName = entry.relativePath();
if (!fileName.endsWith(".java")) {
continue;
}
sources.add(new SourceCode(fileName, new String(entry.contents())));
logger.debug(new String(entry.contents()));
}
if (logger.isDebugEnabled()) {
Path temp = Files.createTempDirectory("automatik-temp-dir");
logger.debug("Dumping generated files in " + temp);
for (GeneratedFile entry : generatedFiles) {
Path fpath = temp.resolve(entry.relativePath());
fpath.getParent().toFile().mkdirs();
Files.write(fpath, entry.contents());
}
}
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
DiagnosticCollector<JavaFileObject> diagnosticsCollector = new DiagnosticCollector<>();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnosticsCollector, null, null);
compilationOutcome = Files.createTempDirectory("compile-test-");
fileManager.setLocation(StandardLocation.CLASS_OUTPUT, Collections.singleton(compilationOutcome.toFile()));
CompilationTask task = compiler.getTask(null, fileManager, diagnosticsCollector, null, null, sources);
boolean result = task.call();
if (result) {
classloader = new URLClassLoader(new URL[] { compilationOutcome.toUri().toURL() });
@SuppressWarnings("unchecked") Class<Application> app = (Class<Application>) Class.forName(this.getClass().getPackage().getName() + ".Application", true, classloader);
Application application = app.getDeclaredConstructor().newInstance();
app.getMethod("setup").invoke(application);
return application;
} else {
List<Diagnostic<? extends JavaFileObject>> diagnostics = diagnosticsCollector.getDiagnostics();
for (Diagnostic<? extends JavaFileObject> d : diagnostics) {
System.err.println(d);
}
throw new RuntimeException("Compilation failed");
}
}
use of io.automatiko.engine.codegen.context.QuarkusApplicationBuildContext in project automatiko-engine by automatiko-io.
the class ResourceGeneratorFactoryTest method testCreateQuarkus.
@Test
void testCreateQuarkus(@Mock GeneratorContext generatorContext) {
when(generatorContext.getBuildContext()).thenReturn(new QuarkusApplicationBuildContext(new AutomatikoBuildConfig(), p -> true, c -> Collections.emptyList()));
Optional<AbstractResourceGenerator> context = tested.create(generatorContext, process, MODEL_FQCN, PROCESS_FQCN, APP_CANONICAL_NAME);
assertThat(context.isPresent()).isTrue();
assertThat(context.get()).isExactlyInstanceOf(ResourceGenerator.class);
}
use of io.automatiko.engine.codegen.context.QuarkusApplicationBuildContext in project automatiko-engine by automatiko-io.
the class ResourceGeneratorFactoryTest method testCreateQuarkusReactive.
@Test
void testCreateQuarkusReactive(@Mock GeneratorContext generatorContext) {
when(generatorContext.getApplicationProperty(GeneratorConfig.REST_RESOURCE_TYPE_PROP)).thenReturn(Optional.of("reactive"));
when(generatorContext.getBuildContext()).thenReturn(new QuarkusApplicationBuildContext(new AutomatikoBuildConfig(), p -> true, c -> Collections.emptyList()));
Optional<AbstractResourceGenerator> context = tested.create(generatorContext, process, MODEL_FQCN, PROCESS_FQCN, APP_CANONICAL_NAME);
assertThat(context.isPresent()).isTrue();
assertThat(context.get()).isExactlyInstanceOf(ReactiveResourceGenerator.class);
}
Aggregations