All Downloads are FREE. Search and download functionalities are using the official Maven repository.

name.remal.gradle_plugins.dsl.extensions.org.gradle.api.tasks.SourceSet.kt Maven / Gradle / Ivy

There is a newer version: 1.9.2
Show newest version
package name.remal.gradle_plugins.dsl.extensions

import name.remal.Services.loadServicesList
import name.remal.buildSet
import name.remal.gradle_plugins.api.AutoService
import name.remal.isStatic
import org.gradle.api.tasks.SourceSet
import java.lang.reflect.Method

@Suppress("NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS")
fun SourceSet.getNounName(noun: String): String = this.getTaskName(null, noun)

@Suppress("NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS")
fun SourceSet.getVerbName(verb: String): String = this.getTaskName(verb, null)


private val sourceSetConfigurationNameGettersFactories = loadServicesList(SourceSetConfigurationNameGettersFactory::class.java)
val SourceSet.sourceSetConfigurationNames: Set
    get() = buildSet {
        sourceSetConfigurationNameGettersFactories.forEach {
            it.getters.forEach { getter ->
                getter(this@sourceSetConfigurationNames)?.let { add(it) }
            }
        }
    }

fun SourceSet.forEachCorrespondingConfigurationName(other: SourceSet, action: (thisConfigurationName: String, otherConfigurationName: String) -> Unit) {
    val processedNames = hashSetOf()
    sourceSetConfigurationNameGettersFactories.forEach {
        it.getters.forEach forEachGetter@{ getter ->
            val thisConfigurationName = getter(this) ?: return@forEachGetter
            if (processedNames.add(thisConfigurationName)) {
                val otherConfigurationName = getter(other) ?: return@forEachGetter
                action(thisConfigurationName, otherConfigurationName)
            }
        }
    }
}


private val sourceSetTaskNameGettersFactories = loadServicesList(SourceSetTaskNameGettersFactory::class.java)
val SourceSet.sourceSetTaskNames: Set
    get() = buildSet {
        sourceSetTaskNameGettersFactories.forEach {
            it.getters.forEach { getter ->
                getter(this@sourceSetTaskNames)?.let { add(it) }
            }
        }
    }


interface SourceSetConfigurationNameGettersFactory {
    val getters: List String?>
}

@AutoService
class DefaultSourceSetConfigurationNameGettersFactory : SourceSetConfigurationNameGettersFactory {
    companion object {
        private val staticGetters = SourceSet::class.java.methods.asSequence()
            .filter { !it.isStatic && String::class.java == it.returnType && 0 == it.parameterCount }
            .filter { it.name.startsWith("get") && it.name.endsWith("ConfigurationName") }
            .sortedBy(Method::getName)
            .map { it.apply { isAccessible = true } }
            .map { val getter: SourceSet.() -> String? = { it.invoke(this)?.toString() }; getter }
            .toList()
    }

    override val getters = staticGetters
}


interface SourceSetTaskNameGettersFactory {
    val getters: List String?>
}

@AutoService
class DefaultSourceSetTaskNameGettersFactory : SourceSetTaskNameGettersFactory {
    companion object {
        private val staticGetters = SourceSet::class.java.methods.asSequence()
            .filter { !it.isStatic && String::class.java == it.returnType && 0 == it.parameterCount }
            .filter { it.name.startsWith("get") && it.name.endsWith("TaskName") }
            .sortedBy(Method::getName)
            .map { it.apply { isAccessible = true } }
            .map { val getter: SourceSet.() -> String? = { it.invoke(this)?.toString() }; getter }
            .toList()
    }

    override val getters = staticGetters
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy