forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request quarkusio#37005 from mkouba/qute-additional-base-path
Qute: allow extensions to register additional template roots
- Loading branch information
Showing
8 changed files
with
325 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
...sions/qute/deployment/src/main/java/io/quarkus/qute/deployment/TemplateRootBuildItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package io.quarkus.qute.deployment; | ||
|
||
import io.quarkus.builder.item.MultiBuildItem; | ||
|
||
/** | ||
* This build item represents a source of template files. | ||
* <p> | ||
* By default, the templates are found in the {@code templates} directory. However, an extension can produce this build item to | ||
* register an additional root path. | ||
* <p> | ||
* The path is relative to the artifact/project root and OS-agnostic, i.e. {@code /} is used as a path separator. | ||
*/ | ||
public final class TemplateRootBuildItem extends MultiBuildItem { | ||
|
||
private final String path; | ||
|
||
public TemplateRootBuildItem(String path) { | ||
this.path = normalize(path); | ||
} | ||
|
||
public String getPath() { | ||
return path; | ||
} | ||
|
||
static String normalize(String path) { | ||
path = path.strip(); | ||
if (path.startsWith("/")) { | ||
path = path.substring(1); | ||
} | ||
if (path.endsWith("/")) { | ||
path = path.substring(0, path.length() - 1); | ||
} | ||
return path; | ||
} | ||
|
||
} |
72 changes: 72 additions & 0 deletions
72
...ions/qute/deployment/src/main/java/io/quarkus/qute/deployment/TemplateRootsBuildItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package io.quarkus.qute.deployment; | ||
|
||
import java.io.File; | ||
import java.nio.file.Path; | ||
import java.util.Iterator; | ||
import java.util.Set; | ||
|
||
import io.quarkus.builder.item.SimpleBuildItem; | ||
|
||
/** | ||
* The set of template root paths. | ||
*/ | ||
public final class TemplateRootsBuildItem extends SimpleBuildItem implements Iterable<String> { | ||
|
||
private Set<String> rootPaths; | ||
|
||
public TemplateRootsBuildItem(Set<String> paths) { | ||
this.rootPaths = paths; | ||
} | ||
|
||
public Set<String> getPaths() { | ||
return rootPaths; | ||
} | ||
|
||
@Override | ||
public Iterator<String> iterator() { | ||
return rootPaths.iterator(); | ||
} | ||
|
||
/** | ||
* The path must be relative to the resource root. | ||
* | ||
* @param path | ||
* @return {@code true} is the given path represents a template root, {@code false} otherwise | ||
*/ | ||
public boolean isRoot(Path path) { | ||
String pathStr = normalize(path); | ||
for (String rootPath : rootPaths) { | ||
if (pathStr.equals(rootPath)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* The path must be relative to the resource root. | ||
* | ||
* @param path | ||
* @return {@code true} is the given path may represent a template root, {@code false} otherwise | ||
*/ | ||
public boolean maybeRoot(Path path) { | ||
String pathStr = normalize(path); | ||
for (String rootPath : rootPaths) { | ||
if ((rootPath.contains("/") && rootPath.startsWith(pathStr)) | ||
|| rootPath.equals(pathStr)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
private static String normalize(Path path) { | ||
String pathStr = path.toString(); | ||
if (File.separatorChar != '/') { | ||
// \foo\bar\templates -> /foo/bar/templates | ||
pathStr = pathStr.replace(File.separatorChar, '/'); | ||
} | ||
return TemplateRootBuildItem.normalize(pathStr); | ||
} | ||
|
||
} |
57 changes: 57 additions & 0 deletions
57
...ent/src/test/java/io/quarkus/qute/deployment/templateroot/AdditionalTemplateRootTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package io.quarkus.qute.deployment.templateroot; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.util.function.Consumer; | ||
|
||
import jakarta.inject.Inject; | ||
|
||
import org.jboss.shrinkwrap.api.asset.StringAsset; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.builder.BuildChainBuilder; | ||
import io.quarkus.builder.BuildContext; | ||
import io.quarkus.builder.BuildStep; | ||
import io.quarkus.qute.Engine; | ||
import io.quarkus.qute.Template; | ||
import io.quarkus.qute.deployment.TemplateRootBuildItem; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class AdditionalTemplateRootTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.withApplicationRoot(root -> root | ||
.addAsResource(new StringAsset("Hi {name}!"), "templates/hi.txt") | ||
.addAsResource(new StringAsset("Hello {name}!"), "web/public/hello.txt")) | ||
.addBuildChainCustomizer(buildCustomizer()); | ||
|
||
static Consumer<BuildChainBuilder> buildCustomizer() { | ||
return new Consumer<BuildChainBuilder>() { | ||
@Override | ||
public void accept(BuildChainBuilder builder) { | ||
builder.addBuildStep(new BuildStep() { | ||
@Override | ||
public void execute(BuildContext context) { | ||
context.produce(new TemplateRootBuildItem("web/public")); | ||
} | ||
}).produces(TemplateRootBuildItem.class) | ||
.build(); | ||
} | ||
}; | ||
} | ||
|
||
@Inject | ||
Template hello; | ||
|
||
@Inject | ||
Engine engine; | ||
|
||
@Test | ||
public void testTemplate() { | ||
assertEquals("Hi M!", engine.getTemplate("hi").data("name", "M").render()); | ||
assertEquals("Hello M!", hello.data("name", "M").render()); | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
...ment/src/test/java/io/quarkus/qute/deployment/templateroot/TemplateRootBuildItemTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package io.quarkus.qute.deployment.templateroot; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import io.quarkus.qute.deployment.TemplateRootBuildItem; | ||
|
||
public class TemplateRootBuildItemTest { | ||
|
||
@Test | ||
public void testNormalizedName() { | ||
assertEquals("foo", new TemplateRootBuildItem("/foo/ ").getPath()); | ||
assertEquals("foo/bar", new TemplateRootBuildItem("/foo/bar").getPath()); | ||
assertEquals("baz", new TemplateRootBuildItem(" baz/").getPath()); | ||
} | ||
|
||
} |
Oops, something went wrong.