Skip to content

Commit 24f1a98

Browse files
committed
basic support for aggregating scoverage across subprojects
1 parent 60f63a0 commit 24f1a98

File tree

5 files changed

+69
-19
lines changed

5 files changed

+69
-19
lines changed
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.scoverage;
2+
3+
import scoverage.Coverage;
4+
import scoverage.report.CoberturaXmlWriter;
5+
import scoverage.report.CoverageAggregator;
6+
import scoverage.report.ScoverageHtmlWriter;
7+
8+
import java.io.File;
9+
10+
public class AggregateReportApp {
11+
12+
public static void main(String... args) {
13+
File rootDir = new File(args[0]);
14+
File reportDir = new File(args[1]);
15+
Boolean clean = Boolean.parseBoolean(args[2]);
16+
reportDir.mkdirs();
17+
Coverage coverage = CoverageAggregator.aggregate(rootDir, clean).get();
18+
new ScoverageHtmlWriter(rootDir, reportDir).write(coverage);
19+
new CoberturaXmlWriter(rootDir, reportDir).write(coverage);
20+
}
21+
22+
}
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.scoverage
2+
3+
import org.gradle.api.tasks.JavaExec
4+
5+
class ScoverageAggregate extends JavaExec {
6+
7+
boolean clean = false
8+
File reportDir
9+
10+
@Override
11+
void exec() {
12+
setClasspath(ScoveragePlugin.extensionIn(project).pluginClasspath)
13+
setMain('org.scoverage.AggregateReportApp')
14+
def reportPath = reportDirOrDefault()
15+
setArgs([project.projectDir, reportPath.absolutePath, clean])
16+
super.exec()
17+
def reportEntryPoint = new File(reportPath, 'index.html').absolutePath
18+
project.logger.lifecycle("Wrote aggregated scoverage report to ${reportEntryPoint}")
19+
}
20+
21+
def reportDirOrDefault() {
22+
return reportDir ? reportDir : new File(project.buildDir, 'scoverage-aggregate')
23+
}
24+
}

Diff for: src/main/groovy/org/scoverage/ScoverageExtension.groovy

+6-18
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ class ScoverageExtension {
4646
/** regex for each excluded file */
4747
List<String> excludedFiles = []
4848

49+
FileCollection pluginClasspath
50+
4951
ScoverageExtension(Project project) {
5052

5153
project.plugins.apply(JavaPlugin.class);
@@ -93,8 +95,9 @@ class ScoverageExtension {
9395
})
9496
}
9597

96-
project.tasks.create(ScoveragePlugin.REPORT_NAME, JavaExec.class) {
98+
project.tasks.create(ScoveragePlugin.REPORT_NAME, ScoverageReport.class) {
9799
dependsOn(project.tasks[ScoveragePlugin.TEST_NAME])
100+
onlyIf { ScoveragePlugin.extensionIn(project).dataDir.list() }
98101
}
99102

100103
project.tasks.create(ScoveragePlugin.CHECK_NAME, OverallCheckTask.class) {
@@ -104,6 +107,8 @@ class ScoverageExtension {
104107
sources = project.projectDir
105108
dataDir = new File(project.buildDir, 'scoverage')
106109
reportDir = new File(project.buildDir, 'reports' + File.separatorChar + 'scoverage')
110+
def classLocation = ScoverageExtension.class.getProtectionDomain().getCodeSource().getLocation()
111+
pluginClasspath = project.files(classLocation.file) + project.configurations.scoverage
107112
}
108113

109114
private Action<Project> configureRuntimeOptions = new Action<Project>() {
@@ -113,7 +118,6 @@ class ScoverageExtension {
113118

114119
def extension = ScoveragePlugin.extensionIn(t)
115120
extension.dataDir.mkdirs()
116-
extension.reportDir.mkdirs()
117121

118122
Configuration configuration = t.configurations[ScoveragePlugin.CONFIGURATION_NAME]
119123
File pluginFile
@@ -122,7 +126,6 @@ class ScoverageExtension {
122126
} catch(NoSuchElementException e) {
123127
throw new GradleException("Could not find a plugin jar in configuration '${ScoveragePlugin.CONFIGURATION_NAME}'")
124128
}
125-
FileCollection pluginDependencies = configuration.filter { it != pluginFile }
126129

127130
t.tasks[ScoveragePlugin.COMPILE_NAME].configure {
128131
List<String> parameters = ['-Xplugin:' + pluginFile.absolutePath]
@@ -151,21 +154,6 @@ class ScoverageExtension {
151154
// the compile task creates a store of measured statements
152155
outputs.file(new File(extension.dataDir, 'scoverage.coverage.xml'))
153156
}
154-
155-
t.tasks[ScoveragePlugin.REPORT_NAME].configure {
156-
def classLocation = ScoverageExtension.class.getProtectionDomain().getCodeSource().getLocation()
157-
classpath = project.files(classLocation.file) + configuration
158-
main = 'org.scoverage.ScoverageReport'
159-
args = [
160-
extension.sources,
161-
extension.dataDir.absolutePath,
162-
extension.reportDir.absolutePath
163-
]
164-
inputs.dir(extension.dataDir)
165-
outputs.dir(extension.reportDir)
166-
}
167-
168157
}
169158
}
170-
171159
}

Diff for: src/main/groovy/org/scoverage/ScoverageReport.groovy

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.scoverage
2+
3+
import org.gradle.api.tasks.JavaExec
4+
5+
class ScoverageReport extends JavaExec {
6+
7+
@Override
8+
void exec() {
9+
def extension = ScoveragePlugin.extensionIn(project)
10+
extension.reportDir.mkdirs()
11+
setClasspath(extension.pluginClasspath)
12+
setMain('org.scoverage.SingleReportApp')
13+
setArgs([extension.sources.absolutePath, extension.dataDir.absolutePath, extension.reportDir.absolutePath])
14+
super.exec()
15+
}
16+
}

Diff for: src/main/groovy/org/scoverage/ScoverageReport.java renamed to src/main/groovy/org/scoverage/SingleReportApp.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
/**
1616
* late binding of scoverage core libraries (without a dependency on groovy)
1717
*/
18-
public class ScoverageReport {
18+
public class SingleReportApp {
1919

2020
public static void main(String... args) {
2121
File sourceDir = new File(args[0]);

0 commit comments

Comments
 (0)