Search in sources :

Example 1 with CachingTaskDependencyResolveContext

use of org.gradle.api.internal.tasks.CachingTaskDependencyResolveContext in project gradle by gradle.

the class DefaultTaskExecutionPlan method addToTaskGraph.

public void addToTaskGraph(Collection<? extends Task> tasks) {
    List<TaskInfo> queue = new ArrayList<TaskInfo>();
    List<Task> sortedTasks = new ArrayList<Task>(tasks);
    Collections.sort(sortedTasks);
    for (Task task : sortedTasks) {
        TaskInfo node = nodeFactory.createNode(task);
        if (node.isMustNotRun()) {
            requireWithDependencies(node);
        } else if (filter.isSatisfiedBy(task)) {
            node.require();
        }
        entryTasks.add(node);
        queue.add(node);
    }
    Set<TaskInfo> visiting = new HashSet<TaskInfo>();
    CachingTaskDependencyResolveContext context = new CachingTaskDependencyResolveContext();
    while (!queue.isEmpty()) {
        TaskInfo node = queue.get(0);
        if (node.getDependenciesProcessed()) {
            // Have already visited this task - skip it
            queue.remove(0);
            continue;
        }
        TaskInternal task = node.getTask();
        boolean filtered = !filter.isSatisfiedBy(task);
        if (filtered) {
            // Task is not required - skip it
            queue.remove(0);
            node.dependenciesProcessed();
            node.doNotRequire();
            filteredTasks.add(task);
            continue;
        }
        if (visiting.add(node)) {
            // Have not seen this task before - add its dependencies to the head of the queue and leave this
            // task in the queue
            // Make sure it has been configured
            ((TaskContainerInternal) task.getProject().getTasks()).prepareForExecution(task);
            Set<? extends Task> dependsOnTasks = context.getDependencies(task, task.getTaskDependencies());
            for (Task dependsOnTask : dependsOnTasks) {
                TaskInfo targetNode = nodeFactory.createNode(dependsOnTask);
                node.addDependencySuccessor(targetNode);
                if (!visiting.contains(targetNode)) {
                    queue.add(0, targetNode);
                }
            }
            for (Task finalizerTask : context.getDependencies(task, task.getFinalizedBy())) {
                TaskInfo targetNode = nodeFactory.createNode(finalizerTask);
                addFinalizerNode(node, targetNode);
                if (!visiting.contains(targetNode)) {
                    queue.add(0, targetNode);
                }
            }
            for (Task mustRunAfter : context.getDependencies(task, task.getMustRunAfter())) {
                TaskInfo targetNode = nodeFactory.createNode(mustRunAfter);
                node.addMustSuccessor(targetNode);
            }
            for (Task shouldRunAfter : context.getDependencies(task, task.getShouldRunAfter())) {
                TaskInfo targetNode = nodeFactory.createNode(shouldRunAfter);
                node.addShouldSuccessor(targetNode);
            }
            if (node.isRequired()) {
                for (TaskInfo successor : node.getDependencySuccessors()) {
                    if (filter.isSatisfiedBy(successor.getTask())) {
                        successor.require();
                    }
                }
            } else {
                tasksInUnknownState.add(node);
            }
        } else {
            // Have visited this task's dependencies - add it to the graph
            queue.remove(0);
            visiting.remove(node);
            node.dependenciesProcessed();
        }
    }
    resolveTasksInUnknownState();
}
Also used : CachingTaskDependencyResolveContext(org.gradle.api.internal.tasks.CachingTaskDependencyResolveContext) Task(org.gradle.api.Task) TaskInternal(org.gradle.api.internal.TaskInternal) ArrayList(java.util.ArrayList) TaskContainerInternal(org.gradle.api.internal.tasks.TaskContainerInternal) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Aggregations

ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 Task (org.gradle.api.Task)1 TaskInternal (org.gradle.api.internal.TaskInternal)1 CachingTaskDependencyResolveContext (org.gradle.api.internal.tasks.CachingTaskDependencyResolveContext)1 TaskContainerInternal (org.gradle.api.internal.tasks.TaskContainerInternal)1