main.name.remal.gradle_plugins.dsl.extensions.groovy.lang.Closure.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gradle-plugins-kotlin-dsl Show documentation
Show all versions of gradle-plugins-kotlin-dsl Show documentation
Remal Gradle plugins: gradle-plugins-kotlin-dsl
The newest version!
package name.remal.gradle_plugins.dsl.extensions
import groovy.lang.Closure
import org.gradle.api.Action
import org.gradle.api.specs.Spec
import org.gradle.api.specs.Specs.convertClosureToSpec
import java.lang.reflect.InvocationTargetException
fun Closure<*>.toRunnable(): Runnable = Runnable { this.call() }
fun Closure<*>.toSpec(): Spec = convertClosureToSpec(this)
fun Closure<*>.toConfigureAction(): Action = Action { configure(this, it) }
fun Closure<*>.toConfigureKotlinFunction() = { arg: T -> configure(this, arg); Unit }
private val configureUtilClass: Class<*> = sequenceOf(
"org.gradle.util.internal.ConfigureUtil",
"org.gradle.util.ConfigureUtil",
)
.map { className ->
try {
Class.forName(className)
} catch (expected: ClassNotFoundException) {
null
}
}
.filterNotNull()
.first()
private val configureMethod = configureUtilClass.getMethod("configure", Closure::class.java, Any::class.java)
private fun configure(closure: Closure<*>?, obj: Any?) {
try {
configureMethod.invoke(null, closure, obj)
} catch (e: InvocationTargetException) {
throw e.targetException
} catch (e: Throwable) {
throw e
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy