Skip to content

Commit

Permalink
Revert "Removed reflection on Kotlin binding"
Browse files Browse the repository at this point in the history
This partially reverts commit 428007b.
  • Loading branch information
gmazzo committed Jan 12, 2024
1 parent 7017e60 commit febf67d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 20 deletions.
9 changes: 0 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,6 @@ internal object BuildConfig {
> buildConfigField("FILE", file("aFile").relativeToOrSelf(projectDir)) // use this instead, for instance
> ```
> [!IMPORTANT]
> When using along with the Kotlin plugin, both needs to be loaded in the same classloader (declared in the same Gradle project, like the root one for instance)
> ```kotlin
> plugins {
> id("org.jetbrains.kotlin.jvm") apply false
> id("com.github.gmazzo.buildconfig") apply false
> }
> ```
## Usage in Groovy
On your `build.gradle` add:
```groovy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,31 @@ import com.github.gmazzo.buildconfig.generators.BuildConfigKotlinGenerator
import org.gradle.api.Named
import org.gradle.api.NamedDomainObjectContainer
import org.gradle.api.Project
import org.gradle.kotlin.dsl.getByName
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSetContainer
import org.gradle.api.file.SourceDirectorySet

internal class KotlinHandler(
project: Project,
private val extension: BuildConfigExtension
) : PluginBindingHandler<KotlinSourceSet> {
) : PluginBindingHandler<Named> { // TODO should be KotlinSourceSet but fails on tests (but not from external project)

override val sourceSets =
project.extensions.getByName<KotlinSourceSetContainer>("kotlin").sourceSets
// project.extensions.getByName<KotlinSourceSetContainer>("kotlin").sourceSets
override val sourceSets = with(project.extensions.getByName("kotlin")) {
@Suppress("UNCHECKED_CAST")
javaClass.getMethod("getSourceSets")
.invoke(this) as NamedDomainObjectContainer<Named>
}

override fun nameOf(sourceSet: KotlinSourceSet): String = sourceSet.name
override fun nameOf(sourceSet: Named): String = sourceSet.name

override fun onBind() {
extension.generator.convention(BuildConfigKotlinGenerator())
}

override fun onSourceSetAdded(sourceSet: KotlinSourceSet, spec: BuildConfigSourceSet) {
sourceSet.kotlin.srcDir(spec)
// sourceSet.kotlin.srcDir(spec)
override fun onSourceSetAdded(sourceSet: Named, spec: BuildConfigSourceSet) {
(sourceSet.javaClass.getMethod("getKotlin")
.invoke(sourceSet) as SourceDirectorySet)
.srcDir(spec)
}

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package com.github.gmazzo.buildconfig.internal.bindings

import org.gradle.api.Named
import org.gradle.api.tasks.SourceSet
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet

internal class KotlinMultiplatformHandler(
private val kotlinHandler: KotlinHandler
) : PluginBindingHandler<KotlinSourceSet> by kotlinHandler {
) : PluginBindingHandler<Named> by kotlinHandler {

override fun nameOf(sourceSet: KotlinSourceSet) = when (val name = kotlinHandler.nameOf(sourceSet)) {
override fun nameOf(sourceSet: Named) = when (val name = kotlinHandler.nameOf(sourceSet)) {
KotlinSourceSet.COMMON_MAIN_SOURCE_SET_NAME -> SourceSet.MAIN_SOURCE_SET_NAME
KotlinSourceSet.COMMON_TEST_SOURCE_SET_NAME -> SourceSet.TEST_SOURCE_SET_NAME
else -> name
Expand Down

0 comments on commit febf67d

Please sign in to comment.