Search in sources :

Example 1 with QuarkusApplicationBuildContext

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();
}
Also used : BiFunction(java.util.function.BiFunction) LoggerFactory(org.slf4j.LoggerFactory) GeneratorContext(io.automatiko.engine.codegen.GeneratorContext) ClassInfo(org.jboss.jandex.ClassInfo) TestRunListener(io.quarkus.deployment.dev.testing.TestRunListener) BuildProducer(io.quarkus.deployment.annotations.BuildProducer) CodeGenConstants(io.automatiko.engine.codegen.CodeGenConstants) GeneratedBeanBuildItem(io.quarkus.arc.deployment.GeneratedBeanBuildItem) MethodInfo(org.jboss.jandex.MethodInfo) ArchiveRootBuildItem(io.quarkus.deployment.builditem.ArchiveRootBuildItem) FeatureBuildItem(io.quarkus.deployment.builditem.FeatureBuildItem) CompilationTask(javax.tools.JavaCompiler.CompilationTask) CompositeIndex(org.jboss.jandex.CompositeIndex) Diagnostic(javax.tools.Diagnostic) Map(java.util.Map) CDIDependencyInjectionAnnotator(io.automatiko.engine.codegen.di.CDIDependencyInjectionAnnotator) URI(java.net.URI) Path(java.nio.file.Path) DiagnosticCollector(javax.tools.DiagnosticCollector) TestListener(io.quarkus.deployment.dev.testing.TestListener) TestListenerBuildItem(io.quarkus.deployment.dev.testing.TestListenerBuildItem) SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) IoUtils(io.automatiko.engine.services.utils.IoUtils) StandardLocation(javax.tools.StandardLocation) Model(io.automatiko.engine.api.Model) JavaCompiler(javax.tools.JavaCompiler) AutomatikoMessages(io.automatiko.engine.workflow.marshalling.impl.AutomatikoMessages) Collection(java.util.Collection) Set(java.util.Set) NativeImageResourceBuildItem(io.quarkus.deployment.builditem.nativeimage.NativeImageResourceBuildItem) QuarkusClassLoader(io.quarkus.bootstrap.classloading.QuarkusClassLoader) Collectors(java.util.stream.Collectors) PackageConfig(io.quarkus.deployment.pkg.PackageConfig) JavaFileObject(javax.tools.JavaFileObject) List(java.util.List) ApplicationModel(io.quarkus.bootstrap.model.ApplicationModel) ConfigProvider(org.eclipse.microprofile.config.ConfigProvider) ResolvedDependency(io.quarkus.maven.dependency.ResolvedDependency) ServiceProviderBuildItem(io.quarkus.deployment.builditem.nativeimage.ServiceProviderBuildItem) QuarkusApplicationBuildContext(io.automatiko.engine.codegen.context.QuarkusApplicationBuildContext) ReflectiveHierarchyIgnoreWarningBuildItem(io.quarkus.deployment.builditem.nativeimage.ReflectiveHierarchyIgnoreWarningBuildItem) ToolProvider(javax.tools.ToolProvider) IndexingUtil(io.quarkus.deployment.index.IndexingUtil) ReflectiveClassBuildItem(io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem) ProcessCodegen(io.automatiko.engine.codegen.process.ProcessCodegen) DotName(org.jboss.jandex.DotName) PersistenceGenerator(io.automatiko.engine.codegen.process.persistence.PersistenceGenerator) LaunchMode(io.quarkus.runtime.LaunchMode) CurateOutcomeBuildItem(io.quarkus.deployment.pkg.builditem.CurateOutcomeBuildItem) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) PathsCollection(io.quarkus.bootstrap.model.PathsCollection) Indexer(org.jboss.jandex.Indexer) BuildStep(io.quarkus.deployment.annotations.BuildStep) BaseWorkItem(io.automatiko.engine.workflow.BaseWorkItem) Index(org.jboss.jandex.Index) LinkedHashSet(java.util.LinkedHashSet) IndexView(org.jboss.jandex.IndexView) GeneratedResourceBuildItem(io.quarkus.deployment.builditem.GeneratedResourceBuildItem) GeneratedFile(io.automatiko.engine.codegen.GeneratedFile) RuntimeInitializedClassBuildItem(io.quarkus.deployment.builditem.nativeimage.RuntimeInitializedClassBuildItem) Logger(org.slf4j.Logger) MemoryClassPathElement(io.quarkus.bootstrap.classloading.MemoryClassPathElement) ClassPathElement(io.quarkus.bootstrap.classloading.ClassPathElement) ApplicationArchivesBuildItem(io.quarkus.deployment.builditem.ApplicationArchivesBuildItem) Files(java.nio.file.Files) ApplicationGenerator(io.automatiko.engine.codegen.ApplicationGenerator) IOException(java.io.IOException) Field(java.lang.reflect.Field) File(java.io.File) Consumer(java.util.function.Consumer) DecisionCodegen(io.automatiko.engine.codegen.decision.DecisionCodegen) ApplicationArchive(io.quarkus.deployment.ApplicationArchive) StandardJavaFileManager(javax.tools.StandardJavaFileManager) BootstrapDependencyProcessingException(io.quarkus.bootstrap.BootstrapDependencyProcessingException) AdditionalIndexedClassesBuildItem(io.quarkus.deployment.builditem.AdditionalIndexedClassesBuildItem) Paths(java.nio.file.Paths) LaunchModeBuildItem(io.quarkus.deployment.builditem.LaunchModeBuildItem) AutomatikoBuildTimeConfig(io.automatiko.engine.quarkus.AutomatikoBuildTimeConfig) Collections(java.util.Collections) AutomatikoConfigProperties(io.automatiko.engine.api.codegen.AutomatikoConfigProperties) LiveReloadBuildItem(io.quarkus.deployment.builditem.LiveReloadBuildItem) QuarkusApplicationBuildContext(io.automatiko.engine.codegen.context.QuarkusApplicationBuildContext) GeneratorContext(io.automatiko.engine.codegen.GeneratorContext) DotName(org.jboss.jandex.DotName)

