@@ -51,9 +51,12 @@ class ScoveragePlugin implements Plugin<PluginAware> {
51
51
project. logger. info(" Project ${ project.name} already has the scoverage plugin" )
52
52
return
53
53
}
54
- project. logger. info(" Applying scoverage plugin to $project . name " )
55
54
55
+ project. logger. info(" Applying scoverage plugin to $project . name " )
56
56
def extension = project. extensions. create(' scoverage' , ScoverageExtension , project)
57
+
58
+ def scalaVersion = resolveScalaVersions(project)
59
+
57
60
if (! project. configurations. asMap[CONFIGURATION_NAME ]) {
58
61
project. configurations. create(CONFIGURATION_NAME ) {
59
62
visible = false
@@ -62,23 +65,27 @@ class ScoveragePlugin implements Plugin<PluginAware> {
62
65
}
63
66
64
67
project. afterEvaluate {
65
- def scalaFullVersion = resolveScalaVersion(project)
66
- def scalaBinaryVersion = scalaFullVersion. substring(0 , scalaFullVersion. lastIndexOf(' .' ))
67
68
def scoverageVersion = project. extensions. scoverage. scoverageVersion. get()
69
+ project. logger. info(" Using scoverage scalac plugin $scoverageVersion for scala $scalaVersion " )
68
70
69
- project. logger. info(" Using scoverage scalac plugin $scoverageVersion for scala $scalaFullVersion " )
71
+ def scalacScoverageVersion = scalaVersion. scalacScoverageVersion
72
+ def scalacScoveragePluginVersion = scalaVersion. scalacScoveragePluginVersion
73
+ def scalacScoverageRuntimeVersion = scalaVersion. scalacScoverageRuntimeVersion
70
74
71
75
project. dependencies {
72
- scoverage(" org.scoverage:scalac-scoverage-plugin_$scalaFullVersion :$scoverageVersion " )
73
- scoverage(" org.scoverage:scalac-scoverage-runtime_$scalaBinaryVersion :$scoverageVersion " )
76
+ scoverage(" org.scoverage:scalac-scoverage-domain_$scalacScoverageVersion :$scoverageVersion " )
77
+ scoverage(" org.scoverage:scalac-scoverage-reporter_$scalacScoverageVersion :$scoverageVersion " )
78
+ scoverage(" org.scoverage:scalac-scoverage-serializer_$scalacScoverageVersion :$scoverageVersion " )
79
+ scoverage(" org.scoverage:scalac-scoverage-runtime_$scalacScoverageRuntimeVersion :$scoverageVersion " )
80
+ scoverage(" org.scoverage:scalac-scoverage-plugin_$scalacScoveragePluginVersion :$scoverageVersion " )
74
81
}
75
82
}
76
83
}
77
84
78
- createTasks(project, extension)
85
+ createTasks(project, extension, scalaVersion )
79
86
}
80
87
81
- private void createTasks (Project project , ScoverageExtension extension ) {
88
+ private void createTasks (Project project , ScoverageExtension extension , ScalaVersion scalaVersion ) {
82
89
83
90
ScoverageRunner scoverageRunner = new ScoverageRunner (project. configurations. scoverage)
84
91
@@ -162,32 +169,43 @@ class ScoveragePlugin implements Plugin<PluginAware> {
162
169
if (existingParameters) {
163
170
parameters. addAll(existingParameters)
164
171
}
165
- parameters. add(" -P:scoverage:dataDir:${ extension.dataDir.get().absolutePath} " . toString())
166
- if (extension. excludedPackages. get()) {
167
- def packages = extension. excludedPackages. get(). join(' ;' )
168
- parameters. add(" -P:scoverage:excludedPackages:$packages " . toString())
169
- }
170
- if (extension. excludedFiles. get()) {
171
- def packages = extension. excludedFiles. get(). join(' ;' )
172
- parameters. add(" -P:scoverage:excludedFiles:$packages " . toString())
173
- }
174
- if (extension. highlighting. get()) {
175
- parameters. add(' -Yrangepos' )
176
- }
177
- scalaCompileOptions. additionalParameters = parameters
178
- // the compile task creates a store of measured statements
179
- outputs. file(new File (extension. dataDir. get(), ' scoverage.coverage' ))
180
-
181
- dependsOn project. configurations[CONFIGURATION_NAME ]
182
- doFirst {
183
- /*
184
- It is crucial that this would run in `doFirst`, as this resolves the (dependencies of the)
185
- configuration, which we do not want to do at configuration time (but only at execution time).
186
- */
187
- def pluginFile = project. configurations[CONFIGURATION_NAME ]. find {
188
- it. name. startsWith(" scalac-scoverage-plugin" )
172
+
173
+ if (scalaVersion. majorVersion < 3 ) {
174
+ parameters. add(" -P:scoverage:dataDir:${ extension.dataDir.get().absolutePath} " . toString())
175
+ parameters. add(" -P:scoverage:sourceRoot:${ extension.project.getRootDir().absolutePath} " . toString())
176
+ if (extension. excludedPackages. get()) {
177
+ def packages = extension. excludedPackages. get(). join(' ;' )
178
+ parameters. add(" -P:scoverage:excludedPackages:$packages " . toString())
179
+ }
180
+ if (extension. excludedFiles. get()) {
181
+ def packages = extension. excludedFiles. get(). join(' ;' )
182
+ parameters. add(" -P:scoverage:excludedFiles:$packages " . toString())
183
+ }
184
+ if (extension. highlighting. get()) {
185
+ parameters. add(' -Yrangepos' )
186
+ }
187
+ scalaCompileOptions. additionalParameters = parameters
188
+ // the compile task creates a store of measured statements
189
+ outputs. file(new File (extension. dataDir. get(), ' scoverage.coverage' ))
190
+
191
+ dependsOn project. configurations[CONFIGURATION_NAME ]
192
+ doFirst {
193
+ /*
194
+ It is crucial that this would run in `doFirst`, as this resolves the (dependencies of the)
195
+ configuration, which we do not want to do at configuration time (but only at execution time).
196
+ */
197
+ def pluginFiles = project. configurations[CONFIGURATION_NAME ]. findAll {
198
+ it. name. startsWith(" scalac-scoverage-plugin" ) ||
199
+ it. name. startsWith(" scalac-scoverage-domain" ) ||
200
+ it. name. startsWith(" scalac-scoverage-serializer" )
201
+ }. collect {
202
+ it. absolutePath
203
+ }
204
+ scalaCompileOptions. additionalParameters. add(' -Xplugin:' + pluginFiles. join(" :" ))
189
205
}
190
- scalaCompileOptions. additionalParameters. add(' -Xplugin:' + pluginFile. absolutePath)
206
+ } else {
207
+ parameters. add(" -coverage-out:${ extension.dataDir.get().absolutePath} " . toString())
208
+ scalaCompileOptions. additionalParameters = parameters
191
209
}
192
210
}
193
211
@@ -364,27 +382,41 @@ class ScoveragePlugin implements Plugin<PluginAware> {
364
382
}
365
383
}
366
384
367
- private String resolveScalaVersion (Project project ) {
368
-
385
+ private ScalaVersion resolveScalaVersions (Project project ) {
369
386
def scalaVersionProperty = project. extensions. scoverage. scoverageScalaVersion
370
387
if (scalaVersionProperty. isPresent()) {
371
388
def configuredScalaVersion = scalaVersionProperty. get()
372
389
project. logger. info(" Using configured Scala version: $configuredScalaVersion " )
373
- return configuredScalaVersion
390
+ return new ScalaVersion ( configuredScalaVersion)
374
391
} else {
375
392
project. logger. info(" No Scala version configured. Detecting scala library..." )
376
393
def components = project. configurations. compileClasspath. incoming. resolutionResult. getAllComponents()
394
+
395
+ def scala3Library = components. find {
396
+ it. moduleVersion. group == " org.scala-lang" && it. moduleVersion. name == " scala3-library_3"
397
+ }
377
398
def scalaLibrary = components. find {
378
399
it. moduleVersion. group == " org.scala-lang" && it. moduleVersion. name == " scala-library"
379
400
}
401
+
402
+ // Scala 3
403
+ if (scala3Library != null ) {
404
+ def scala3Version = scala3Library. moduleVersion. version
405
+ def scala2Version = scalaLibrary. moduleVersion. version
406
+ project. logger. info(" Detected scala 3 library in compilation classpath. Scala 3 version: $scala3Version ; using Scala 2 library: $scala2Version " )
407
+ return new ScalaVersion (scala3Version, Optional . of(scala2Version))
408
+ }
409
+
410
+ // Scala 2
380
411
if (scalaLibrary != null ) {
381
- def scalaVersion = scalaLibrary. moduleVersion. version
382
- project. logger. info(" Detected scala library in compilation classpath. Scala version: $scalaVersion " )
383
- return scalaVersion
384
- } else {
385
- project. logger. info(" No scala library detected. Using default Scala version: $DEFAULT_SCALA_VERSION " )
386
- return DEFAULT_SCALA_VERSION
412
+ def scala2Version = scalaLibrary. moduleVersion. version
413
+ project. logger. info(" Detected scala library in compilation classpath. Scala version: $scala2Version " )
414
+ return new ScalaVersion (scala2Version)
387
415
}
416
+
417
+ // No Scala library was found, using default Scala version
418
+ project. logger. info(" No scala library detected. Using default Scala version: $DEFAULT_SCALA_VERSION " )
419
+ return new ScalaVersion (DEFAULT_SCALA_VERSION )
388
420
}
389
421
}
390
422
0 commit comments