Skip to content

Commit 291429d

Browse files
authored
Extract: JacocoReport task creation into a function (#78)
1 parent 1d8baad commit 291429d

File tree

2 files changed

+22
-31
lines changed

2 files changed

+22
-31
lines changed

plugin/src/main/kotlin/org/neotech/plugin/rootcoverage/JacocoTaskFactory.kt

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ import org.gradle.api.Project
44
import org.gradle.testing.jacoco.tasks.JacocoReport
55
import org.neotech.plugin.rootcoverage.utilities.getReportOutputFile
66

7-
private const val TASK_NAME = "coverageReport"
8-
private const val TASK_GROUP_NAME = "reporting"
9-
private const val TASK_DESCRIPTION = "Generates a Jacoco for this Gradle module."
107
private const val JACOCO_PLUGIN_NAME = "jacoco"
118

12-
internal fun Project.createJacocoReportTask(rootProjectExtension: RootCoveragePluginExtension): JacocoReport {
13-
val task = tasks.create(TASK_NAME, JacocoReport::class.java)
9+
internal fun Project.createJacocoReportTask(
10+
taskName: String,
11+
taskGroup: String,
12+
taskDescription: String,
13+
rootProjectExtension: RootCoveragePluginExtension
14+
): JacocoReport {
15+
val task = tasks.create(taskName, JacocoReport::class.java)
1416

1517
// Make sure to only read from the rootProjectExtension after the project has been evaluated
1618
afterEvaluate {
@@ -25,17 +27,14 @@ internal fun Project.createJacocoReportTask(rootProjectExtension: RootCoveragePl
2527
// otherwise the JaCoCoPlugin may override settings in configureJacocoReportsDefaults()
2628
// https://github.com/gradle/gradle/blob/c177053ff95a1582c7919befe67993e0f1677f53/subprojects/jacoco/src/main/java/org/gradle/testing/jacoco/plugins/JacocoPlugin.java#L211
2729
pluginManager.withPlugin(JACOCO_PLUGIN_NAME) {
28-
task.group = TASK_GROUP_NAME
29-
task.description = TASK_DESCRIPTION
30-
30+
task.group = taskGroup
31+
task.description = taskDescription
3132
task.reports.apply {
3233
html.outputLocation.set(getReportOutputFile("jacoco"))
3334
xml.outputLocation.set(getReportOutputFile("jacoco.xml"))
3435
csv.outputLocation.set(getReportOutputFile("jacoco.csv"))
3536
}
3637
}
3738

38-
// assertAndroidCodeCoverageVariantExists()
39-
4039
return task
4140
}

plugin/src/main/kotlin/org/neotech/plugin/rootcoverage/RootCoveragePlugin.kt

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import org.neotech.plugin.rootcoverage.utilities.afterAndroidPluginApplied
1818
import org.neotech.plugin.rootcoverage.utilities.assertMinimumRequiredAGPVersion
1919
import org.neotech.plugin.rootcoverage.utilities.fileTree
2020
import org.neotech.plugin.rootcoverage.utilities.onVariant
21-
import org.neotech.plugin.rootcoverage.utilities.getReportOutputFile
2221
import java.io.File
2322

2423
class RootCoveragePlugin : Plugin<Project> {
@@ -53,30 +52,23 @@ class RootCoveragePlugin : Plugin<Project> {
5352

5453

5554
private fun createSubProjectCoverageTask(subProject: Project) {
56-
val task = subProject.createJacocoReportTask(rootProjectExtension)
55+
val task = subProject.createJacocoReportTask(
56+
taskName = "coverageReport",
57+
taskGroup = "reporting",
58+
taskDescription = "Generates a Jacoco for this Gradle module.",
59+
rootProjectExtension = rootProjectExtension
60+
)
61+
// subProject.assertAndroidCodeCoverageVariantExists()
5762
task.addSubProject(task.project)
5863
}
5964

6065
private fun createCoverageTaskForRoot(project: Project) {
61-
val task = project.tasks.create("rootCoverageReport", JacocoReport::class.java)
62-
63-
// Make sure to only read from the rootProjectExtension after the project has been evaluated
64-
project.afterEvaluate {
65-
task.reports.html.required.set(rootProjectExtension.generateHtml)
66-
task.reports.xml.required.set(rootProjectExtension.generateXml)
67-
task.reports.csv.required.set(rootProjectExtension.generateCsv)
68-
}
69-
70-
// Make sure to configure this JacocoReport task after the JaCoCoPlugin itself has been fully applied, otherwise the JaCoCoPlugin
71-
// may override settings in configureJacocoReportsDefaults()
72-
// https://github.com/gradle/gradle/blob/c177053ff95a1582c7919befe67993e0f1677f53/subprojects/jacoco/src/main/java/org/gradle/testing/jacoco/plugins/JacocoPlugin.java#L211
73-
project.pluginManager.withPlugin("jacoco") {
74-
task.group = "reporting"
75-
task.description = "Generates a Jacoco report with combined results from all the subprojects."
76-
task.reports.html.outputLocation.set(project.getReportOutputFile("jacoco"))
77-
task.reports.xml.outputLocation.set(project.getReportOutputFile("jacoco.xml"))
78-
task.reports.csv.outputLocation.set(project.getReportOutputFile("jacoco.csv"))
79-
}
66+
val task = project.createJacocoReportTask(
67+
taskName = "rootCoverageReport",
68+
taskGroup = "reporting",
69+
taskDescription = "Generates a Jacoco report with combined results from all the subprojects.",
70+
rootProjectExtension = rootProjectExtension
71+
)
8072

8173
project.allprojects.forEach { subProject ->
8274
subProject.assertAndroidCodeCoverageVariantExists()

0 commit comments

Comments
 (0)