Skip to content

Commit

Permalink
[Gradle] Support Build Cache for TwirlCompile task
Browse files Browse the repository at this point in the history
  • Loading branch information
ihostage committed Sep 12, 2023
1 parent cff0390 commit c00e6ab
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
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")
}
}

0 comments on commit c00e6ab

Please sign in to comment.