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 Build Cache for TwirlCompile task #677

Merged
merged 1 commit into from
Sep 12, 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
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.gradle.api.provider.MapProperty;
import org.gradle.api.provider.Property;
import org.gradle.api.provider.SetProperty;
import org.gradle.api.tasks.CacheableTask;
import org.gradle.api.tasks.Classpath;
import org.gradle.api.tasks.IgnoreEmptyDirectories;
import org.gradle.api.tasks.Input;
Expand All @@ -34,6 +35,7 @@
import play.twirl.gradle.internal.TwirlCompileAction;

/** Gradle task for compiling Twirl templates into Scala code. */
@CacheableTask
public abstract class TwirlCompile extends DefaultTask {

@InputFiles
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.Map;
import org.apache.commons.io.FileUtils;
import org.apache.groovy.util.Maps;
Expand Down Expand Up @@ -37,6 +38,11 @@ protected String getBuildFileContent() {
return templateProcess("build.gradle.kts.ftlh", params);
}

@Override
protected String getSettingsFileContent() {
return templateProcess("settings.gradle.kts.ftlh", Collections.emptyMap());
}

@ParameterizedTest
@MethodSource("gradleVersions")
@DisplayName("Test common build")
Expand Down Expand Up @@ -125,4 +131,63 @@ void testIncrementalBuild(String gradleVersion) throws IOException {
assertThat(compileScalaResult.getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
assertThat(projectBuildPath("classes/scala/main/a/b/html/d.class")).doesNotExist();
}

@ParameterizedTest
@MethodSource("gradleVersions")
@DisplayName("Test build cache")
void testBuildCache(String gradleVersion) throws IOException {
File simpleSources = projectPath("src").toFile();
FileUtils.copyDirectory(projectSourcePath("src").toFile(), simpleSources);
Path newTemplate = projectPath("src/main/twirl/a/b/d.scala.html");
Files.copy(projectSourcePath("src/main/twirl/a/b/c.scala.html"), newTemplate);

BuildResult result = build(gradleVersion, "--build-cache", "compileTwirl");

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();
assertThat(projectBuildPath("generated/sources/twirl/main/a/b/html/d.template.scala"))
.isNotEmptyFile();

build(gradleVersion, "clean");

assertThat(projectBuildPath("generated/sources/twirl/main/a/b/html/c.template.scala"))
.doesNotExist();
assertThat(projectBuildPath("generated/sources/twirl/main/a/b/html/d.template.scala"))
.doesNotExist();

result = build(gradleVersion, "--build-cache", "compileTwirl");

compileTwirlResult = result.task(":compileTwirl");
assertThat(compileTwirlResult).isNotNull();
assertThat(compileTwirlResult.getOutcome()).isEqualTo(TaskOutcome.FROM_CACHE);

assertThat(projectBuildPath("generated/sources/twirl/main/a/b/html/c.template.scala"))
.isNotEmptyFile();
assertThat(projectBuildPath("generated/sources/twirl/main/a/b/html/d.template.scala"))
.isNotEmptyFile();

build(gradleVersion, "clean");

assertThat(projectBuildPath("generated/sources/twirl/main/a/b/html/c.template.scala"))
.doesNotExist();
assertThat(projectBuildPath("generated/sources/twirl/main/a/b/html/d.template.scala"))
.doesNotExist();

Files.delete(newTemplate);

result = build(gradleVersion, "--build-cache", "compileTwirl");

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();
assertThat(projectBuildPath("generated/sources/twirl/main/a/b/html/d.template.scala"))
.doesNotExist();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
buildCache {
local {
directory = File(rootProject.projectDir, "build-cache")
}
}
Loading