Skip to content

Commit a53da31

Browse files
committed
add module-wise exclude logic
1 parent 078b9dc commit a53da31

File tree

2 files changed

+32
-27
lines changed

2 files changed

+32
-27
lines changed

build-logic/src/main/java/conventions/ProjectJacocoConventionPlugin.kt

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package conventions
33
import org.gradle.api.Plugin
44
import org.gradle.api.Project
55
import org.gradle.api.artifacts.VersionCatalogsExtension
6+
import org.gradle.api.file.FileTree
67
import org.gradle.configurationcache.extensions.capitalized
78
import org.gradle.kotlin.dsl.extra
89
import org.gradle.kotlin.dsl.getByType
@@ -18,47 +19,47 @@ import ytemplate.android.jacoco.jacoco
1819
*
1920
* @constructor Create empty Library jacoco convention plugin
2021
*/
22+
@Suppress("UNCHECKED_CAST")
2123
class ProjectJacocoConventionPlugin : Plugin<Project> {
22-
private val project_level_limits = mutableMapOf(
23-
"instruction" to 0.0,
24-
"branch" to 0.0,
25-
"line" to 0.0,
26-
"complexity" to 0.0,
27-
"method" to 0.0,
28-
"class" to 0.0
29-
)
30-
31-
32-
fun Project.setProjectTestCoverageLimits(projectLimits: Map<String, Double>? = null) {
33-
if (projectLimits != null) {
34-
extra.set("limits", projectLimits)
35-
} else {
36-
extra.set("limits", project_level_limits)
37-
}
38-
}
39-
4024
override fun apply(target: Project) {
4125
with(target) {
42-
val libs = extensions.getByType<VersionCatalogsExtension>().named("versionCatalogLibs")
4326
with(pluginManager) {
4427
apply("jacoco")
4528
}
46-
tasks.register<JacocoReport>("MergeHTMLJacocoReports") {
29+
30+
tasks.register<JacocoReport>("createMergedJacocoReport") {
4731
val jacocoReport = this
4832
group = "Reporting"
49-
description = "Merge all generated JacocoReport"
33+
description = "create Project Jacoco Report for debug builds for all submodules with jacoco plugin"
5034
logger.quiet("======Merging HTML Reports=========")
51-
val javaClasses: MutableCollection<String> = mutableListOf()
52-
val kotlinClasses: MutableCollection<String> = mutableListOf()
35+
val javaClasses: MutableCollection<FileTree> = mutableListOf()
36+
val kotlinClasses: MutableCollection<FileTree> = mutableListOf()
5337
val sourceDir: MutableCollection<String> = mutableListOf()
5438
val coverageFiles: MutableCollection<String> = mutableListOf()
39+
5540
subprojects {
5641
val subProject = this
5742
subProject.plugins.withType<JacocoPlugin>().configureEach {
5843
val moduleTask = tasks.findByName("createDemoDebugJacocoReport")
5944
jacocoReport.dependsOn(moduleTask)
60-
javaClasses.add("${subProject.buildDir}/intermediates/javac/demoDebug/classes")
61-
kotlinClasses.add("${subProject.buildDir}/tmp/kotlin-classes/demoDebug")
45+
}
46+
if(subProject.plugins.findPlugin(JacocoPlugin::class.java)!=null) {
47+
val excludedFiles: MutableCollection<String> = mutableListOf()
48+
if (subProject.extra.has("excludes")) {
49+
excludedFiles.addAll(subProject.extra.get("excludes") as List<String>)
50+
}
51+
javaClasses.add(fileTree("${subProject.buildDir}/intermediates/javac/demoDebug/classes") {
52+
if (excludedFiles.isNotEmpty()) {
53+
exclude(excludedFiles)
54+
}
55+
56+
}.asFileTree)
57+
kotlinClasses.add(fileTree("${subProject.buildDir}/tmp/kotlin-classes/demoDebug") {
58+
if (excludedFiles.isNotEmpty()) {
59+
exclude(excludedFiles)
60+
}
61+
}.asFileTree)
62+
6263
sourceDir.add("${subProject.projectDir}/src/main/java")
6364
sourceDir.add("${subProject.projectDir}/src/main/kotlin")
6465
sourceDir.add("${subProject.projectDir}/src/demoDebug/java")

build-logic/src/main/java/ytemplate/android/jacoco/Jacoco.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ private val excludedFiles = mutableSetOf(
4141
"**/Dagger*Component.class",
4242
"**/Dagger*Component\$Builder.class",
4343
"**/*Module_*Factory.class",
44-
"**hilt_aggregated_deps**",
44+
"**hilt*aggregated*deps**",
4545
"**/dagger/**",
4646
"**/di/module/*",
4747
"**/*_Factory*.*",
@@ -90,6 +90,7 @@ fun Project.addExclusion(excludes: Set<String>? = null) {
9090
if (excludes != null) {
9191
excludedFiles.addAll(excludes)
9292
}
93+
extra.set("excludes",excludedFiles.toList())
9394
}
9495

9596
internal fun Project.setupJacocoPlugin() {
@@ -100,6 +101,9 @@ internal fun Project.setupJacocoPlugin() {
100101
if (!extra.has("limits")) {
101102
setModuleTestCoverageLimits()
102103
}
104+
if (!extra.has("excludes")) {
105+
addExclusion()
106+
}
103107

104108
val buildTypes = android.buildTypes.map { type -> type.name }
105109
var productFlavors = android.productFlavors.map { flavor -> flavor.name }
@@ -209,7 +213,7 @@ private fun Project.addTestCoverageTask(
209213
}
210214
}
211215

212-
fun Project.jacocoTestReport(taskName: String) {
216+
fun Project.jacocoTestReport(taskName: String) {
213217
val reportDir = jacoco.reportsDirectory.asFile.get()
214218
val report = file("$reportDir/$taskName/${taskName}.xml")
215219
logger.lifecycle("Checking coverage results:$report")

0 commit comments

Comments
 (0)