Example 2 with QuarkusApplicationBuildContext

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");
    }
}
Also used : Arrays(java.util.Arrays) ProcessCodegen(io.automatiko.engine.codegen.process.ProcessCodegen) URL(java.net.URL) LoggerFactory(org.slf4j.LoggerFactory) ArrayList(java.util.ArrayList) URLClassLoader(java.net.URLClassLoader) Application(io.automatiko.engine.api.Application) CompilationTask(javax.tools.JavaCompiler.CompilationTask) Diagnostic(javax.tools.Diagnostic) URI(java.net.URI) Path(java.nio.file.Path) DiagnosticCollector(javax.tools.DiagnosticCollector) SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) StandardLocation(javax.tools.StandardLocation) Logger(org.slf4j.Logger) AutomatikoBuildConfig(io.automatiko.engine.api.config.AutomatikoBuildConfig) Files(java.nio.file.Files) JavaCompiler(javax.tools.JavaCompiler) Collection(java.util.Collection) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) File(java.io.File) JavaFileObject(javax.tools.JavaFileObject) DecisionCodegen(io.automatiko.engine.codegen.decision.DecisionCodegen) StandardJavaFileManager(javax.tools.StandardJavaFileManager) List(java.util.List) AfterEach(org.junit.jupiter.api.AfterEach) Stream(java.util.stream.Stream) Paths(java.nio.file.Paths) Comparator(java.util.Comparator) QuarkusApplicationBuildContext(io.automatiko.engine.codegen.context.QuarkusApplicationBuildContext) Collections(java.util.Collections) ToolProvider(javax.tools.ToolProvider) ArrayList(java.util.ArrayList) Diagnostic(javax.tools.Diagnostic) URL(java.net.URL) SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) JavaFileObject(javax.tools.JavaFileObject) StandardJavaFileManager(javax.tools.StandardJavaFileManager) DiagnosticCollector(javax.tools.DiagnosticCollector) Path(java.nio.file.Path) JavaCompiler(javax.tools.JavaCompiler) CompilationTask(javax.tools.JavaCompiler.CompilationTask) QuarkusApplicationBuildContext(io.automatiko.engine.codegen.context.QuarkusApplicationBuildContext) URLClassLoader(java.net.URLClassLoader) File(java.io.File) Application(io.automatiko.engine.api.Application)

