Skip to content

Commit

Permalink
Removed reflection on Kotlin binding
Browse files Browse the repository at this point in the history
  • Loading branch information
gmazzo committed Dec 28, 2023
1 parent 7611612 commit e119607
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 17 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ 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
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ mockk = { module = "io.mockk:mockk", version = "1.13.8" }

[plugins]
android = { id = "com.android.application", version = "8.2.0" }
buildConfig = "com.github.gmazzo.buildconfig:5.2.0"
gradle-pluginPublish = { id = "com.gradle.plugin-publish", version = "1.2.1" }
jacoco-testkit = { id = "pl.droidsonroids.jacoco.testkit", version = "1.0.12" }
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
Expand Down
7 changes: 7 additions & 0 deletions plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ plugins {
alias(libs.plugins.gradle.pluginPublish)
alias(libs.plugins.jacoco.testkit)
alias(libs.plugins.publicationsReport)
alias(libs.plugins.buildConfig)
}

group = "com.github.gmazzo.buildconfig"
Expand Down Expand Up @@ -58,6 +59,12 @@ tasks.withType<Test> {
doLast { Thread.sleep(5000) } // allows GradleRunner to store JaCoCo data before computing task outputs
}

val localRepo = publishing.repositories.maven(layout.buildDirectory.dir("repo")) { name = "Local" }
buildConfig {
buildConfigField("LOCAL_REPO", localRepo.url)
buildConfigField("LOCAL_VERSION", provider { version.toString() })
}

tasks.check {
dependsOn("jacocoTestReport")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,26 @@ 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.api.file.SourceDirectorySet
import org.gradle.kotlin.dsl.getByName
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSetContainer

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

// 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 val sourceSets =
project.extensions.getByName<KotlinSourceSetContainer>("kotlin").sourceSets

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

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

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

}
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
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<Named> by kotlinHandler {
) : PluginBindingHandler<KotlinSourceSet> by kotlinHandler {

override fun nameOf(sourceSet: Named) = when (val name = kotlinHandler.nameOf(sourceSet)) {
override fun nameOf(sourceSet: KotlinSourceSet) = 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 e119607

Please sign in to comment.