/
ScalaBasePlugin.java
275 lines (245 loc) · 14.1 KB
/
ScalaBasePlugin.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
* Copyright 2016 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.scala;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableSet;
import org.codehaus.groovy.runtime.InvokerHelper;
import org.gradle.api.Action;
import org.gradle.api.ActionConfiguration;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.artifacts.ArtifactView;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.DependencySet;
import org.gradle.api.artifacts.component.ComponentIdentifier;
import org.gradle.api.artifacts.component.ProjectComponentIdentifier;
import org.gradle.api.attributes.AttributeDisambiguationRule;
import org.gradle.api.attributes.AttributeMatchingStrategy;
import org.gradle.api.attributes.MultipleCandidatesDetails;
import org.gradle.api.attributes.Usage;
import org.gradle.api.file.FileCollection;
import org.gradle.api.file.FileTreeElement;
import org.gradle.api.file.SourceDirectorySet;
import org.gradle.api.internal.file.SourceDirectorySetFactory;
import org.gradle.api.internal.tasks.DefaultScalaSourceSet;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.plugins.Convention;
import org.gradle.api.plugins.JavaBasePlugin;
import org.gradle.api.plugins.JavaPluginConvention;
import org.gradle.api.plugins.internal.SourceSetUtil;
import org.gradle.api.reporting.ReportingExtension;
import org.gradle.api.specs.Spec;
import org.gradle.api.tasks.ScalaRuntime;
import org.gradle.api.tasks.ScalaSourceSet;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.TaskProvider;
import org.gradle.api.tasks.scala.IncrementalCompileOptions;
import org.gradle.api.tasks.scala.ScalaCompile;
import org.gradle.api.tasks.scala.ScalaDoc;
import org.gradle.jvm.tasks.Jar;
import org.gradle.language.scala.internal.toolchain.DefaultScalaToolProvider;
import javax.inject.Inject;
import java.io.File;
import java.util.concurrent.Callable;
/**
* <p>A {@link Plugin} which compiles and tests Scala sources.</p>
*/
public class ScalaBasePlugin implements Plugin<Project> {
@VisibleForTesting
public static final String ZINC_CONFIGURATION_NAME = "zinc";
public static final String SCALA_RUNTIME_EXTENSION_NAME = "scalaRuntime";
private final SourceDirectorySetFactory sourceDirectorySetFactory;
private final ObjectFactory objectFactory;
@Inject
public ScalaBasePlugin(SourceDirectorySetFactory sourceDirectorySetFactory, ObjectFactory objectFactory) {
this.sourceDirectorySetFactory = sourceDirectorySetFactory;
this.objectFactory = objectFactory;
}
public void apply(final Project project) {
project.getPluginManager().apply(JavaBasePlugin.class);
Usage incrementalAnalysisUsage = objectFactory.named(Usage.class, "incremental-analysis");
configureConfigurations(project, incrementalAnalysisUsage);
ScalaRuntime scalaRuntime = project.getExtensions().create(SCALA_RUNTIME_EXTENSION_NAME, ScalaRuntime.class, project);
configureCompileDefaults(project, scalaRuntime);
configureSourceSetDefaults(project, incrementalAnalysisUsage, sourceDirectorySetFactory);
configureScaladoc(project, scalaRuntime);
}
private void configureConfigurations(final Project project, final Usage incrementalAnalysisUsage) {
project.getConfigurations().create(ZINC_CONFIGURATION_NAME).setVisible(false).setDescription("The Zinc incremental compiler to be used for this Scala project.")
.defaultDependencies(new Action<DependencySet>() {
@Override
public void execute(DependencySet dependencies) {
dependencies.add(project.getDependencies().create("com.typesafe.zinc:zinc:" + DefaultScalaToolProvider.DEFAULT_ZINC_VERSION));
}
});
final Configuration incrementalAnalysisElements = project.getConfigurations().create("incrementalScalaAnalysisElements");
incrementalAnalysisElements.setVisible(false);
incrementalAnalysisElements.setDescription("Incremental compilation analysis files");
incrementalAnalysisElements.setCanBeResolved(false);
incrementalAnalysisElements.setCanBeConsumed(true);
incrementalAnalysisElements.getAttributes().attribute(Usage.USAGE_ATTRIBUTE, incrementalAnalysisUsage);
AttributeMatchingStrategy<Usage> matchingStrategy = project.getDependencies().getAttributesSchema().attribute(Usage.USAGE_ATTRIBUTE);
matchingStrategy.getDisambiguationRules().add(UsageDisambiguationRules.class, new Action<ActionConfiguration>() {
@Override
public void execute(ActionConfiguration actionConfiguration) {
actionConfiguration.params(incrementalAnalysisUsage);
actionConfiguration.params(objectFactory.named(Usage.class, Usage.JAVA_API));
actionConfiguration.params(objectFactory.named(Usage.class, Usage.JAVA_RUNTIME_JARS));
}
});
}
private static void configureSourceSetDefaults(final Project project, final Usage incrementalAnalysisUsage, final SourceDirectorySetFactory sourceDirectorySetFactory) {
project.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets().all(new Action<SourceSet>() {
@Override
public void execute(final SourceSet sourceSet) {
String displayName = (String) InvokerHelper.invokeMethod(sourceSet, "getDisplayName", null);
Convention sourceSetConvention = (Convention) InvokerHelper.getProperty(sourceSet, "convention");
DefaultScalaSourceSet scalaSourceSet = new DefaultScalaSourceSet(displayName, sourceDirectorySetFactory);
sourceSetConvention.getPlugins().put("scala", scalaSourceSet);
final SourceDirectorySet scalaDirectorySet = scalaSourceSet.getScala();
scalaDirectorySet.srcDir(project.file("src/" + sourceSet.getName() + "/scala"));
sourceSet.getAllJava().source(scalaDirectorySet);
sourceSet.getAllSource().source(scalaDirectorySet);
sourceSet.getResources().getFilter().exclude(new Spec<FileTreeElement>() {
@Override
public boolean isSatisfiedBy(FileTreeElement element) {
return scalaDirectorySet.contains(element.getFile());
}
});
Configuration classpath = project.getConfigurations().getByName(sourceSet.getImplementationConfigurationName());
Configuration incrementalAnalysis = project.getConfigurations().create("incrementalScalaAnalysisFor" + sourceSet.getName());
incrementalAnalysis.setVisible(false);
incrementalAnalysis.setDescription("Incremental compilation analysis files for " + displayName);
incrementalAnalysis.setCanBeResolved(true);
incrementalAnalysis.setCanBeConsumed(false);
incrementalAnalysis.extendsFrom(classpath);
incrementalAnalysis.getAttributes().attribute(Usage.USAGE_ATTRIBUTE, incrementalAnalysisUsage);
configureScalaCompile(project, sourceSet, incrementalAnalysis, incrementalAnalysisUsage);
}
});
}
private static void configureScalaCompile(final Project project, final SourceSet sourceSet, final Configuration incrementalAnalysis, final Usage incrementalAnalysisUsage) {
Convention scalaConvention = (Convention) InvokerHelper.getProperty(sourceSet, "convention");
final ScalaSourceSet scalaSourceSet = scalaConvention.findPlugin(ScalaSourceSet.class);
SourceSetUtil.configureOutputDirectoryForSourceSet(sourceSet, scalaSourceSet.getScala(), project);
final TaskProvider<ScalaCompile> scalaCompile = project.getTasks().register(sourceSet.getCompileTaskName("scala"), ScalaCompile.class, new Action<ScalaCompile>() {
@Override
public void execute(ScalaCompile scalaCompile) {
scalaCompile.dependsOn(sourceSet.getCompileJavaTaskName());
SourceSetUtil.configureForSourceSet(sourceSet, scalaSourceSet.getScala(), scalaCompile, scalaCompile.getOptions(), project);
scalaCompile.setDescription("Compiles the " + scalaSourceSet.getScala() + ".");
scalaCompile.setSource(scalaSourceSet.getScala());
scalaCompile.getAnalysisMappingFile().set(project.getLayout().getBuildDirectory().file("tmp/scala/compilerAnalysis/" + scalaCompile.getName() + ".mapping"));
// cannot compute at task execution time because we need association with source set
IncrementalCompileOptions incrementalOptions = scalaCompile.getScalaCompileOptions().getIncrementalOptions();
incrementalOptions.getAnalysisFile().set(
project.getLayout().getBuildDirectory().file("tmp/scala/compilerAnalysis/" + scalaCompile.getName() + ".analysis")
);
// TODO: Replace this with Providers in Jar/AbstractArchiveTask
final Jar jarTask = (Jar) project.getTasks().findByName(sourceSet.getJarTaskName());
if (jarTask != null) {
incrementalOptions.getPublishedCode().set(project.getLayout().file(project.provider(new Callable<File>() {
@Override
public File call() throws Exception {
return jarTask.getArchivePath();
}
})));
}
scalaCompile.getAnalysisFiles().from(incrementalAnalysis.getIncoming().artifactView(new Action<ArtifactView.ViewConfiguration>() {
@Override
public void execute(ArtifactView.ViewConfiguration viewConfiguration) {
viewConfiguration.lenient(true);
viewConfiguration.componentFilter(new Spec<ComponentIdentifier>() {
@Override
public boolean isSatisfiedBy(ComponentIdentifier element) {
return element instanceof ProjectComponentIdentifier;
}
});
}
}).getFiles());
}
});
project.getTasks().named(sourceSet.getClassesTaskName()).configure(new Action<Task>() {
@Override
public void execute(Task task) {
task.dependsOn(scalaCompile);
}
});
}
private static void configureCompileDefaults(final Project project, final ScalaRuntime scalaRuntime) {
project.getTasks().withType(ScalaCompile.class).configureEach(new Action<ScalaCompile>() {
@Override
public void execute(final ScalaCompile compile) {
compile.getConventionMapping().map("scalaClasspath", new Callable<FileCollection>() {
@Override
public FileCollection call() throws Exception {
return scalaRuntime.inferScalaClasspath(compile.getClasspath());
}
});
compile.getConventionMapping().map("zincClasspath", new Callable<Configuration>() {
@Override
public Configuration call() throws Exception {
return project.getConfigurations().getAt(ZINC_CONFIGURATION_NAME);
}
});
}
});
}
private static void configureScaladoc(final Project project, final ScalaRuntime scalaRuntime) {
project.getTasks().withType(ScalaDoc.class).configureEach(new Action<ScalaDoc>() {
@Override
public void execute(final ScalaDoc scalaDoc) {
scalaDoc.getConventionMapping().map("destinationDir", new Callable<File>() {
@Override
public File call() throws Exception {
File docsDir = project.getConvention().getPlugin(JavaPluginConvention.class).getDocsDir();
return project.file(docsDir.getPath() + "/scaladoc");
}
});
scalaDoc.getConventionMapping().map("title", new Callable<String>() {
@Override
public String call() throws Exception {
return project.getExtensions().getByType(ReportingExtension.class).getApiDocTitle();
}
});
scalaDoc.getConventionMapping().map("scalaClasspath", new Callable<FileCollection>() {
@Override
public FileCollection call() throws Exception {
return scalaRuntime.inferScalaClasspath(scalaDoc.getClasspath());
}
});
}
});
}
static class UsageDisambiguationRules implements AttributeDisambiguationRule<Usage> {
private final ImmutableSet<Usage> expectedUsages;
private final Usage javaRuntimeJars;
@Inject
UsageDisambiguationRules(Usage incrementalAnalysis, Usage javaApi, Usage javaRuntimeJars) {
this.javaRuntimeJars = javaRuntimeJars;
this.expectedUsages = ImmutableSet.of(incrementalAnalysis, javaApi, javaRuntimeJars);
}
@Override
public void execute(MultipleCandidatesDetails<Usage> details) {
if (details.getConsumerValue() == null) {
if (details.getCandidateValues().equals(expectedUsages)) {
details.closestMatch(javaRuntimeJars);
}
}
}
}
}