From b71d5a66b8cac817168d74a93afba100f2eb1cfb Mon Sep 17 00:00:00 2001 From: Sergey Morgunov Date: Wed, 6 Sep 2023 15:53:38 +0300 Subject: [PATCH] [Gradle] Support Gradle 7.1+ --- .../java/play/twirl/gradle/TwirlPlugin.java | 18 ++++++++++++++---- .../DefaultTwirlSourceDirectorySet.java | 13 +++++++++++++ .../Gradle7TwirlSourceDirectorySet.java | 18 ++++++++++++++++++ .../twirl/gradle/AbstractFunctionalTest.java | 16 ++++++++++++---- .../gradle/TwirlPluginFunctionalTest.java | 17 +++++++++++------ .../resources/simple/build.gradle.kts.ftlh | 9 +++++++++ .../simple/src/main/twirl/a/b/c.scala.html | 1 + 7 files changed, 78 insertions(+), 14 deletions(-) create mode 100644 gradle-twirl/src/main/java/play/twirl/gradle/internal/Gradle7TwirlSourceDirectorySet.java diff --git a/gradle-twirl/src/main/java/play/twirl/gradle/TwirlPlugin.java b/gradle-twirl/src/main/java/play/twirl/gradle/TwirlPlugin.java index 7c7b33a0..96b5befe 100644 --- a/gradle-twirl/src/main/java/play/twirl/gradle/TwirlPlugin.java +++ b/gradle-twirl/src/main/java/play/twirl/gradle/TwirlPlugin.java @@ -19,7 +19,9 @@ import org.gradle.api.tasks.ScalaSourceDirectorySet; import org.gradle.api.tasks.SourceSet; import org.gradle.api.tasks.TaskProvider; +import org.gradle.util.GradleVersion; import play.twirl.gradle.internal.DefaultTwirlSourceDirectorySet; +import play.twirl.gradle.internal.Gradle7TwirlSourceDirectorySet; /** A simple 'hello world' plugin. */ public class TwirlPlugin implements Plugin { @@ -140,10 +142,18 @@ private TaskProvider createTwirlCompileTask( private TwirlSourceDirectorySet getTwirlSourceDirectorySet(SourceSet sourceSet) { String displayName = ((DefaultSourceSet) sourceSet).getDisplayName(); - TwirlSourceDirectorySet twirlSourceDirectorySet = - objectFactory.newInstance( - DefaultTwirlSourceDirectorySet.class, - objectFactory.sourceDirectorySet("twirl", displayName + " Twirl source")); + TwirlSourceDirectorySet twirlSourceDirectorySet; + if (GradleVersion.current().compareTo(GradleVersion.version("8.0")) < 0) { // Gradle < 8.0 + twirlSourceDirectorySet = + objectFactory.newInstance( + Gradle7TwirlSourceDirectorySet.class, + objectFactory.sourceDirectorySet("twirl", displayName + " Twirl source")); + } else { // Gradle 8+ + twirlSourceDirectorySet = + objectFactory.newInstance( + DefaultTwirlSourceDirectorySet.class, + objectFactory.sourceDirectorySet("twirl", displayName + " Twirl source")); + } twirlSourceDirectorySet.getFilter().include("**/*.scala.*"); twirlSourceDirectorySet.getTemplateFormats().convention(DEFAULT_TEMPLATE_FORMATS); return twirlSourceDirectorySet; diff --git a/gradle-twirl/src/main/java/play/twirl/gradle/internal/DefaultTwirlSourceDirectorySet.java b/gradle-twirl/src/main/java/play/twirl/gradle/internal/DefaultTwirlSourceDirectorySet.java index 83c360b5..553a7145 100644 --- a/gradle-twirl/src/main/java/play/twirl/gradle/internal/DefaultTwirlSourceDirectorySet.java +++ b/gradle-twirl/src/main/java/play/twirl/gradle/internal/DefaultTwirlSourceDirectorySet.java @@ -39,6 +39,19 @@ public DefaultTwirlSourceDirectorySet( this.sourceEncoding = objectFactory.property(String.class).convention(UTF_8.name()); } + /** + * @deprecated Constructor for support Gradle 7.x + */ + @Deprecated + public DefaultTwirlSourceDirectorySet( + SourceDirectorySet sourceDirectorySet, ObjectFactory objectFactory) { + super(sourceDirectorySet); + this.templateFormats = objectFactory.mapProperty(String.class, String.class); + this.templateImports = objectFactory.setProperty(String.class); + this.constructorAnnotations = objectFactory.listProperty(String.class); + this.sourceEncoding = objectFactory.property(String.class).convention(UTF_8.name()); + } + @Override public MapProperty getTemplateFormats() { return templateFormats; diff --git a/gradle-twirl/src/main/java/play/twirl/gradle/internal/Gradle7TwirlSourceDirectorySet.java b/gradle-twirl/src/main/java/play/twirl/gradle/internal/Gradle7TwirlSourceDirectorySet.java new file mode 100644 index 00000000..f79330e6 --- /dev/null +++ b/gradle-twirl/src/main/java/play/twirl/gradle/internal/Gradle7TwirlSourceDirectorySet.java @@ -0,0 +1,18 @@ +/* + * Copyright (C) from 2022 The Play Framework Contributors , 2011-2021 Lightbend Inc. + */ +package play.twirl.gradle.internal; + +import javax.inject.Inject; +import org.gradle.api.file.SourceDirectorySet; +import org.gradle.api.model.ObjectFactory; + +@Deprecated +public class Gradle7TwirlSourceDirectorySet extends DefaultTwirlSourceDirectorySet { + + @Inject + public Gradle7TwirlSourceDirectorySet( + SourceDirectorySet sourceDirectorySet, ObjectFactory objectFactory) { + super(sourceDirectorySet, objectFactory); + } +} diff --git a/gradle-twirl/src/test/java/play/twirl/gradle/AbstractFunctionalTest.java b/gradle-twirl/src/test/java/play/twirl/gradle/AbstractFunctionalTest.java index c52bae43..7396a90a 100644 --- a/gradle-twirl/src/test/java/play/twirl/gradle/AbstractFunctionalTest.java +++ b/gradle-twirl/src/test/java/play/twirl/gradle/AbstractFunctionalTest.java @@ -7,13 +7,13 @@ import freemarker.template.Configuration; import freemarker.template.Template; -import freemarker.template.TemplateException; import java.io.File; import java.io.IOException; import java.io.StringWriter; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Map; +import java.util.stream.Stream; import org.apache.commons.io.FileUtils; import org.gradle.testkit.runner.BuildResult; import org.gradle.testkit.runner.GradleRunner; @@ -59,7 +59,7 @@ protected Path projectBuildPath(String path) { } @BeforeEach - void init() throws IOException, TemplateException { + void init() throws IOException { projectSourceDir = getProjectSourceDir(); runner = GradleRunner.create().withProjectDir(projectDir).withPluginClasspath().forwardOutput(); @@ -87,7 +87,15 @@ protected String templateProcess(String template, Map params) { return writer.toString(); } - protected BuildResult build(String... args) { - return runner.withArguments(args).build(); + protected BuildResult build(String gradleVersion, String... args) { + return runner.withGradleVersion(gradleVersion).withArguments(args).build(); + } + + static Stream gradleVersions() { + // https://docs.gradle.org/current/userguide/scala_plugin.html#sec:configure_zinc_compiler + if (getScalaVersion().equals("3")) { // Gradle 7.5+ + return Stream.of("7.6.2", "8.0.2", "8.3"); + } + return Stream.of("7.1.1", "7.6.2", "8.0.2", "8.3"); } } diff --git a/gradle-twirl/src/test/java/play/twirl/gradle/TwirlPluginFunctionalTest.java b/gradle-twirl/src/test/java/play/twirl/gradle/TwirlPluginFunctionalTest.java index d78c6032..de5cb1e9 100644 --- a/gradle-twirl/src/test/java/play/twirl/gradle/TwirlPluginFunctionalTest.java +++ b/gradle-twirl/src/test/java/play/twirl/gradle/TwirlPluginFunctionalTest.java @@ -14,7 +14,8 @@ import org.gradle.testkit.runner.BuildTask; import org.gradle.testkit.runner.TaskOutcome; import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; /** A simple functional test to check a Twirl Gradle Plugin. */ public class TwirlPluginFunctionalTest extends AbstractFunctionalTest { @@ -33,26 +34,30 @@ protected String getBuildFileContent() { return templateProcess("build.gradle.kts.ftlh", params); } - @Test + @ParameterizedTest + @MethodSource("gradleVersions") @DisplayName("Test simple Gradle project with Twirl HTML template") - void testSimpleGradleProject() throws IOException { + void testSimpleGradleProject(String gradleVersion) throws IOException { File simpleSources = projectPath("src").toFile(); FileUtils.copyDirectory(projectSourcePath("src").toFile(), simpleSources); - BuildResult result = build("build"); + BuildResult result = build(gradleVersion, "build"); BuildTask compileTwirlResult = result.task(":compileTwirl"); assertThat(compileTwirlResult).isNotNull(); assertThat(compileTwirlResult.getOutcome()).isEqualTo(TaskOutcome.SUCCESS); assertThat(projectBuildPath("generated/sources/twirl/main/a/b/html/c.template.scala")) - .isNotEmptyFile(); + .isNotEmptyFile() + .binaryContent() + .asString() + .contains("import java.lang._", "class c @java.lang.Deprecated()"); BuildTask compileScalaResult = result.task(":compileScala"); assertThat(compileScalaResult).isNotNull(); assertThat(compileScalaResult.getOutcome()).isEqualTo(TaskOutcome.SUCCESS); assertThat(projectBuildPath("classes/scala/main/a/b/html/c.class")).isNotEmptyFile(); - result = build("build"); + result = build(gradleVersion, "build"); compileTwirlResult = result.task(":compileTwirl"); assertThat(compileTwirlResult).isNotNull(); diff --git a/gradle-twirl/src/test/resources/simple/build.gradle.kts.ftlh b/gradle-twirl/src/test/resources/simple/build.gradle.kts.ftlh index 0425e83b..8732cfe5 100644 --- a/gradle-twirl/src/test/resources/simple/build.gradle.kts.ftlh +++ b/gradle-twirl/src/test/resources/simple/build.gradle.kts.ftlh @@ -15,3 +15,12 @@ dependencies { twirl { scalaVersion.set("${scalaVersion}") } + +sourceSets { + main { + twirl { + templateImports.add("java.lang._") + constructorAnnotations.add("@java.lang.Deprecated()") + } + } +} diff --git a/gradle-twirl/src/test/resources/simple/src/main/twirl/a/b/c.scala.html b/gradle-twirl/src/test/resources/simple/src/main/twirl/a/b/c.scala.html index 7f0f12e5..31770d9d 100644 --- a/gradle-twirl/src/test/resources/simple/src/main/twirl/a/b/c.scala.html +++ b/gradle-twirl/src/test/resources/simple/src/main/twirl/a/b/c.scala.html @@ -2,5 +2,6 @@ * Copyright (C) from 2022 The Play Framework Contributors , 2011-2021 Lightbend Inc. * ****************************************************************************************************************************************************@ +@this(str: String) @(name: String) Hello, @name.