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..6fca9429 100644 --- a/gradle-twirl/src/test/java/play/twirl/gradle/AbstractFunctionalTest.java +++ b/gradle-twirl/src/test/java/play/twirl/gradle/AbstractFunctionalTest.java @@ -7,7 +7,6 @@ import freemarker.template.Configuration; import freemarker.template.Template; -import freemarker.template.TemplateException; import java.io.File; import java.io.IOException; import java.io.StringWriter; @@ -59,7 +58,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 +86,7 @@ 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(); } } 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..7a25c959 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.ValueSource; /** 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 + @ValueSource(strings = {"7.1.1", "7.6.2", "8.0.2", "8.3"}) @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.