Skip to content

Commit 723fdb3

Browse files
authored
Extract JacocoReport task creation function to a seperate file (#77)
1 parent 4ee5da6 commit 723fdb3

File tree

3 files changed

+56
-29
lines changed

3 files changed

+56
-29
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.neotech.plugin.rootcoverage
2+
3+
import org.gradle.api.Project
4+
import org.gradle.testing.jacoco.tasks.JacocoReport
5+
import org.neotech.plugin.rootcoverage.utilities.getReportOutputFile
6+
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."
10+
private const val JACOCO_PLUGIN_NAME = "jacoco"
11+
12+
internal fun Project.createJacocoReportTask(rootProjectExtension: RootCoveragePluginExtension): JacocoReport {
13+
val task = tasks.create(TASK_NAME, JacocoReport::class.java)
14+
15+
// Make sure to only read from the rootProjectExtension after the project has been evaluated
16+
afterEvaluate {
17+
task.reports.apply {
18+
html.required.set(rootProjectExtension.generateHtml)
19+
xml.required.set(rootProjectExtension.generateXml)
20+
csv.required.set(rootProjectExtension.generateCsv)
21+
}
22+
}
23+
24+
// Make sure to configure this JacocoReport task after the JaCoCoPlugin itself has been fully applied,
25+
// otherwise the JaCoCoPlugin may override settings in configureJacocoReportsDefaults()
26+
// https://github.com/gradle/gradle/blob/c177053ff95a1582c7919befe67993e0f1677f53/subprojects/jacoco/src/main/java/org/gradle/testing/jacoco/plugins/JacocoPlugin.java#L211
27+
pluginManager.withPlugin(JACOCO_PLUGIN_NAME) {
28+
task.group = TASK_GROUP_NAME
29+
task.description = TASK_DESCRIPTION
30+
31+
task.reports.apply {
32+
html.outputLocation.set(getReportOutputFile("jacoco"))
33+
xml.outputLocation.set(getReportOutputFile("jacoco.xml"))
34+
csv.outputLocation.set(getReportOutputFile("jacoco.csv"))
35+
}
36+
}
37+
38+
// assertAndroidCodeCoverageVariantExists()
39+
40+
return task
41+
}

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

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ import com.android.build.api.artifact.MultipleArtifact
55
import com.android.build.api.dsl.BuildType
66
import com.android.build.api.variant.AndroidComponentsExtension
77
import com.android.build.api.variant.Variant
8-
import com.android.build.gradle.AppExtension
9-
import com.android.build.gradle.LibraryExtension
10-
import com.android.build.gradle.api.SourceKind
118
import org.gradle.api.GradleException
129
import org.gradle.api.NamedDomainObjectContainer
1310
import org.gradle.api.Plugin
@@ -20,6 +17,7 @@ import org.neotech.plugin.rootcoverage.utilities.afterAndroidPluginApplied
2017
import org.neotech.plugin.rootcoverage.utilities.assertMinimumRequiredAGPVersion
2118
import org.neotech.plugin.rootcoverage.utilities.fileTree
2219
import org.neotech.plugin.rootcoverage.utilities.onVariant
20+
import org.neotech.plugin.rootcoverage.utilities.getReportOutputFile
2321
import java.io.File
2422

2523
class RootCoveragePlugin : Plugin<Project> {
@@ -52,30 +50,9 @@ class RootCoveragePlugin : Plugin<Project> {
5250
}
5351
}
5452

55-
private fun createSubProjectCoverageTask(subProject: Project) {
56-
val task = subProject.tasks.create("coverageReport", JacocoReport::class.java)
57-
58-
// Make sure to only read from the rootProjectExtension after the project has been evaluated
59-
subProject.afterEvaluate {
60-
task.reports.html.required.set(rootProjectExtension.generateHtml)
61-
task.reports.xml.required.set(rootProjectExtension.generateXml)
62-
task.reports.csv.required.set(rootProjectExtension.generateCsv)
63-
}
64-
65-
// Make sure to configure this JacocoReport task after the JaCoCoPlugin itself has been fully applied, otherwise the JaCoCoPlugin
66-
// may override settings in configureJacocoReportsDefaults()
67-
// https://github.com/gradle/gradle/blob/c177053ff95a1582c7919befe67993e0f1677f53/subprojects/jacoco/src/main/java/org/gradle/testing/jacoco/plugins/JacocoPlugin.java#L211
68-
subProject.pluginManager.withPlugin("jacoco") {
69-
task.group = "reporting"
70-
task.description = "Generates a Jacoco for this Gradle module."
71-
72-
task.reports.html.outputLocation.set(subProject.file("${subProject.buildDir}/reports/jacoco"))
73-
task.reports.xml.outputLocation.set(subProject.file("${subProject.buildDir}/reports/jacoco.xml"))
74-
task.reports.csv.outputLocation.set(subProject.file("${subProject.buildDir}/reports/jacoco.csv"))
75-
}
76-
77-
//subProject.assertAndroidCodeCoverageVariantExists()
7853

54+
private fun createSubProjectCoverageTask(subProject: Project) {
55+
val task = subProject.createJacocoReportTask(rootProjectExtension)
7956
task.addSubProject(task.project)
8057
}
8158

@@ -95,9 +72,9 @@ class RootCoveragePlugin : Plugin<Project> {
9572
project.pluginManager.withPlugin("jacoco") {
9673
task.group = "reporting"
9774
task.description = "Generates a Jacoco report with combined results from all the subprojects."
98-
task.reports.html.outputLocation.set(project.file("${project.buildDir}/reports/jacoco"))
99-
task.reports.xml.outputLocation.set(project.file("${project.buildDir}/reports/jacoco.xml"))
100-
task.reports.csv.outputLocation.set(project.file("${project.buildDir}/reports/jacoco.csv"))
75+
task.reports.html.outputLocation.set(project.getReportOutputFile("jacoco"))
76+
task.reports.xml.outputLocation.set(project.getReportOutputFile("jacoco.xml"))
77+
task.reports.csv.outputLocation.set(project.getReportOutputFile("jacoco.csv"))
10178
}
10279

10380
project.allprojects.forEach { subProject ->
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.neotech.plugin.rootcoverage.utilities
2+
3+
import org.gradle.api.Project
4+
import java.io.File
5+
6+
/**
7+
* Returns the output report path composed from the given [fileName] as a [File].
8+
*/
9+
internal fun Project.getReportOutputFile(fileName: String): File = file("$buildDir/reports/$fileName")

0 commit comments

Comments
 (0)