Skip to content

Commit

Permalink
REVIEW-4876 Merge two BuildConfigurationActions.
Browse files Browse the repository at this point in the history
  • Loading branch information
radimk committed May 14, 2014
1 parent 70096c1 commit 8407f3e
Show file tree
Hide file tree
Showing 9 changed files with 163 additions and 186 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.gradle.execution;

import com.google.common.collect.Multimap;
import org.gradle.TaskParameter;
import org.gradle.api.Task;
import org.gradle.api.internal.GradleInternal;
import org.gradle.execution.commandline.CommandLineTaskParser;
Expand All @@ -41,11 +42,11 @@ public TaskNameResolvingBuildConfigurationAction(CommandLineTaskParser commandLi

public void configure(BuildExecutionContext context) {
GradleInternal gradle = context.getGradle();
List<String> taskNames = gradle.getStartParameter().getTaskNames();
Multimap<String, Task> selectedTasks = commandLineTaskParser.parseTasks(taskNames, selector);
List<TaskParameter> taskParameters = gradle.getStartParameter().getTaskParameters();
Multimap<TaskParameter, Task> selectedTasks = commandLineTaskParser.parseTasks(taskParameters, selector);

TaskGraphExecuter executer = gradle.getTaskGraph();
for (String name : selectedTasks.keySet()) {
for (TaskParameter name : selectedTasks.keySet()) {
executer.addTasks(selectedTasks.get(name));
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@

package org.gradle.execution.commandline;

import com.google.common.base.Function;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import org.gradle.TaskParameter;
import org.gradle.api.Task;
import org.gradle.api.internal.tasks.options.OptionDescriptor;
import org.gradle.api.internal.tasks.options.OptionReader;
import org.gradle.cli.CommandLineArgumentException;
import org.gradle.cli.CommandLineParser;
import org.gradle.cli.ParsedCommandLine;
import org.gradle.cli.ParsedCommandLineOption;
import org.gradle.internal.DefaultTaskParameter;
import org.gradle.internal.typeconversion.TypeConversionException;

import java.util.Collection;
Expand All @@ -36,16 +41,29 @@ public CommandLineTaskConfigurer(OptionReader optionReader) {
this.optionReader = optionReader;
}

public List<String> configureTasks(Collection<Task> tasks, List<String> arguments) {
public List<TaskParameter> configureTasks(Collection<Task> tasks, List<TaskParameter> arguments) {
assert !tasks.isEmpty();
if (arguments.isEmpty()) {
return arguments;
}
return configureTasksNow(tasks, arguments);
}

private List<String> configureTasksNow(Collection<Task> tasks, List<String> arguments) {
private List<TaskParameter> configureTasksNow(Collection<Task> tasks, List<TaskParameter> arguments) {
List<String> remainingArguments = null;
List<String> argumentsOrParameters = Lists.newArrayList();
List<TaskParameter> parameters = Lists.newArrayList();
boolean notArgument = false;
for (TaskParameter parameter : arguments) {
if (parameter.getProjectPath() != null) {
notArgument = true;
}
if (notArgument) {
parameters.add(parameter);
} else {
argumentsOrParameters.add(parameter.getTaskName());
}
}
for (Task task : tasks) {
CommandLineParser parser = new CommandLineParser();
final List<OptionDescriptor> commandLineOptions = optionReader.getOptions(task);
Expand All @@ -58,7 +76,7 @@ private List<String> configureTasksNow(Collection<Task> tasks, List<String> argu

ParsedCommandLine parsed;
try {
parsed = parser.parse(arguments);
parsed = parser.parse(argumentsOrParameters);
} catch (CommandLineArgumentException e) {
//we expect that all options must be applicable for each task
throw new TaskConfigurationException(task.getPath(), "Problem configuring task " + task.getPath() + " from command line.", e);
Expand All @@ -80,6 +98,14 @@ private List<String> configureTasksNow(Collection<Task> tasks, List<String> argu
: "we expect all options to be consumed by each task so remainingArguments should be the same for each task";
remainingArguments = parsed.getExtraArguments();
}
return remainingArguments;
return Lists.newArrayList(Iterables.concat(
Iterables.transform(
remainingArguments,
new Function<String, TaskParameter>() {
public TaskParameter apply(String input) {
return new DefaultTaskParameter(input);
}
}),
parameters));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.common.collect.LinkedHashMultimap;
import com.google.common.collect.Multimap;
import com.google.common.collect.SetMultimap;
import org.gradle.TaskParameter;
import org.gradle.api.Task;
import org.gradle.execution.TaskSelector;

Expand All @@ -33,16 +34,16 @@ public CommandLineTaskParser(CommandLineTaskConfigurer commandLineTaskConfigurer
this.taskConfigurer = commandLineTaskConfigurer;
}

public Multimap<String, Task> parseTasks(List<String> taskPaths, TaskSelector taskSelector) {
SetMultimap<String, Task> out = LinkedHashMultimap.create();
List<String> remainingPaths = new LinkedList<String>(taskPaths);
public Multimap<TaskParameter, Task> parseTasks(List<TaskParameter> taskParameters, TaskSelector taskSelector) {
SetMultimap<TaskParameter, Task> out = LinkedHashMultimap.create();
List<TaskParameter> remainingPaths = new LinkedList<TaskParameter>(taskParameters);
while (!remainingPaths.isEmpty()) {
String path = remainingPaths.remove(0);
TaskParameter path = remainingPaths.remove(0);
TaskSelector.TaskSelection selection = taskSelector.getSelection(path);
Set<Task> tasks = selection.getTasks();
remainingPaths = taskConfigurer.configureTasks(tasks, remainingPaths);

out.putAll(selection.getTaskName(), tasks);
out.putAll(path, tasks);
}
return out;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ BuildExecuter createBuildExecuter(CommandLineTaskParser commandLineTaskParser, T
configs.add(new ProjectEvaluatingAction());
}
configs.add(new DefaultTasksBuildExecutionAction());
configs.add(new TaskParameterResolvingBuildConfigurationAction(taskSelector));
configs.add(new ExcludedTaskFilteringBuildConfigurationAction());
configs.add(new TaskNameResolvingBuildConfigurationAction(commandLineTaskParser, taskSelector));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,32 @@
* limitations under the License.
*/

package org.gradle
package org.gradle.execution

import com.google.common.collect.Sets
import org.gradle.StartParameter
import org.gradle.TaskParameter
import org.gradle.api.Task
import org.gradle.api.internal.GradleInternal
import org.gradle.execution.BuildExecutionContext
import org.gradle.execution.TaskParameterResolvingBuildConfigurationAction
import org.gradle.execution.TaskSelector
import org.gradle.api.internal.tasks.options.OptionReader
import org.gradle.execution.commandline.CommandLineTaskConfigurer
import org.gradle.execution.commandline.CommandLineTaskParser
import org.gradle.internal.DefaultTaskParameter
import spock.lang.Specification

class TaskParameterResolvingBuildConfigurationActionSpec extends Specification {
class TaskNameResolvingBuildConfigurationActionSpec extends Specification {
TaskSelector selector
GradleInternal gradle
BuildExecutionContext context
def TaskParameterResolvingBuildConfigurationAction action
def TaskNameResolvingBuildConfigurationAction action

def setup() {
selector = Mock(TaskSelector)
gradle = Mock(GradleInternal)
context = Mock(BuildExecutionContext)
action = new TaskParameterResolvingBuildConfigurationAction(selector)
OptionReader optionReader = new OptionReader();
CommandLineTaskParser parser = new CommandLineTaskParser(new CommandLineTaskConfigurer(optionReader));
action = new TaskNameResolvingBuildConfigurationAction(parser, selector)
}

def "empty task parameters are no-op action"() {
Expand All @@ -54,31 +59,12 @@ class TaskParameterResolvingBuildConfigurationActionSpec extends Specification {
0 * startParameters._()
}

def "skip task name parsing when no selectors with project path are given"() {
given:
def startParameters = Mock(StartParameter)
// selectors with null projectPath
TaskParameter taskParameter1 = Mock(TaskParameter)
TaskParameter taskParameter2 = Mock(TaskParameter)

when:
_ * context.getGradle() >> gradle
_ * gradle.getStartParameter() >> startParameters
_ * startParameters.getTaskParameters() >> [taskParameter1, taskParameter2]

action.configure(context)

then:
1 * context.proceed()
0 * context._()
0 * startParameters._()
}

def "expand task parameters to tasks"() {
given:
def startParameters = Mock(StartParameter)
TaskParameter taskParameter1 = Mock(TaskParameter)
TaskParameter taskParameter2 = Mock(TaskParameter)
def executer = Mock(TaskGraphExecuter)
TaskParameter taskParameter1 = new DefaultTaskParameter('task1', ':')
TaskParameter taskParameter2 = new DefaultTaskParameter('task2', ':')
def selection1 = Mock(TaskSelector.TaskSelection)
def task1a = Mock(Task)
def task1b = Mock(Task)
Expand All @@ -88,22 +74,20 @@ class TaskParameterResolvingBuildConfigurationActionSpec extends Specification {
when:
_ * context.getGradle() >> gradle
_ * gradle.getStartParameter() >> startParameters
_ * taskParameter1.projectPath >> ':'
_ * taskParameter2.projectPath >> ':'
_ * startParameters.getTaskParameters() >> [taskParameter1, taskParameter2]
_ * selector.getSelection(taskParameter1) >> selection1
_ * selector.getSelection(taskParameter2) >> selection2
_ * gradle.taskGraph >> executer

1 * selection1.tasks >> Sets.newLinkedHashSet([task1a, task1b])
1 * selection2.tasks >> [task2]
1 * task1a.path >> ':task1'
1 * task1b.path >> ':sub:task1'
1 * task2.path >> ':task2'

action.configure(context)

then:
1 * startParameters.setTaskNames([':task1', ':sub:task1', ':task2'])
0 * startParameters.setTaskNames(_)
1 * executer.addTasks(Sets.newLinkedHashSet([task1a, task1b]))
1 * executer.addTasks(Sets.newLinkedHashSet([task2]))
1 * context.proceed()
0 * context._()
}
Expand Down
Loading

0 comments on commit 8407f3e

Please sign in to comment.