1
1
package org.scoverage
2
2
3
+ import org.apache.commons.io.FileUtils
3
4
import org.gradle.api.Plugin
4
5
import org.gradle.api.Project
5
6
import org.gradle.api.invocation.Gradle
6
7
import org.gradle.api.plugins.PluginAware
7
8
import org.gradle.api.tasks.SourceSet
8
9
import org.gradle.api.tasks.bundling.Jar
9
- import org.gradle.util.GFileUtils
10
+
11
+ import java.nio.file.Files
12
+
13
+ import static groovy.io.FileType.FILES
10
14
11
15
class ScoveragePlugin implements Plugin<PluginAware > {
12
16
@@ -82,18 +86,19 @@ class ScoveragePlugin implements Plugin<PluginAware> {
82
86
83
87
ScoverageRunner scoverageRunner = new ScoverageRunner (project. configurations. scoverage)
84
88
89
+ def originalSourceSet = project. sourceSets. getByName(SourceSet . MAIN_SOURCE_SET_NAME )
85
90
def instrumentedSourceSet = project. sourceSets. create(' scoverage' ) {
86
- def original = project. sourceSets. getByName(SourceSet . MAIN_SOURCE_SET_NAME )
87
91
88
- resources. source(original . resources)
89
- scala. source(original . java)
90
- scala. source(original . scala)
92
+ resources. source(originalSourceSet . resources)
93
+ scala. source(originalSourceSet . java)
94
+ scala. source(originalSourceSet . scala)
91
95
92
- compileClasspath + = original . compileClasspath + project. configurations. scoverage
93
- runtimeClasspath = it. output + project. configurations. scoverage + original . runtimeClasspath
96
+ compileClasspath + = originalSourceSet . compileClasspath + project. configurations. scoverage
97
+ runtimeClasspath = it. output + project. configurations. scoverage + originalSourceSet . runtimeClasspath
94
98
}
95
99
96
100
def compileTask = project. tasks[instrumentedSourceSet. getCompileTaskName(" scala" )]
101
+ compileTask. mustRunAfter(originalSourceSet. getCompileTaskName(" scala" ))
97
102
project. test. mustRunAfter(compileTask)
98
103
99
104
def scoverageJar = project. tasks. create(' jarScoverage' , Jar . class) {
@@ -127,7 +132,6 @@ class ScoveragePlugin implements Plugin<PluginAware> {
127
132
128
133
project. gradle. taskGraph. whenReady { graph ->
129
134
if (graph. hasTask(reportTask)) {
130
-
131
135
project. test. configure {
132
136
project. logger. debug(" Adding instrumented classes to '${ path} ' classpath" )
133
137
@@ -164,6 +168,10 @@ class ScoveragePlugin implements Plugin<PluginAware> {
164
168
}
165
169
166
170
compileTask. configure {
171
+ doFirst {
172
+ destinationDir. deleteDir()
173
+ }
174
+
167
175
File pluginFile = project. configurations[CONFIGURATION_NAME ]. find {
168
176
it. name. startsWith(" scalac-scoverage-plugin" )
169
177
}
@@ -184,13 +192,49 @@ class ScoveragePlugin implements Plugin<PluginAware> {
184
192
if (extension. highlighting. get()) {
185
193
parameters. add(' -Yrangepos' )
186
194
}
187
- doFirst {
188
- GFileUtils . deleteDirectory(destinationDir)
189
- }
190
195
scalaCompileOptions. additionalParameters = parameters
191
196
// the compile task creates a store of measured statements
192
197
outputs. file(new File (extension. dataDir. get(), ' scoverage.coverage.xml' ))
198
+
199
+ doLast {
200
+ def originalCompileTaskName = project. sourceSets. getByName(SourceSet . MAIN_SOURCE_SET_NAME )
201
+ .getCompileTaskName(" scala" )
202
+ def originalDestinationDir = project. tasks[originalCompileTaskName]. destinationDir
203
+
204
+ def findFiles = { File dir , Closure<Boolean > condition = null ->
205
+ def files = []
206
+
207
+ if (dir. exists()) {
208
+ dir. eachFileRecurse(FILES ) { f ->
209
+ if (condition == null || condition(f)) {
210
+ def relativePath = dir. relativePath(f)
211
+ files << relativePath
212
+ }
213
+ }
214
+ }
215
+
216
+ return files
217
+ }
218
+
219
+ def isSameFile = { String relativePath ->
220
+ def fileA = new File (originalDestinationDir, relativePath)
221
+ def fileB = new File (destinationDir, relativePath)
222
+ return FileUtils . contentEquals(fileA, fileB)
223
+ }
224
+
225
+ def originalClasses = findFiles(originalDestinationDir)
226
+ def identicalInstrumentedClasses = findFiles(destinationDir, { f ->
227
+ def relativePath = destinationDir. relativePath(f)
228
+ return originalClasses. contains(relativePath) && isSameFile(relativePath)
229
+ })
230
+
231
+ identicalInstrumentedClasses. each { f ->
232
+ Files . deleteIfExists(destinationDir. toPath(). resolve(f))
233
+ }
234
+ }
193
235
}
194
236
}
195
237
}
238
+
239
+
196
240
}
0 commit comments