|
1 | | -plugins { id("gg.grounds.kotlin-conventions") } |
| 1 | +import com.github.gmazzo.buildconfig.BuildConfigExtension |
| 2 | +import groovy.json.JsonSlurper |
| 3 | +import org.gradle.language.jvm.tasks.ProcessResources |
| 4 | + |
| 5 | +plugins { |
| 6 | + id("com.github.gmazzo.buildconfig") |
| 7 | + id("gg.grounds.kotlin-conventions") |
| 8 | +} |
2 | 9 |
|
3 | 10 | dependencies { testImplementation(kotlin("test")) } |
| 11 | + |
| 12 | +val runtimeCatalogFile = |
| 13 | + rootProject.layout.projectDirectory.file("runtime-catalog/grounds-runtime-libraries.json") |
| 14 | + |
| 15 | +data class RuntimeCatalogLibrary(val group: String, val name: String, val version: String) |
| 16 | + |
| 17 | +val RuntimeCatalogLibrary.coordinate: String |
| 18 | + get() = "$group:$name:$version" |
| 19 | + |
| 20 | +fun runtimeCatalogLibraries(catalogContent: String): List<RuntimeCatalogLibrary> { |
| 21 | + val catalog = |
| 22 | + (JsonSlurper().parseText(catalogContent) as? Map<*, *>) |
| 23 | + ?: error("Runtime catalog must be a JSON object") |
| 24 | + val libraries = |
| 25 | + (catalog["libraries"] as? List<*>) |
| 26 | + ?: error("Runtime catalog field libraries must be an array") |
| 27 | + |
| 28 | + return libraries.mapIndexed { index, value -> |
| 29 | + val library = |
| 30 | + (value as? Map<*, *>) |
| 31 | + ?: error("Runtime catalog field libraries[$index] must be an object") |
| 32 | + RuntimeCatalogLibrary( |
| 33 | + group = library.requiredString(index, "group"), |
| 34 | + name = library.requiredString(index, "name"), |
| 35 | + version = library.requiredString(index, "version"), |
| 36 | + ) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +fun Map<*, *>.requiredString(index: Int, field: String): String { |
| 41 | + val value = |
| 42 | + this[field] as? String |
| 43 | + ?: error("Runtime catalog field libraries[$index].$field must be a string") |
| 44 | + require(value.isNotBlank()) { |
| 45 | + "Runtime catalog field libraries[$index].$field must not be blank" |
| 46 | + } |
| 47 | + return value |
| 48 | +} |
| 49 | + |
| 50 | +val runtimeLibraryCoordinates = |
| 51 | + providers.fileContents(runtimeCatalogFile).asText.map { catalogContent -> |
| 52 | + ArrayList(runtimeCatalogLibraries(catalogContent).map { it.coordinate }) |
| 53 | + } |
| 54 | + |
| 55 | +configure<BuildConfigExtension> { |
| 56 | + className("RuntimeLibraryCatalog") |
| 57 | + packageName("gg.grounds.runtime") |
| 58 | + useKotlinOutput() |
| 59 | + buildConfigField("List<String>", "providedCoordinates", runtimeLibraryCoordinates) |
| 60 | +} |
| 61 | + |
| 62 | +tasks.named<ProcessResources>("processResources") { from(runtimeCatalogFile) { into("") } } |
0 commit comments