Skip to content

Commit 3fc4cef

Browse files
committed
#30 Configure kotlinOptions{} after project evaluation to respect provided outputPath for report
1 parent 79890ee commit 3fc4cef

File tree

1 file changed

+31
-24
lines changed
  • gradle-plugin/src/main/kotlin/dev/shreyaspatil/composeCompilerMetricsGenerator/plugin

1 file changed

+31
-24
lines changed

gradle-plugin/src/main/kotlin/dev/shreyaspatil/composeCompilerMetricsGenerator/plugin/ReportGenPlugin.kt

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@ import com.android.build.api.dsl.CommonExtension
2727
import com.android.build.api.variant.AndroidComponentsExtension
2828
import dev.shreyaspatil.composeCompilerMetricsGenerator.plugin.task.createComposeCompilerReportGenTaskForVariant
2929
import dev.shreyaspatil.composeCompilerMetricsGenerator.plugin.task.executingComposeCompilerReportGenerationGradleTask
30-
import dev.shreyaspatil.composeCompilerMetricsGenerator.plugin.utils.kotlinOptions
3130
import org.gradle.api.Plugin
3231
import org.gradle.api.Project
32+
import org.gradle.kotlin.dsl.getByType
33+
import org.jetbrains.kotlin.gradle.dsl.KotlinAndroidProjectExtension
34+
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmOptions
3335

3436
@Suppress("UnstableApiUsage")
3537
class ReportGenPlugin : Plugin<Project> {
@@ -53,33 +55,38 @@ class ReportGenPlugin : Plugin<Project> {
5355
if (isComposeEnabled != true) {
5456
error("Jetpack Compose is not found enabled in this module '$name'")
5557
}
56-
}
5758

58-
// When this method returns true it means gradle task for generating report is executing otherwise
59-
// normal compilation task is executing.
60-
val isFromReportGenGradleTask = target.executingComposeCompilerReportGenerationGradleTask()
61-
if (isFromReportGenGradleTask) {
62-
commonExtension?.configureKotlinOptionsForComposeCompilerReport(reportExt)
59+
// When this method returns true it means gradle task for generating report is executing otherwise
60+
// normal compilation task is executing.
61+
val isFromReportGenGradleTask = target.executingComposeCompilerReportGenerationGradleTask()
62+
if (isFromReportGenGradleTask) {
63+
val kotlinAndroidExt = extensions.getByType<KotlinAndroidProjectExtension>()
64+
kotlinAndroidExt.target {
65+
// Exclude for test variants, no use!
66+
compilations.filter { !it.name.endsWith("Test") }.forEach {
67+
it.kotlinOptions {
68+
configureKotlinOptionsForComposeCompilerReport(this@afterEvaluate)
69+
}
70+
}
71+
}
72+
}
6373
}
6474
}
6575

66-
private fun CommonExtension<*, *, *, *>.configureKotlinOptionsForComposeCompilerReport(
67-
reportExtension: ComposeCompilerReportExtension,
68-
) {
69-
kotlinOptions {
70-
val outputPath = reportExtension.composeRawMetricsOutputDirectory.absolutePath
71-
if (reportExtension.enableReport.get()) {
72-
freeCompilerArgs += listOf(
73-
"-P",
74-
"plugin:androidx.compose.compiler.plugins.kotlin:reportsDestination=$outputPath"
75-
)
76-
}
77-
if (reportExtension.enableMetrics.get()) {
78-
freeCompilerArgs += listOf(
79-
"-P",
80-
"plugin:androidx.compose.compiler.plugins.kotlin:metricsDestination=$outputPath"
81-
)
82-
}
76+
private fun KotlinJvmOptions.configureKotlinOptionsForComposeCompilerReport(project: Project) {
77+
val reportExtension = project.extensions.getByType<ComposeCompilerReportExtension>()
78+
val outputPath = reportExtension.composeRawMetricsOutputDirectory.absolutePath
79+
if (reportExtension.enableReport.get()) {
80+
freeCompilerArgs += listOf(
81+
"-P",
82+
"plugin:androidx.compose.compiler.plugins.kotlin:reportsDestination=$outputPath"
83+
)
84+
}
85+
if (reportExtension.enableMetrics.get()) {
86+
freeCompilerArgs += listOf(
87+
"-P",
88+
"plugin:androidx.compose.compiler.plugins.kotlin:metricsDestination=$outputPath"
89+
)
8390
}
8491
}
8592
}

0 commit comments

Comments
 (0)