1
1
package org.scoverage
2
2
3
-
4
3
import org.gradle.api.Plugin
5
4
import org.gradle.api.Project
6
5
import org.gradle.api.invocation.Gradle
7
6
import org.gradle.api.plugins.PluginAware
8
7
import org.gradle.api.tasks.SourceSet
9
8
import org.gradle.api.tasks.bundling.Jar
10
- import org.gradle.api.tasks.testing.Test
11
9
import org.gradle.util.GFileUtils
12
10
13
- import java.util.concurrent.Callable
14
-
15
11
class ScoveragePlugin implements Plugin<PluginAware > {
16
12
17
13
static final String CONFIGURATION_NAME = ' scoverage'
18
- static final String TEST_NAME = ' testScoverage'
19
14
static final String REPORT_NAME = ' reportScoverage'
20
15
static final String CHECK_NAME = ' checkScoverage'
21
16
static final String COMPILE_NAME = ' compileScoverageScala'
@@ -80,17 +75,12 @@ class ScoveragePlugin implements Plugin<PluginAware> {
80
75
}
81
76
}
82
77
83
- ScoverageRunner scoverageRunner = new ScoverageRunner (project. configurations. scoverage)
84
-
85
- createTasks(project, extension, scoverageRunner)
86
-
87
- project. afterEvaluate {
88
- configureAfterEvaluation(project, extension, scoverageRunner)
89
- }
78
+ createTasks(project, extension)
90
79
}
91
80
92
- private void createTasks (Project project , ScoverageExtension extension , ScoverageRunner scoverageRunner ) {
81
+ private void createTasks (Project project , ScoverageExtension extension ) {
93
82
83
+ ScoverageRunner scoverageRunner = new ScoverageRunner (project. configurations. scoverage)
94
84
95
85
def instrumentedSourceSet = project. sourceSets. create(' scoverage' ) {
96
86
def original = project. sourceSets. getByName(SourceSet . MAIN_SOURCE_SET_NAME )
@@ -103,38 +93,20 @@ class ScoveragePlugin implements Plugin<PluginAware> {
103
93
runtimeClasspath = it. output + project. configurations. scoverage + original. runtimeClasspath
104
94
}
105
95
96
+ def compileTask = project. tasks[instrumentedSourceSet. getCompileTaskName(" scala" )]
97
+ project. test. mustRunAfter(compileTask)
98
+
106
99
def scoverageJar = project. tasks. create(' jarScoverage' , Jar . class) {
107
- dependsOn(' scoverageClasses ' )
100
+ dependsOn(instrumentedSourceSet . classesTaskName )
108
101
classifier = CONFIGURATION_NAME
109
102
from instrumentedSourceSet. output
110
103
}
111
104
project. artifacts {
112
105
scoverage scoverageJar
113
106
}
114
107
115
- project. tasks. create(TEST_NAME , Test . class) {
116
- conventionMapping. map(" classpath" , new Callable<Object > () {
117
- Object call () throws Exception {
118
- def testSourceSet = project. sourceSets. getByName(SourceSet . TEST_SOURCE_SET_NAME )
119
- return testSourceSet. output +
120
- instrumentedSourceSet. output +
121
- project. configurations. scoverage +
122
- testSourceSet. runtimeClasspath
123
- }
124
- })
125
- group = ' verification'
126
-
127
- FilenameFilter measurementFile = new FilenameFilter () {
128
- @Override
129
- boolean accept (File dir , String name ) {
130
- return name. startsWith(" scoverage.measurements." )
131
- }
132
- }
133
- outputs. upToDateWhen { extension. dataDir. get(). listFiles(measurementFile) }
134
- }
135
-
136
- project. tasks. create(REPORT_NAME , ScoverageReport . class) {
137
- dependsOn(project. tasks[TEST_NAME ])
108
+ def reportTask = project. tasks. create(REPORT_NAME , ScoverageReport . class) {
109
+ dependsOn compileTask, project. test
138
110
onlyIf { extension. dataDir. get(). list() }
139
111
group = ' verification'
140
112
runner = scoverageRunner
@@ -148,58 +120,77 @@ class ScoveragePlugin implements Plugin<PluginAware> {
148
120
}
149
121
150
122
project. tasks. create(CHECK_NAME , OverallCheckTask . class) {
151
- dependsOn(project . tasks[ REPORT_NAME ] )
123
+ dependsOn(reportTask )
152
124
group = ' verification'
153
125
reportDir = extension. reportDir
154
126
}
155
127
156
- }
128
+ project. gradle. taskGraph. whenReady { graph ->
129
+ if (graph. hasTask(reportTask)) {
157
130
158
- private void configureAfterEvaluation ( Project project , ScoverageExtension extension , ScoverageRunner scoverageRunner ) {
159
-
160
- if (project . childProjects . size() > 0 ) {
161
- def reportTasks = project. getAllprojects() . collect { it . tasks . withType( ScoverageReport ) }
162
- def aggregationTask = project . tasks . create( AGGREGATE_NAME , ScoverageAggregate . class) {
163
- dependsOn(reportTasks)
164
- group = ' verification '
165
- runner = scoverageRunner
166
- reportDir = extension . reportDir
167
- deleteReportsOnAggregation = extension . deleteReportsOnAggregation
168
- coverageOutputCobertura = extension . coverageOutputCobertura
169
- coverageOutputXML = extension . coverageOutputXML
170
- coverageOutputHTML = extension . coverageOutputHTML
171
- coverageDebug = extension . coverageDebug
131
+ project . test . configure {
132
+ project . logger . debug( " Adding instrumented classes to ' ${ path } ' classpath " )
133
+
134
+ classpath = project. configurations . scoverage + instrumentedSourceSet . output + classpath
135
+
136
+ outputs . upToDateWhen {
137
+ extension . dataDir . get() . listFiles( new FilenameFilter () {
138
+ @Override
139
+ boolean accept ( File dir , String name ) {
140
+ return name . startsWith( " scoverage.measurements. " )
141
+ }
142
+ })
143
+ }
144
+ }
172
145
}
173
- project. tasks[CHECK_NAME ]. mustRunAfter(aggregationTask)
174
146
}
175
147
176
- project. tasks[COMPILE_NAME ]. configure {
177
- File pluginFile = project. configurations[CONFIGURATION_NAME ]. find {
178
- it. name. startsWith(" scalac-scoverage-plugin" )
179
- }
180
- List<String > parameters = [' -Xplugin:' + pluginFile. absolutePath]
181
- List<String > existingParameters = scalaCompileOptions. additionalParameters
182
- if (existingParameters) {
183
- parameters. addAll(existingParameters)
184
- }
185
- parameters. add(" -P:scoverage:dataDir:${ extension.dataDir.get().absolutePath} " . toString())
186
- if (extension. excludedPackages. get()) {
187
- def packages = extension. excludedPackages. get(). join(' ;' )
188
- parameters. add(" -P:scoverage:excludedPackages:$packages " . toString())
189
- }
190
- if (extension. excludedFiles. get()) {
191
- def packages = extension. excludedFiles. get(). join(' ;' )
192
- parameters. add(" -P:scoverage:excludedFiles:$packages " . toString())
193
- }
194
- if (extension. highlighting. get()) {
195
- parameters. add(' -Yrangepos' )
148
+ project. afterEvaluate {
149
+
150
+ if (project. childProjects. size() > 0 ) {
151
+ def reportTasks = project. getAllprojects(). collect { it. tasks. withType(ScoverageReport ) }
152
+ def aggregationTask = project. tasks. create(AGGREGATE_NAME , ScoverageAggregate . class) {
153
+ dependsOn(reportTasks)
154
+ group = ' verification'
155
+ runner = scoverageRunner
156
+ reportDir = extension. reportDir
157
+ deleteReportsOnAggregation = extension. deleteReportsOnAggregation
158
+ coverageOutputCobertura = extension. coverageOutputCobertura
159
+ coverageOutputXML = extension. coverageOutputXML
160
+ coverageOutputHTML = extension. coverageOutputHTML
161
+ coverageDebug = extension. coverageDebug
162
+ }
163
+ project. tasks[CHECK_NAME ]. mustRunAfter(aggregationTask)
196
164
}
197
- doFirst {
198
- GFileUtils . deleteDirectory(destinationDir)
165
+
166
+ compileTask. configure {
167
+ File pluginFile = project. configurations[CONFIGURATION_NAME ]. find {
168
+ it. name. startsWith(" scalac-scoverage-plugin" )
169
+ }
170
+ List<String > parameters = [' -Xplugin:' + pluginFile. absolutePath]
171
+ List<String > existingParameters = scalaCompileOptions. additionalParameters
172
+ if (existingParameters) {
173
+ parameters. addAll(existingParameters)
174
+ }
175
+ parameters. add(" -P:scoverage:dataDir:${ extension.dataDir.get().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
+ doFirst {
188
+ GFileUtils . deleteDirectory(destinationDir)
189
+ }
190
+ scalaCompileOptions. additionalParameters = parameters
191
+ // the compile task creates a store of measured statements
192
+ outputs. file(new File (extension. dataDir. get(), ' scoverage.coverage.xml' ))
199
193
}
200
- scalaCompileOptions. additionalParameters = parameters
201
- // the compile task creates a store of measured statements
202
- outputs. file(new File (extension. dataDir. get(), ' scoverage.coverage.xml' ))
203
194
}
204
195
}
205
196
}
0 commit comments