|
| 1 | +package org.gradle.kotlin.dsl.com.github.gmazzo.buildconfig.internal.bindings |
| 2 | + |
| 3 | +import com.github.gmazzo.buildconfig.BuildConfigExtension |
| 4 | +import com.github.gmazzo.buildconfig.BuildConfigTask |
| 5 | +import com.github.gmazzo.buildconfig.generators.BuildConfigJavaGenerator |
| 6 | +import com.github.gmazzo.buildconfig.generators.BuildConfigKotlinGenerator |
| 7 | +import com.github.gmazzo.buildconfig.internal.bindings.JavaBinder.registerExtension |
| 8 | +import org.gradle.api.Action |
| 9 | +import org.gradle.api.Named |
| 10 | +import org.gradle.api.NamedDomainObjectContainer |
| 11 | +import org.gradle.api.Project |
| 12 | +import org.gradle.api.Task |
| 13 | +import org.gradle.api.file.Directory |
| 14 | +import org.gradle.api.file.DirectoryProperty |
| 15 | +import org.gradle.api.plugins.ExtensionAware |
| 16 | +import org.gradle.api.provider.Provider |
| 17 | +import org.gradle.api.tasks.SourceSet.MAIN_SOURCE_SET_NAME |
| 18 | +import org.gradle.api.tasks.SourceSet.TEST_SOURCE_SET_NAME |
| 19 | +import org.gradle.api.tasks.TaskProvider |
| 20 | + |
| 21 | +internal object AndroidBinder { |
| 22 | + |
| 23 | + fun Project.configure(extension: BuildConfigExtension) { |
| 24 | + androidSourceSets.all { |
| 25 | + val spec = extension.sourceSets.maybeCreate(it.name) |
| 26 | + |
| 27 | + (it as ExtensionAware).registerExtension(spec) |
| 28 | + it.javaSrcDir(spec.generateTask.flatMap { it.outputDir }) |
| 29 | + } |
| 30 | + |
| 31 | + androidComponentsOnVariants { variant -> |
| 32 | + val unitTest = variant.unitTest |
| 33 | + val androidTest = variant.androidTest |
| 34 | + |
| 35 | + for (component in listOfNotNull(variant, unitTest, androidTest)) { |
| 36 | + val specNames = when(component) { |
| 37 | + variant -> sequenceOf(MAIN_SOURCE_SET_NAME, component.name, component.buildType, component.flavorName) + |
| 38 | + component.productFlavors.asSequence().map { (_, flavor) -> flavor } |
| 39 | + unitTest -> sequenceOf(TEST_SOURCE_SET_NAME, component.name) |
| 40 | + androidTest -> sequenceOf("androidTest", component.name) |
| 41 | + else -> error("Unsupported component $component") |
| 42 | + } |
| 43 | + val specs = specNames |
| 44 | + .filterNotNull() |
| 45 | + .filter(String::isNotBlank) |
| 46 | + .map(extension.sourceSets::maybeCreate) |
| 47 | + |
| 48 | + for (spec in specs) { |
| 49 | + component.sourcesJavaAddGeneratedSourceDirectory(spec.generateTask, BuildConfigTask::outputDir) |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + extension.generator.convention(project.provider { |
| 55 | + if (plugins.hasPlugin("org.jetbrains.kotlin.android")) BuildConfigKotlinGenerator() |
| 56 | + else BuildConfigJavaGenerator() |
| 57 | + }) |
| 58 | + } |
| 59 | + |
| 60 | + // project.androidComponents.onVariants |
| 61 | + private fun Project.androidComponentsOnVariants(onVariant: Action<Any>) = |
| 62 | + with(extensions.getByName("androidComponents")) { |
| 63 | + val selectorsAll = with(javaClass.getMethod("selector").invoke(this)) { |
| 64 | + javaClass.getMethod("all").invoke(this) |
| 65 | + } |
| 66 | + val selectorInterface = selectorsAll.javaClass.superclass.interfaces[0] |
| 67 | + |
| 68 | + @Suppress("UNCHECKED_CAST") |
| 69 | + javaClass.getMethod("onVariants", selectorInterface, Action::class.java) |
| 70 | + .invoke(this, selectorsAll, onVariant) |
| 71 | + } |
| 72 | + |
| 73 | + // Variant.unitTest |
| 74 | + private val Any.unitTest |
| 75 | + get() = try { |
| 76 | + javaClass.getMethod("getUnitTest").invoke(this) |
| 77 | + |
| 78 | + } catch (_: NoSuchMethodException) { |
| 79 | + null |
| 80 | + } |
| 81 | + |
| 82 | + // Variant.androidTest |
| 83 | + private val Any.androidTest |
| 84 | + get() = try { |
| 85 | + javaClass.getMethod("getAndroidTest").invoke(this) |
| 86 | + |
| 87 | + } catch (_: NoSuchMethodException) { |
| 88 | + null |
| 89 | + } |
| 90 | + |
| 91 | + // Component.name |
| 92 | + private val Any.name |
| 93 | + get() = javaClass.getMethod("getName").invoke(this) as String? |
| 94 | + |
| 95 | + // Component.buildType |
| 96 | + private val Any.buildType |
| 97 | + get() = javaClass.getMethod("getBuildType").invoke(this) as String? |
| 98 | + |
| 99 | + // Component.flavorName |
| 100 | + private val Any.flavorName |
| 101 | + get() = javaClass.getMethod("getFlavorName").invoke(this) as String? |
| 102 | + |
| 103 | + // Component.productFlavors |
| 104 | + @Suppress("UNCHECKED_CAST") |
| 105 | + private val Any.productFlavors |
| 106 | + get() = javaClass.getMethod("getProductFlavors").invoke(this) as List<Pair<String, String>> |
| 107 | + |
| 108 | + // Component.sources.java!!.addGeneratedSourceDirectory |
| 109 | + private fun <Type : Task> Any.sourcesJavaAddGeneratedSourceDirectory( |
| 110 | + task: TaskProvider<out Type>, |
| 111 | + wiredWith: (Type) -> DirectoryProperty, |
| 112 | + ) = with(javaClass.getMethod("getSources").invoke(this)) { |
| 113 | + with(javaClass.getMethod("getJava").invoke(this)) { |
| 114 | + javaClass.getMethod("addGeneratedSourceDirectory", TaskProvider::class.java, Function1::class.java) |
| 115 | + .invoke(this, task, wiredWith) |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + // project.android.sourceSets |
| 120 | + private val Project.androidSourceSets |
| 121 | + get() = with(extensions.getByName("android")) { |
| 122 | + @Suppress("UNCHECKED_CAST") |
| 123 | + javaClass.getMethod("getSourceSets") |
| 124 | + .invoke(this) as NamedDomainObjectContainer<Named> |
| 125 | + } |
| 126 | + |
| 127 | + // AndroidSourceSet.java.srcDir(dir) |
| 128 | + private fun Named.javaSrcDir(dir: Provider<Directory>) { |
| 129 | + with(javaClass.getMethod("getJava").invoke(this)) { |
| 130 | + javaClass.getMethod("srcDir", Any::class.java) |
| 131 | + .invoke(this, dir) |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | +} |
0 commit comments