Skip to content

Commit

Permalink
[incubator-kie-issues#847] Include generated-resources to quarkus art…
Browse files Browse the repository at this point in the history
…ifact (apache#3427)

* [incubator-kie-issues#847] WIP

* [incubator-kie-issues#847] Fix for execution during test

* [incubator-kie-issues#847] Fix for execution during test

* [incubator-kie-issues#847] Small refactoring for test. Add KogitoAssetsProcessorTest

---------

Co-authored-by: Gabriele-Cardosi <[email protected]>
  • Loading branch information
2 people authored and rgdoliveira committed Mar 11, 2024
1 parent 11f0e5e commit dfd83f3
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ public class KogitoDMNScenarioRunnerHelper extends DMNScenarioRunnerHelper {

private DMNRuntime dmnRuntime = initDmnRuntime();

private static final String targetFolder = File.separator + "target" + File.separator;
private static final String generatedResourcesFolder = targetFolder + "generated-resources" + File.separator;

@Override
protected Map<String, Object> executeScenario(KieContainer kieContainer,
ScenarioRunnerData scenarioRunnerData,
Expand Down Expand Up @@ -102,8 +105,9 @@ private DMNRuntime initDmnRuntime() {
}

private boolean filterResource(Path path, String extension) {
String targetFolder = File.separator + "target" + File.separator;
return path.toString().endsWith(extension) && !path.toString().contains(targetFolder) && Files.isRegularFile(path);
return path.toString().endsWith(extension) &&
(path.toString().contains(generatedResourcesFolder) || !(path.toString().contains(targetFolder)))
&& Files.isRegularFile(path);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
*/
package org.kie.kogito.quarkus.common.deployment;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
Expand All @@ -27,6 +29,7 @@
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down Expand Up @@ -64,6 +67,8 @@
import io.quarkus.deployment.pkg.builditem.OutputTargetBuildItem;
import io.quarkus.maven.dependency.Dependency;
import io.quarkus.maven.dependency.ResolvedDependency;
import io.quarkus.paths.PathCollection;
import io.quarkus.paths.PathList;
import io.quarkus.resteasy.reactive.spi.GeneratedJaxRsResourceBuildItem;
import io.quarkus.vertx.http.deployment.spi.AdditionalStaticResourceBuildItem;

Expand Down Expand Up @@ -102,9 +107,10 @@ public class KogitoAssetsProcessor {
@BuildStep
public KogitoBuildContextBuildItem generateKogitoBuildContext(List<KogitoBuildContextAttributeBuildItem> attributes) {
// configure the application generator
PathCollection rootPaths = getRootPaths( root.getResolvedPaths());
KogitoBuildContext context =
kogitoBuildContext(outputTargetBuildItem.getOutputDirectory(),
root.getResolvedPaths(),
rootPaths,
combinedIndexBuildItem.getIndex(),
curateOutcomeBuildItem.getApplicationModel().getAppArtifact());
attributes.forEach(attribute -> context.addContextAttribute(attribute.getName(), attribute.getValue()));
Expand Down Expand Up @@ -212,6 +218,28 @@ public EfestoGeneratedClassBuildItem reflectiveEfestoGeneratedClassBuildItem(Kog
return new EfestoGeneratedClassBuildItem(kogitoGeneratedSourcesBuildItem.getGeneratedFiles());
}

static PathCollection getRootPaths(PathCollection resolvedPaths) {
AtomicReference<PathCollection> toReturnRef = new AtomicReference<>(resolvedPaths);
if (resolvedPaths.stream().noneMatch(path -> path.endsWith(File.separator + "generated-resources"))) {
Optional<Path> optClassesPath =
resolvedPaths.stream().filter(path -> {
String fullPath = path.toString();
String dir = fullPath.substring(fullPath.lastIndexOf(File.separator) + 1);
return dir.equals("classes");
}).findFirst();
optClassesPath.ifPresent(classesPath -> {
Path toAdd = Path.of(classesPath.toString().replace(File.separator + "classes",
File.separator +
"generated" +
"-resources"));
List<Path> prevPaths = toReturnRef.get().stream().collect(Collectors.toList());
prevPaths.add(toAdd);
toReturnRef.set(PathList.from(prevPaths));
});
}
return toReturnRef.get();
}

private Collection<GeneratedFile> collectGeneratedFiles(KogitoGeneratedSourcesBuildItem sources, List<KogitoAddonsPreGeneratedSourcesBuildItem> preSources,
List<KogitoAddonsPostGeneratedSourcesBuildItem> postSources) {
Map<String, GeneratedFile> map = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.kie.kogito.quarkus.common.deployment;

import java.io.File;
import java.nio.file.Path;
import java.util.Arrays;

import io.quarkus.bootstrap.model.PathsCollection;
import io.quarkus.paths.PathCollection;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

class KogitoAssetsProcessorTest {

@Test
void getRootPathsWithoutClasses() {
String projectDirPath = "projectDir";
String outputTargetPath = "outputTarget";
Path projectDir = Path.of(projectDirPath);
Path outputTarget = Path.of(outputTargetPath);
Iterable<Path> paths = Arrays.asList(projectDir, outputTarget);

PathCollection resolvedPaths = PathsCollection.from(paths);
PathCollection retrieved = KogitoAssetsProcessor.getRootPaths(resolvedPaths);
assertEquals(resolvedPaths.size(), retrieved.size());
paths.forEach(expected -> assertTrue(retrieved.contains(expected)));
}

@Test
void getRootPathsWithClasses() {
String projectDirPath = "projectDir";
String outputTargetPath = "outputTarget";
String outputTargetPathClasses = String.format("%s/%s/classes", projectDirPath, outputTargetPath).replace("/", File.separator);
Path projectDir = Path.of(projectDirPath);
Path outputTarget = Path.of(outputTargetPathClasses);
Iterable<Path> paths = Arrays.asList(projectDir, outputTarget);

PathCollection resolvedPaths = PathsCollection.from(paths);
PathCollection retrieved = KogitoAssetsProcessor.getRootPaths(resolvedPaths);
assertEquals(resolvedPaths.size() + 1, retrieved.size());
paths.forEach(expected -> assertTrue(retrieved.contains(expected)));
String expectedPath = String.format("%s/%s/generated-resources", projectDirPath, outputTargetPath).replace("/", File.separator);
assertTrue(retrieved.contains(Path.of(expectedPath)));
}
}

0 comments on commit dfd83f3

Please sign in to comment.