Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix handling of custom package for generated resources as config para… #514

Merged
merged 4 commits into from
Sep 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public Collection<GeneratedFile> generate() {
}

if (context != null) {
CompilationUnit cp = context.write(DEFAULT_PACKAGE_NAME);
CompilationUnit cp = context.write();

String packageName = cp.getPackageDeclaration().map(pd -> pd.getName().toString()).orElse("");
String clazzName = packageName + "."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ public static GeneratorContext ofResourcePath(File resourcePath, File classesPat
private File resourcePath;
private File classesPath;

private Optional<String> packageName = Optional.empty();

private Properties applicationProperties = new Properties();
private Map<String, String> modifiedApplicationProperties = new LinkedHashMap<String, String>();

Expand Down Expand Up @@ -114,12 +116,12 @@ public void setApplicationProperty(String property, String value) {
}
}

public CompilationUnit write(String packageName) {
public CompilationUnit write() {

CompilationUnit clazz = parse(
this.getClass().getResourceAsStream("/class-templates/config/ConfigPropertiesTemplate.java"));

clazz.setPackageDeclaration(packageName);
clazz.setPackageDeclaration("io.automatiko.application.app");
ClassOrInterfaceDeclaration template = clazz.findFirst(ClassOrInterfaceDeclaration.class).get();

BlockStmt constructorBody = new BlockStmt();
Expand Down Expand Up @@ -284,6 +286,14 @@ public List<String> getInstructions() {
return this.instructions;
}

public Optional<String> getPackageName() {
return packageName;
}

public void withPackageName(String packageName) {
this.packageName = Optional.of(packageName);
}

public void logInstructions() {
if (!instructions.isEmpty()) {
LOGGER.info("****************** Automatiko Instructions *********************");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ public DecisionCodegen(List<DMNResource> resources) {
this.resources = resources;

// set default package name
setPackageName(ApplicationGenerator.DEFAULT_PACKAGE_NAME);
setPackageName(context == null ? ApplicationGenerator.DEFAULT_PACKAGE_NAME
: context.getPackageName().orElse(ApplicationGenerator.DEFAULT_PACKAGE_NAME));
this.moduleGenerator = new DecisionContainerGenerator(applicationCanonicalName, resources);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,11 @@ public CompilationUnit generateCompilationUnit() {
}

}
} else {
// no user tasks remove the list tasks method
Optional<MethodDeclaration> createResourceMethod = template.findAll(MethodDeclaration.class).stream()
.filter(md -> md.getNameAsString().equals("getTasks_" + processName)).findFirst();
createResourceMethod.ifPresent(template::remove);
}

template.findAll(StringLiteralExpr.class).forEach(this::interpolateStrings);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,8 @@ public ProcessCodegen(Collection<? extends Process> processes) {
}

// set default package name
setPackageName(ApplicationGenerator.DEFAULT_PACKAGE_NAME);
setPackageName(context == null ? ApplicationGenerator.DEFAULT_PACKAGE_NAME
: context.getPackageName().orElse(ApplicationGenerator.DEFAULT_PACKAGE_NAME));
contextClassLoader = Thread.currentThread().getContextClassLoader();

resourceGeneratorFactory = new DefaultResourceGeneratorFactory();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,8 @@ private MethodDeclaration internalConfigure(ProcessMetaData processMetaData) {

processMetaData.getGeneratedHandlers().forEach((name, descriptor) -> {

CompilationUnit handler = descriptor.generateHandlerClassForService();
CompilationUnit handler = descriptor
.generateHandlerClassForService(context.getPackageName().orElse("io.automatiko.engine.app.handlers"));
ClassOrInterfaceDeclaration clazz = handler.findFirst(ClassOrInterfaceDeclaration.class).get();
if (useInjection()) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class CassandraContainer implements QuarkusTestResourceLifecycleManager {
@SuppressWarnings("resource")
@Override
public Map<String, String> start() {
cassandra = new FixedHostPortGenericContainer<>("launcher.gcr.io/google/cassandra3").withFixedExposedPort(9042, 9042)
cassandra = new FixedHostPortGenericContainer<>("cassandra:3.11.2").withFixedExposedPort(9042, 9042)
// wait for the server to be fully started
.waitingFor(Wait.forLogMessage(".*\\bStarting listening for CQL clients\\b.*", 1))
.withLogConsumer(new Slf4jLogConsumer(LOGGER));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ public class AutomatikoQuarkusProcessor {
"target/generated-resources/automatiko");

private static final String generatedSourcesDir = "target/generated-sources/automatiko/";
private static final String generatedCustomizableSourcesDir = System
.getProperty("io.automatiko.codegen.sources.directory", "target/generated-sources/automatiko/");
private static final String generatedTestSourcesDir = "target/generated-test-sources/automatiko/";

private static final Logger logger = LoggerFactory.getLogger(AutomatikoQuarkusProcessor.class);
private final transient String generatedClassesDir = System.getProperty("quarkus.debug.generated-classes-dir");
private final transient String persistenceFactoryClass = "io.automatiko.engine.addons.persistence.AbstractProcessInstancesFactory";
Expand Down Expand Up @@ -191,7 +191,7 @@ public void testRunStarted(Consumer<TestRunListener> listenerConsumer) {
Collection<GeneratedFile> javaFiles = generatedFiles.stream().filter(f -> f.relativePath().endsWith(".java"))
.collect(Collectors.toCollection(ArrayList::new));
String sourceFolder = sourceFolder(appGen);
writeGeneratedFiles(sourceFolder, appPaths, generatedFiles);
writeGeneratedFiles(appGen, sourceFolder, appPaths, generatedFiles);

if (!javaFiles.isEmpty()) {

Expand Down Expand Up @@ -407,7 +407,8 @@ public void runtimeInitializationRegistrationStep(BuildProducer<RuntimeInitializ
}

@BuildStep
public void serviceProviderRegistrationStep(BuildProducer<ServiceProviderBuildItem> providerProducer) {
public void serviceProviderRegistrationStep(AutomatikoBuildTimeConfig config,
BuildProducer<ServiceProviderBuildItem> providerProducer) {

providerProducer.produce(new ServiceProviderBuildItem(AutomatikoConfigProperties.class.getCanonicalName(),
"io.automatiko.application.app.GeneratedAutomatikoConfigProperties"));
Expand Down Expand Up @@ -450,7 +451,7 @@ private void generatePersistenceInfo(ApplicationGenerator appGen, AutomatikoBuil

if (!generatedFiles.isEmpty()) {
String sourceFolder = sourceFolder(appGen);
writeGeneratedFiles(sourceFolder, appPaths, generatedFiles);
writeGeneratedFiles(appGen, sourceFolder, appPaths, generatedFiles);

compile(appGen, appPaths, curateOutcomeBuildItem.getApplicationModel(), generatedFiles, launchMode.getLaunchMode(),
generatedBeans, additionalIndexClass, GeneratedBeanBuildItem::new, pconfig);
Expand Down Expand Up @@ -479,21 +480,22 @@ private Collection<GeneratedFile> getGeneratedPersistenceFiles(AutomatikoBuildTi
return generatedFiles;
}

private void writeGeneratedFiles(String sourceFolder, AppPaths appPaths, Collection<GeneratedFile> resourceFiles) {
private void writeGeneratedFiles(ApplicationGenerator appGen, String sourceFolder, AppPaths appPaths,
Collection<GeneratedFile> resourceFiles) {
String generateToFolder = System.getProperty("automatiko.testOnly") != null ? generatedTestSourcesDir
: generatedSourcesDir;
for (Path projectPath : appPaths.projectPaths) {
String restResourcePath = projectPath.resolve(generatedCustomizableSourcesDir).toString();

String resourcePath = projectPath.resolve(generatedResourcesDir).toString();
String jsonSchemaPath = projectPath.resolve(generatedResourcesDir).resolve("jsonSchema").toString();
String sourcePath = projectPath.resolve(generatedSourcesDir).toString();
String sourcePath = projectPath.resolve(generateToFolder).toString();

for (GeneratedFile f : resourceFiles) {
try {
if (f.getType() == GeneratedFile.Type.RESOURCE) {
writeGeneratedFile(f, sourceFolder, resourcePath);
} else if (f.getType() == GeneratedFile.Type.JSON_SCHEMA) {
writeGeneratedFile(f, sourceFolder, jsonSchemaPath);
} else if (f.getType().isCustomizable()) {
writeGeneratedFile(f, sourceFolder, restResourcePath);
} else {
writeGeneratedFile(f, sourceFolder, sourcePath);
}
Expand Down Expand Up @@ -543,6 +545,11 @@ private void compile(ApplicationGenerator appGen, AppPaths appPaths, Application
File buildDir = appPaths.getFirstClassesPath().toFile();
if (buildDir.isFile()) {
buildDir = new File(buildDir.getParentFile(), "classes");

}
// if environment is configured with test only write all generated and compiled classes into the test-classes folder
if (System.getProperty("automatiko.testOnly") != null) {
buildDir = new File(buildDir.getParentFile(), "test-classes");
}

fileManager.setLocation(StandardLocation.CLASS_OUTPUT, Collections.singleton(buildDir));
Expand Down Expand Up @@ -712,7 +719,7 @@ private void addProcessGenerator(AppPaths appPaths, boolean usePersistence, Appl

WorkflowBuilder builder = (WorkflowBuilder) method.invoke(workflowsBuilderInstance);
ExecutableProcess process = builder.get();
process.setPackageName(builderClass.getPackageName());
process.setPackageName(builderClass.getPackageName() + ".generated");

// sets the category from annotation if not already set workflow
if (process.getMetaData("category") == null && !category.isBlank()) {
Expand Down Expand Up @@ -827,6 +834,7 @@ private GeneratorContext buildContext(Capabilities capabilities, AutomatikoBuild
IndexView index) {
GeneratorContext generationContext = QuarkusGeneratorContext.ofResourcePath(appPaths.getResourceFiles()[0],
appPaths.getFirstClassesPath().toFile());
generationContext.withPackageName(config.packageName().orElse(DEFAULT_PACKAGE_NAME));

generationContext
.withBuildContext(new QuarkusApplicationBuildContext(config, className -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,8 @@ private String mangledHandlerName(String processId, String interfaceName, String
return String.format("%s_%s_%s_%s_Handler", interfaceName, operationName, processId, nodeName);
}

public CompilationUnit generateHandlerClassForService() {
CompilationUnit compilationUnit = new CompilationUnit("io.automatiko.engine.app.handlers");
public CompilationUnit generateHandlerClassForService(String packageName) {
CompilationUnit compilationUnit = new CompilationUnit(packageName);

compilationUnit.getTypes().add(classDeclaration());

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.automatiko.engine.workflow.builder;

import java.time.Duration;
import java.time.Period;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
Expand Down Expand Up @@ -212,6 +214,34 @@ public WorkflowBuilder tags(Supplier<String> tagExpression) {
return this;
}

/**
* Sets expiration of the workflow instance that instructs to remove the instance from storage after
* <code>expiresAfter</code>
* after completion
*
* @param expiresAfter not null duration
* @return the builder
*/
public WorkflowBuilder expiresAfter(Duration expiresAfter) {
Objects.requireNonNull(expiresAfter, "expiresAfter must not be null");
process.setMetaData("expiresAfter", expiresAfter.toString());
return this;
}

/**
* Sets expiration of the workflow instance that instructs to remove the instance from storage after
* <code>expiresAfter</code>
* after completion
*
* @param expiresAfter not null period
* @return the builder
*/
public WorkflowBuilder expiresAfter(Period expiresAfter) {
Objects.requireNonNull(expiresAfter, "expiresAfter must not be null");
process.setMetaData("expiresAfter", expiresAfter.toString());
return this;
}

/**
* Sets category of the workflow that is used to group workflows at documentation level
*
Expand Down