Search in sources :

Example 1 with OptionDescriptor

use of org.gradle.api.internal.tasks.options.OptionDescriptor in project gradle by gradle.

the class TaskDetailPrinter method optionToAvailableValues.

@SuppressWarnings("unchecked")
private Map<String, Set<String>> optionToAvailableValues(List<OptionDescriptor> allOptions) {
    Map<String, Set<String>> result = new LinkedHashMap<String, Set<String>>();
    for (OptionDescriptor optionDescriptor : allOptions) {
        if (result.containsKey(optionDescriptor.getName())) {
            Collection<String> commonValues = intersection(optionDescriptor.getAvailableValues(), result.get(optionDescriptor.getName()));
            result.put(optionDescriptor.getName(), new TreeSet<String>(commonValues));
        } else {
            result.put(optionDescriptor.getName(), optionDescriptor.getAvailableValues());
        }
    }
    return result;
}
Also used : OptionDescriptor(org.gradle.api.internal.tasks.options.OptionDescriptor)

Example 2 with OptionDescriptor

use of org.gradle.api.internal.tasks.options.OptionDescriptor in project gradle by gradle.

the class TaskDetailPrinter method printlnCommandlineOptions.

private void printlnCommandlineOptions(StyledTextOutput output, List<Task> tasks) {
    List<OptionDescriptor> allOptions = new ArrayList<OptionDescriptor>();
    for (Task task : tasks) {
        allOptions.addAll(optionReader.getOptions(task));
    }
    if (!allOptions.isEmpty()) {
        output.println();
        output.text("Options").println();
    }
    Map<String, Set<String>> optionToAvailableOptionsValues = optionToAvailableValues(allOptions);
    Map<String, String> optionToDescription = optionToDescription(allOptions);
    Iterator<String> optionNames = optionToAvailableOptionsValues.keySet().iterator();
    while (optionNames.hasNext()) {
        String currentOption = optionNames.next();
        Set<String> availableValues = optionToAvailableOptionsValues.get(currentOption);
        String optionString = "--" + currentOption;
        output.text(INDENT).withStyle(UserInput).text(optionString);
        output.text(INDENT).text(optionToDescription.get(currentOption));
        if (!availableValues.isEmpty()) {
            final int optionDescriptionOffset = 2 * INDENT.length() + optionString.length();
            final LinePrefixingStyledTextOutput prefixedOutput = createIndentedOutput(output, optionDescriptionOffset);
            prefixedOutput.println();
            prefixedOutput.println("Available values are:");
            for (String value : availableValues) {
                prefixedOutput.text(INDENT);
                prefixedOutput.withStyle(UserInput).println(value);
            }
        } else {
            output.println();
        }
        if (optionNames.hasNext()) {
            output.println();
        }
    }
}
Also used : Task(org.gradle.api.Task) DefaultTask(org.gradle.api.DefaultTask) OptionDescriptor(org.gradle.api.internal.tasks.options.OptionDescriptor) LinePrefixingStyledTextOutput(org.gradle.internal.logging.text.LinePrefixingStyledTextOutput)

Example 3 with OptionDescriptor

use of org.gradle.api.internal.tasks.options.OptionDescriptor in project gradle by gradle.

the class CommandLineTaskConfigurer method configureTasksNow.

private List<String> configureTasksNow(Collection<Task> tasks, List<String> arguments) {
    List<String> remainingArguments = null;
    for (Task task : tasks) {
        CommandLineParser parser = new CommandLineParser();
        final List<OptionDescriptor> commandLineOptions = optionReader.getOptions(task);
        for (OptionDescriptor optionDescriptor : commandLineOptions) {
            String optionName = optionDescriptor.getName();
            org.gradle.cli.CommandLineOption option = parser.option(optionName);
            option.hasDescription(optionDescriptor.getDescription());
            option.hasArgument(optionDescriptor.getArgumentType());
        }
        ParsedCommandLine parsed;
        try {
            parsed = parser.parse(arguments);
        } 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);
        }
        for (OptionDescriptor commandLineOptionDescriptor : commandLineOptions) {
            final String name = commandLineOptionDescriptor.getName();
            if (parsed.hasOption(name)) {
                ParsedCommandLineOption o = parsed.option(name);
                try {
                    commandLineOptionDescriptor.apply(task, o.getValues());
                } catch (TypeConversionException ex) {
                    throw new TaskConfigurationException(task.getPath(), String.format("Problem configuring option '%s' on task '%s' from command line.", name, task.getPath()), ex);
                }
            }
        }
        assert remainingArguments == null || remainingArguments.equals(parsed.getExtraArguments()) : "we expect all options to be consumed by each task so remainingArguments should be the same for each task";
        remainingArguments = parsed.getExtraArguments();
    }
    return remainingArguments;
}
Also used : Task(org.gradle.api.Task) OptionDescriptor(org.gradle.api.internal.tasks.options.OptionDescriptor) TypeConversionException(org.gradle.internal.typeconversion.TypeConversionException) ParsedCommandLineOption(org.gradle.cli.ParsedCommandLineOption) ParsedCommandLine(org.gradle.cli.ParsedCommandLine) CommandLineParser(org.gradle.cli.CommandLineParser) CommandLineArgumentException(org.gradle.cli.CommandLineArgumentException)

Aggregations

OptionDescriptor (org.gradle.api.internal.tasks.options.OptionDescriptor)3 Task (org.gradle.api.Task)2 DefaultTask (org.gradle.api.DefaultTask)1 CommandLineArgumentException (org.gradle.cli.CommandLineArgumentException)1 CommandLineParser (org.gradle.cli.CommandLineParser)1 ParsedCommandLine (org.gradle.cli.ParsedCommandLine)1 ParsedCommandLineOption (org.gradle.cli.ParsedCommandLineOption)1 LinePrefixingStyledTextOutput (org.gradle.internal.logging.text.LinePrefixingStyledTextOutput)1 TypeConversionException (org.gradle.internal.typeconversion.TypeConversionException)1