Skip to content

Commit

Permalink
Merge pull request #40415 from aloubyansky/pathtree-walk-subtree
Browse files Browse the repository at this point in the history
Added a method to walk a subtree of a given PathTree
  • Loading branch information
aloubyansky authored Aug 1, 2024
2 parents a398b4d + f53be2f commit 346a834
Show file tree
Hide file tree
Showing 15 changed files with 348 additions and 307 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,8 @@
import io.quarkus.maven.dependency.DependencyFlags;
import io.quarkus.maven.dependency.ResolvedDependency;
import io.quarkus.panache.common.deployment.PanacheEntityClassesBuildItem;
import io.quarkus.paths.FilteredPathTree;
import io.quarkus.paths.PathFilter;
import io.quarkus.paths.PathTree;
import io.quarkus.paths.PathTreeUtils;
import io.quarkus.qute.CheckedTemplate;
import io.quarkus.qute.Engine;
import io.quarkus.qute.EngineBuilder;
Expand Down Expand Up @@ -2179,14 +2178,22 @@ private void scanPathTree(PathTree pathTree, TemplateRootsBuildItem templateRoot
BuildProducer<NativeImageResourceBuildItem> nativeImageResources,
QuteConfig config) {
for (String templateRoot : templateRoots) {
pathTree.accept(templateRoot, visit -> {
if (visit != null) {
// if template root is found in this tree then walk over its subtree
scanTemplateRootSubtree(
new FilteredPathTree(pathTree, PathFilter.forIncludes(List.of(templateRoot + "/**"))),
visit.getRelativePath(), watchedPaths, templatePaths, nativeImageResources, config);
}
});
if (PathTreeUtils.containsCaseSensitivePath(pathTree, templateRoot)) {
pathTree.walkIfContains(templateRoot, visit -> {
if (Files.isRegularFile(visit.getPath())) {
LOGGER.debugf("Found template: %s", visit.getPath());
// remove templateRoot + /
final String relativePath = visit.getRelativePath();
String templatePath = relativePath.substring(templateRoot.length() + 1);
if (config.templatePathExclude.matcher(templatePath).matches()) {
LOGGER.debugf("Template file excluded: %s", visit.getPath());
return;
}
produceTemplateBuildItems(templatePaths, watchedPaths, nativeImageResources,
relativePath, templatePath, visit.getPath(), config);
}
});
}
}
}

Expand Down Expand Up @@ -3414,27 +3421,6 @@ private static void produceTemplateBuildItems(BuildProducer<TemplatePathBuildIte
readTemplateContent(originalPath, config.defaultCharset)));
}

private void scanTemplateRootSubtree(PathTree pathTree, String templateRoot,
BuildProducer<HotDeploymentWatchedFileBuildItem> watchedPaths,
BuildProducer<TemplatePathBuildItem> templatePaths,
BuildProducer<NativeImageResourceBuildItem> nativeImageResources,
QuteConfig config) {
pathTree.walk(visit -> {
if (Files.isRegularFile(visit.getPath())) {
LOGGER.debugf("Found template: %s", visit.getPath());
// remove templateRoot + /
final String relativePath = visit.getRelativePath();
String templatePath = relativePath.substring(templateRoot.length() + 1);
if (config.templatePathExclude.matcher(templatePath).matches()) {
LOGGER.debugf("Template file excluded: %s", visit.getPath());
return;
}
produceTemplateBuildItems(templatePaths, watchedPaths, nativeImageResources,
relativePath, templatePath, visit.getPath(), config);
}
});
}

