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

Added support for customizing generated output #181

Merged
merged 1 commit into from
Sep 17, 2024
Merged
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
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
Loading