Skip to content

Commit

Permalink
Merge pull request #22037 from mkouba/issue-21864
Browse files Browse the repository at this point in the history
Qute - fix templates and tags scanning from extension runtime artifacts
  • Loading branch information
mkouba authored Dec 9, 2021
2 parents 963e380 + 965ec14 commit 99a76b7
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.UncheckedIOException;
import java.lang.reflect.Modifier;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
Expand Down Expand Up @@ -80,6 +81,7 @@
import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem;
import io.quarkus.deployment.pkg.builditem.CurateOutcomeBuildItem;
import io.quarkus.deployment.util.JandexUtil;
import io.quarkus.fs.util.ZipUtils;
import io.quarkus.gizmo.ClassOutput;
import io.quarkus.maven.dependency.Dependency;
import io.quarkus.maven.dependency.ResolvedDependency;
Expand Down Expand Up @@ -383,22 +385,17 @@ public CompletionStage<ResultNode> resolve(SectionResolutionContext context) {
public Optional<TemplateLocation> locate(String id) {
TemplatePathBuildItem found = templatePaths.stream().filter(p -> p.getPath().equals(id)).findAny().orElse(null);
if (found != null) {
try {
byte[] content = Files.readAllBytes(found.getFullPath());
return Optional.of(new TemplateLocation() {
@Override
public Reader read() {
return new StringReader(new String(content, StandardCharsets.UTF_8));
}
return Optional.of(new TemplateLocation() {
@Override
public Reader read() {
return new StringReader(found.getContent());
}

@Override
public Optional<Variant> getVariant() {
return Optional.empty();
}
});
} catch (IOException e) {
LOGGER.warn("Unable to read the template from path: " + found.getFullPath(), e);
}
@Override
public Optional<Variant> getVariant() {
return Optional.empty();
}
});
}
return Optional.empty();
}
Expand Down Expand Up @@ -1230,15 +1227,27 @@ void collectTemplates(ApplicationArchivesBuildItem applicationArchives,
// Skip extension archives that are also application archives
continue;
}
for (Path p : artifact.getResolvedPaths()) {
if (Files.isDirectory(p)) {
for (Path path : artifact.getResolvedPaths()) {
if (Files.isDirectory(path)) {
// Try to find the templates in the root dir
Path basePath = Files.list(p).filter(QuteProcessor::isBasePath).findFirst().orElse(null);
Path basePath = Files.list(path).filter(QuteProcessor::isBasePath).findFirst().orElse(null);
if (basePath != null) {
LOGGER.debugf("Found extension templates dir: %s", p);
basePaths.add(basePath);
LOGGER.debugf("Found extension templates dir: %s", path);
scan(basePath, basePath, BASE_PATH + "/", watchedPaths, templatePaths, nativeImageResources,
config.templatePathExclude);
break;
}
} else {
try (FileSystem artifactFs = ZipUtils.newFileSystem(path)) {
Path basePath = artifactFs.getPath(BASE_PATH);
if (Files.exists(basePath)) {
LOGGER.debugf("Found extension templates in: %s", path);
scan(basePath, basePath, BASE_PATH + "/", watchedPaths, templatePaths, nativeImageResources,
config.templatePathExclude);
}
} catch (IOException e) {
LOGGER.warnf(e, "Unable to create the file system from the path: %s", path);
}
}
}
}
Expand All @@ -1250,14 +1259,12 @@ void collectTemplates(ApplicationArchivesBuildItem applicationArchives,
if (basePath != null) {
LOGGER.debugf("Found templates dir: %s", basePath);
basePaths.add(basePath);
scan(basePath, basePath, BASE_PATH + "/", watchedPaths, templatePaths, nativeImageResources,
config.templatePathExclude);
break;
}
}
}
for (Path base : basePaths) {
scan(base, base, BASE_PATH + "/", watchedPaths, templatePaths, nativeImageResources,
config.templatePathExclude);
}
}

@BuildStep
Expand Down Expand Up @@ -2141,7 +2148,7 @@ private static void produceTemplateBuildItems(BuildProducer<TemplatePathBuildIte
// NOTE: we cannot just drop the template because a template param can be added
watchedPaths.produce(new HotDeploymentWatchedFileBuildItem(fullPath, true));
nativeImageResources.produce(new NativeImageResourceBuildItem(fullPath));
templatePaths.produce(new TemplatePathBuildItem(filePath, originalPath));
templatePaths.produce(new TemplatePathBuildItem(filePath, originalPath, readTemplateContent(originalPath)));
}

private void scan(Path root, Path directory, String basePath, BuildProducer<HotDeploymentWatchedFileBuildItem> watchedPaths,
Expand Down Expand Up @@ -2218,6 +2225,14 @@ private boolean isApplicationArchive(ResolvedDependency dependency, Set<Applicat
return false;
}

static String readTemplateContent(Path path) {
try {
return Files.readString(path);
} catch (IOException e) {
throw new UncheckedIOException("Unable to read the template content from path: " + path, e);
}
}

/**
* Java members lookup config.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ public final class TemplatePathBuildItem extends MultiBuildItem {

private final String path;
private final Path fullPath;
private final String content;

public TemplatePathBuildItem(String path, Path fullPath) {
public TemplatePathBuildItem(String path, Path fullPath, String content) {
this.path = path;
this.fullPath = fullPath;
this.content = content;
}

/**
Expand Down Expand Up @@ -49,4 +51,8 @@ public boolean isRegular() {
return !isTag();
}

public String getContent() {
return content;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@
import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_TYPE;
import static io.quarkus.deployment.annotations.ExecutionTime.STATIC_INIT;

import java.io.IOException;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -124,18 +120,9 @@ private CheckedTemplateBuildItem findCheckedTemplate(String basePath, List<Check
private Map<String, String> processVariants(List<TemplatePathBuildItem> templatePaths, List<String> variants) {
Map<String, String> variantsMap = new HashMap<>();
for (String variant : variants) {
String source = "";
Path sourcePath = templatePaths.stream().filter(p -> p.getPath().equals(variant))
.map(TemplatePathBuildItem::getFullPath).findFirst()
String source = templatePaths.stream().filter(p -> p.getPath().equals(variant))
.map(TemplatePathBuildItem::getContent).findFirst()
.orElse(null);
if (sourcePath != null) {
try {
byte[] content = Files.readAllBytes(sourcePath);
source = new String(content, StandardCharsets.UTF_8);
} catch (IOException e) {
LOG.warn("Unable to read the template from path: " + sourcePath, e);
}
}
source = source.replace("\n", "\\n");
variantsMap.put(variant, source);
}
Expand Down

0 comments on commit 99a76b7

Please sign in to comment.