From 4ed2183dbd1e51f247689ca973b69a048129f4dd Mon Sep 17 00:00:00 2001 From: Cedric Beust Date: Tue, 12 Apr 2016 18:28:49 -0700 Subject: [PATCH] Extract graph calculation. --- .../com/beust/kobalt/internal/TaskManager.kt | 140 +++++++++--------- 1 file changed, 74 insertions(+), 66 deletions(-) diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/TaskManager.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/TaskManager.kt index f86176ea..9ef2c417 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/TaskManager.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/TaskManager.kt @@ -86,77 +86,13 @@ public class TaskManager @Inject constructor(val args: Args, val incrementalMana tasksByNames.keys().forEach { log(3, " $it: " + tasksByNames.get(it)) } - val graph = DynamicGraph() - taskNames.forEach { taskName -> - val ti = TaskInfo(taskName) - if (!tasksByNames.keys().contains(ti.taskName)) { - throw KobaltException("Unknown task: $taskName") - } - if (ti.matches(projectName)) { - tasksByNames[ti.taskName].forEach { task -> - if (task != null && task.plugin.accept(project)) { - val reverseAfter = hashMapOf() - alwaysRunAfter.keys().forEach { from -> - val tasks = alwaysRunAfter.get(from) - tasks.forEach { - reverseAfter.put(it, from) - } - } - - // - // If the current target is free, add it as a single node to the graph - // - val allFreeTasks = calculateFreeTasks(tasksByNames, reverseAfter) - val currentFreeTask = allFreeTasks.filter { - TaskInfo(projectName, it.name).taskName == task.name - } - if (currentFreeTask.size == 1) { - currentFreeTask[0].let { - graph.addNode(it) - } - } - - // - // Add the transitive closure of the current task as edges to the graph - // - val transitiveClosure = calculateTransitiveClosure(project, tasksByNames, ti) - transitiveClosure.forEach { pluginTask -> - val rb = runBefore.get(pluginTask.name) - rb.forEach { - val tos = tasksByNames[it] - if (tos != null && tos.size > 0) { - tos.forEach { to -> - graph.addEdge(pluginTask, to) - } - } else { - log(2, "Couldn't find node $it: not applicable to project ${project.name}") - } - } - } - - // - // If any of the nodes in the graph has an "alwaysRunAfter", add that edge too - // - val allNodes = arrayListOf() - allNodes.addAll(graph.nodes) - allNodes.forEach { node -> - val other = alwaysRunAfter.get(node.name) - other?.forEach { o -> - tasksByNames[o]?.forEach { - graph.addEdge(it, node) - } - } - } - } - } - } - } + val graph = createGraph(project, taskNames, tasksByNames) // // Now that we have a full graph, run it // - log(3, "About to run graph:\n ${graph.dump()} ") + log(1, "About to run graph:\n ${graph.dump()} ") val factory = object : IThreadWorkerFactory { override public fun createWorkers(nodes: List): List> { @@ -186,6 +122,78 @@ public class TaskManager @Inject constructor(val args: Args, val incrementalMana return RunTargetResult(result, messages) } + private fun createGraph(project: Project, taskNames: List, tasksByNames: Multimap): + DynamicGraph { + val graph = DynamicGraph() + taskNames.forEach { taskName -> + val ti = TaskInfo(taskName) + if (!tasksByNames.keys().contains(ti.taskName)) { + throw KobaltException("Unknown task: $taskName") + } + + if (ti.matches(project.name)) { + tasksByNames[ti.taskName].forEach { task -> + if (task != null && task.plugin.accept(project)) { + val reverseAfter = hashMapOf() + alwaysRunAfter.keys().forEach { from -> + val tasks = alwaysRunAfter.get(from) + tasks.forEach { + reverseAfter.put(it, from) + } + } + + // + // If the current target is free, add it as a single node to the graph + // + val allFreeTasks = calculateFreeTasks(tasksByNames, reverseAfter) + val currentFreeTask = allFreeTasks.filter { + TaskInfo(project.name, it.name).taskName == task.name + } + if (currentFreeTask.size == 1) { + currentFreeTask[0].let { + graph.addNode(it) + } + } + + // + // Add the transitive closure of the current task as edges to the graph + // + val transitiveClosure = calculateTransitiveClosure(project, tasksByNames, ti) + transitiveClosure.forEach { pluginTask -> + val rb = runBefore.get(pluginTask.name) + rb.forEach { + val tos = tasksByNames[it] + if (tos != null && tos.size > 0) { + tos.forEach { to -> + graph.addEdge(pluginTask, to) + } + } else { + log(2, "Couldn't find node $it: not applicable to project ${project.name}") + } + } + } + + // + // If any of the nodes in the graph has an "alwaysRunAfter", add that edge too + // + val allNodes = arrayListOf() + allNodes.addAll(graph.nodes) + allNodes.forEach { node -> + val other = alwaysRunAfter.get(node.name) + other?.forEach { o -> + tasksByNames[o]?.forEach { + graph.addEdge(it, node) + } + } + } + } + } + } + } + + return graph + } + /** * Find the free tasks of the graph. */