Skip to content

Commit

Permalink
Added support native for File and URI types
Browse files Browse the repository at this point in the history
  • Loading branch information
gmazzo committed Dec 28, 2023
1 parent 3ca4406 commit 0212c2b
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 3 deletions.
4 changes: 4 additions & 0 deletions demo-project/groovy-gen-kotlin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ buildConfig {
)
buildConfigField(Map.class, "MAP_GENERIC", expression("mapOf(\"a\" to 1, \"b\" to 2)"))
buildConfigField(Map.class, "MAP_GENERIC_PROVIDER", provider { expression("mapOf(\"a\" to 1, \"b\" to 2)") })
buildConfigField(File.class, "FILE", new File("aFile"))
buildConfigField(File.class, "FILE_PROVIDER", provider { new File("aFile") })
buildConfigField(URI.class, "URI", uri("https://example.io"))
buildConfigField(URI.class, "URI_PROVIDER", provider { uri("https://example.io") })
buildConfigField(
"com.github.gmazzo.buildconfig.demos.groovy.SomeData",
"DATA",
Expand Down
4 changes: 4 additions & 0 deletions demo-project/groovy/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ buildConfig {
)
buildConfigField(Map.class, "MAP_GENERIC", expression("java.util.Map.of(\"a\", 1, \"b\", 2)"))
buildConfigField(Map.class, "MAP_GENERIC_PROVIDER", provider { expression("java.util.Map.of(\"a\", 1, \"b\", 2)") })
buildConfigField(File.class, "FILE", new File("aFile"))
buildConfigField(File.class, "FILE_PROVIDER", provider { new File("aFile") })
buildConfigField(URI.class, "URI", uri("https://example.io"))
buildConfigField(URI.class, "URI_PROVIDER", provider { uri("https://example.io") })
buildConfigField(
"com.github.gmazzo.buildconfig.demos.groovy.SomeData",
"DATA",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import org.junit.Test;

import java.io.File;
import java.net.URI;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
Expand Down Expand Up @@ -178,6 +180,10 @@ public void testCustomTypes() {
assertEquals(Map.of("a", 1, "b", 2), BuildConfig.MAP_PROVIDER);
assertEquals(Map.of("a", 1, "b", 2), BuildConfig.MAP_GENERIC);
assertEquals(Map.of("a", 1, "b", 2), BuildConfig.MAP_GENERIC_PROVIDER);
assertEquals(new File("aFile"), BuildConfig.FILE);
assertEquals(new File("aFile"), BuildConfig.FILE_PROVIDER);
assertEquals(URI.create("https://example.io"), BuildConfig.URI);
assertEquals(URI.create("https://example.io"), BuildConfig.URI_PROVIDER);
assertEquals(new SomeData("a", 1), BuildConfig.DATA);
assertEquals(new SomeData("a", 1), BuildConfig.DATA_PROVIDER);
}
Expand Down
4 changes: 4 additions & 0 deletions demo-project/kts-gen-java/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ buildConfig {
buildConfigField<Map<String, Int>>("MAP_PROVIDER", provider { expression("java.util.Map.of(\"a\", 1, \"b\", 2)") })
buildConfigField<Map<*, *>>("MAP_GENERIC", expression("java.util.Map.of(\"a\", 1, \"b\", 2)"))
buildConfigField<Map<*, *>>("MAP_GENERIC_PROVIDER", provider { expression("java.util.Map.of(\"a\", 1, \"b\", 2)") })
buildConfigField("FILE", File("aFile"))
buildConfigField("FILE_PROVIDER", provider { File("aFile") })
buildConfigField("URI", uri("https://example.io"))
buildConfigField("URI_PROVIDER", provider { uri("https://example.io") })
buildConfigField(
"com.github.gmazzo.buildconfig.demos.kts.SomeData",
"DATA",
Expand Down
4 changes: 4 additions & 0 deletions demo-project/kts/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ buildConfig {
buildConfigField<Map<String, Int>>("MAP_PROVIDER", provider { expression("mapOf(\"a\" to 1, \"b\" to 2)") })
buildConfigField<Map<*, *>>("MAP_GENERIC", expression("mapOf(\"a\" to 1, \"b\" to 2)"))
buildConfigField<Map<*, *>>("MAP_GENERIC_PROVIDER", provider { expression("mapOf(\"a\" to 1, \"b\" to 2)") })
buildConfigField("FILE", File("aFile"))
buildConfigField("FILE_PROVIDER", provider { File("aFile") })
buildConfigField("URI", uri("https://example.io"))
buildConfigField("URI_PROVIDER", provider { uri("https://example.io") })
buildConfigField(
"com.github.gmazzo.buildconfig.demos.kts.SomeData",
"DATA",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import com.squareup.javapoet.TypeSpec
import com.squareup.javapoet.WildcardTypeName
import org.gradle.api.logging.Logging
import org.gradle.api.tasks.Input
import java.io.File
import java.net.URI
import javax.lang.model.element.Modifier

data class BuildConfigJavaGenerator(
Expand Down Expand Up @@ -122,6 +124,8 @@ data class BuildConfigJavaGenerator(
TypeName.LONG -> "\$LL"
TypeName.FLOAT -> "\$Lf"
STRING -> "\$S"
FILE -> "new java.io.File(\$S)"
URI -> "java.net.URI.create(\$S)"
else -> "\$L"
}

Expand Down Expand Up @@ -165,6 +169,8 @@ data class BuildConfigJavaGenerator(
private val STRING = ClassName.get(String::class.java)
private val LIST = ClassName.get(List::class.java)
private val SET = ClassName.get(Set::class.java)
private val FILE = ClassName.get(File::class.java)
private val URI = ClassName.get(URI::class.java)
private val GENERIC_LIST = ClassName.get("", "List")
private val GENERIC_SET = ClassName.get("", "Set")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@ import com.squareup.kotlinpoet.STAR
import com.squareup.kotlinpoet.STRING
import com.squareup.kotlinpoet.TypeName
import com.squareup.kotlinpoet.TypeSpec
import com.squareup.kotlinpoet.asClassName
import com.squareup.kotlinpoet.asTypeName
import org.gradle.api.logging.Logging
import org.gradle.api.tasks.Input
import java.io.File
import java.net.URI

data class BuildConfigKotlinGenerator(
@get:Input var topLevelConstants: Boolean = false,
Expand Down Expand Up @@ -141,6 +144,8 @@ data class BuildConfigKotlinGenerator(
LONG -> "%LL"
FLOAT -> "%Lf"
STRING -> "%S"
FILE -> "java.io.File(%S)"
URI -> "java.net.URI.create(%S)"
else -> "%L"
}

Expand Down Expand Up @@ -198,7 +203,9 @@ data class BuildConfigKotlinGenerator(
companion object {
private val CONST_TYPES = setOf(STRING, BOOLEAN, BYTE, SHORT, INT, LONG, CHAR, FLOAT, DOUBLE)
private val GENERIC_LIST = ClassName("", "List")
private val GENERIC_SET = ClassName("", "SET")
private val GENERIC_SET = ClassName("", "Set")
private val FILE = File::class.asClassName()
private val URI = URI::class.asClassName()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ inline fun <reified Type : Serializable?> BuildConfigClassSpec.buildConfigField(
value: List<Type>,
) = buildConfigField(name, Action {
it.type(typeOf<List<Type>>())
it.value(if (value is Serializable) value else ArrayList(value))
it.value(ArrayList(value))
})

@BuildConfigDsl
Expand All @@ -84,7 +84,7 @@ inline fun <reified Type : Serializable?> BuildConfigClassSpec.buildConfigField(
value: Set<Type>,
) = buildConfigField(name, Action {
it.type(typeOf<Set<Type>>())
it.value(if (value is Serializable) value else LinkedHashSet(value))
it.value(LinkedHashSet(value))
})

@BuildConfigDsl
Expand Down

0 comments on commit 0212c2b

Please sign in to comment.