1
1
package org.scoverage
2
2
3
3
import org.apache.commons.io.FileUtils
4
- import org.gradle.api.GradleException
5
4
import org.gradle.api.Plugin
6
5
import org.gradle.api.Project
7
6
import org.gradle.api.Task
@@ -25,10 +24,10 @@ class ScoveragePlugin implements Plugin<PluginAware> {
25
24
static final String CHECK_NAME = ' checkScoverage'
26
25
static final String COMPILE_NAME = ' compileScoverageScala'
27
26
static final String AGGREGATE_NAME = ' aggregateScoverage'
27
+ static final String DEFAULT_SCALA_VERSION = ' 2.12'
28
28
29
29
static final String DEFAULT_REPORT_DIR = ' reports' + File . separatorChar + ' scoverage'
30
30
31
- private volatile File pluginFile = null
32
31
private final ConcurrentHashMap<Task , Set<? extends Task > > taskDependencies = new ConcurrentHashMap<> ();
33
32
34
33
@Override
@@ -173,13 +172,7 @@ class ScoveragePlugin implements Plugin<PluginAware> {
173
172
}
174
173
175
174
compileTask. configure {
176
- if (pluginFile == null ) {
177
- pluginFile = project. configurations[CONFIGURATION_NAME ]. find {
178
- it. name. startsWith(" scalac-scoverage-plugin" )
179
- }
180
- }
181
-
182
- List<String > parameters = [' -Xplugin:' + pluginFile. absolutePath]
175
+ List<String > parameters = []
183
176
List<String > existingParameters = scalaCompileOptions. additionalParameters
184
177
if (existingParameters) {
185
178
parameters. addAll(existingParameters)
@@ -199,6 +192,18 @@ class ScoveragePlugin implements Plugin<PluginAware> {
199
192
scalaCompileOptions. additionalParameters = parameters
200
193
// the compile task creates a store of measured statements
201
194
outputs. file(new File (extension. dataDir. get(), ' scoverage.coverage.xml' ))
195
+
196
+ dependsOn project. configurations[CONFIGURATION_NAME ]
197
+ doFirst {
198
+ /*
199
+ It is crucial that this would run in `doFirst`, as this resolves the (dependencies of the)
200
+ configuration, which we do not want to do at configuration time (but only at execution time).
201
+ */
202
+ def pluginFile = project. configurations[CONFIGURATION_NAME ]. find {
203
+ it. name. startsWith(" scalac-scoverage-plugin" )
204
+ }
205
+ scalaCompileOptions. additionalParameters. add(' -Xplugin:' + pluginFile. absolutePath)
206
+ }
202
207
}
203
208
204
209
project. gradle. taskGraph. whenReady { graph ->
@@ -343,19 +348,26 @@ class ScoveragePlugin implements Plugin<PluginAware> {
343
348
344
349
private String resolveScalaVersion (Project project ) {
345
350
346
- def resolvedDependencies = project. configurations. compileClasspath. resolvedConfiguration. firstLevelModuleDependencies
347
-
348
- def scalaLibrary = resolvedDependencies. find {
349
- it. moduleGroup == " org.scala-lang" && it. moduleName == " scala-library"
350
- }
351
-
352
- if (scalaLibrary == null ) {
353
- project. logger. info(" No scala library detected. Using property 'scoverageScalaVersion'" )
354
- return project. extensions. scoverage. scoverageScalaVersion. get()
351
+ def scalaVersionProperty = project. extensions. scoverage. scoverageScalaVersion
352
+ if (scalaVersionProperty. isPresent()) {
353
+ def configuredScalaVersion = scalaVersionProperty. get()
354
+ project. logger. info(" Using configured Scala version: $configuredScalaVersion " )
355
+ return configuredScalaVersion
355
356
} else {
356
- project. logger. info(" Detected scala library in compilation classpath" )
357
- def fullScalaVersion = scalaLibrary. moduleVersion
358
- return fullScalaVersion. substring(0 , fullScalaVersion. lastIndexOf(" ." ))
357
+ project. logger. info(" No Scala version configured. Detecting scala library..." )
358
+ def components = project. configurations. compileClasspath. incoming. resolutionResult. getAllComponents()
359
+ def scalaLibrary = components. find {
360
+ it. moduleVersion. group == " org.scala-lang" && it. moduleVersion. name == " scala-library"
361
+ }
362
+ if (scalaLibrary != null ) {
363
+ def fullScalaVersion = scalaLibrary. moduleVersion. version
364
+ def scalaVersion = fullScalaVersion. substring(0 , fullScalaVersion. lastIndexOf(" ." ))
365
+ project. logger. info(" Detected scala library in compilation classpath. Scala version: $scalaVersion " )
366
+ return scalaVersion
367
+ } else {
368
+ project. logger. info(" No scala library detected. Using default Scala version: $DEFAULT_SCALA_VERSION " )
369
+ return DEFAULT_SCALA_VERSION
370
+ }
359
371
}
360
372
}
361
373
0 commit comments