From 2eb7a7fc9bd128c3565d3b24ead8f9b16b886a2c Mon Sep 17 00:00:00 2001 From: Andy Damevin Date: Mon, 2 May 2022 09:46:34 +0200 Subject: [PATCH] Make templates adjust with scala version --- .../buildtool/maven/scala/pom.tpl.qute.xml | 5 +++ .../core/reader/QuteCodestartFileReader.java | 45 +++++++++++++++---- 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/independent-projects/tools/base-codestarts/src/main/resources/codestarts/quarkus/buildtool/maven/scala/pom.tpl.qute.xml b/independent-projects/tools/base-codestarts/src/main/resources/codestarts/quarkus/buildtool/maven/scala/pom.tpl.qute.xml index 36f80ff35aa40..94f4093a1e466 100644 --- a/independent-projects/tools/base-codestarts/src/main/resources/codestarts/quarkus/buildtool/maven/scala/pom.tpl.qute.xml +++ b/independent-projects/tools/base-codestarts/src/main/resources/codestarts/quarkus/buildtool/maven/scala/pom.tpl.qute.xml @@ -52,7 +52,12 @@ -deprecation -feature -explaintypes + {#if scala.version.startsWith("2.12.")} + -target:jvm-1.8 + -Ypartial-unification + {#else} -target:jvm-11 + {/if} diff --git a/independent-projects/tools/codestarts/src/main/java/io/quarkus/devtools/codestarts/core/reader/QuteCodestartFileReader.java b/independent-projects/tools/codestarts/src/main/java/io/quarkus/devtools/codestarts/core/reader/QuteCodestartFileReader.java index 595d17981b581..fc32e43d46e47 100644 --- a/independent-projects/tools/codestarts/src/main/java/io/quarkus/devtools/codestarts/core/reader/QuteCodestartFileReader.java +++ b/independent-projects/tools/codestarts/src/main/java/io/quarkus/devtools/codestarts/core/reader/QuteCodestartFileReader.java @@ -3,19 +3,13 @@ import io.quarkus.devtools.codestarts.CodestartException; import io.quarkus.devtools.codestarts.CodestartResource; import io.quarkus.devtools.codestarts.CodestartResource.Source; -import io.quarkus.qute.Engine; -import io.quarkus.qute.Expression; -import io.quarkus.qute.ResultMapper; -import io.quarkus.qute.Results; -import io.quarkus.qute.TemplateException; -import io.quarkus.qute.TemplateLocator; -import io.quarkus.qute.TemplateNode; -import io.quarkus.qute.Variant; +import io.quarkus.qute.*; import java.io.IOException; import java.io.Reader; import java.io.StringReader; import java.util.Map; import java.util.Optional; +import java.util.concurrent.CompletionStage; import org.apache.commons.io.FilenameUtils; final class QuteCodestartFileReader implements CodestartFileReader { @@ -49,6 +43,7 @@ public static String readQuteFile(CodestartResource projectResource, Source sour final String content = source.read(); final String templateId = source.absolutePath(); final Engine engine = Engine.builder().addDefaults() + .addValueResolver(new StringValueResolver()) .addResultMapper(new MissingValueMapper()) .removeStandaloneLines(true) // For now we need to disable strict rendering for codestarts @@ -142,4 +137,38 @@ public String map(Object result, Expression expression) { * } * } **/ + + private static class StringValueResolver implements ValueResolver { + @Override + public boolean appliesTo(EvalContext context) { + return ValueResolver.matchClass(context, String.class); + } + + @Override + public CompletionStage resolve(EvalContext context) { + String value = (String) context.getBase(); + switch (context.getName()) { + case "startsWith": + if (context.getParams().size() == 1) { + return context.evaluate(context.getParams().get(0)).thenCompose(e -> { + return CompletedStage.of(value.startsWith((String) e)); + }); + } + case "contains": + if (context.getParams().size() == 1) { + return context.evaluate(context.getParams().get(0)).thenCompose(e -> { + return CompletedStage.of(value.contains((CharSequence) e)); + }); + } + case "endsWith": + if (context.getParams().size() == 1) { + return context.evaluate(context.getParams().get(0)).thenCompose(e -> { + return CompletedStage.of(value.endsWith((String) e)); + }); + } + default: + return Results.notFound(context); + } + } + } }