Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use Groovy metaprogramming to access KGP when applied via settings plugin #25

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ private fun kotlinMultiplatformProjectWithBcvSettingsPlugin() =
private val settingsGradleKtsWithBcvPlugin = """
buildscript {
dependencies {
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin-api:1.7.20")
//classpath("org.jetbrains.kotlin:kotlin-gradle-plugin-api:1.7.20")
}
}

Expand Down
98 changes: 79 additions & 19 deletions modules/bcv-gradle-plugin/src/main/kotlin/BCVProjectPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import dev.adamko.kotlin.binary_compatibility_validator.tasks.BCVApiDumpTask
import dev.adamko.kotlin.binary_compatibility_validator.tasks.BCVApiGenerateTask
import dev.adamko.kotlin.binary_compatibility_validator.tasks.BCVDefaultTask
import javax.inject.Inject
import org.gradle.api.NamedDomainObjectContainer
import org.gradle.api.NamedDomainObjectProvider
import org.gradle.api.Plugin
import org.gradle.api.Project
Expand Down Expand Up @@ -181,31 +182,90 @@ constructor(
extension: BCVProjectExtension,
) {
project.pluginManager.withPlugin("kotlin-multiplatform") {
val kotlinTargetsContainer = project.extensions.getByType<KotlinTargetsContainer>()

kotlinTargetsContainer.targets
.matching {
it.platformType in arrayOf(KotlinPlatformType.jvm, KotlinPlatformType.androidJvm)
}.all {
val targetPlatformType = platformType

extension.targets.register(targetName) {
enabled.convention(true)
compilations
.matching {
when (targetPlatformType) {
KotlinPlatformType.jvm -> it.name == "main"
KotlinPlatformType.androidJvm -> it.name == "release"
else -> false
try {
val kotlinTargetsContainer = project.extensions.getByType<KotlinTargetsContainer>()

kotlinTargetsContainer.targets
.matching {
it.platformType in arrayOf(KotlinPlatformType.jvm, KotlinPlatformType.androidJvm)
}.all {
val targetPlatformType = platformType

extension.targets.register(targetName) {
enabled.convention(true)
compilations
.matching {
when (targetPlatformType) {
KotlinPlatformType.jvm -> it.name == "main"
KotlinPlatformType.androidJvm -> it.name == "release"
else -> false
}
}.all {
inputClasses.from(output.classesDirs)
}
}.all {
inputClasses.from(output.classesDirs)
}
}
}
} catch (e: Throwable) {
when (e) {
is NoClassDefFoundError,
is TypeNotPresentException -> {
logger.info("Failed to apply BCVProjectPlugin to project ${project.path} with plugin $id using KGP classes $e")
createKotlinMultiplatformTargetsHack(project, extension)
}

else -> throw e
}
}
}
}

private fun createKotlinMultiplatformTargetsHack(
project: Project,
extension: BCVProjectExtension,
) {
logger.info("Falling back to Groovy metaprogramming to access to KGP classes ${project.path} https://github.com/adamko-dev/kotlin-binary-compatibility-validator-mu/issues/1")
val kotlinExtension = project.extensions.findByName("kotlin")
?: return

val targets = kotlinExtension.withGroovyBuilder { "getTargets"() }
as NamedDomainObjectContainer<*>

targets
.matching { target ->
val platformType = target.withGroovyBuilder { "getPlatformType"() }
?: return@matching false
val platformName = platformType.withGroovyBuilder { "getName"() }
platformName in listOf("jvm", "androidJvm")
}
.all action@{
val platformType = withGroovyBuilder { "getPlatformType"() } ?: return@action
val platformName = platformType.withGroovyBuilder { "getName"() }
val targetName = withGroovyBuilder { "getTargetName"() }
val compilations = withGroovyBuilder { "getCompilations"() }
as NamedDomainObjectContainer<*>

extension.targets.register(targetName.toString()) {
enabled.convention(true)

compilations
.matching { comp ->
val compName = comp.withGroovyBuilder { "getName"() }

when (platformName) {
"jvm" -> compName == "main"
"androidJvm" -> compName == "release"
else -> false
}
}
.all {
val output = withGroovyBuilder { "getOutput"() } ?: return@all
val classesDirs = output.withGroovyBuilder { "getClassesDirs"() }
inputClasses.from(classesDirs)
}
}
}
}

private fun createJavaTestFixtureTargets(
project: Project,
extension: BCVProjectExtension,
Expand Down