Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Gradle] Support Gradle 7.1+ #670

Merged
merged 1 commit into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions gradle-twirl/src/main/java/play/twirl/gradle/TwirlPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Project> {
Expand Down Expand Up @@ -140,10 +142,18 @@ private TaskProvider<TwirlCompile> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> getTemplateFormats() {
return templateFormats;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright (C) from 2022 The Play Framework Contributors <https://github.com/playframework>, 2011-2021 Lightbend Inc. <https://www.lightbend.com>
*/
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -87,7 +87,15 @@ protected String templateProcess(String template, Map<String, Object> 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<String> 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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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();
Expand Down
9 changes: 9 additions & 0 deletions gradle-twirl/src/test/resources/simple/build.gradle.kts.ftlh
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,12 @@ dependencies {
twirl {
scalaVersion.set("${scalaVersion}")
}

sourceSets {
main {
twirl {
templateImports.add("java.lang._")
constructorAnnotations.add("@java.lang.Deprecated()")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
* Copyright (C) from 2022 The Play Framework Contributors <https://github.com/playframework>, 2011-2021 Lightbend Inc. <https://www.lightbend.com> *
****************************************************************************************************************************************************@

@this(str: String)
@(name: String)
Hello, @name.
Loading