Skip to content

Commit

Permalink
Added support for addLater
Browse files Browse the repository at this point in the history
  • Loading branch information
gmazzo committed Dec 18, 2023
1 parent 58212ca commit 947d893
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 24 deletions.
10 changes: 10 additions & 0 deletions demo-project/generic/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import com.github.gmazzo.buildconfig.BuildConfigField
import kotlin.test.assertEquals
import kotlin.test.assertTrue

Expand All @@ -21,6 +22,13 @@ buildConfig {
forClass("BuildResources") {
buildConfigField("String","A_CONSTANT", "\"aConstant\"")
}

buildConfigFields.addLater(provider {
objects.newInstance<BuildConfigField>("PROVIDED").apply {
type(String::class.java)
value("byAddLater")
}
})
}

// everything below here are just helper code to allow testing the plugin as we can't rely on any framework like JUnit
Expand All @@ -37,6 +45,8 @@ val generateBuildConfigTest = task<AssertGeneratedFile>("generateBuildConfigTest
public final class BuildConfig {
public static final String APP_NAME = "generic";
public static final String PROVIDED = "byAddLater";
public static final String APP_SECRET = "Z3JhZGxlLWphdmEtYnVpbGRjb25maWctcGx1Z2lu";
public static final long BUILD_TIME = 172800000;
Expand Down
18 changes: 14 additions & 4 deletions demo-project/groovy/build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import com.github.gmazzo.buildconfig.BuildConfigField

plugins {
id 'java'
id 'com.github.gmazzo.buildconfig'
Expand Down Expand Up @@ -198,9 +200,17 @@ sourceSets {
buildConfig.forClass("BuildResources") {
buildConfigField('String', 'A_CONSTANT', '"aConstant"')

sourceSets.main.resources.asFileTree.visit {
def name = it.path.toUpperCase().replaceAll("\\W", "_")
def files = sourceSets.main.resources.sourceDirectories.asFileTree
buildConfigFields.addAllLater(provider {
List<BuildConfigField> fields = []
files.asFileTree.visit {
def name = it.path.toUpperCase().replaceAll("\\W", "_")

buildConfigField('java.io.File', name, "new File(\"${it.path}\")")
}
fields.add(objects.newInstance(BuildConfigField, name).with { _ ->
type(File)
expression("new File(\"${it.path}\")")
})
}
return fields
})
}
17 changes: 13 additions & 4 deletions demo-project/kts/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import com.github.gmazzo.buildconfig.BuildConfigField
import com.github.gmazzo.buildconfig.BuildConfigValue
import com.github.gmazzo.buildconfig.generators.BuildConfigGenerator
import com.github.gmazzo.buildconfig.generators.BuildConfigGeneratorSpec
Expand Down Expand Up @@ -209,11 +210,19 @@ val versionsSS = buildConfig.sourceSets.register("Versions") {
buildConfig.forClass("BuildResources") {
buildConfigField("A_CONSTANT", "aConstant")

sourceSets["main"].resources.asFileTree.visit {
val name = path.uppercase().replace("\\W".toRegex(), "_")
val files = sourceSets["main"].resources.sourceDirectories.asFileTree
buildConfigFields.addAllLater(provider {
val fields = mutableListOf<BuildConfigField>()
files.visit {
val name = path.uppercase().replace("\\W".toRegex(), "_")

buildConfigField("java.io.File", name, "File(\"$path\")")
}
fields.add(objects.newInstance<BuildConfigField>( name).apply {
type(File::class.java)
expression("File(\"$path\")")
})
}
fields
})
}

// example of a custom generator that builds into XML
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,9 @@ class BuildConfigPlugin : Plugin<Project> {

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()
})

specs.add(provider { isolate(sourceSet) })
specs.addAll(provider { sourceSet.extraSpecs.map { isolate(it) } })
}
}

Expand All @@ -109,18 +107,17 @@ class BuildConfigPlugin : Plugin<Project> {
* It makes it compatible with Configuration Cache
*/
private fun Project.isolate(source: BuildConfigClassSpec) =
objects.newInstance<BuildConfigClassSpec>(source.name).apply {
objects.newInstance<BuildConfigClassSpec>(source.name).apply spec@{
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)
buildConfigFields.addAll(source.buildConfigFields.map { field ->
objects.newInstance<BuildConfigField>(field.name).apply field@{
this@field.type.set(field.type)
this@field.value.set(field.value)
this@field.position.set(field.position)
}
})
}

private val Project.defaultPackage
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.github.gmazzo.buildconfig.internal

import com.github.gmazzo.buildconfig.BuildConfigClassSpec
import groovy.lang.GroovyObjectSupport

/**
* Workaround for Groovy's `null` value issue when calling overloaded methods limitation:
Expand All @@ -12,7 +11,7 @@ import groovy.lang.GroovyObjectSupport
* [class java.lang.Class, class java.lang.String, interface org.gradle.api.provider.Provider]
* ```
*/
internal abstract class GroovyNullValueWorkaround : BuildConfigClassSpec, GroovyObjectSupport() {
internal abstract class GroovyNullValueWorkaround : BuildConfigClassSpec {

fun buildConfigField(
type: Class<*>,
Expand Down

0 comments on commit 947d893

Please sign in to comment.