Skip to content

Commit

Permalink
Added reified configure Action signature
Browse files Browse the repository at this point in the history
  • Loading branch information
gmazzo committed Dec 18, 2023
1 parent 947d893 commit 7ba5f8b
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 16 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ buildConfig {
buildConfigField("FEATURE_ENABLED", true)
buildConfigField("MAGIC_NUMBERS", intArrayOf(1, 2, 3, 4))
buildConfigField("STRING_LIST", arrayOf("a", "b", "c"))
buildConfigField("kotlin.collections.Map<String, Int>", "MAP", "mapOf(\"a\" to 1, \"b\" to 2)")
buildConfigField<Map<String, Int>>("MAP") {
expression("mapOf(\"a\" to 1, \"b\" to 2)")
}
buildConfigField("com.github.gmazzo.buildconfig.demos.kts.SomeData", "DATA", "SomeData(\"a\", 1)")

}
Expand Down
20 changes: 8 additions & 12 deletions demo-project/kts/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -172,16 +172,12 @@ buildConfig {
buildConfigField("BOOLEAN_SET_PROVIDER", provider { setOf(true, null, false) })

// custom formats with expressions, including Map and custom types
buildConfigField(
"kotlin.collections.Map<String, Int>",
"MAP",
"mapOf(\"a\" to 1, \"b\" to 2)"
)
buildConfigField(
"kotlin.collections.Map<String, Int>",
"MAP_PROVIDER",
provider { "mapOf(\"a\" to 1, \"b\" to 2)" }
)
buildConfigField<Map<String, Int>>("MAP") {
expression("mapOf(\"a\" to 1, \"b\" to 2)")
}
buildConfigField<Map<String, Int>>("MAP_PROVIDER") {
expression("mapOf(\"a\" to 1, \"b\" to 2)")
}
buildConfigField(
"com.github.gmazzo.buildconfig.demos.kts.SomeData",
"DATA",
Expand Down Expand Up @@ -216,8 +212,8 @@ buildConfig.forClass("BuildResources") {
files.visit {
val name = path.uppercase().replace("\\W".toRegex(), "_")

fields.add(objects.newInstance<BuildConfigField>( name).apply {
type(File::class.java)
fields.add(objects.newInstance<BuildConfigField>(name).apply {
type(File::class)
expression("File(\"$path\")")
})
}
Expand Down
2 changes: 1 addition & 1 deletion plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ gradlePlugin {
tasks.withType<Test> {
workingDir = temporaryDir
useJUnitPlatform()
doLast { Thread.sleep(2000) } // allows GradleRunner to store JaCoCo data before computing task outputs
doLast { Thread.sleep(5000) } // allows GradleRunner to store JaCoCo data before computing task outputs
}

tasks.check {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Optional
import java.io.Serializable
import java.lang.reflect.Type
import kotlin.reflect.KClass
import kotlin.reflect.KType

interface BuildConfigField : Named {
Expand Down Expand Up @@ -40,6 +41,10 @@ interface BuildConfigField : Named {
this.type.value(nameOf(type)).disallowChanges()
}

fun type(type: KClass<*>) = apply {
type(type.java)
}

fun type(type: KType) = apply {
this.type.value(nameOf(type)).disallowChanges()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ data class BuildConfigKotlinGenerator(
}

private fun BuildConfigType.toTypeName(): TypeName {
var type: TypeName = when (className.lowercase()) {
val kotlinClassName = runCatching { Class.forName(className).kotlin.qualifiedName!! }.getOrDefault(className)
var type: TypeName = when (kotlinClassName.lowercase()) {
"boolean" -> if (array && !nullable) BOOLEAN_ARRAY else BOOLEAN
"byte" -> if (array && !nullable) BYTE_ARRAY else BYTE
"short" -> if (array && !nullable) SHORT_ARRAY else SHORT
Expand All @@ -104,7 +105,7 @@ data class BuildConfigKotlinGenerator(
"string" -> STRING
"list" -> LIST
"set" -> SET
else -> ClassName.bestGuess(className)
else -> ClassName.bestGuess(kotlinClassName)
}
if (typeArguments.isNotEmpty())
type = (type as ClassName).parameterizedBy(*typeArguments.map { it.toTypeName() }.toTypedArray())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ import org.gradle.api.provider.Provider
import java.io.Serializable
import kotlin.reflect.typeOf

@BuildConfigDsl
inline fun <reified Type : Any> BuildConfigClassSpec.buildConfigField(
name: String,
crossinline configure: BuildConfigField.() -> Unit
): NamedDomainObjectProvider<BuildConfigField> = buildConfigField(name, Action {
it.type(typeOf<Type>())
configure.invoke(it)
})

@BuildConfigDsl
inline fun <reified Type : Serializable?> BuildConfigClassSpec.buildConfigField(
name: String,
Expand Down

0 comments on commit 7ba5f8b

Please sign in to comment.