Skip to content

Commit

Permalink
Added support for customizing generated output (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
gmazzo authored Sep 17, 2024
1 parent 72db037 commit 90b6792
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
14 changes: 14 additions & 0 deletions demo-project/kts-multiplatform/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import com.github.gmazzo.buildconfig.generators.BuildConfigKotlinGenerator
import com.squareup.kotlinpoet.AnnotationSpec
import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.TypeSpec

plugins {
alias(libs.plugins.kotlin.multiplatform)
id("com.github.gmazzo.buildconfig")
Expand All @@ -15,9 +20,18 @@ dependencies {
}

buildConfig {
generator = object : BuildConfigKotlinGenerator() {
override fun adaptSpec(spec: TypeSpec) = spec.toBuilder()
.addAnnotation(AnnotationSpec.builder(ClassName.bestGuess("kotlin.js.JsName"))
.addMember("name = %S", spec.name!!)
.build())
.build()
}

buildConfigField("COMMON_VALUE", "aCommonValue")

sourceSets.named("jvmMain") {
useKotlinOutput() // resets `generator` back to default's Kotlin generator for JVM
buildConfigField("PLATFORM", "jvm")
buildConfigField( "JVM_VALUE", "aJvmValue")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,20 @@ import java.io.File
import javax.lang.model.element.Modifier
import java.net.URI as JavaURI

data class BuildConfigJavaGenerator(
open class BuildConfigJavaGenerator(
@get:Input var defaultVisibility: Boolean = false
) : BuildConfigGenerator {

/**
* Extension point allowing to modify the final Java class output
*/
protected open fun adaptSpec(spec: TypeSpec) = spec

/**
* Extension point allowing to modify the final Java file output
*/
protected open fun adaptSpec(spec: JavaFile) = spec

private val logger = Logging.getLogger(javaClass)

private fun BuildConfigType.toTypeName(): TypeName {
Expand Down Expand Up @@ -111,8 +121,10 @@ data class BuildConfigJavaGenerator(
.build()
)
.build()
.let(::adaptSpec)
)
.build()
.let(::adaptSpec)
.writeTo(spec.outputDir)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,23 @@ import org.gradle.api.tasks.Input
import java.io.File
import java.net.URI as JavaURI

data class BuildConfigKotlinGenerator(
open class BuildConfigKotlinGenerator(
@get:Input var topLevelConstants: Boolean = false,
@get:Input var internalVisibility: Boolean = true
) : BuildConfigGenerator {

private val logger = Logging.getLogger(javaClass)

/**
* Extension point allowing to modify the final Kotlin class output
*/
protected open fun adaptSpec(spec: TypeSpec) = spec

/**
* Extension point allowing to modify the final Kotlin file output
*/
protected open fun adaptSpec(spec: FileSpec) = spec

override fun execute(spec: BuildConfigGeneratorSpec) {
logger.debug("Generating {} for fields {}", spec.className, spec.fields)

Expand All @@ -57,6 +67,7 @@ data class BuildConfigKotlinGenerator(
FileSpec.builder(spec.packageName, spec.className)
.addFields(fields, spec.documentation)
.build()
.let(::adaptSpec)
.writeTo(spec.outputDir)
}

Expand Down Expand Up @@ -132,6 +143,7 @@ data class BuildConfigKotlinGenerator(
.addModifiers(kModifiers)
.addProperties(fields)
.build()
.let(::adaptSpec)
)
}

Expand Down

0 comments on commit 90b6792

Please sign in to comment.