diff --git a/subprojects/core/src/main/groovy/org/gradle/execution/TaskNameResolvingBuildConfigurationAction.java b/subprojects/core/src/main/groovy/org/gradle/execution/TaskNameResolvingBuildConfigurationAction.java index 87263c11c5f84..9b8279004895e 100644 --- a/subprojects/core/src/main/groovy/org/gradle/execution/TaskNameResolvingBuildConfigurationAction.java +++ b/subprojects/core/src/main/groovy/org/gradle/execution/TaskNameResolvingBuildConfigurationAction.java @@ -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; @@ -41,11 +42,11 @@ public TaskNameResolvingBuildConfigurationAction(CommandLineTaskParser commandLi public void configure(BuildExecutionContext context) { GradleInternal gradle = context.getGradle(); - List taskNames = gradle.getStartParameter().getTaskNames(); - Multimap selectedTasks = commandLineTaskParser.parseTasks(taskNames, selector); + List taskParameters = gradle.getStartParameter().getTaskParameters(); + Multimap selectedTasks = commandLineTaskParser.parseTasks(taskParameters, selector); TaskGraphExecuter executer = gradle.getTaskGraph(); - for (String name : selectedTasks.keySet()) { + for (TaskParameter name : selectedTasks.keySet()) { executer.addTasks(selectedTasks.get(name)); } diff --git a/subprojects/core/src/main/groovy/org/gradle/execution/TaskParameterResolvingBuildConfigurationAction.java b/subprojects/core/src/main/groovy/org/gradle/execution/TaskParameterResolvingBuildConfigurationAction.java deleted file mode 100644 index 9eb4181929d89..0000000000000 --- a/subprojects/core/src/main/groovy/org/gradle/execution/TaskParameterResolvingBuildConfigurationAction.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Copyright 2013 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.gradle.execution; - -import com.google.common.base.Function; -import com.google.common.base.Predicate; -import com.google.common.collect.Iterables; -import com.google.common.collect.Lists; -import org.gradle.StartParameter; -import org.gradle.TaskParameter; -import org.gradle.api.Task; -import org.gradle.api.internal.GradleInternal; - -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Set; - -/** - * A {@link org.gradle.execution.BuildConfigurationAction} which selects tasks which match the task parameter. - * For each name, selects all tasks in all projects whose name is the given name. - */ -public class TaskParameterResolvingBuildConfigurationAction implements BuildConfigurationAction { - private final TaskSelector selector; - - public TaskParameterResolvingBuildConfigurationAction(TaskSelector selector) { - this.selector = selector; - } - - public void configure(BuildExecutionContext context) { - GradleInternal gradle = context.getGradle(); - StartParameter parameters = gradle.getStartParameter(); - List taskParameters = parameters.getTaskParameters(); - if (taskParameters == null || taskParameters.isEmpty()) { - context.proceed(); - return; - } - boolean hasParametersWithProjectPath = Iterables.any( - taskParameters, - new Predicate() { - public boolean apply(TaskParameter input) { - return input.getProjectPath() != null; - } - }); - if (!hasParametersWithProjectPath) { - context.proceed(); - return; - } - LinkedHashSet selectedTasks = new LinkedHashSet(); - for (TaskParameter parameter : taskParameters) { - Set tasks = selector.getSelection(parameter).getTasks(); - for (String selectedTask : Iterables.transform( - tasks, - new Function() { - public String apply(Task input) { - return input.getPath(); - } - })) { - selectedTasks.add(selectedTask); - } - - } - gradle.getStartParameter().setTaskNames(Lists.newArrayList(selectedTasks)); - - context.proceed(); - } -} diff --git a/subprojects/core/src/main/groovy/org/gradle/execution/commandline/CommandLineTaskConfigurer.java b/subprojects/core/src/main/groovy/org/gradle/execution/commandline/CommandLineTaskConfigurer.java index 18da44c86370e..4e630c559f7d1 100644 --- a/subprojects/core/src/main/groovy/org/gradle/execution/commandline/CommandLineTaskConfigurer.java +++ b/subprojects/core/src/main/groovy/org/gradle/execution/commandline/CommandLineTaskConfigurer.java @@ -16,6 +16,10 @@ 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; @@ -23,6 +27,7 @@ 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; @@ -36,7 +41,7 @@ public CommandLineTaskConfigurer(OptionReader optionReader) { this.optionReader = optionReader; } - public List configureTasks(Collection tasks, List arguments) { + public List configureTasks(Collection tasks, List arguments) { assert !tasks.isEmpty(); if (arguments.isEmpty()) { return arguments; @@ -44,8 +49,21 @@ public List configureTasks(Collection tasks, List argument return configureTasksNow(tasks, arguments); } - private List configureTasksNow(Collection tasks, List arguments) { + private List configureTasksNow(Collection tasks, List arguments) { List remainingArguments = null; + List argumentsOrParameters = Lists.newArrayList(); + List 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 commandLineOptions = optionReader.getOptions(task); @@ -58,7 +76,7 @@ private List configureTasksNow(Collection tasks, List 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); @@ -80,6 +98,14 @@ private List configureTasksNow(Collection tasks, List 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() { + public TaskParameter apply(String input) { + return new DefaultTaskParameter(input); + } + }), + parameters)); } } \ No newline at end of file diff --git a/subprojects/core/src/main/groovy/org/gradle/execution/commandline/CommandLineTaskParser.java b/subprojects/core/src/main/groovy/org/gradle/execution/commandline/CommandLineTaskParser.java index 305658c0f87be..90880bee3da6e 100644 --- a/subprojects/core/src/main/groovy/org/gradle/execution/commandline/CommandLineTaskParser.java +++ b/subprojects/core/src/main/groovy/org/gradle/execution/commandline/CommandLineTaskParser.java @@ -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; @@ -33,16 +34,16 @@ public CommandLineTaskParser(CommandLineTaskConfigurer commandLineTaskConfigurer this.taskConfigurer = commandLineTaskConfigurer; } - public Multimap parseTasks(List taskPaths, TaskSelector taskSelector) { - SetMultimap out = LinkedHashMultimap.create(); - List remainingPaths = new LinkedList(taskPaths); + public Multimap parseTasks(List taskParameters, TaskSelector taskSelector) { + SetMultimap out = LinkedHashMultimap.create(); + List remainingPaths = new LinkedList(taskParameters); while (!remainingPaths.isEmpty()) { - String path = remainingPaths.remove(0); + TaskParameter path = remainingPaths.remove(0); TaskSelector.TaskSelection selection = taskSelector.getSelection(path); Set tasks = selection.getTasks(); remainingPaths = taskConfigurer.configureTasks(tasks, remainingPaths); - out.putAll(selection.getTaskName(), tasks); + out.putAll(path, tasks); } return out; } diff --git a/subprojects/core/src/main/groovy/org/gradle/internal/service/scopes/GradleScopeServices.java b/subprojects/core/src/main/groovy/org/gradle/internal/service/scopes/GradleScopeServices.java index 64a14524e2543..7f30327f4b00c 100644 --- a/subprojects/core/src/main/groovy/org/gradle/internal/service/scopes/GradleScopeServices.java +++ b/subprojects/core/src/main/groovy/org/gradle/internal/service/scopes/GradleScopeServices.java @@ -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)); diff --git a/subprojects/core/src/test/groovy/org/gradle/TaskParameterResolvingBuildConfigurationActionSpec.groovy b/subprojects/core/src/test/groovy/org/gradle/execution/TaskNameResolvingBuildConfigurationActionSpec.groovy similarity index 61% rename from subprojects/core/src/test/groovy/org/gradle/TaskParameterResolvingBuildConfigurationActionSpec.groovy rename to subprojects/core/src/test/groovy/org/gradle/execution/TaskNameResolvingBuildConfigurationActionSpec.groovy index ac144d0014a90..3a9b06c698545 100644 --- a/subprojects/core/src/test/groovy/org/gradle/TaskParameterResolvingBuildConfigurationActionSpec.groovy +++ b/subprojects/core/src/test/groovy/org/gradle/execution/TaskNameResolvingBuildConfigurationActionSpec.groovy @@ -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"() { @@ -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) @@ -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._() } diff --git a/subprojects/core/src/test/groovy/org/gradle/execution/TaskNameResolvingBuildConfigurationActionTest.java b/subprojects/core/src/test/groovy/org/gradle/execution/TaskNameResolvingBuildConfigurationActionTest.java index ee0abe1cd95c0..92666977ffcd8 100644 --- a/subprojects/core/src/test/groovy/org/gradle/execution/TaskNameResolvingBuildConfigurationActionTest.java +++ b/subprojects/core/src/test/groovy/org/gradle/execution/TaskNameResolvingBuildConfigurationActionTest.java @@ -15,9 +15,13 @@ */ package org.gradle.execution; +import com.google.common.base.Function; +import com.google.common.collect.Iterables; import com.google.common.collect.LinkedHashMultimap; +import com.google.common.collect.Lists; import com.google.common.collect.Multimap; import org.gradle.StartParameter; +import org.gradle.TaskParameter; import org.gradle.api.InvalidUserDataException; import org.gradle.api.Task; import org.gradle.api.internal.GradleInternal; @@ -27,6 +31,7 @@ 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 org.gradle.util.GUtil; import org.gradle.util.JUnit4GroovyMockery; import org.jmock.Expectations; @@ -36,8 +41,10 @@ import org.junit.Test; import org.junit.runner.RunWith; +import javax.annotation.Nullable; import java.util.Arrays; import java.util.HashSet; +import java.util.List; import java.util.Set; import static org.gradle.util.WrapUtil.*; @@ -82,8 +89,8 @@ public void selectsAllTasksWithTheProvidedNameInCurrentProjectAndSubprojects() { final Task task3 = task("other"); context.checking(new Expectations() {{ - allowing(startParameter).getTaskNames(); - will(returnValue(toList("name"))); + allowing(startParameter).getTaskParameters(); + will(returnValue(toParameters("name"))); one(resolver).selectAll("name", project); will(returnValue(tasks(task1, task2, task3))); @@ -121,8 +128,8 @@ private void assertMatches(final String pattern, final String matches, String... tasks.add(task("other")); context.checking(new Expectations() {{ - one(startParameter).getTaskNames(); - will(returnValue(toList(pattern))); + one(startParameter).getTaskParameters(); + will(returnValue(toParameters(pattern))); one(resolver).selectAll(pattern, project); will(returnValue(tasks(tasks))); @@ -139,8 +146,8 @@ public void selectsTaskWithMatchingRelativePath() { final Task task2 = task("a"); context.checking(new Expectations() {{ - allowing(startParameter).getTaskNames(); - will(returnValue(toList("a:b"))); + allowing(startParameter).getTaskParameters(); + will(returnValue(toParameters("a:b"))); one(project).getChildProjects(); will(returnValue(toMap("a", otherProject))); @@ -159,8 +166,8 @@ public void selectsTaskWithMatchingTaskInRootProject() { final Task task2 = task("a"); context.checking(new Expectations() {{ - allowing(startParameter).getTaskNames(); - will(returnValue(toList(":b"))); + allowing(startParameter).getTaskParameters(); + will(returnValue(toParameters(":b"))); one(project).getRootProject(); will(returnValue(rootProject)); @@ -179,8 +186,8 @@ public void selectsTaskWithMatchingAbsolutePath() { final Task task2 = task("a"); context.checking(new Expectations() {{ - allowing(startParameter).getTaskNames(); - will(returnValue(toList(":a:b"))); + allowing(startParameter).getTaskParameters(); + will(returnValue(toParameters(":a:b"))); one(project).getRootProject(); will(returnValue(rootProject)); @@ -201,8 +208,8 @@ public void usesCamelCaseAbbreviationToSelectTasksWhenNoExactMatchAndPathProvide final Task task2 = task("other"); context.checking(new Expectations() {{ - allowing(startParameter).getTaskNames(); - will(returnValue(toList("anotherProject:soTa"))); + allowing(startParameter).getTaskParameters(); + will(returnValue(toParameters("anotherProject:soTa"))); one(project).getChildProjects(); will(returnValue(toMap("anotherProject", otherProject))); @@ -221,8 +228,8 @@ public void usesCamelCaseAbbreviationToSelectProjectWhenPathProvided() { final Task task2 = task("other"); context.checking(new Expectations() {{ - allowing(startParameter).getTaskNames(); - will(returnValue(toList("anPr:soTa"))); + allowing(startParameter).getTaskParameters(); + will(returnValue(toParameters("anPr:soTa"))); one(project).getChildProjects(); will(returnValue(toMap("anotherProject", otherProject))); @@ -241,8 +248,8 @@ public void failsWhenProvidedTaskNameIsAmbiguous() { final Task task2 = task("someTasks"); context.checking(new Expectations() {{ - allowing(startParameter).getTaskNames(); - will(returnValue(toList("soTa"))); + allowing(startParameter).getTaskParameters(); + will(returnValue(toParameters("soTa"))); one(resolver).selectAll("soTa", project); will(returnValue(tasks(task1, task2))); @@ -264,8 +271,8 @@ public void reportsTyposInTaskName() { final Task task4 = task("other"); context.checking(new Expectations() {{ - allowing(startParameter).getTaskNames(); - will(returnValue(toList("ssomeTask"))); + allowing(startParameter).getTaskParameters(); + will(returnValue(toParameters("ssomeTask"))); one(resolver).selectAll("ssomeTask", project); will(returnValue(tasks(task1, task2, task3, task4))); @@ -285,8 +292,8 @@ public void treatsEachProvidedNameAsASeparateGroup() { final Task task2 = task("name2"); context.checking(new Expectations() {{ - allowing(startParameter).getTaskNames(); - will(returnValue(toList("child:name1", "name2"))); + allowing(startParameter).getTaskParameters(); + will(returnValue(toParameters("child:name1", "name2"))); one(project).getChildProjects(); will(returnValue(toMap("child", otherProject))); @@ -315,8 +322,8 @@ public void canConfigureSingleTaskUsingCommandLineOptions() { final Task task2 = task("name2"); context.checking(new Expectations() {{ - allowing(startParameter).getTaskNames(); - will(returnValue(toList("name1", "--all", "name2"))); + allowing(startParameter).getTaskParameters(); + will(returnValue(toParameters("name1", "--all", "name2"))); one(resolver).selectAll("name1", project); will(returnValue(tasks(task1))); @@ -345,8 +352,8 @@ public void failsWhenUnknownTaskNameIsProvided() { final Task task2 = task("t2"); context.checking(new Expectations() {{ - allowing(startParameter).getTaskNames(); - will(returnValue(toList("b3"))); + allowing(startParameter).getTaskParameters(); + will(returnValue(toParameters("b3"))); one(resolver).selectAll("b3", project); will(returnValue(tasks(task1, task2))); @@ -363,8 +370,8 @@ public void failsWhenUnknownTaskNameIsProvided() { @Test public void failsWhenCannotFindProjectInPath() { context.checking(new Expectations() {{ - allowing(startParameter).getTaskNames(); - will(returnValue(toList("a:b", "name2"))); + allowing(startParameter).getTaskParameters(); + will(returnValue(toParameters("a:b", "name2"))); one(project).getChildProjects(); will(returnValue(GUtil.map("aa", otherProject, "ab", otherProject))); @@ -378,6 +385,17 @@ public void failsWhenCannotFindProjectInPath() { } } + private List toParameters(String ... parameters) { + return Lists.newArrayList(Iterables.transform( + Lists.newArrayList(parameters), + new Function() { + @Nullable + public TaskParameter apply(@Nullable String input) { + return new DefaultTaskParameter(input); + } + })); + } + private Task task(String name) { return task(name, Task.class); } diff --git a/subprojects/core/src/test/groovy/org/gradle/execution/commandline/CommandLineTaskConfigurerSpec.groovy b/subprojects/core/src/test/groovy/org/gradle/execution/commandline/CommandLineTaskConfigurerSpec.groovy index b48548309d86e..443a9529795b1 100644 --- a/subprojects/core/src/test/groovy/org/gradle/execution/commandline/CommandLineTaskConfigurerSpec.groovy +++ b/subprojects/core/src/test/groovy/org/gradle/execution/commandline/CommandLineTaskConfigurerSpec.groovy @@ -21,6 +21,7 @@ import org.gradle.api.internal.tasks.options.Option import org.gradle.api.internal.tasks.options.OptionReader import org.gradle.api.tasks.TaskAction import org.gradle.execution.TaskSelector +import org.gradle.internal.DefaultTaskParameter import org.gradle.internal.typeconversion.TypeConversionException import org.gradle.testfixtures.ProjectBuilder import spock.lang.Specification @@ -38,7 +39,7 @@ class CommandLineTaskConfigurerSpec extends Specification { def "does not configure if option doesn't match"() { when: - configurer.configureTasks([task, task2], ['foo']) + configurer.configureTasks([task, task2], toTaskParameters(['foo'])) then: task.content == 'default content' task2.content == 'default content' @@ -52,16 +53,16 @@ class CommandLineTaskConfigurerSpec extends Specification { configurer = Spy(CommandLineTaskConfigurer, constructorArgs: [new OptionReader()]) when: - def out = configurer.configureTasks([task, task2], ['foo']) + def out = configurer.configureTasks([task, task2], toTaskParameters(['foo'])) then: - out == ['foo'] + out == toTaskParameters(['foo']) 0 * configurer.configureTasksNow(_, _) } def "configures string option on all tasks"() { when: - configurer.configureTasks([task, task2], ['--content', 'Hey!', 'foo']) + configurer.configureTasks([task, task2], toTaskParameters(['--content', 'Hey!', 'foo'])) then: task.content == 'Hey!' task2.content == 'Hey!' @@ -69,19 +70,19 @@ class CommandLineTaskConfigurerSpec extends Specification { def "configures boolean option"() { when: - configurer.configureTasks([task], ['--someFlag']) + configurer.configureTasks([task], toTaskParameters(['--someFlag'])) then: task.someFlag } def "configures enum option"() { when: - configurer.configureTasks([task], ['--someEnum', "value1"]) + configurer.configureTasks([task], toTaskParameters(['--someEnum', "value1"])) then: task.anEnum == TestEnum.value1 when: - configurer.configureTasks([task], ['--someEnum', "unsupportedEnumValue"]) + configurer.configureTasks([task], toTaskParameters(['--someEnum', "unsupportedEnumValue"])) then: def e = thrown(TaskConfigurationException) e.message == "Problem configuring option 'someEnum' on task ':someTask' from command line." @@ -92,7 +93,7 @@ class CommandLineTaskConfigurerSpec extends Specification { def "configures options on all types that can accommodate the setting"() { when: - configurer.configureTasks([task, otherTask], ['--someFlag']) + configurer.configureTasks([task, otherTask], toTaskParameters(['--someFlag'])) then: task.someFlag otherTask.someFlag @@ -100,7 +101,7 @@ class CommandLineTaskConfigurerSpec extends Specification { def "fails if some of the types cannot accommodate the setting"() { when: - configurer.configureTasks([task, defaultTask], ['--someFlag']) + configurer.configureTasks([task, defaultTask], toTaskParameters(['--someFlag'])) then: def ex = thrown(TaskConfigurationException) @@ -116,19 +117,19 @@ class CommandLineTaskConfigurerSpec extends Specification { ex.cause.message.contains('someFlag2') where: - input << [['--someFlag', '--someFlag2'], ['--someFlag2', '--someFlag']] + input << [toTaskParameters(['--someFlag', '--someFlag2']), toTaskParameters(['--someFlag2', '--someFlag'])] } def "configures the Boolean option"() { when: - configurer.configureTasks([task], ['--someFlag2']) + configurer.configureTasks([task], toTaskParameters(['--someFlag2'])) then: task.someFlag2 } def "configures mixed options"() { when: - configurer.configureTasks([task, task2], ['--someFlag', '--content', 'Hey!']) + configurer.configureTasks([task, task2], toTaskParameters(['--someFlag', '--content', 'Hey!'])) then: task.someFlag task2.someFlag @@ -137,15 +138,24 @@ class CommandLineTaskConfigurerSpec extends Specification { } def "configures options and returns unused arguments"() { - def args = ['--someFlag', '--content', 'Hey!', 'foo', '--baz', '--someFlag'] + def args = toTaskParameters(['--someFlag', '--content', 'Hey!', 'foo', '--baz', '--someFlag']) when: def out = configurer.configureTasks([task, task2], args) then: - out == ['foo', '--baz', '--someFlag'] + out == toTaskParameters(['foo', '--baz', '--someFlag']) + } + + def "configures options and returns unused arguments when TaskParameter is used"() { + def args = toTaskParameters(['--someFlag', '--content', 'Hey!']) + def parameter = new DefaultTaskParameter('foo', ':') + when: + def out = configurer.configureTasks([task, task2], args + parameter) + then: + out == [parameter] } def "fails on unknown option"() { - def args = ['--xxx'] + def args = toTaskParameters(['--xxx']) when: configurer.configureTasks([task, task2], args) @@ -155,7 +165,7 @@ class CommandLineTaskConfigurerSpec extends Specification { } def "fails neatly when short option used"() { - def args = ['--someFlag', '-c'] + def args = toTaskParameters(['--someFlag', '-c']) when: configurer.configureTasks([task], args) @@ -164,6 +174,10 @@ class CommandLineTaskConfigurerSpec extends Specification { ex.cause.message.contains("Unknown command-line option '-c'") } + def toTaskParameters(List arguments) { + arguments.collect { new DefaultTaskParameter(it) } + } + public static class SomeTask extends DefaultTask { String content = 'default content' diff --git a/subprojects/core/src/test/groovy/org/gradle/execution/commandline/CommandLineTaskParserSpec.groovy b/subprojects/core/src/test/groovy/org/gradle/execution/commandline/CommandLineTaskParserSpec.groovy index a93383d2014eb..87237d70d2edd 100644 --- a/subprojects/core/src/test/groovy/org/gradle/execution/commandline/CommandLineTaskParserSpec.groovy +++ b/subprojects/core/src/test/groovy/org/gradle/execution/commandline/CommandLineTaskParserSpec.groovy @@ -16,11 +16,13 @@ package org.gradle.execution.commandline +import org.gradle.TaskParameter import org.gradle.api.DefaultTask import org.gradle.api.Project import org.gradle.api.tasks.TaskAction import org.gradle.execution.TaskSelectionResult import org.gradle.execution.TaskSelector +import org.gradle.internal.DefaultTaskParameter import org.gradle.testfixtures.ProjectBuilder import spock.lang.Specification @@ -50,14 +52,19 @@ class CommandLineTaskParserSpec extends Specification { def "parses a single task"() { given: - selector.getSelection('foo') >> new TaskSelector.TaskSelection('foo task', asTaskSelectionResults(task)) + def foo = new DefaultTaskParameter('foo') + selector.getSelection(foo) >> new TaskSelector.TaskSelection('foo task', asTaskSelectionResults(task)) when: - def out = parser.parseTasks(['foo'], selector) + def out = parser.parseTasks([foo], selector) then: out.size() == 1 - out.get('foo task') == [task] as Set + out.get(foo) == [task] as Set + } + + List asTaskParameters(List arguments) { + arguments.collect { it instanceof TaskParameter ? it : new DefaultTaskParameter(it) } } Set asTaskSelectionResults(SomeTask... someTasks) { @@ -70,43 +77,49 @@ class CommandLineTaskParserSpec extends Specification { def "parses single task with multiple matches"() { given: - selector.getSelection('foo') >> new TaskSelector.TaskSelection('foo task', asTaskSelectionResults(task, task2)) + def foo = new DefaultTaskParameter('foo') + selector.getSelection(foo) >> new TaskSelector.TaskSelection('foo task', asTaskSelectionResults(task, task2)) when: - def out = parser.parseTasks(['foo'], selector) + def out = parser.parseTasks([foo], selector) then: out.size() == 2 - out.get('foo task') == [task, task2] as Set + out.get(foo) == [task, task2] as Set } def "parses multiple matching tasks"() { given: - selector.getSelection('foo') >> new TaskSelector.TaskSelection('foo task', asTaskSelectionResults(task, task2)) - selector.getSelection('bar') >> new TaskSelector.TaskSelection('bar task', asTaskSelectionResults(task3)) + def foo = new DefaultTaskParameter('foo') + def bar = new DefaultTaskParameter('bar') + selector.getSelection(foo) >> new TaskSelector.TaskSelection('foo task', asTaskSelectionResults(task, task2)) + selector.getSelection(bar) >> new TaskSelector.TaskSelection('bar task', asTaskSelectionResults(task3)) when: - def out = parser.parseTasks(['foo', 'bar'], selector) + def out = parser.parseTasks([foo, bar], selector) then: out.size() == 3 - out.get('foo task') == [task, task2] as Set - out.get('bar task') == [task3] as Set + out.get(foo) == [task, task2] as Set + out.get(bar) == [task3] as Set } def "configures tasks if configuration options specified"() { given: - selector.getSelection('foo') >> new TaskSelector.TaskSelection('foo task', asTaskSelectionResults(task, task2)) - selector.getSelection('bar') >> new TaskSelector.TaskSelection('bar task', asTaskSelectionResults(task3)) - selector.getSelection('lastTask') >> new TaskSelector.TaskSelection('last task', asTaskSelectionResults(task3)) + def foo = new DefaultTaskParameter('foo') + def bar = new DefaultTaskParameter('bar') + def last = new DefaultTaskParameter('lastTask') + selector.getSelection(foo) >> new TaskSelector.TaskSelection('foo task', asTaskSelectionResults(task, task2)) + selector.getSelection(bar) >> new TaskSelector.TaskSelection('bar task', asTaskSelectionResults(task3)) + selector.getSelection(last) >> new TaskSelector.TaskSelection('last task', asTaskSelectionResults(task3)) when: - def out = parser.parseTasks(['foo', '--all', 'bar', '--include', 'stuff', 'lastTask'], selector) + def out = parser.parseTasks(asTaskParameters([foo, '--all', bar, '--include', 'stuff', last]), selector) then: out.size() == 4 - 1 * parser.taskConfigurer.configureTasks(newHashSet(task, task2), ['--all', 'bar', '--include', 'stuff', 'lastTask']) >> ['bar', '--include', 'stuff', 'lastTask'] - 1 * parser.taskConfigurer.configureTasks(newHashSet(task3), ['--include', 'stuff', 'lastTask']) >> ['lastTask'] + 1 * parser.taskConfigurer.configureTasks(newHashSet(task, task2), asTaskParameters(['--all', bar, '--include', 'stuff', last])) >> asTaskParameters([bar, '--include', 'stuff', last]) + 1 * parser.taskConfigurer.configureTasks(newHashSet(task3), asTaskParameters(['--include', 'stuff', last])) >> [last] 1 * parser.taskConfigurer.configureTasks(newHashSet(task3), []) >> [] 0 * parser.taskConfigurer._ }