private static boolean isExcluded(TypeCheck check, Iterable<Predicate<TypeCheck>> excludes) {
for (Predicate<TypeCheck> exclude : excludes) {
if (exclude.test(check)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
import io.quarkus.deployment.builditem.LaunchModeBuildItem;
import io.quarkus.deployment.builditem.nativeimage.NativeImageResourceBuildItem;
import io.quarkus.deployment.pkg.steps.NativeOrNativeSourcesBuild;
import io.quarkus.paths.FilteredPathTree;
import io.quarkus.paths.PathFilter;
import io.quarkus.paths.PathVisitor;
import io.quarkus.vertx.core.deployment.CoreVertxBuildItem;
import io.quarkus.vertx.http.deployment.spi.AdditionalStaticResourceBuildItem;
Expand Down Expand Up @@ -85,7 +83,7 @@ public void nativeImageResource(Optional<StaticResourcesBuildItem> staticResourc
final Set<String> collectedDirs = new HashSet<>();
visitRuntimeMetaInfResources(visit -> {
if (Files.isDirectory(visit.getPath())) {
final String relativePath = visit.getRelativePath("/");
final String relativePath = visit.getRelativePath();
if (collectedDirs.add(relativePath)) {
producer.produce(new NativeImageResourceBuildItem(relativePath));
}
Expand All @@ -104,7 +102,7 @@ private Set<StaticResourcesBuildItem.Entry> getClasspathResources() {
visitRuntimeMetaInfResources(visit -> {
if (!Files.isDirectory(visit.getPath())) {
knownPaths.add(new StaticResourcesBuildItem.Entry(
visit.getRelativePath("/").substring(StaticResourcesRecorder.META_INF_RESOURCES.length()),
visit.getRelativePath().substring(StaticResourcesRecorder.META_INF_RESOURCES.length()),
false));
}
});
Expand All @@ -120,13 +118,10 @@ private static void visitRuntimeMetaInfResources(PathVisitor visitor) {
final List<ClassPathElement> elements = QuarkusClassLoader.getElements(StaticResourcesRecorder.META_INF_RESOURCES,
false);
if (!elements.isEmpty()) {
final PathFilter filter = PathFilter.forIncludes(List.of(
StaticResourcesRecorder.META_INF_RESOURCES + "/**",
StaticResourcesRecorder.META_INF_RESOURCES));
for (var element : elements) {
if (element.isRuntime()) {
element.apply(tree -> {
new FilteredPathTree(tree, filter).walk(visitor);
tree.walkIfContains(StaticResourcesRecorder.META_INF_RESOURCES, visitor);
return null;
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,25 @@ public Collection<Path> getRoots() {
public void walk(PathVisitor visitor) {
try (FileSystem fs = openFs()) {
final Path dir = fs.getPath("/");
PathTreeVisit.walk(archive, dir, pathFilter, getMultiReleaseMapping(), visitor);
PathTreeVisit.walk(archive, dir, dir, pathFilter, getMultiReleaseMapping(), visitor);
} catch (IOException e) {
throw new UncheckedIOException("Failed to read " + archive, e);
}
}

@Override
public void walkIfContains(String relativePath, PathVisitor visitor) {
ensureResourcePath(relativePath);
if (!PathFilter.isVisible(pathFilter, relativePath)) {
return;
}
try (FileSystem fs = openFs()) {
for (Path root : fs.getRootDirectories()) {
final Path walkDir = root.resolve(relativePath);
if (Files.exists(walkDir)) {
PathTreeVisit.walk(archive, root, walkDir, pathFilter, getMultiReleaseMapping(), visitor);
}
}
} catch (IOException e) {
throw new UncheckedIOException("Failed to read " + archive, e);
}
Expand Down Expand Up @@ -293,6 +311,17 @@ public void walk(PathVisitor visitor) {
}
}

@Override
public void walkIfContains(String relativePath, PathVisitor visitor) {
lock.readLock().lock();
try {
ensureOpen();
super.walkIfContains(relativePath, visitor);
} finally {
lock.readLock().unlock();
}
}

@Override
public boolean contains(String relativePath) {
lock.readLock().lock();
Expand Down

This file was deleted.

Loading

0 comments on commit 346a834

Please sign in to comment.