[mpp] Migrate UnusedSourceSetsChecker to the new infra
This commit is contained in:
committed by
Space Team
parent
ee9a952da2
commit
4e479cb54b
+3
-7
@@ -1605,25 +1605,21 @@ open class NewMultiplatformIT : BaseGradleIT() {
|
||||
|
||||
build {
|
||||
assertSuccessful()
|
||||
assertContains(UnusedSourceSetsChecker.WARNING_PREFIX_ONE, UnusedSourceSetsChecker.WARNING_INTRO)
|
||||
assertHasDiagnostic(KotlinToolingDiagnostics.UnusedSourceSetsWarning)
|
||||
}
|
||||
|
||||
gradleBuildScript().appendText("\nkotlin { sourceSets { bar { dependsOn foo } } }")
|
||||
|
||||
build {
|
||||
assertSuccessful()
|
||||
assertContains(UnusedSourceSetsChecker.WARNING_PREFIX_MANY, UnusedSourceSetsChecker.WARNING_INTRO)
|
||||
assertHasDiagnostic(KotlinToolingDiagnostics.UnusedSourceSetsWarning)
|
||||
}
|
||||
|
||||
gradleBuildScript().appendText("\nkotlin { sourceSets { jvm6Main { dependsOn bar } } }")
|
||||
|
||||
build {
|
||||
assertSuccessful()
|
||||
assertNotContains(
|
||||
UnusedSourceSetsChecker.WARNING_PREFIX_ONE,
|
||||
UnusedSourceSetsChecker.WARNING_PREFIX_MANY,
|
||||
UnusedSourceSetsChecker.WARNING_INTRO
|
||||
)
|
||||
assertNoDiagnostic(KotlinToolingDiagnostics.UnusedSourceSetsWarning)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+3
-5
@@ -6,7 +6,7 @@
|
||||
package org.jetbrains.kotlin.gradle.android
|
||||
|
||||
import org.gradle.util.GradleVersion
|
||||
import org.jetbrains.kotlin.gradle.plugin.mpp.UnusedSourceSetsChecker
|
||||
import org.jetbrains.kotlin.gradle.plugin.diagnostics.KotlinToolingDiagnostics
|
||||
import org.jetbrains.kotlin.gradle.testbase.*
|
||||
import org.jetbrains.kotlin.gradle.testbase.TestVersions.AGP.AGP_70
|
||||
import org.jetbrains.kotlin.gradle.testbase.TestVersions.AGP.AGP_71
|
||||
@@ -1015,10 +1015,8 @@ class KotlinAndroidMppIT : KGPBaseTest() {
|
||||
buildJdk = jdkVersion.location
|
||||
) {
|
||||
build("assembleDebug") {
|
||||
assertOutputDoesNotContain(UnusedSourceSetsChecker.WARNING_PREFIX_ONE)
|
||||
assertOutputDoesNotContain(UnusedSourceSetsChecker.WARNING_PREFIX_MANY)
|
||||
assertOutputDoesNotContain(UnusedSourceSetsChecker.WARNING_INTRO)
|
||||
output.assertNoDiagnostic(KotlinToolingDiagnostics.UnusedSourceSetsWarning)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+21
@@ -54,4 +54,25 @@ object KotlinToolingDiagnostics {
|
||||
""".trimIndent()
|
||||
)
|
||||
}
|
||||
|
||||
object UnusedSourceSetsWarning : ToolingDiagnosticFactory(WARNING) {
|
||||
operator fun invoke(sourceSetNames: Collection<String>): ToolingDiagnostic {
|
||||
|
||||
val cause = if (sourceSetNames.size == 1) {
|
||||
"The Kotlin source set ${sourceSetNames.single()} was configured but not added to any Kotlin compilation.\n"
|
||||
} else {
|
||||
val sourceSetNames = sourceSetNames.joinToString("\n") { " * $it" }
|
||||
"The following Kotlin source sets were configured but not added to any Kotlin compilation:\n" +
|
||||
sourceSetNames
|
||||
}
|
||||
|
||||
val details =
|
||||
"""
|
||||
|You can add a source set to a target's compilation by connecting it with the compilation's default source set using 'dependsOn'.
|
||||
|See https://kotl.in/connecting-source-sets
|
||||
""".trimMargin()
|
||||
|
||||
return build(cause + "\n" + details)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.gradle.plugin.diagnostics.checkers
|
||||
|
||||
import org.jetbrains.kotlin.gradle.plugin.diagnostics.KotlinGradleProjectChecker
|
||||
import org.jetbrains.kotlin.gradle.plugin.diagnostics.KotlinGradleProjectCheckerContext
|
||||
import org.jetbrains.kotlin.gradle.plugin.diagnostics.KotlinToolingDiagnostics
|
||||
import org.jetbrains.kotlin.gradle.plugin.diagnostics.KotlinToolingDiagnosticsCollector
|
||||
import org.jetbrains.kotlin.gradle.plugin.sources.android.androidSourceSetInfoOrNull
|
||||
import org.jetbrains.kotlin.gradle.plugin.sources.internal
|
||||
|
||||
internal object UnusedSourceSetsChecker : KotlinGradleProjectChecker {
|
||||
|
||||
override fun KotlinGradleProjectCheckerContext.runChecks(collector: KotlinToolingDiagnosticsCollector) {
|
||||
val unusedSourceSets = multiplatformExtension?.sourceSets.orEmpty()
|
||||
// Ignoring Android source sets
|
||||
.filter { it.androidSourceSetInfoOrNull == null }
|
||||
.filter { it.internal.compilations.isEmpty() }
|
||||
|
||||
if (unusedSourceSets.isNotEmpty()) {
|
||||
collector.report(project, KotlinToolingDiagnostics.UnusedSourceSetsWarning(unusedSourceSets.toSet().map { it.name }))
|
||||
}
|
||||
}
|
||||
}
|
||||
-2
@@ -191,8 +191,6 @@ class KotlinMultiplatformPlugin : Plugin<Project> {
|
||||
KotlinBuildStatsService.getInstance()?.report(StringMetrics.MPP_PLATFORMS, targetName)
|
||||
}
|
||||
|
||||
UnusedSourceSetsChecker.checkSourceSets(project)
|
||||
|
||||
project.launchInStage(KotlinPluginLifecycle.Stage.ReadyForExecution) {
|
||||
project.runProjectConfigurationHealthCheck {
|
||||
checkSourceSetVisibilityRequirements(project)
|
||||
|
||||
-52
@@ -1,52 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.gradle.plugin.mpp
|
||||
|
||||
import org.gradle.api.Project
|
||||
import org.jetbrains.kotlin.gradle.dsl.kotlinExtension
|
||||
import org.jetbrains.kotlin.gradle.plugin.*
|
||||
import org.jetbrains.kotlin.gradle.plugin.KotlinPluginLifecycle
|
||||
import org.jetbrains.kotlin.gradle.plugin.launchInStage
|
||||
import org.jetbrains.kotlin.gradle.plugin.sources.android.androidSourceSetInfoOrNull
|
||||
import org.jetbrains.kotlin.gradle.plugin.sources.internal
|
||||
|
||||
object UnusedSourceSetsChecker {
|
||||
const val WARNING_PREFIX_ONE =
|
||||
"The Kotlin source set"
|
||||
|
||||
const val WARNING_PREFIX_MANY =
|
||||
"The following Kotlin source sets were"
|
||||
|
||||
const val WARNING_INTRO = "configured but not added to any Kotlin compilation"
|
||||
|
||||
const val WARNING_BOTTOM_LINE =
|
||||
"You can add a source set to a target's compilation by connecting it with the compilation's default source set using 'dependsOn'.\n" +
|
||||
"See https://kotl.in/connecting-source-sets"
|
||||
|
||||
private fun reportUnusedSourceSets(project: Project, sourceSets: Set<KotlinSourceSet>) {
|
||||
require(sourceSets.isNotEmpty())
|
||||
val message = when (sourceSets.size) {
|
||||
1 -> "$WARNING_PREFIX_ONE ${sourceSets.single().name} was $WARNING_INTRO. $WARNING_BOTTOM_LINE"
|
||||
else -> {
|
||||
val list = sourceSets.joinToString("\n", "\n", "\n") { " * ${it.name}" }
|
||||
"$WARNING_PREFIX_MANY $WARNING_INTRO:$list$WARNING_BOTTOM_LINE"
|
||||
}
|
||||
}
|
||||
project.logger.warn("\n" + message) // make sure the message stands out
|
||||
}
|
||||
|
||||
fun checkSourceSets(project: Project) {
|
||||
project.launchInStage(KotlinPluginLifecycle.Stage.ReadyForExecution) {
|
||||
val unusedSourceSets = project.kotlinExtension.sourceSets
|
||||
// Ignoring Android source sets
|
||||
.filter { it.androidSourceSetInfoOrNull == null }
|
||||
.filter { it.internal.compilations.isEmpty() }
|
||||
if (unusedSourceSets.isNotEmpty()) {
|
||||
reportUnusedSourceSets(project, unusedSourceSets.toSet())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user