Skip to content

Commit 901cabf

Browse files
committed
cannot inspect the scala version until after project evaluation; delay the compile task configuration until this point
1 parent 48aa632 commit 901cabf

File tree

1 file changed

+46
-47
lines changed

1 file changed

+46
-47
lines changed

src/main/groovy/org/scoverage/ScoveragePlugin.groovy

Lines changed: 46 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,52 @@ class ScoveragePlugin implements Plugin<PluginAware> {
6363

6464
project.afterEvaluate {
6565
def scalaVersion = resolveScalaVersions(project)
66+
def instrumentedSourceSet = project.sourceSets['scoverage']
67+
project.tasks[instrumentedSourceSet.getCompileTaskName("scala")].configure {
68+
List<String> parameters = []
69+
List<String> existingParameters = scalaCompileOptions.additionalParameters
70+
if (existingParameters) {
71+
parameters.addAll(existingParameters)
72+
}
73+
if (scalaVersion.majorVersion < 3) {
74+
parameters.add("-P:scoverage:dataDir:${extension.dataDir.get().absolutePath}".toString())
75+
parameters.add("-P:scoverage:sourceRoot:${extension.project.getRootDir().absolutePath}".toString())
76+
if (extension.excludedPackages.get()) {
77+
def packages = extension.excludedPackages.get().join(';')
78+
parameters.add("-P:scoverage:excludedPackages:$packages".toString())
79+
}
80+
if (extension.excludedFiles.get()) {
81+
def packages = extension.excludedFiles.get().join(';')
82+
parameters.add("-P:scoverage:excludedFiles:$packages".toString())
83+
}
84+
if (extension.highlighting.get()) {
85+
parameters.add('-Yrangepos')
86+
}
87+
scalaCompileOptions.additionalParameters = parameters
88+
// the compile task creates a store of measured statements
89+
outputs.file(new File(extension.dataDir.get(), 'scoverage.coverage'))
90+
91+
dependsOn project.configurations[CONFIGURATION_NAME]
92+
doFirst {
93+
/*
94+
It is crucial that this would run in `doFirst`, as this resolves the (dependencies of the)
95+
configuration, which we do not want to do at configuration time (but only at execution time).
96+
*/
97+
def pluginFiles = project.configurations[CONFIGURATION_NAME].findAll {
98+
it.name.startsWith("scalac-scoverage-plugin") ||
99+
it.name.startsWith("scalac-scoverage-domain") ||
100+
it.name.startsWith("scalac-scoverage-serializer")
101+
}.collect {
102+
it.absolutePath
103+
}
104+
scalaCompileOptions.additionalParameters.add('-Xplugin:' + pluginFiles.join(":"))
105+
}
106+
} else {
107+
parameters.add("-coverage-out:${extension.dataDir.get().absolutePath}".toString())
108+
scalaCompileOptions.additionalParameters = parameters
109+
}
110+
111+
}
66112

67113
def scoverageVersion = project.extensions.scoverage.scoverageVersion.get()
68114
project.logger.info("Using scoverage scalac plugin $scoverageVersion for scala $scalaVersion")
@@ -162,53 +208,6 @@ class ScoveragePlugin implements Plugin<PluginAware> {
162208

163209
configureCheckTask(project, extension, globalCheckTask, globalReportTask)
164210

165-
compileTask.configure {
166-
List<String> parameters = []
167-
List<String> existingParameters = scalaCompileOptions.additionalParameters
168-
if (existingParameters) {
169-
parameters.addAll(existingParameters)
170-
}
171-
172-
def scalaVersion = resolveScalaVersions(project)
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(":"))
205-
}
206-
} else {
207-
parameters.add("-coverage-out:${extension.dataDir.get().absolutePath}".toString())
208-
scalaCompileOptions.additionalParameters = parameters
209-
}
210-
}
211-
212211
if (project.hasProperty(SCOVERAGE_COMPILE_ONLY_PROPERTY)) {
213212
project.logger.info("Making scoverage compilation the primary compilation task (instead of compileScala)")
214213

0 commit comments

Comments
 (0)