org.unbrokendome.gradle.pluginutils.test.spek.MemoizedGradleTask.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gradle-plugin-test-utils Show documentation
Show all versions of gradle-plugin-test-utils Show documentation
A collection of utilities for developing Gradle plugins
package org.unbrokendome.gradle.pluginutils.test.spek
import org.gradle.api.Action
import org.gradle.api.Project
import org.gradle.api.Task
import org.spekframework.spek2.dsl.LifecycleAware
import org.spekframework.spek2.lifecycle.MemoizedValue
import kotlin.reflect.KClass
/**
* Creates a memoized Gradle task.
*
* @param taskType the type of task to create
* @param name the task name
* @param config an optional block for configuring the task
*/
fun LifecycleAware.gradleTask(
taskType: KClass,
name: String? = null,
config: T.() -> Unit = {}
): MemoizedValue {
val project: Project by memoized()
val actualName = name ?: taskType.simpleName?.decapitalize() ?: "task"
return memoized {
project.tasks.create(actualName, taskType.java, Action(config))
}
}
/**
* Creates a memoized Gradle task.
*
* @param T the type of task to create
* @param name the task name
* @param config an optional block for configuring the task
*/
inline fun LifecycleAware.gradleTask(
name: String? = null, noinline config: T.() -> Unit = {}
) =
gradleTask(T::class, name, config)