mirror of
https://github.com/ethauvin/kobalt.git
synced 2025-04-27 00:38:11 -07:00
addTask/addStaticTask.
This commit is contained in:
parent
0afe72fdfa
commit
2d23d082f2
6 changed files with 77 additions and 46 deletions
|
@ -3,11 +3,11 @@ package com.beust.kobalt
|
||||||
import com.beust.kobalt.api.Plugin
|
import com.beust.kobalt.api.Plugin
|
||||||
import com.beust.kobalt.api.PluginTask
|
import com.beust.kobalt.api.PluginTask
|
||||||
import com.beust.kobalt.api.Project
|
import com.beust.kobalt.api.Project
|
||||||
|
import com.beust.kobalt.internal.TaskResult
|
||||||
|
import com.beust.kobalt.internal.TaskResult2
|
||||||
|
|
||||||
public abstract class BasePluginTask(override val plugin: Plugin,
|
public abstract class BasePluginTask(override val plugin: Plugin,
|
||||||
override val name: String,
|
override val name: String,
|
||||||
override val doc: String,
|
override val doc: String,
|
||||||
override val project: Project)
|
override val project: Project)
|
||||||
: PluginTask {
|
: PluginTask()
|
||||||
override val dependsOn = arrayListOf<String>()
|
|
||||||
}
|
|
||||||
|
|
|
@ -105,7 +105,13 @@ public class Plugins @Inject constructor (val taskManagerProvider : Provider<Tas
|
||||||
plugin.taskManager = taskManagerProvider.get()
|
plugin.taskManager = taskManagerProvider.get()
|
||||||
plugin.plugins = this
|
plugin.plugins = this
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Call apply() on each plug-in that accepts a project
|
||||||
log(2, "Applying plug-in \"${plugin.name}\"")
|
log(2, "Applying plug-in \"${plugin.name}\"")
|
||||||
|
projects.filter { plugin.accept(it) }.forEach { project ->
|
||||||
|
plugin.apply(project, context)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
var currentClass : Class<in Any> = plugin.javaClass
|
var currentClass : Class<in Any> = plugin.javaClass
|
||||||
|
|
||||||
|
@ -147,11 +153,9 @@ public class Plugins @Inject constructor (val taskManagerProvider : Provider<Tas
|
||||||
}
|
}
|
||||||
|
|
||||||
projects.filter { plugin.accept(it) }.forEach { project ->
|
projects.filter { plugin.accept(it) }.forEach { project ->
|
||||||
plugin.addTask(annotation, project, toTask(method, project, plugin))
|
plugin.addStaticTask(annotation, project, toTask(method, project, plugin))
|
||||||
annotation.runBefore.forEach { plugin.dependsOn(it, annotation.name) }
|
annotation.runBefore.forEach { plugin.dependsOn(it, annotation.name) }
|
||||||
annotation.runAfter.forEach { plugin.dependsOn(annotation.name, it) }
|
annotation.runAfter.forEach { plugin.dependsOn(annotation.name, it) }
|
||||||
|
|
||||||
plugin.apply(project, context)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import com.beust.kobalt.BasePluginTask
|
||||||
import com.beust.kobalt.Plugins
|
import com.beust.kobalt.Plugins
|
||||||
import com.beust.kobalt.internal.TaskManager
|
import com.beust.kobalt.internal.TaskManager
|
||||||
import com.beust.kobalt.internal.TaskResult
|
import com.beust.kobalt.internal.TaskResult
|
||||||
|
import com.beust.kobalt.internal.TaskResult2
|
||||||
import java.util.ArrayList
|
import java.util.ArrayList
|
||||||
import java.util.concurrent.Callable
|
import java.util.concurrent.Callable
|
||||||
import kotlin.properties.Delegates
|
import kotlin.properties.Delegates
|
||||||
|
@ -12,9 +13,22 @@ abstract public class BasePlugin : Plugin {
|
||||||
override val tasks: ArrayList<PluginTask> = arrayListOf()
|
override val tasks: ArrayList<PluginTask> = arrayListOf()
|
||||||
override var taskManager : TaskManager by Delegates.notNull()
|
override var taskManager : TaskManager by Delegates.notNull()
|
||||||
override var methodTasks = arrayListOf<Plugin.MethodTask>()
|
override var methodTasks = arrayListOf<Plugin.MethodTask>()
|
||||||
|
|
||||||
override fun accept(project: Project) = true
|
override fun accept(project: Project) = true
|
||||||
|
|
||||||
var plugins : Plugins by Delegates.notNull()
|
var plugins : Plugins by Delegates.notNull()
|
||||||
|
|
||||||
|
fun addSyntheticTask(name: String, project: Project, task: (Project) -> TaskResult) {
|
||||||
|
val task = object: PluginTask() {
|
||||||
|
override val doc = "A synthetic task"
|
||||||
|
override val name = name
|
||||||
|
override val plugin = this@BasePlugin
|
||||||
|
override val project = project
|
||||||
|
|
||||||
|
override fun call(): TaskResult2<PluginTask>? {
|
||||||
|
val taskResult = task(project)
|
||||||
|
return TaskResult2(taskResult.success, this)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tasks.add(task)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,7 +17,7 @@ public interface Plugin {
|
||||||
class MethodTask(val method: Method, val taskAnnotation: Task)
|
class MethodTask(val method: Method, val taskAnnotation: Task)
|
||||||
val methodTasks : ArrayList<MethodTask>
|
val methodTasks : ArrayList<MethodTask>
|
||||||
|
|
||||||
fun addTask(annotation: Task, project: Project, task: (Project) -> TaskResult) {
|
fun addStaticTask(annotation: Task, project: Project, task: (Project) -> TaskResult) {
|
||||||
tasks.add(object : BasePluginTask(this, annotation.name, annotation.description, project) {
|
tasks.add(object : BasePluginTask(this, annotation.name, annotation.description, project) {
|
||||||
override fun call(): TaskResult2<PluginTask> {
|
override fun call(): TaskResult2<PluginTask> {
|
||||||
val taskResult = task(project)
|
val taskResult = task(project)
|
||||||
|
|
|
@ -4,12 +4,12 @@ import com.beust.kobalt.internal.TaskResult2
|
||||||
import com.beust.kobalt.misc.ToString
|
import com.beust.kobalt.misc.ToString
|
||||||
import java.util.concurrent.Callable
|
import java.util.concurrent.Callable
|
||||||
|
|
||||||
public interface PluginTask : Callable<TaskResult2<PluginTask>> {
|
abstract public class PluginTask : Callable<TaskResult2<PluginTask>> {
|
||||||
val plugin: Plugin
|
abstract val plugin: Plugin
|
||||||
val name: String
|
open val name: String = ""
|
||||||
val doc: String
|
open val doc: String = ""
|
||||||
val project: Project
|
abstract val project: Project
|
||||||
val dependsOn : List<String>
|
val dependsOn = arrayListOf<String>()
|
||||||
|
|
||||||
override public fun toString() : String {
|
override public fun toString() : String {
|
||||||
return ToString("PluginTask", "id", project.name + ":" + name).s
|
return ToString("PluginTask", "id", project.name + ":" + name).s
|
||||||
|
|
|
@ -4,15 +4,11 @@ import com.beust.kobalt.Args
|
||||||
import com.beust.kobalt.Plugins
|
import com.beust.kobalt.Plugins
|
||||||
import com.beust.kobalt.api.PluginTask
|
import com.beust.kobalt.api.PluginTask
|
||||||
import com.beust.kobalt.api.Project
|
import com.beust.kobalt.api.Project
|
||||||
import com.beust.kobalt.api.Task
|
|
||||||
import com.beust.kobalt.misc.KobaltLogger
|
import com.beust.kobalt.misc.KobaltLogger
|
||||||
import com.beust.kobalt.maven.KobaltException
|
import com.beust.kobalt.maven.KobaltException
|
||||||
import com.google.common.collect.ArrayListMultimap
|
|
||||||
import com.google.common.collect.HashMultimap
|
import com.google.common.collect.HashMultimap
|
||||||
import com.google.common.collect.TreeMultimap
|
import com.google.common.collect.TreeMultimap
|
||||||
import java.util.HashSet
|
import java.util.HashSet
|
||||||
import java.util.concurrent.LinkedBlockingQueue
|
|
||||||
import java.util.concurrent.TimeUnit
|
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
import javax.inject.Singleton
|
import javax.inject.Singleton
|
||||||
|
|
||||||
|
@ -28,6 +24,13 @@ public class TaskManager @Inject constructor(val plugins: Plugins, val args: Arg
|
||||||
dependentTaskMap.put(task1, task2)
|
dependentTaskMap.put(task1, task2)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class TaskInfo(val id: String) {
|
||||||
|
val project: String?
|
||||||
|
get() = if (id.contains(":")) id.split(":").get(0) else null
|
||||||
|
val task: String
|
||||||
|
get() = if (id.contains(":")) id.split(":").get(1) else id
|
||||||
|
}
|
||||||
|
|
||||||
public fun runTargets(targets: List<String>, projects: List<Project>) {
|
public fun runTargets(targets: List<String>, projects: List<Project>) {
|
||||||
val tasksByNames = HashMultimap.create<String, PluginTask>()
|
val tasksByNames = HashMultimap.create<String, PluginTask>()
|
||||||
|
|
||||||
|
@ -36,6 +39,9 @@ public class TaskManager @Inject constructor(val plugins: Plugins, val args: Arg
|
||||||
log(1, " Building project ${project.name}")
|
log(1, " Building project ${project.name}")
|
||||||
log(1, "")
|
log(1, "")
|
||||||
|
|
||||||
|
val allTasksByNames = hashMapOf<String, PluginTask>()
|
||||||
|
plugins.allTasks.forEach { allTasksByNames.put(TaskInfo(it.name).task, it)}
|
||||||
|
|
||||||
//
|
//
|
||||||
// Locate all the tasks
|
// Locate all the tasks
|
||||||
//
|
//
|
||||||
|
@ -70,38 +76,45 @@ public class TaskManager @Inject constructor(val plugins: Plugins, val args: Arg
|
||||||
val newToProcess = hashSetOf<String>()
|
val newToProcess = hashSetOf<String>()
|
||||||
log(3, "toProcess size: " + toProcess.size())
|
log(3, "toProcess size: " + toProcess.size())
|
||||||
toProcess.forEach { target ->
|
toProcess.forEach { target ->
|
||||||
log(3, "Processing ${target}")
|
val pluginTask = allTasksByNames.get(target)
|
||||||
val actualTarget =
|
// Only calculate the transitive closure for this target if its plug-in accepts the
|
||||||
if (target.contains(":")) {
|
// current project
|
||||||
// The target specifies a project explicitly
|
if (pluginTask != null && pluginTask.plugin.accept(project)) {
|
||||||
target.split(":").let {
|
log(3, "Processing ${target}")
|
||||||
val projectName = it[0]
|
val actualTarget =
|
||||||
if (projectName == project.name) {
|
if (target.contains(":")) {
|
||||||
it[1]
|
// The target specifies a project explicitly
|
||||||
|
target.split(":").let {
|
||||||
|
val projectName = it[0]
|
||||||
|
if (projectName == project.name) {
|
||||||
|
it[1]
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
null
|
target
|
||||||
|
}
|
||||||
|
if (actualTarget != null) {
|
||||||
|
transitiveClosure.add(actualTarget)
|
||||||
|
val tasks = tasksByNames.get(actualTarget)
|
||||||
|
if (tasks.isEmpty()) {
|
||||||
|
throw KobaltException("Unknown task: ${target}")
|
||||||
|
}
|
||||||
|
tasks.forEach { task ->
|
||||||
|
val dependencyNames = dependentTaskMap.get(task.name)
|
||||||
|
dependencyNames.forEach { dependencyName ->
|
||||||
|
if (!seen.contains(dependencyName)) {
|
||||||
|
newToProcess.add(dependencyName)
|
||||||
|
seen.add(dependencyName)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
target
|
log(2, "Target ${target} specified so not running it for project ${project.name}")
|
||||||
}
|
}
|
||||||
if (actualTarget != null) {
|
} else if (pluginTask == null) {
|
||||||
transitiveClosure.add(actualTarget)
|
throw AssertionError("Should have found the task for $target")
|
||||||
val tasks = tasksByNames.get(actualTarget)
|
|
||||||
if (tasks.isEmpty()) {
|
|
||||||
throw KobaltException("Unknown task: ${target}")
|
|
||||||
}
|
|
||||||
tasks.forEach { task ->
|
|
||||||
val dependencyNames = dependentTaskMap.get(task.name)
|
|
||||||
dependencyNames.forEach { dependencyName ->
|
|
||||||
if (!seen.contains(dependencyName)) {
|
|
||||||
newToProcess.add(dependencyName)
|
|
||||||
seen.add(dependencyName)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
log(2, "Target ${target} specified so not running it for project ${project.name}")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
done = newToProcess.isEmpty()
|
done = newToProcess.isEmpty()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue