/
GroovyBasePlugin.java
184 lines (165 loc) · 8.41 KB
/
GroovyBasePlugin.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
* Copyright 2009 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gradle.api.plugins;
import org.gradle.api.Action;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.Transformer;
import org.gradle.api.attributes.LibraryElements;
import org.gradle.api.file.ConfigurableFileCollection;
import org.gradle.api.file.FileCollection;
import org.gradle.api.file.FileTreeElement;
import org.gradle.api.internal.classpath.ModuleRegistry;
import org.gradle.api.internal.plugins.DslObject;
import org.gradle.api.internal.tasks.DefaultGroovySourceSet;
import org.gradle.api.internal.tasks.DefaultSourceSet;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.plugins.internal.JvmPluginsHelper;
import org.gradle.api.provider.Provider;
import org.gradle.api.reporting.ReportingExtension;
import org.gradle.api.specs.Spec;
import org.gradle.api.tasks.GroovyRuntime;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.compile.CompileOptions;
import org.gradle.api.tasks.compile.GroovyCompile;
import org.gradle.api.tasks.javadoc.Groovydoc;
import javax.inject.Inject;
import java.io.File;
import java.util.concurrent.Callable;
/**
* Extends {@link org.gradle.api.plugins.JavaBasePlugin} to provide support for compiling and documenting Groovy
* source files.
*/
public class GroovyBasePlugin implements Plugin<Project> {
public static final String GROOVY_RUNTIME_EXTENSION_NAME = "groovyRuntime";
private final ObjectFactory objectFactory;
private final ModuleRegistry moduleRegistry;
private Project project;
private GroovyRuntime groovyRuntime;
@Inject
public GroovyBasePlugin(ObjectFactory objectFactory, ModuleRegistry moduleRegistry) {
this.objectFactory = objectFactory;
this.moduleRegistry = moduleRegistry;
}
@Override
public void apply(Project project) {
this.project = project;
project.getPluginManager().apply(JavaBasePlugin.class);
configureGroovyRuntimeExtension();
configureCompileDefaults();
configureSourceSetDefaults();
configureGroovydoc();
}
private void configureGroovyRuntimeExtension() {
groovyRuntime = project.getExtensions().create(GROOVY_RUNTIME_EXTENSION_NAME, GroovyRuntime.class, project);
}
private void configureCompileDefaults() {
project.getTasks().withType(GroovyCompile.class).configureEach(new Action<GroovyCompile>() {
@Override
public void execute(final GroovyCompile compile) {
compile.getConventionMapping().map("groovyClasspath", new Callable<Object>() {
@Override
public Object call() {
return groovyRuntime.inferGroovyClasspath(compile.getClasspath());
}
});
}
});
}
private void configureSourceSetDefaults() {
project.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets().all(new Action<SourceSet>() {
@Override
public void execute(final SourceSet sourceSet) {
final DefaultGroovySourceSet groovySourceSet = new DefaultGroovySourceSet("groovy", ((DefaultSourceSet) sourceSet).getDisplayName(), objectFactory);
new DslObject(sourceSet).getConvention().getPlugins().put("groovy", groovySourceSet);
groovySourceSet.getGroovy().srcDir("src/" + sourceSet.getName() + "/groovy");
sourceSet.getResources().getFilter().exclude(new Spec<FileTreeElement>() {
@Override
public boolean isSatisfiedBy(FileTreeElement element) {
return groovySourceSet.getGroovy().contains(element.getFile());
}
});
sourceSet.getAllJava().source(groovySourceSet.getGroovy());
sourceSet.getAllSource().source(groovySourceSet.getGroovy());
final Provider<GroovyCompile> compileTask = project.getTasks().register(sourceSet.getCompileTaskName("groovy"), GroovyCompile.class, new Action<GroovyCompile>() {
@Override
public void execute(final GroovyCompile compile) {
JvmPluginsHelper.configureForSourceSet(sourceSet, groovySourceSet.getGroovy(), compile, compile.getOptions(), project);
compile.dependsOn(sourceSet.getCompileJavaTaskName());
compile.setDescription("Compiles the " + sourceSet.getName() + " Groovy source.");
compile.setSource(groovySourceSet.getGroovy());
}
});
JvmPluginsHelper.configureOutputDirectoryForSourceSet(sourceSet, groovySourceSet.getGroovy(), project, compileTask, compileTask.map(new Transformer<CompileOptions, GroovyCompile>() {
@Override
public CompileOptions transform(GroovyCompile groovyCompile) {
return groovyCompile.getOptions();
}
}));
// TODO: `classes` should be a little more tied to the classesDirs for a SourceSet so every plugin
// doesn't need to do this.
project.getTasks().named(sourceSet.getClassesTaskName(), new Action<Task>() {
@Override
public void execute(Task task) {
task.dependsOn(compileTask);
}
});
// Explain that Groovy, for compile, also needs the resources (#9872)
project.getConfigurations().findByName(sourceSet.getCompileClasspathConfigurationName()).attributes(attrs ->
attrs.attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, project.getObjects().named(LibraryElements.class, LibraryElements.CLASSES_AND_RESOURCES))
);
}
});
}
private void configureGroovydoc() {
project.getTasks().withType(Groovydoc.class).configureEach(new Action<Groovydoc>() {
@Override
public void execute(final Groovydoc groovydoc) {
groovydoc.getConventionMapping().map("groovyClasspath", new Callable<Object>() {
@Override
public Object call() throws Exception {
FileCollection groovyClasspath = groovyRuntime.inferGroovyClasspath(groovydoc.getClasspath());
// Jansi is required to log errors when generating Groovydoc
ConfigurableFileCollection jansi = project.getObjects().fileCollection().from(moduleRegistry.getExternalModule("jansi").getImplementationClasspath().getAsFiles());
return groovyClasspath.plus(jansi);
}
});
groovydoc.getConventionMapping().map("destinationDir", new Callable<Object>() {
@Override
public Object call() throws Exception {
return new File(java(project.getConvention()).getDocsDir(), "groovydoc");
}
});
groovydoc.getConventionMapping().map("docTitle", new Callable<Object>() {
@Override
public Object call() throws Exception {
return project.getExtensions().getByType(ReportingExtension.class).getApiDocTitle();
}
});
groovydoc.getConventionMapping().map("windowTitle", new Callable<Object>() {
@Override
public Object call() throws Exception {
return project.getExtensions().getByType(ReportingExtension.class).getApiDocTitle();
}
});
}
});
}
private JavaPluginConvention java(Convention convention) {
return convention.getPlugin(JavaPluginConvention.class);
}
}