Skip to content
This repository has been archived by the owner on Jan 27, 2020. It is now read-only.

Commit

Permalink
Configure net.ltgt.apt-eclipse tasks lazily in Gradle 4.9
Browse files Browse the repository at this point in the history
  • Loading branch information
tbroyer committed Jul 20, 2018
1 parent 5e8b10f commit 432509e
Show file tree
Hide file tree
Showing 13 changed files with 219 additions and 124 deletions.
271 changes: 156 additions & 115 deletions src/main/java/net/ltgt/gradle/apt/AptEclipsePlugin.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,51 @@
package net.ltgt.gradle.apt;

import static net.ltgt.gradle.apt.CompatibilityUtils.getOutputs;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Callable;
import javax.annotation.Nullable;
import javax.inject.Inject;
import org.codehaus.groovy.runtime.MethodClosure;
import org.gradle.api.Action;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.internal.ConventionMapping;
import org.gradle.api.internal.IConventionAware;
import org.gradle.api.internal.PropertiesTransformer;
import org.gradle.api.plugins.ExtensionAware;
import org.gradle.api.plugins.JavaPlugin;
import org.gradle.api.plugins.JavaPluginConvention;
import org.gradle.api.tasks.Delete;
import org.gradle.api.tasks.SourceSet;
import org.gradle.internal.reflect.Instantiator;
import org.gradle.internal.xml.XmlTransformer;
import org.gradle.plugins.ide.api.PropertiesFileContentMerger;
import org.gradle.plugins.ide.api.XmlFileContentMerger;
import org.gradle.plugins.ide.eclipse.EclipsePlugin;
import org.gradle.plugins.ide.eclipse.model.EclipseModel;

public class AptEclipsePlugin implements Plugin<Project> {

private static Action<Task> dependsOn(final Object taskDependency) {
return new Action<Task>() {
@Override
public void execute(Task task) {
task.dependsOn(taskDependency);
}
};
}

private final Instantiator instantiator;

@Inject
public AptEclipsePlugin(Instantiator instantiator) {
this.instantiator = instantiator;
}

@Override
public void apply(final Project project) {
project.getPlugins().apply(AptPlugin.class);
Expand Down Expand Up @@ -77,124 +98,144 @@ public void execute(final Project project) {
}
});
if (project.getTasks().findByName("eclipseJdtApt") == null) {
GenerateEclipseJdtApt task =
project
.getTasks()
.create(
"eclipseJdtApt",
GenerateEclipseJdtApt.class,
new Action<GenerateEclipseJdtApt>() {
final EclipseJdtApt jdtApt =
instantiator.newInstance(
EclipseJdtApt.class,
project,
new PropertiesFileContentMerger(new PropertiesTransformer()));
((ExtensionAware) eclipseModel.getJdt()).getExtensions().add("apt", jdtApt);
ConventionMapping conventionMapping = ((IConventionAware) jdtApt).getConventionMapping();
conventionMapping.map(
"aptEnabled",
new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return project
.getTasks()
.getByName(mainSourceSet.getCompileJavaTaskName())
.getExtensions()
.getByType(AptPlugin.AptOptions.class)
.isAnnotationProcessing();
}
});
conventionMapping.map(
"genSrcDir",
new Callable<File>() {
@Override
public File call() throws Exception {
return project.file(".apt_generated");
}
});
conventionMapping.map(
"processorOptions",
new Callable<Map<String, ?>>() {
@Nullable
@Override
public Map<String, ?> call() throws Exception {
return project
.getTasks()
.getByName(mainSourceSet.getCompileJavaTaskName())
.getExtensions()
.getByType(AptPlugin.AptOptions.class)
.getProcessorArgs();
}
});

eclipseModel
.getJdt()
.getFile()
.withProperties(
// withProperties(Action) overload was added in Gradle 2.14
new MethodClosure(
new Action<Properties>() {
@Override
public void execute(GenerateEclipseJdtApt generateEclipseJdtApt) {
generateEclipseJdtApt.setDescription(
"Generates the Eclipse JDT APT settings file.");
generateEclipseJdtApt.setInputFile(
project.file(".settings/org.eclipse.jdt.apt.core.prefs"));
generateEclipseJdtApt.setOutputFile(
project.file(".settings/org.eclipse.jdt.apt.core.prefs"));
public void execute(Properties properties) {
properties.setProperty(
"org.eclipse.jdt.core.compiler.processAnnotations",
jdtApt.isAptEnabled() ? "enabled" : "disabled");
}
},
"execute"));

final EclipseJdtApt jdtApt = generateEclipseJdtApt.getJdtApt();
((ExtensionAware) eclipseModel.getJdt()).getExtensions().add("apt", jdtApt);
ConventionMapping conventionMapping =
((IConventionAware) jdtApt).getConventionMapping();
conventionMapping.map(
"aptEnabled",
new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return project
.getTasks()
.getByName(mainSourceSet.getCompileJavaTaskName())
.getExtensions()
.getByType(AptPlugin.AptOptions.class)
.isAnnotationProcessing();
}
});
conventionMapping.map(
"genSrcDir",
new Callable<File>() {
@Override
public File call() throws Exception {
return project.file(".apt_generated");
}
});
conventionMapping.map(
"processorOptions",
new Callable<Map<String, ?>>() {
@Nullable
@Override
public Map<String, ?> call() throws Exception {
return project
.getTasks()
.getByName(mainSourceSet.getCompileJavaTaskName())
.getExtensions()
.getByType(AptPlugin.AptOptions.class)
.getProcessorArgs();
}
});
final Object task =
AptPlugin.IMPL.createTask(
project,
"eclipseJdtApt",
GenerateEclipseJdtApt.class,
new Action<GenerateEclipseJdtApt>() {
@Override
public void execute(GenerateEclipseJdtApt generateEclipseJdtApt) {
generateEclipseJdtApt.setDescription(
"Generates the Eclipse JDT APT settings file.");
generateEclipseJdtApt.setInputFile(
project.file(".settings/org.eclipse.jdt.apt.core.prefs"));
generateEclipseJdtApt.setOutputFile(
project.file(".settings/org.eclipse.jdt.apt.core.prefs"));

eclipseModel
.getJdt()
.getFile()
.withProperties(
// withProperties(Action) overload was added in Gradle 2.14
new MethodClosure(
new Action<Properties>() {
@Override
public void execute(Properties properties) {
properties.setProperty(
"org.eclipse.jdt.core.compiler.processAnnotations",
jdtApt.isAptEnabled() ? "enabled" : "disabled");
}
},
"execute"));
}
});
project.getTasks().getByName("eclipse").dependsOn(task);
Delete cleanTask = project.getTasks().create("cleanEclipseJdtApt", Delete.class);
cleanTask.delete(getOutputs(task));
project.getTasks().getByName("cleanEclipse").dependsOn(cleanTask);
generateEclipseJdtApt.setJdtApt(jdtApt);
}
});
AptPlugin.IMPL.configureTask(project, Task.class, "eclipse", dependsOn(task));
final Object cleanTask =
AptPlugin.IMPL.createTask(
project,
"cleanEclipseJdtApt",
Delete.class,
new Action<Delete>() {
@Override
public void execute(Delete cleanEclipseJdtApt) {
cleanEclipseJdtApt.delete(task);
}
});
AptPlugin.IMPL.configureTask(project, Task.class, "cleanEclipse", dependsOn(cleanTask));
}
if (project.getTasks().findByName("eclipseFactorypath") == null) {
GenerateEclipseFactorypath task =
project
.getTasks()
.create(
"eclipseFactorypath",
GenerateEclipseFactorypath.class,
new Action<GenerateEclipseFactorypath>() {
@Override
public void execute(GenerateEclipseFactorypath generateEclipseFactorypath) {
generateEclipseFactorypath.setDescription(
"Generates the Eclipse factorypath file.");
generateEclipseFactorypath.setInputFile(project.file(".factorypath"));
generateEclipseFactorypath.setOutputFile(project.file(".factorypath"));
if (!project.getTasks().getNames().contains("eclipseFactorypath")) {
final EclipseFactorypath factorypath =
instantiator.newInstance(
EclipseFactorypath.class, new XmlFileContentMerger(new XmlTransformer()));
((ExtensionAware) eclipseModel).getExtensions().add("factorypath", factorypath);
factorypath.setPlusConfigurations(
new ArrayList<>(
Arrays.asList(
project
.getConfigurations()
.getByName(
AptPlugin.IMPL.getAnnotationProcessorConfigurationName(mainSourceSet)),
project
.getConfigurations()
.getByName(
AptPlugin.IMPL.getAnnotationProcessorConfigurationName(testSourceSet)))));
final Object task =
AptPlugin.IMPL.createTask(
project,
"eclipseFactorypath",
GenerateEclipseFactorypath.class,
new Action<GenerateEclipseFactorypath>() {
@Override
public void execute(GenerateEclipseFactorypath generateEclipseFactorypath) {
generateEclipseFactorypath.setDescription(
"Generates the Eclipse factorypath file.");
generateEclipseFactorypath.setInputFile(project.file(".factorypath"));
generateEclipseFactorypath.setOutputFile(project.file(".factorypath"));

EclipseFactorypath factorypath = generateEclipseFactorypath.getFactorypath();
((ExtensionAware) eclipseModel)
.getExtensions()
.add("factorypath", factorypath);
factorypath.setPlusConfigurations(
new ArrayList<>(
Arrays.asList(
project
.getConfigurations()
.getByName(
AptPlugin.IMPL.getAnnotationProcessorConfigurationName(
mainSourceSet)),
project
.getConfigurations()
.getByName(
AptPlugin.IMPL.getAnnotationProcessorConfigurationName(
testSourceSet)))));
generateEclipseFactorypath.dependsOn(
factorypath.getPlusConfigurations().toArray());
}
});
project.getTasks().getByName("eclipse").dependsOn(task);
Delete cleanTask = project.getTasks().create("cleanEclipseFactorypath", Delete.class);
cleanTask.delete(getOutputs(task));
project.getTasks().getByName("cleanEclipse").dependsOn(cleanTask);
generateEclipseFactorypath.setFactorypath(factorypath);
generateEclipseFactorypath.dependsOn(
factorypath.getPlusConfigurations().toArray());
}
});
AptPlugin.IMPL.configureTask(project, Task.class, "eclipse", dependsOn(task));
final Object cleanTask =
AptPlugin.IMPL.createTask(
project,
"cleanEclipseFactorypath",
Delete.class,
new Action<Delete>() {
@Override
public void execute(Delete cleanEclipseFactorypath) {
cleanEclipseFactorypath.delete(task);
}
});
AptPlugin.IMPL.configureTask(project, Task.class, "cleanEclipse", dependsOn(cleanTask));
}
}
}
3 changes: 3 additions & 0 deletions src/main/java/net/ltgt/gradle/apt/AptPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,9 @@ static Impl newInstance() {
}
}

protected abstract <T extends Task> Object createTask(
Project project, String taskName, Class<T> taskClass, Action<T> configure);

protected abstract <T extends Task> void configureTasks(
Project project, Class<T> taskClass, Action<T> configure);

Expand Down
6 changes: 6 additions & 0 deletions src/main/java/net/ltgt/gradle/apt/AptPlugin212to214.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@

class AptPlugin212to214 extends AptPlugin.Impl {

@Override
protected <T extends Task> Object createTask(
Project project, String taskName, Class<T> taskClass, Action<T> configure) {
return project.getTasks().create(taskName, taskClass, configure);
}

@Override
protected <T extends Task> void configureTasks(
Project project, Class<T> taskClass, Action<T> configure) {
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/net/ltgt/gradle/apt/AptPlugin25to211.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@

class AptPlugin25to211 extends AptPlugin.Impl {

@Override
protected <T extends Task> Object createTask(
Project project, String taskName, Class<T> taskClass, Action<T> configure) {
return project.getTasks().create(taskName, taskClass, configure);
}

@Override
protected <T extends Task> void configureTasks(
Project project, Class<T> taskClass, Action<T> configure) {
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/net/ltgt/gradle/apt/AptPlugin30to33.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@

class AptPlugin30to33 extends AptPlugin.Impl {

@Override
protected <T extends Task> Object createTask(
Project project, String taskName, Class<T> taskClass, Action<T> configure) {
return project.getTasks().create(taskName, taskClass, configure);
}

@Override
protected <T extends Task> void configureTasks(
Project project, Class<T> taskClass, Action<T> configure) {
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/net/ltgt/gradle/apt/AptPlugin34.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ class AptPlugin34 extends AptPlugin.Impl {
// and keep using the previous implementation otherwise.
final AptPlugin.Impl prevImpl = new AptPlugin30to33();

@Override
protected <T extends Task> Object createTask(
Project project, String taskName, Class<T> taskClass, Action<T> configure) {
return project.getTasks().create(taskName, taskClass, configure);
}

@Override
protected <T extends Task> void configureTasks(
Project project, Class<T> taskClass, Action<T> configure) {
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/net/ltgt/gradle/apt/AptPlugin35to42.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ class AptPlugin35to42 extends AptPlugin.Impl {
// and keep using the previous implementation otherwise.
final AptPlugin.Impl prevImpl = new AptPlugin30to33();

@Override
protected <T extends Task> Object createTask(
Project project, String taskName, Class<T> taskClass, Action<T> configure) {
return project.getTasks().create(taskName, taskClass, configure);
}

@Override
protected <T extends Task> void configureTasks(
Project project, Class<T> taskClass, Action<T> configure) {
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/net/ltgt/gradle/apt/AptPlugin43to44.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ class AptPlugin43to44 extends AptPlugin.Impl {
static final String APT_OPTIONS_PROCESSORPATH_DEPRECATION_MESSAGE =
"The aptOptions.processorpath property has been deprecated. Please use the options.annotationProcessorPath property instead.";

@Override
protected <T extends Task> Object createTask(
Project project, String taskName, Class<T> taskClass, Action<T> configure) {
return project.getTasks().create(taskName, taskClass, configure);
}

@Override
protected <T extends Task> void configureTasks(
Project project, Class<T> taskClass, Action<T> configure) {
Expand Down
Loading

0 comments on commit 432509e

Please sign in to comment.