Example 3 with QuarkusApplicationBuildContext

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);
}
Also used : AutomatikoBuildConfig(io.automatiko.engine.api.config.AutomatikoBuildConfig) BeforeEach(org.junit.jupiter.api.BeforeEach) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) AutomatikoBuildConfig(io.automatiko.engine.api.config.AutomatikoBuildConfig) Mock(org.mockito.Mock) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) WorkflowProcess(io.automatiko.engine.api.definition.process.WorkflowProcess) ResourceGenerator(io.automatiko.engine.codegen.process.ResourceGenerator) Mockito.when(org.mockito.Mockito.when) Mockito.lenient(org.mockito.Mockito.lenient) Test(org.junit.jupiter.api.Test) AbstractResourceGenerator(io.automatiko.engine.codegen.process.AbstractResourceGenerator) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Optional(java.util.Optional) ReactiveResourceGenerator(io.automatiko.engine.codegen.process.ReactiveResourceGenerator) QuarkusApplicationBuildContext(io.automatiko.engine.codegen.context.QuarkusApplicationBuildContext) Collections(java.util.Collections) AbstractResourceGenerator(io.automatiko.engine.codegen.process.AbstractResourceGenerator) QuarkusApplicationBuildContext(io.automatiko.engine.codegen.context.QuarkusApplicationBuildContext) Test(org.junit.jupiter.api.Test)

Example 4 with QuarkusApplicationBuildContext

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);
}
Also used : AutomatikoBuildConfig(io.automatiko.engine.api.config.AutomatikoBuildConfig) BeforeEach(org.junit.jupiter.api.BeforeEach) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) AutomatikoBuildConfig(io.automatiko.engine.api.config.AutomatikoBuildConfig) Mock(org.mockito.Mock) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) WorkflowProcess(io.automatiko.engine.api.definition.process.WorkflowProcess) ResourceGenerator(io.automatiko.engine.codegen.process.ResourceGenerator) Mockito.when(org.mockito.Mockito.when) Mockito.lenient(org.mockito.Mockito.lenient) Test(org.junit.jupiter.api.Test) AbstractResourceGenerator(io.automatiko.engine.codegen.process.AbstractResourceGenerator) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Optional(java.util.Optional) ReactiveResourceGenerator(io.automatiko.engine.codegen.process.ReactiveResourceGenerator) QuarkusApplicationBuildContext(io.automatiko.engine.codegen.context.QuarkusApplicationBuildContext) Collections(java.util.Collections) AbstractResourceGenerator(io.automatiko.engine.codegen.process.AbstractResourceGenerator) QuarkusApplicationBuildContext(io.automatiko.engine.codegen.context.QuarkusApplicationBuildContext) Test(org.junit.jupiter.api.Test)

Aggregations

QuarkusApplicationBuildContext (io.automatiko.engine.codegen.context.QuarkusApplicationBuildContext)4 Collections (java.util.Collections)4 AutomatikoBuildConfig (io.automatiko.engine.api.config.AutomatikoBuildConfig)3 WorkflowProcess (io.automatiko.engine.api.definition.process.WorkflowProcess)2 DecisionCodegen (io.automatiko.engine.codegen.decision.DecisionCodegen)2 AbstractResourceGenerator (io.automatiko.engine.codegen.process.AbstractResourceGenerator)2 ProcessCodegen (io.automatiko.engine.codegen.process.ProcessCodegen)2 ReactiveResourceGenerator (io.automatiko.engine.codegen.process.ReactiveResourceGenerator)2 ResourceGenerator (io.automatiko.engine.codegen.process.ResourceGenerator)2 File (java.io.File)2 IOException (java.io.IOException)2 URI (java.net.URI)2 Files (java.nio.file.Files)2 Path (java.nio.file.Path)2 Paths (java.nio.file.Paths)2 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 List (java.util.List)2 Optional (java.util.Optional)2 Collectors (java.util.stream.Collectors)2