mirror of
https://github.com/ethauvin/kobalt.git
synced 2025-04-26 08:27:12 -07:00
Introducing ITaskContributor.
This commit is contained in:
parent
235ff7337f
commit
b1860e64e2
10 changed files with 99 additions and 45 deletions
|
@ -89,6 +89,11 @@ public class Plugins @Inject constructor (val taskManagerProvider : Provider<Tas
|
|||
|
||||
currentClass = currentClass.superclass
|
||||
}
|
||||
|
||||
// Collect all the tasks from the task contributors
|
||||
context.pluginInfo.taskContributors.forEach {
|
||||
taskManager.dynamicTasks.addAll(it.tasksFor(context).map { TaskManager.PluginDynamicTask(plugin, it) })
|
||||
}
|
||||
}
|
||||
|
||||
// Now that we have collected all static and dynamic tasks, turn them all into plug-in tasks
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
package com.beust.kobalt.api
|
||||
|
||||
import com.beust.kobalt.Plugins
|
||||
import com.beust.kobalt.TaskResult
|
||||
import com.beust.kobalt.Variant
|
||||
import com.beust.kobalt.internal.TaskManager
|
||||
import kotlin.properties.Delegates
|
||||
|
||||
|
@ -22,25 +20,4 @@ abstract public class BasePlugin : IPlugin {
|
|||
fun addProject(project: Project, dependsOn: Array<out Project>) {
|
||||
projects.add(ProjectDescription(project, dependsOn.toList()))
|
||||
}
|
||||
|
||||
/**
|
||||
* Register dynamic tasks corresponding to the variants found in the project,e.g. assembleDevDebug,
|
||||
* assembleDevRelease, etc...
|
||||
*/
|
||||
protected fun addVariantTasks(project: Project, taskName: String,
|
||||
runBefore : List<String> = emptyList(),
|
||||
runAfter : List<String> = emptyList(),
|
||||
runTask: (Project) -> TaskResult) {
|
||||
Variant.allVariants(project).forEach { variant ->
|
||||
val variantTaskName = variant.toTask(taskName)
|
||||
taskManager.addTask(this, project, variantTaskName, variantTaskName,
|
||||
runBefore = runBefore.map { variant.toTask(it) },
|
||||
runAfter = runAfter.map { variant.toTask(it) },
|
||||
task = { p: Project ->
|
||||
context.variant = variant
|
||||
runTask(project)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
17
src/main/kotlin/com/beust/kobalt/api/ITaskContributor.kt
Normal file
17
src/main/kotlin/com/beust/kobalt/api/ITaskContributor.kt
Normal file
|
@ -0,0 +1,17 @@
|
|||
package com.beust.kobalt.api
|
||||
|
||||
import com.beust.kobalt.TaskResult
|
||||
|
||||
/**
|
||||
* Plug-ins that need to add dynamic tasks (tasks that are not methods annotated with @Task) need
|
||||
* to implement this interface.
|
||||
*/
|
||||
interface ITaskContributor : IContributor {
|
||||
fun tasksFor(context: KobaltContext) : List<DynamicTask>
|
||||
}
|
||||
|
||||
class DynamicTask(val taskName: String, val taskDescription: String = "",
|
||||
val runBefore: List<String> = listOf<String>(),
|
||||
val runAfter: List<String> = listOf<String>(),
|
||||
val alwaysRunAfter: List<String> = listOf<String>(),
|
||||
val closure: (Project) -> TaskResult)
|
34
src/main/kotlin/com/beust/kobalt/api/TaskContributor.kt
Normal file
34
src/main/kotlin/com/beust/kobalt/api/TaskContributor.kt
Normal file
|
@ -0,0 +1,34 @@
|
|||
package com.beust.kobalt.api
|
||||
|
||||
import com.beust.kobalt.TaskResult
|
||||
import com.beust.kobalt.Variant
|
||||
|
||||
/**
|
||||
* Plug-ins that are ITaskContributor can use this class to manage their collection of tasks and
|
||||
* implement the interface by delegating to an instance of this class (if injection permits).
|
||||
*/
|
||||
class TaskContributor : ITaskContributor {
|
||||
val dynamicTasks = arrayListOf<DynamicTask>()
|
||||
|
||||
/**
|
||||
* Register dynamic tasks corresponding to the variants found in the project,e.g. assembleDevDebug,
|
||||
* assembleDevRelease, etc...
|
||||
*/
|
||||
fun addVariantTasks(project: Project, context: KobaltContext, taskName: String,
|
||||
runBefore : List<String> = emptyList(),
|
||||
runAfter : List<String> = emptyList(),
|
||||
runTask: (Project) -> TaskResult) {
|
||||
Variant.allVariants(project).forEach { variant ->
|
||||
val variantTaskName = variant.toTask(taskName)
|
||||
dynamicTasks.add(DynamicTask(variantTaskName, variantTaskName,
|
||||
runBefore = runBefore.map { variant.toTask(it) },
|
||||
runAfter = runAfter.map { variant.toTask(it) },
|
||||
closure = { p: Project ->
|
||||
context.variant = variant
|
||||
runTask(project)
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
override fun tasksFor(context: KobaltContext) : List<DynamicTask> = dynamicTasks
|
||||
}
|
|
@ -25,7 +25,9 @@ abstract class JvmCompilerPlugin @Inject constructor(
|
|||
open val depFactory: DepFactory,
|
||||
open val dependencyManager: DependencyManager,
|
||||
open val executors: KobaltExecutors,
|
||||
open val jvmCompiler: JvmCompiler) : BasePlugin(), IProjectContributor {
|
||||
open val jvmCompiler: JvmCompiler,
|
||||
val taskContributor : TaskContributor = TaskContributor())
|
||||
: BasePlugin(), IProjectContributor, ITaskContributor by taskContributor {
|
||||
|
||||
companion object {
|
||||
@ExportedProjectProperty(doc = "Projects this project depends on", type = "List<ProjectDescription>")
|
||||
|
@ -52,7 +54,7 @@ abstract class JvmCompilerPlugin @Inject constructor(
|
|||
override fun apply(project: Project, context: KobaltContext) {
|
||||
super.apply(project, context)
|
||||
project.projectProperties.put(DEPENDENT_PROJECTS, projects())
|
||||
addVariantTasks(project, "compile", runTask = { taskCompile(project) })
|
||||
taskContributor.addVariantTasks(project, context, "compile", runTask = { taskCompile(project) })
|
||||
}
|
||||
|
||||
@Task(name = TASK_TEST, description = "Run the tests", runAfter = arrayOf("compile", "compileTest"))
|
||||
|
|
|
@ -72,6 +72,7 @@ class PluginInfo(val xml: KobaltPluginXml, val classLoader: ClassLoader?) {
|
|||
val docContributors = arrayListOf<IDocContributor>()
|
||||
val sourceDirContributors = arrayListOf<ISourceDirectoryContributor>()
|
||||
val buildConfigFieldContributors = arrayListOf<IBuildConfigFieldContributor>()
|
||||
val taskContributors = arrayListOf<ITaskContributor>()
|
||||
|
||||
// Future contributors:
|
||||
// source files
|
||||
|
@ -169,6 +170,7 @@ class PluginInfo(val xml: KobaltPluginXml, val classLoader: ClassLoader?) {
|
|||
docContributors.addAll(pluginInfo.docContributors)
|
||||
sourceDirContributors.addAll(pluginInfo.sourceDirContributors)
|
||||
buildConfigFieldContributors.addAll(pluginInfo.buildConfigFieldContributors)
|
||||
taskContributors.addAll(pluginInfo.taskContributors)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package com.beust.kobalt.internal
|
||||
|
||||
import com.beust.kobalt.*
|
||||
import com.beust.kobalt.api.DynamicTask
|
||||
import com.beust.kobalt.api.IPlugin
|
||||
import com.beust.kobalt.api.PluginTask
|
||||
import com.beust.kobalt.api.Project
|
||||
|
@ -220,15 +221,11 @@ public class TaskManager @Inject constructor(val args: Args) {
|
|||
//
|
||||
|
||||
class StaticTask(val plugin: IPlugin, val method: Method, val taskAnnotation: Task)
|
||||
class DynamicTask(val plugin: IPlugin, val name: String, val description: String,
|
||||
val runBefore: List<String> = listOf<String>(),
|
||||
val runAfter: List<String> = listOf<String>(),
|
||||
val alwaysRunAfter: List<String> = listOf<String>(),
|
||||
val closure: (Project) -> TaskResult)
|
||||
class PluginDynamicTask(val plugin: IPlugin, val task: DynamicTask)
|
||||
|
||||
val tasks = arrayListOf<PluginTask>()
|
||||
val staticTasks = arrayListOf<StaticTask>()
|
||||
val dynamicTasks = arrayListOf<DynamicTask>()
|
||||
val dynamicTasks = arrayListOf<PluginDynamicTask>()
|
||||
|
||||
/**
|
||||
* Turn all the static and dynamic tasks into plug-in tasks, which are then suitable to be executed.
|
||||
|
@ -239,9 +236,10 @@ public class TaskManager @Inject constructor(val args: Args) {
|
|||
}
|
||||
|
||||
private fun addDynamicTasks(projects: List<Project>) {
|
||||
dynamicTasks.forEach { task ->
|
||||
projects.filter { task.plugin.accept(it) }.forEach { project ->
|
||||
addTask(task.plugin, project, task.name, task.description, task.runBefore, task.runAfter,
|
||||
dynamicTasks.forEach { dynamicTask ->
|
||||
val task = dynamicTask.task
|
||||
projects.filter { dynamicTask.plugin.accept(it) }.forEach { project ->
|
||||
addTask(dynamicTask.plugin, project, task.taskName, task.taskDescription, task.runBefore, task.runAfter,
|
||||
task.alwaysRunAfter, task.closure)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,7 +23,7 @@ public class AndroidPlugin @Inject constructor(val javaCompiler: JavaCompiler, v
|
|||
val executors: KobaltExecutors)
|
||||
: ConfigPlugin<AndroidConfig>(), IClasspathContributor, IRepoContributor, ICompilerFlagContributor,
|
||||
ICompilerInterceptor, IBuildDirectoryIncerceptor, IRunnerContributor, IClasspathInterceptor,
|
||||
ISourceDirectoryContributor, IBuildConfigFieldContributor {
|
||||
ISourceDirectoryContributor, IBuildConfigFieldContributor, ITaskContributor {
|
||||
companion object {
|
||||
const val PLUGIN_NAME = "Android"
|
||||
const val TASK_GENERATE_DEX = "generateDex"
|
||||
|
@ -35,20 +35,25 @@ public class AndroidPlugin @Inject constructor(val javaCompiler: JavaCompiler, v
|
|||
|
||||
fun isAndroid(project: Project) = configurationFor(project) != null
|
||||
|
||||
val taskContributor : TaskContributor = TaskContributor()
|
||||
|
||||
override fun apply(project: Project, context: KobaltContext) {
|
||||
super.apply(project, context)
|
||||
if (accept(project)) {
|
||||
project.compileDependencies.add(FileDependency(androidJar(project).toString()))
|
||||
|
||||
addVariantTasks(project, "generateR", runBefore = listOf("compile"),
|
||||
taskContributor.addVariantTasks(project, context, "generateR", runBefore = listOf("compile"),
|
||||
runTask = { taskGenerateRFile(project) })
|
||||
addVariantTasks(project, "generateDex", runAfter = listOf("compile"), runBefore = listOf("assemble"),
|
||||
taskContributor.addVariantTasks(project, context, "generateDex", runAfter = listOf("compile"),
|
||||
runBefore = listOf("assemble"),
|
||||
runTask = { taskGenerateDex(project) })
|
||||
addVariantTasks(project, "signApk", runAfter = listOf("generateDex"), runBefore = listOf("assemble"),
|
||||
taskContributor.addVariantTasks(project, context, "signApk", runAfter = listOf("generateDex"),
|
||||
runBefore = listOf("assemble"),
|
||||
runTask = { taskSignApk(project) })
|
||||
addVariantTasks(project, "install", runAfter = listOf("signApk"),
|
||||
taskContributor.addVariantTasks(project, context, "install", runAfter = listOf("signApk"),
|
||||
runTask = { taskInstall(project) })
|
||||
addVariantTasks(project, "proguard", runBefore = listOf("install"), runAfter = listOf("compile"),
|
||||
taskContributor.addVariantTasks(project, context, "proguard", runBefore = listOf("install"),
|
||||
runAfter = listOf("compile"),
|
||||
runTask = { taskProguard(project) })
|
||||
}
|
||||
context.pluginInfo.classpathContributors.add(this)
|
||||
|
@ -462,7 +467,8 @@ public class AndroidPlugin @Inject constructor(val javaCompiler: JavaCompiler, v
|
|||
return result
|
||||
}
|
||||
|
||||
|
||||
//ITaskContributor
|
||||
override fun tasksFor(context: KobaltContext): List<DynamicTask> = taskContributor.dynamicTasks
|
||||
}
|
||||
|
||||
class AndroidConfig(val project: Project,
|
||||
|
|
|
@ -34,7 +34,8 @@ fun Project.application(init: ApplicationConfig.() -> Unit) {
|
|||
|
||||
@Singleton
|
||||
class ApplicationPlugin @Inject constructor(val executors: KobaltExecutors,
|
||||
val dependencyManager: DependencyManager) : ConfigPlugin<ApplicationConfig>(), IRunnerContributor {
|
||||
val dependencyManager: DependencyManager)
|
||||
: ConfigPlugin<ApplicationConfig>(), IRunnerContributor, ITaskContributor {
|
||||
|
||||
companion object {
|
||||
const val PLUGIN_NAME = "Application"
|
||||
|
@ -42,9 +43,12 @@ class ApplicationPlugin @Inject constructor(val executors: KobaltExecutors,
|
|||
|
||||
override val name = PLUGIN_NAME
|
||||
|
||||
val taskContributor : TaskContributor = TaskContributor()
|
||||
|
||||
override fun apply(project: Project, context: KobaltContext) {
|
||||
super.apply(project, context)
|
||||
addVariantTasks(project, "run", runAfter = listOf("install"), runTask = { taskRun(project) })
|
||||
taskContributor.addVariantTasks(project, context, "run", runAfter = listOf("install"),
|
||||
runTask = { taskRun(project) })
|
||||
}
|
||||
|
||||
@Task(name = "run", description = "Run the main class", runAfter = arrayOf("install"))
|
||||
|
@ -121,5 +125,7 @@ class ApplicationPlugin @Inject constructor(val executors: KobaltExecutors,
|
|||
return TaskResult(exitCode == 0)
|
||||
}
|
||||
|
||||
//ITaskContributor
|
||||
override fun tasksFor(context: KobaltContext): List<DynamicTask> = taskContributor.dynamicTasks
|
||||
}
|
||||
|
||||
|
|
|
@ -28,7 +28,8 @@ import javax.inject.Singleton
|
|||
|
||||
@Singleton
|
||||
class PackagingPlugin @Inject constructor(val dependencyManager : DependencyManager,
|
||||
val executors: KobaltExecutors, val localRepo: LocalRepo) : ConfigPlugin<InstallConfig>() {
|
||||
val executors: KobaltExecutors, val localRepo: LocalRepo)
|
||||
: ConfigPlugin<InstallConfig>(), ITaskContributor {
|
||||
|
||||
companion object {
|
||||
const val PLUGIN_NAME = "Packaging"
|
||||
|
@ -51,10 +52,13 @@ class PackagingPlugin @Inject constructor(val dependencyManager : DependencyMana
|
|||
|
||||
private val packages = arrayListOf<PackageConfig>()
|
||||
|
||||
val taskContributor : TaskContributor = TaskContributor()
|
||||
|
||||
override fun apply(project: Project, context: KobaltContext) {
|
||||
super.apply(project, context)
|
||||
project.projectProperties.put(LIBS_DIR, libsDir(project))
|
||||
addVariantTasks(project, "assemble", runAfter = listOf("compile"), runTask = { taskAssemble(project) })
|
||||
taskContributor.addVariantTasks(project, context, "assemble", runAfter = listOf("compile"),
|
||||
runTask = { taskAssemble(project) })
|
||||
}
|
||||
|
||||
private fun libsDir(project: Project) = KFiles.makeDir(buildDir(project).path, "libs").path
|
||||
|
@ -269,6 +273,9 @@ class PackagingPlugin @Inject constructor(val dependencyManager : DependencyMana
|
|||
|
||||
return TaskResult()
|
||||
}
|
||||
|
||||
//ITaskContributor
|
||||
override fun tasksFor(context: KobaltContext): List<DynamicTask> = taskContributor.dynamicTasks
|
||||
}
|
||||
|
||||
@Directive
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue