Skip to content

Commit

Permalink
Fixed support with Configuration Cache
Browse files Browse the repository at this point in the history
  • Loading branch information
gmazzo committed Dec 18, 2023
1 parent 3af5002 commit 7f16671
Show file tree
Hide file tree
Showing 17 changed files with 290 additions and 200 deletions.
51 changes: 22 additions & 29 deletions demo-project/groovy/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id 'groovy'
id 'java'
id 'com.github.gmazzo.buildconfig'
id 'java-test-fixtures'
}
Expand Down Expand Up @@ -45,8 +45,8 @@ buildConfig {
buildConfigField(byte[], "BYTE_NATIVE_ARRAY_PROVIDER", provider { [1, 2, 3] })
buildConfigField(Byte[], "BYTE_ARRAY", [1, 2, 3])
buildConfigField(Byte[], "BYTE_ARRAY_PROVIDER", provider { [1, 2, 3] })
buildConfigField(Byte[], "BYTE_ARRAY_NULLABLE", [1, null, 3])
buildConfigField(Byte[], "BYTE_ARRAY_NULLABLE_PROVIDER", provider { [1, null, 3] })
buildConfigField('Byte?[]', "BYTE_ARRAY_NULLABLE", [1, null, 3])
buildConfigField('Byte?[]', "BYTE_ARRAY_NULLABLE_PROVIDER", provider { [1, null, 3] })
buildConfigField('List<Byte>', "BYTE_LIST", [1, null, 3])
buildConfigField('List<Byte>', "BYTE_LIST_PROVIDER", provider { [1, null, 3] })
buildConfigField('Set<Byte>', "BYTE_SET", [1, null, 3])
Expand All @@ -60,8 +60,8 @@ buildConfig {
buildConfigField(short[], "SHORT_NATIVE_ARRAY_PROVIDER", provider { [1, 2, 3] })
buildConfigField(Short[], "SHORT_ARRAY", [1, 2, 3])
buildConfigField(Short[], "SHORT_ARRAY_PROVIDER", provider { [1, 2, 3] })
buildConfigField(Short[], "SHORT_ARRAY_NULLABLE", [1, null, 3])
buildConfigField(Short[], "SHORT_ARRAY_NULLABLE_PROVIDER", provider { [1, null, 3] })
buildConfigField('Short?[]', "SHORT_ARRAY_NULLABLE", [1, null, 3])
buildConfigField('Short?[]', "SHORT_ARRAY_NULLABLE_PROVIDER", provider { [1, null, 3] })
buildConfigField('List<Short>', "SHORT_LIST", [1, null, 3])
buildConfigField('List<Short>', "SHORT_LIST_PROVIDER", provider { [1, null, 3] })
buildConfigField('Set<Short>', "SHORT_SET", [1, null, 3])
Expand All @@ -75,8 +75,8 @@ buildConfig {
buildConfigField(char[], "CHAR_NATIVE_ARRAY_PROVIDER", provider { ['a' as char, 'b' as char, 'c' as char] })
buildConfigField(Character[], "CHAR_ARRAY", ['a' as char, 'b' as char, 'c' as char])
buildConfigField(Character[], "CHAR_ARRAY_PROVIDER", provider { ['a' as char, 'b' as char, 'c' as char] })
buildConfigField(Character[], "CHAR_ARRAY_NULLABLE", ['a' as char, null, 'c' as char])
buildConfigField(Character[], "CHAR_ARRAY_NULLABLE_PROVIDER", provider { ['a' as char, null, 'c' as char] })
buildConfigField('Character?[]', "CHAR_ARRAY_NULLABLE", ['a' as char, null, 'c' as char])
buildConfigField('Character?[]', "CHAR_ARRAY_NULLABLE_PROVIDER", provider { ['a' as char, null, 'c' as char] })
buildConfigField('List<Character>', "CHAR_LIST", ['a' as char, null, 'c' as char])
buildConfigField('List<Character>', "CHAR_LIST_PROVIDER", provider { ['a' as char, null, 'c' as char] })
buildConfigField('Set<Character>', "CHAR_SET", ['a' as char, null, 'c' as char])
Expand All @@ -90,8 +90,8 @@ buildConfig {
buildConfigField(int[], "INT_NATIVE_ARRAY_PROVIDER", provider { [1, 2, 3] })
buildConfigField(Integer[], "INT_ARRAY", [1, 2, 3])
buildConfigField(Integer[], "INT_ARRAY_PROVIDER", provider { [1, 2, 3] })
buildConfigField(Integer[], "INT_ARRAY_NULLABLE", [1, null, 3])
buildConfigField(Integer[], "INT_ARRAY_NULLABLE_PROVIDER", provider { [1, null, 3] })
buildConfigField('Integer?[]', "INT_ARRAY_NULLABLE", [1, null, 3])
buildConfigField('Integer?[]', "INT_ARRAY_NULLABLE_PROVIDER", provider { [1, null, 3] })
buildConfigField('List<Integer>', "INT_LIST", [1, null, 3])
buildConfigField('List<Integer>', "INT_LIST_PROVIDER", provider { [1, null, 3] })
buildConfigField('Set<Integer>', "INT_SET", [1, null, 3])
Expand All @@ -105,8 +105,8 @@ buildConfig {
buildConfigField(long[], "LONG_NATIVE_ARRAY_PROVIDER", provider { [1L, 2L, 3L] })
buildConfigField(Long[], "LONG_ARRAY", [1L, 2L, 3L])
buildConfigField(Long[], "LONG_ARRAY_PROVIDER", provider { [1L, 2L, 3L] })
buildConfigField(Long[], "LONG_ARRAY_NULLABLE", [1L, null, 3L])
buildConfigField(Long[], "LONG_ARRAY_NULLABLE_PROVIDER", provider { [1L, null, 3L] })
buildConfigField('Long?[]', "LONG_ARRAY_NULLABLE", [1L, null, 3L])
buildConfigField('Long?[]', "LONG_ARRAY_NULLABLE_PROVIDER", provider { [1L, null, 3L] })
buildConfigField('List<Long>', "LONG_LIST", [1L, null, 3L])
buildConfigField('List<Long>', "LONG_LIST_PROVIDER", provider { [1L, null, 3L] })
buildConfigField('Set<Long>', "LONG_SET", [1L, null, 3L])
Expand All @@ -120,8 +120,8 @@ buildConfig {
buildConfigField(float[], "FLOAT_NATIVE_ARRAY_PROVIDER", provider { [1f, 2f, 3f] })
buildConfigField(Float[], "FLOAT_ARRAY", [1f, 2f, 3f])
buildConfigField(Float[], "FLOAT_ARRAY_PROVIDER", provider { [1f, 2f, 3f] })
buildConfigField(Float[], "FLOAT_ARRAY_NULLABLE", [1f, null, 3f])
buildConfigField(Float[], "FLOAT_ARRAY_NULLABLE_PROVIDER", provider { [1f, null, 3f] })
buildConfigField('Float?[]', "FLOAT_ARRAY_NULLABLE", [1f, null, 3f])
buildConfigField('Float?[]', "FLOAT_ARRAY_NULLABLE_PROVIDER", provider { [1f, null, 3f] })
buildConfigField('List<Float>', "FLOAT_LIST", [1f, null, 3f])
buildConfigField('List<Float>', "FLOAT_LIST_PROVIDER", provider { [1f, null, 3f] })
buildConfigField('Set<Float>', "FLOAT_SET", [1f, null, 3f])
Expand All @@ -135,8 +135,8 @@ buildConfig {
buildConfigField(double[], "DOUBLE_NATIVE_ARRAY_PROVIDER", provider { [1.0, 2.0, 3.0] })
buildConfigField(Double[], "DOUBLE_ARRAY", [1.0, 2.0, 3.0])
buildConfigField(Double[], "DOUBLE_ARRAY_PROVIDER", provider { [1.0, 2.0, 3.0] })
buildConfigField(Double[], "DOUBLE_ARRAY_NULLABLE", [1.0, null, 3.0])
buildConfigField(Double[], "DOUBLE_ARRAY_NULLABLE_PROVIDER", provider { [1.0, null, 3.0] })
buildConfigField('Double?[]', "DOUBLE_ARRAY_NULLABLE", [1.0, null, 3.0])
buildConfigField('Double?[]', "DOUBLE_ARRAY_NULLABLE_PROVIDER", provider { [1.0, null, 3.0] })
buildConfigField('List<Double>', "DOUBLE_LIST", [1.0, null, 3.0])
buildConfigField('List<Double>', "DOUBLE_LIST_PROVIDER", provider { [1.0, null, 3.0] })
buildConfigField('Set<Double>', "DOUBLE_SET", [1.0, null, 3.0])
Expand All @@ -150,8 +150,8 @@ buildConfig {
buildConfigField(boolean[], "BOOLEAN_NATIVE_ARRAY_PROVIDER", provider { [true, false, false] })
buildConfigField(Boolean[], "BOOLEAN_ARRAY", [true, false, false])
buildConfigField(Boolean[], "BOOLEAN_ARRAY_PROVIDER", provider { [true, false, false] })
buildConfigField(Boolean[], "BOOLEAN_ARRAY_NULLABLE", [true, null, false])
buildConfigField(Boolean[], "BOOLEAN_ARRAY_NULLABLE_PROVIDER", provider { [true, null, false] })
buildConfigField('Boolean?[]', "BOOLEAN_ARRAY_NULLABLE", [true, null, false])
buildConfigField('Boolean?[]', "BOOLEAN_ARRAY_NULLABLE_PROVIDER", provider { [true, null, false] })
buildConfigField('List<Boolean>', "BOOLEAN_LIST", [true, null, false])
buildConfigField('List<Boolean>', "BOOLEAN_LIST_PROVIDER", provider { [true, null, false] })
buildConfigField('Set<Boolean>', "BOOLEAN_SET", [true, null, false])
Expand Down Expand Up @@ -195,19 +195,12 @@ sourceSets {
/**
* A task that iterates over your classpath resources and generate constants for them
*/
def buildResources = buildConfig.forClass("BuildResources") {
buildConfig.forClass("BuildResources") {
buildConfigField('String', 'A_CONSTANT', '"aConstant"')
}
def generateResourcesConstants = tasks.register("generateResourcesConstants") {
doFirst {
sourceSets.main.resources.asFileTree.visit { file ->
def name = file.path.toUpperCase().replaceAll("\\W", "_")

buildResources.buildConfigField('java.io.File', name, "new File(\"$file.path\")")
}
}
}
sourceSets.main.resources.asFileTree.visit {
def name = it.path.toUpperCase().replaceAll("\\W", "_")

tasks.generateBuildConfig {
dependsOn(generateResourcesConstants)
buildConfigField('java.io.File', name, "new File(\"${it.path}\")")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@

import org.junit.Test;

import java.lang.reflect.Array;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import static org.junit.Assert.*;

Expand Down Expand Up @@ -46,8 +43,8 @@ public void testBytes() {
assertEquals(64, BuildConfig.BYTE_PROVIDER);
assertArrayEquals(new byte[]{1, 2, 3}, BuildConfig.BYTE_NATIVE_ARRAY);
assertArrayEquals(new byte[]{1, 2, 3}, BuildConfig.BYTE_NATIVE_ARRAY_PROVIDER);
assertArrayEquals(new Byte[]{1, 2, 3}, BuildConfig.BYTE_ARRAY);
assertArrayEquals(new Byte[]{1, 2, 3}, BuildConfig.BYTE_ARRAY_PROVIDER);
assertArrayEquals(new byte[]{1, 2, 3}, BuildConfig.BYTE_ARRAY);
assertArrayEquals(new byte[]{1, 2, 3}, BuildConfig.BYTE_ARRAY_PROVIDER);
assertArrayEquals(new Byte[]{1, null, 3}, BuildConfig.BYTE_ARRAY_NULLABLE);
assertArrayEquals(new Byte[]{1, null, 3}, BuildConfig.BYTE_ARRAY_NULLABLE_PROVIDER);
assertEquals(Arrays.asList((byte) 1, null, (byte) 3), BuildConfig.BYTE_LIST);
Expand All @@ -63,8 +60,8 @@ public void testShorts() {
assertEquals(64, BuildConfig.SHORT_PROVIDER);
assertArrayEquals(new short[]{1, 2, 3}, BuildConfig.SHORT_NATIVE_ARRAY);
assertArrayEquals(new short[]{1, 2, 3}, BuildConfig.SHORT_NATIVE_ARRAY_PROVIDER);
assertArrayEquals(new Short[]{1, 2, 3}, BuildConfig.SHORT_ARRAY);
assertArrayEquals(new Short[]{1, 2, 3}, BuildConfig.SHORT_ARRAY_PROVIDER);
assertArrayEquals(new short[]{1, 2, 3}, BuildConfig.SHORT_ARRAY);
assertArrayEquals(new short[]{1, 2, 3}, BuildConfig.SHORT_ARRAY_PROVIDER);
assertArrayEquals(new Short[]{1, null, 3}, BuildConfig.SHORT_ARRAY_NULLABLE);
assertArrayEquals(new Short[]{1, null, 3}, BuildConfig.SHORT_ARRAY_NULLABLE_PROVIDER);
assertEquals(Arrays.asList((short) 1, null, (short) 3), BuildConfig.SHORT_LIST);
Expand All @@ -80,8 +77,8 @@ public void testChars() {
assertEquals('a', BuildConfig.CHAR_PROVIDER);
assertArrayEquals(new char[]{'a', 'b', 'c'}, BuildConfig.CHAR_NATIVE_ARRAY);
assertArrayEquals(new char[]{'a', 'b', 'c'}, BuildConfig.CHAR_NATIVE_ARRAY_PROVIDER);
assertArrayEquals(new Character[]{'a', 'b', 'c'}, BuildConfig.CHAR_ARRAY);
assertArrayEquals(new Character[]{'a', 'b', 'c'}, BuildConfig.CHAR_ARRAY_PROVIDER);
assertArrayEquals(new char[]{'a', 'b', 'c'}, BuildConfig.CHAR_ARRAY);
assertArrayEquals(new char[]{'a', 'b', 'c'}, BuildConfig.CHAR_ARRAY_PROVIDER);
assertArrayEquals(new Character[]{'a', null, 'c'}, BuildConfig.CHAR_ARRAY_NULLABLE);
assertArrayEquals(new Character[]{'a', null, 'c'}, BuildConfig.CHAR_ARRAY_NULLABLE_PROVIDER);
assertEquals(Arrays.asList('a', null, 'c'), BuildConfig.CHAR_LIST);
Expand All @@ -97,8 +94,8 @@ public void testIntegers() {
assertEquals(1, BuildConfig.INT_PROVIDER);
assertArrayEquals(new int[]{1, 2, 3}, BuildConfig.INT_NATIVE_ARRAY);
assertArrayEquals(new int[]{1, 2, 3}, BuildConfig.INT_NATIVE_ARRAY_PROVIDER);
assertArrayEquals(new Integer[]{1, 2, 3}, BuildConfig.INT_ARRAY);
assertArrayEquals(new Integer[]{1, 2, 3}, BuildConfig.INT_ARRAY_PROVIDER);
assertArrayEquals(new int[]{1, 2, 3}, BuildConfig.INT_ARRAY);
assertArrayEquals(new int[]{1, 2, 3}, BuildConfig.INT_ARRAY_PROVIDER);
assertArrayEquals(new Integer[]{1, null, 3}, BuildConfig.INT_ARRAY_NULLABLE);
assertArrayEquals(new Integer[]{1, null, 3}, BuildConfig.INT_ARRAY_NULLABLE_PROVIDER);
assertEquals(Arrays.asList(1, null, 3), BuildConfig.INT_LIST);
Expand All @@ -114,8 +111,8 @@ public void testLongs() {
assertEquals(1, BuildConfig.LONG_PROVIDER);
assertArrayEquals(new long[]{1L, 2L, 3L}, BuildConfig.LONG_NATIVE_ARRAY);
assertArrayEquals(new long[]{1L, 2L, 3L}, BuildConfig.LONG_NATIVE_ARRAY_PROVIDER);
assertArrayEquals(new Long[]{1L, 2L, 3L}, BuildConfig.LONG_ARRAY);
assertArrayEquals(new Long[]{1L, 2L, 3L}, BuildConfig.LONG_ARRAY_PROVIDER);
assertArrayEquals(new long[]{1L, 2L, 3L}, BuildConfig.LONG_ARRAY);
assertArrayEquals(new long[]{1L, 2L, 3L}, BuildConfig.LONG_ARRAY_PROVIDER);
assertArrayEquals(new Long[]{1L, null, 3L}, BuildConfig.LONG_ARRAY_NULLABLE);
assertArrayEquals(new Long[]{1L, null, 3L}, BuildConfig.LONG_ARRAY_NULLABLE_PROVIDER);
assertEquals(Arrays.asList(1L, null, 3L), BuildConfig.LONG_LIST);
Expand All @@ -131,8 +128,8 @@ public void testFloats() {
assertEquals(1, BuildConfig.FLOAT_PROVIDER, 0);
assertArrayEquals(new float[]{1f, 2f, 3f}, BuildConfig.FLOAT_NATIVE_ARRAY, 0);
assertArrayEquals(new float[]{1f, 2f, 3f}, BuildConfig.FLOAT_NATIVE_ARRAY_PROVIDER, 0);
assertArrayEquals(new Float[]{1f, 2f, 3f}, BuildConfig.FLOAT_ARRAY);
assertArrayEquals(new Float[]{1f, 2f, 3f}, BuildConfig.FLOAT_ARRAY_PROVIDER);
assertArrayEquals(new float[]{1f, 2f, 3f}, BuildConfig.FLOAT_ARRAY, 0);
assertArrayEquals(new float[]{1f, 2f, 3f}, BuildConfig.FLOAT_ARRAY_PROVIDER, 0);
assertArrayEquals(new Float[]{1f, null, 3f}, BuildConfig.FLOAT_ARRAY_NULLABLE);
assertArrayEquals(new Float[]{1f, null, 3f}, BuildConfig.FLOAT_ARRAY_NULLABLE_PROVIDER);
assertEquals(Arrays.asList(1f, null, 3f), BuildConfig.FLOAT_LIST);
Expand All @@ -148,8 +145,8 @@ public void testDoubles() {
assertEquals(1, BuildConfig.DOUBLE_PROVIDER, 0);
assertArrayEquals(new double[]{1.0, 2.0, 3.0}, BuildConfig.DOUBLE_NATIVE_ARRAY, 0);
assertArrayEquals(new double[]{1.0, 2.0, 3.0}, BuildConfig.DOUBLE_NATIVE_ARRAY_PROVIDER, 0);
assertArrayEquals(new Double[]{1.0, 2.0, 3.0}, BuildConfig.DOUBLE_ARRAY);
assertArrayEquals(new Double[]{1.0, 2.0, 3.0}, BuildConfig.DOUBLE_ARRAY_PROVIDER);
assertArrayEquals(new double[]{1.0, 2.0, 3.0}, BuildConfig.DOUBLE_ARRAY, 0);
assertArrayEquals(new double[]{1.0, 2.0, 3.0}, BuildConfig.DOUBLE_ARRAY_PROVIDER, 0);
assertArrayEquals(new Double[]{1.0, null, 3.0}, BuildConfig.DOUBLE_ARRAY_NULLABLE);
assertArrayEquals(new Double[]{1.0, null, 3.0}, BuildConfig.DOUBLE_ARRAY_NULLABLE_PROVIDER);
assertEquals(Arrays.asList(1.0, null, 3.0), BuildConfig.DOUBLE_LIST);
Expand All @@ -165,8 +162,8 @@ public void testBooleans() {
assertTrue(BuildConfig.BOOLEAN_PROVIDER);
assertArrayEquals(new boolean[]{true, false, false}, BuildConfig.BOOLEAN_NATIVE_ARRAY);
assertArrayEquals(new boolean[]{true, false, false}, BuildConfig.BOOLEAN_NATIVE_ARRAY_PROVIDER);
assertArrayEquals(new Boolean[]{true, false, false}, BuildConfig.BOOLEAN_ARRAY);
assertArrayEquals(new Boolean[]{true, false, false}, BuildConfig.BOOLEAN_ARRAY_PROVIDER);
assertArrayEquals(new boolean[]{true, false, false}, BuildConfig.BOOLEAN_ARRAY);
assertArrayEquals(new boolean[]{true, false, false}, BuildConfig.BOOLEAN_ARRAY_PROVIDER);
assertArrayEquals(new Boolean[]{true, null, false}, BuildConfig.BOOLEAN_ARRAY_NULLABLE);
assertArrayEquals(new Boolean[]{true, null, false}, BuildConfig.BOOLEAN_ARRAY_NULLABLE_PROVIDER);
assertEquals(Arrays.asList(true, null, false), BuildConfig.BOOLEAN_LIST);
Expand Down
4 changes: 4 additions & 0 deletions demo-project/kts-multiplatform/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,7 @@ buildConfig {
tasks.register("test") {
dependsOn("allTests")
}

tasks.named("compileTestDevelopmentExecutableKotlinJs") {
notCompatibleWithConfigurationCache("uses Task.project")
}
22 changes: 8 additions & 14 deletions demo-project/kts/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -203,23 +203,17 @@ val versionsSS = buildConfig.sourceSets.register("Versions") {
buildConfigField("myDependencyVersion", "1.0.1")
}

val buildResources = buildConfig.forClass("BuildResources") {
/**
* A task that iterates over your classpath resources and generate constants for them
*/
buildConfig.forClass("BuildResources") {
buildConfigField("A_CONSTANT", "aConstant")
}
val generateResourcesConstants by tasks.registering {
doFirst {
sourceSets["main"].resources.asFileTree.visit {
val name = path.uppercase().replace("\\W".toRegex(), "_")

with(buildResources) {
buildConfigField("java.io.File", name, "File(\"$path\")")
}
}
}
}
sourceSets["main"].resources.asFileTree.visit {
val name = path.uppercase().replace("\\W".toRegex(), "_")

tasks.generateBuildConfig {
dependsOn(generateResourcesConstants)
buildConfigField("java.io.File", name, "File(\"$path\")")
}
}

// example of a custom generator that builds into XML
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
org.gradle.jvmargs=-Xmx2g
org.gradle.caching=true
org.gradle.configuration-cache=true FIXME `kotlin-multiplatform` are not compatible
org.gradle.configuration-cache=true
4 changes: 4 additions & 0 deletions plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,7 @@ tasks.check {
tasks.publish {
dependsOn(tasks.publishPlugins)
}

tasks.generateJacocoTestKitProperties {
notCompatibleWithConfigurationCache("uses Task.extensions")
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ interface BuildConfigClassSpec : Named {
type: Class<out Type>,
name: String,
value: Type?,
) = addField(typeOf(type), name, valueOf(castToType(value, type) as Serializable))
) = addField(nameOf(type), name, valueOf(castToType(value, type) as Serializable))

fun buildConfigField(
type: String,
Expand All @@ -71,6 +71,6 @@ interface BuildConfigClassSpec : Named {
type: Class<out Type>,
name: String,
value: Provider<out Type>,
) = addField(typeOf(type), name, value.map { valueOf(castToType(it, type) as Serializable) })
) = addField(nameOf(type), name, value.map { valueOf(castToType(it, type) as Serializable) })

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@ package com.github.gmazzo.buildconfig
import org.gradle.api.Named
import org.gradle.api.provider.Property
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Optional

interface BuildConfigField : Named {

@Input
override fun getName(): String

@get:Input
val type: Property<BuildConfigType<*>>
val type: Property<BuildConfigType>

@get:Input
val value: Property<BuildConfigValue>

@get:Input
@get:Optional
val position: Property<Int>

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import org.gradle.api.plugins.PluginContainer
import org.gradle.api.tasks.SourceSet
import org.gradle.kotlin.dsl.add
import org.gradle.kotlin.dsl.domainObjectContainer
import org.gradle.kotlin.dsl.newInstance
import org.gradle.kotlin.dsl.register

class BuildConfigPlugin : Plugin<Project> {
Expand Down Expand Up @@ -93,13 +94,35 @@ class BuildConfigPlugin : Plugin<Project> {
group = "BuildConfig"
description = "Generates the build constants class for '${sourceSet.name}' source"

specs.add(sourceSet.classSpec)
specs.addAll(sourceSet.extraSpecs)
generator.set(sourceSet.generator)
outputDir.set(layout.buildDirectory.dir("generated/sources/buildConfig/${sourceSet.name}"))
specs.addAll(provider {
(sequenceOf(sourceSet) + sourceSet.extraSpecs)
.map { isolate(it) }
.toList()
})
}
}

/**
* Helper method to create a copy of a BuildConfigClassSpec not linked to Gradle related extensions/DSL
* It makes it compatible with Configuration Cache
*/
private fun Project.isolate(source: BuildConfigClassSpec) =
objects.newInstance<BuildConfigClassSpec>(source.name).apply {
className.set(source.className)
packageName.set(source.packageName)
documentation.set(source.documentation)
buildConfigFields.addAll(source.buildConfigFields.map { isolate(it) })
}

private fun Project.isolate(source: BuildConfigField) =
objects.newInstance<BuildConfigField>(source.name).apply {
type.set(source.type)
value.set(source.value)
position.set(source.position)
}

private val Project.defaultPackage
get() = provider {
group
Expand Down
Loading

0 comments on commit 7f16671

Please sign in to comment.