From 90b6792aeeb2e2c61d1b7e2c47ecb86035099d3c Mon Sep 17 00:00:00 2001 From: Guillermo Mazzola Date: Tue, 17 Sep 2024 14:37:41 +0200 Subject: [PATCH] Added support for customizing generated output (#181) --- demo-project/kts-multiplatform/build.gradle.kts | 14 ++++++++++++++ .../generators/BuildConfigJavaGenerator.kt | 14 +++++++++++++- .../generators/BuildConfigKotlinGenerator.kt | 14 +++++++++++++- 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/demo-project/kts-multiplatform/build.gradle.kts b/demo-project/kts-multiplatform/build.gradle.kts index dade11d..366db6d 100644 --- a/demo-project/kts-multiplatform/build.gradle.kts +++ b/demo-project/kts-multiplatform/build.gradle.kts @@ -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") @@ -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") } diff --git a/plugin/src/main/kotlin/com/github/gmazzo/buildconfig/generators/BuildConfigJavaGenerator.kt b/plugin/src/main/kotlin/com/github/gmazzo/buildconfig/generators/BuildConfigJavaGenerator.kt index 5806958..f55ad3e 100644 --- a/plugin/src/main/kotlin/com/github/gmazzo/buildconfig/generators/BuildConfigJavaGenerator.kt +++ b/plugin/src/main/kotlin/com/github/gmazzo/buildconfig/generators/BuildConfigJavaGenerator.kt @@ -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 { @@ -111,8 +121,10 @@ data class BuildConfigJavaGenerator( .build() ) .build() + .let(::adaptSpec) ) .build() + .let(::adaptSpec) .writeTo(spec.outputDir) } diff --git a/plugin/src/main/kotlin/com/github/gmazzo/buildconfig/generators/BuildConfigKotlinGenerator.kt b/plugin/src/main/kotlin/com/github/gmazzo/buildconfig/generators/BuildConfigKotlinGenerator.kt index e5e947a..8cc6dc0 100644 --- a/plugin/src/main/kotlin/com/github/gmazzo/buildconfig/generators/BuildConfigKotlinGenerator.kt +++ b/plugin/src/main/kotlin/com/github/gmazzo/buildconfig/generators/BuildConfigKotlinGenerator.kt @@ -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) @@ -57,6 +67,7 @@ data class BuildConfigKotlinGenerator( FileSpec.builder(spec.packageName, spec.className) .addFields(fields, spec.documentation) .build() + .let(::adaptSpec) .writeTo(spec.outputDir) } @@ -132,6 +143,7 @@ data class BuildConfigKotlinGenerator( .addModifiers(kModifiers) .addProperties(fields) .build() + .let(::adaptSpec) ) }