Skip to content

Commit

Permalink
Create SourceDirectorySet for Twirl
Browse files Browse the repository at this point in the history
  • Loading branch information
ihostage committed Aug 25, 2023
1 parent 86c08b4 commit 21aa4d2
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 17 deletions.
2 changes: 1 addition & 1 deletion gradle-twirl/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ gradlePlugin {
website.set("https://www.playframework.com/documentation/latest/ScalaTemplates")
vcsUrl.set("https://github.com/playframework/twirl")
// Define the plugin
val greeting by plugins.creating {
val twirl by plugins.creating {
id = "com.typesafe.play.twirl" // TODO: rename to "com.playframework.twirl"
displayName = "Twirl Plugin"
description = "A Gradle plugin to compile Twirl templates"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
*/
package play.twirl.gradle;

import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
Expand All @@ -29,18 +27,17 @@ private File getSettingsFile() {
@Test
void canRunTask() throws IOException {
writeString(getSettingsFile(), "");
writeString(getBuildFile(), "plugins {" + " id('com.typesafe.play.twirl')" + "}");
writeString(
getBuildFile(),
"plugins {" + "id 'application'\n" + " id('com.typesafe.play.twirl')" + "}");

// Run the build
GradleRunner runner = GradleRunner.create();
runner.forwardOutput();
runner.withPluginClasspath();
runner.withArguments("greeting");
runner.withArguments("compileTwirl");
runner.withProjectDir(projectDir);
BuildResult result = runner.build();

// Verify the result
assertTrue(result.getOutput().contains("Hello from plugin 'com.playframework.twirl'"));
}

private void writeString(File file, String string) throws IOException {
Expand Down
27 changes: 27 additions & 0 deletions gradle-twirl/src/main/java/play/twirl/gradle/TwirlCompile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (C) from 2022 The Play Framework Contributors <https://github.com/playframework>, 2011-2021 Lightbend Inc. <https://www.lightbend.com>
*/
package play.twirl.gradle;

import java.io.File;
import org.gradle.api.file.DirectoryProperty;
import org.gradle.api.tasks.OutputDirectory;
import org.gradle.api.tasks.SourceTask;
import org.gradle.api.tasks.TaskAction;

public abstract class TwirlCompile extends SourceTask {

@OutputDirectory
public abstract DirectoryProperty getDestinationDirectory();

@TaskAction
void compile() {
for (File sourceFile : getSource().getFiles()) {
try {
System.out.println("Compile Twirl template " + sourceFile.getName());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
}
87 changes: 79 additions & 8 deletions gradle-twirl/src/main/java/play/twirl/gradle/TwirlPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,90 @@
*/
package play.twirl.gradle;

import javax.inject.Inject;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.internal.lambdas.SerializableLambdas;
import org.gradle.api.internal.tasks.DefaultSourceSet;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.plugins.ExtensionAware;
import org.gradle.api.plugins.JavaPluginExtension;
import org.gradle.api.plugins.scala.ScalaBasePlugin;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.TaskProvider;
import play.twirl.gradle.internal.DefaultTwirlSourceDirectorySet;

/** A simple 'hello world' plugin. */
public class TwirlPlugin implements Plugin<Project> {
public void apply(Project project) {
// Register a task
project
.getTasks()
.register(
"greeting",
task -> {
task.doLast(s -> System.out.println("Hello from plugin 'com.playframework.twirl'"));

private final ObjectFactory objectFactory;

@Inject
public TwirlPlugin(ObjectFactory objectFactory) {
this.objectFactory = objectFactory;
}

@Override
public void apply(final Project project) {
project.getPluginManager().apply(ScalaBasePlugin.class);

configureSourceSetDefaults(project);
}

private void configureSourceSetDefaults(final Project project) {
javaPluginExtension(project)
.getSourceSets()
.all(
(sourceSet) -> {
TwirlSourceDirectorySet twirlSource = getTwirlSourceDirectorySet(sourceSet);
sourceSet.getExtensions().add(TwirlSourceDirectorySet.class, "twirl", twirlSource);
twirlSource.srcDir(project.file("src/" + sourceSet.getName() + "/twirl"));
sourceSet
.getResources()
.getFilter()
.exclude(
SerializableLambdas.spec(
(element) -> twirlSource.contains(element.getFile())));
sourceSet.getAllJava().source(twirlSource);
sourceSet.getAllSource().source(twirlSource);
createTwirlCompileTask(project, sourceSet, twirlSource);
});
}

private void createTwirlCompileTask(
final Project project, final SourceSet sourceSet, TwirlSourceDirectorySet twirlSource) {
final TaskProvider<TwirlCompile> twirlTask =
project
.getTasks()
.register(
sourceSet.getCompileTaskName("twirl"),
TwirlCompile.class,
twirlCompile -> {
twirlCompile.setDescription("Compiles the " + twirlSource + ".");
twirlCompile.setSource(twirlSource);
twirlCompile
.getDestinationDirectory()
.convention(project.getLayout().getBuildDirectory())
.dir(
"generated/sources/" + twirlSource.getName() + "/" + sourceSet.getName());
});
}

private TwirlSourceDirectorySet getTwirlSourceDirectorySet(SourceSet sourceSet) {
String displayName = ((DefaultSourceSet) sourceSet).getDisplayName();
TwirlSourceDirectorySet twirlSourceDirectorySet =
objectFactory.newInstance(
DefaultTwirlSourceDirectorySet.class,
objectFactory.sourceDirectorySet("twirl", displayName + " Twirl source"));
twirlSourceDirectorySet.getFilter().include("**/*.scala.*");
return twirlSourceDirectorySet;
}

private static JavaPluginExtension javaPluginExtension(Project project) {
return extensionOf(project, JavaPluginExtension.class);
}

private static <T> T extensionOf(ExtensionAware extensionAware, Class<T> type) {
return extensionAware.getExtensions().getByType(type);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* Copyright (C) from 2022 The Play Framework Contributors <https://github.com/playframework>, 2011-2021 Lightbend Inc. <https://www.lightbend.com>
*/
package play.twirl.gradle;

import org.gradle.api.file.SourceDirectorySet;

/**
* A {@code TwirlSourceDirectorySet} defines the properties and methods added to a {@link
* org.gradle.api.tasks.SourceSet} by the {@code TwirlPlugin}.
*/
public interface TwirlSourceDirectorySet extends SourceDirectorySet {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* 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.internal.file.DefaultSourceDirectorySet;
import org.gradle.api.internal.tasks.TaskDependencyFactory;
import play.twirl.gradle.TwirlSourceDirectorySet;

public class DefaultTwirlSourceDirectorySet extends DefaultSourceDirectorySet
implements TwirlSourceDirectorySet {
@Inject
public DefaultTwirlSourceDirectorySet(
SourceDirectorySet sourceDirectorySet, TaskDependencyFactory taskDependencyFactory) {
super(sourceDirectorySet, taskDependencyFactory); // Gradle 8+
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ class TwirlPluginTest {
void pluginRegistersATask() {
// Create a test project and apply the plugin
Project project = ProjectBuilder.builder().build();
project.getPlugins().apply("application");
project.getPlugins().apply("com.typesafe.play.twirl");

// Verify the result
assertNotNull(project.getTasks().findByName("greeting"));
assertNotNull(project.getTasks().findByName("compileTwirl"));
assertNotNull(project.getTasks().findByName("compileTestTwirl"));
}
}

0 comments on commit 21aa4d2

Please sign in to comment.