Search in sources :

Example 1 with CommandLineArgumentException

use of org.gradle.cli.CommandLineArgumentException in project intellij-community by JetBrains.

the class GradleExecuteTaskAction method actionPerformed.

@Override
public void actionPerformed(@NotNull final AnActionEvent e) {
    final Project project = e.getRequiredData(CommonDataKeys.PROJECT);
    ExecuteGradleTaskHistoryService historyService = ExecuteGradleTaskHistoryService.getInstance(project);
    GradleRunTaskDialog dialog = new GradleRunTaskDialog(project, historyService.getHistory());
    String lastWorkingDirectory = historyService.getWorkDirectory();
    if (lastWorkingDirectory.length() == 0) {
        lastWorkingDirectory = obtainAppropriateWorkingDirectory(e);
    }
    dialog.setWorkDirectory(lastWorkingDirectory);
    if (StringUtil.isEmptyOrSpaces(historyService.getCanceledCommand())) {
        if (historyService.getHistory().size() > 0) {
            dialog.setCommandLine(historyService.getHistory().get(0));
        }
    } else {
        dialog.setCommandLine(historyService.getCanceledCommand());
    }
    if (!dialog.showAndGet()) {
        historyService.setCanceledCommand(dialog.getCommandLine());
        return;
    }
    historyService.setCanceledCommand(null);
    String fullCommandLine = dialog.getCommandLine();
    fullCommandLine = fullCommandLine.trim();
    String workDirectory = dialog.getWorkDirectory();
    historyService.addCommand(fullCommandLine, workDirectory);
    final ExternalTaskExecutionInfo taskExecutionInfo;
    try {
        taskExecutionInfo = buildTaskInfo(workDirectory, fullCommandLine);
    } catch (CommandLineArgumentException ex) {
        final NotificationData notificationData = new NotificationData("<b>Command-line arguments cannot be parsed</b>", "<i>" + fullCommandLine + "</i> \n" + ex.getMessage(), NotificationCategory.WARNING, NotificationSource.TASK_EXECUTION);
        notificationData.setBalloonNotification(true);
        ExternalSystemNotificationManager.getInstance(project).showNotification(GradleConstants.SYSTEM_ID, notificationData);
        return;
    }
    RunManagerEx runManager = RunManagerEx.getInstanceEx(project);
    ExternalSystemUtil.runTask(taskExecutionInfo.getSettings(), taskExecutionInfo.getExecutorId(), project, GradleConstants.SYSTEM_ID);
    RunnerAndConfigurationSettings configuration = ExternalSystemUtil.createExternalSystemRunnerAndConfigurationSettings(taskExecutionInfo.getSettings(), project, GradleConstants.SYSTEM_ID);
    if (configuration == null)
        return;
    final RunnerAndConfigurationSettings existingConfiguration = runManager.findConfigurationByName(configuration.getName());
    if (existingConfiguration == null) {
        runManager.setTemporaryConfiguration(configuration);
    } else {
        runManager.setSelectedConfiguration(existingConfiguration);
    }
}
Also used : Project(com.intellij.openapi.project.Project) RunManagerEx(com.intellij.execution.RunManagerEx) ExternalTaskExecutionInfo(com.intellij.openapi.externalSystem.model.execution.ExternalTaskExecutionInfo) GradleRunTaskDialog(org.jetbrains.plugins.gradle.service.task.GradleRunTaskDialog) RunnerAndConfigurationSettings(com.intellij.execution.RunnerAndConfigurationSettings) CommandLineArgumentException(org.gradle.cli.CommandLineArgumentException) ExecuteGradleTaskHistoryService(org.jetbrains.plugins.gradle.service.task.ExecuteGradleTaskHistoryService) NotificationData(com.intellij.openapi.externalSystem.service.notification.NotificationData)

Example 2 with CommandLineArgumentException

use of org.gradle.cli.CommandLineArgumentException in project intellij-community by JetBrains.

the class GradleExecuteTaskAction method buildTaskInfo.

private static ExternalTaskExecutionInfo buildTaskInfo(@NotNull String projectPath, @NotNull String fullCommandLine) throws CommandLineArgumentException {
    CommandLineParser gradleCmdParser = new CommandLineParser();
    GradleCommandLineOptionsConverter commandLineConverter = new GradleCommandLineOptionsConverter();
    commandLineConverter.configure(gradleCmdParser);
    ParsedCommandLine parsedCommandLine = gradleCmdParser.parse(ParametersListUtil.parse(fullCommandLine, true));
    final Map<String, List<String>> optionsMap = commandLineConverter.convert(parsedCommandLine, new HashMap<>());
    final List<String> systemProperties = optionsMap.remove("system-prop");
    final String vmOptions = systemProperties == null ? "" : StringUtil.join(systemProperties, entry -> "-D" + entry, " ");
    final String scriptParameters = StringUtil.join(optionsMap.entrySet(), entry -> {
        final List<String> values = entry.getValue();
        final String longOptionName = entry.getKey();
        if (values != null && !values.isEmpty()) {
            return StringUtil.join(values, entry1 -> "--" + longOptionName + ' ' + entry1, " ");
        } else {
            return "--" + longOptionName;
        }
    }, " ");
    final List<String> tasks = parsedCommandLine.getExtraArguments();
    ExternalSystemTaskExecutionSettings settings = new ExternalSystemTaskExecutionSettings();
    settings.setExternalProjectPath(projectPath);
    settings.setTaskNames(tasks);
    settings.setScriptParameters(scriptParameters);
    settings.setVmOptions(vmOptions);
    settings.setExternalSystemIdString(GradleConstants.SYSTEM_ID.toString());
    return new ExternalTaskExecutionInfo(settings, DefaultRunExecutor.EXECUTOR_ID);
}
Also used : ExternalSystemActionUtil(com.intellij.openapi.externalSystem.action.ExternalSystemActionUtil) RunnerAndConfigurationSettings(com.intellij.execution.RunnerAndConfigurationSettings) Presentation(com.intellij.openapi.actionSystem.Presentation) NotificationCategory(com.intellij.openapi.externalSystem.service.notification.NotificationCategory) ExecuteGradleTaskHistoryService(org.jetbrains.plugins.gradle.service.task.ExecuteGradleTaskHistoryService) HashMap(java.util.HashMap) NotificationData(com.intellij.openapi.externalSystem.service.notification.NotificationData) CommandLineArgumentException(org.gradle.cli.CommandLineArgumentException) ExternalSystemAction(com.intellij.openapi.externalSystem.action.ExternalSystemAction) ExternalSystemDataKeys(com.intellij.openapi.externalSystem.model.ExternalSystemDataKeys) ExternalSystemApiUtil(com.intellij.openapi.externalSystem.util.ExternalSystemApiUtil) CommandLineParser(org.gradle.cli.CommandLineParser) Map(java.util.Map) Project(com.intellij.openapi.project.Project) RunManagerEx(com.intellij.execution.RunManagerEx) CommonDataKeys(com.intellij.openapi.actionSystem.CommonDataKeys) Module(com.intellij.openapi.module.Module) ParametersListUtil(com.intellij.util.execution.ParametersListUtil) DefaultRunExecutor(com.intellij.execution.executors.DefaultRunExecutor) ExternalSystemNotificationManager(com.intellij.openapi.externalSystem.service.notification.ExternalSystemNotificationManager) GradleRunTaskDialog(org.jetbrains.plugins.gradle.service.task.GradleRunTaskDialog) StringUtil(com.intellij.openapi.util.text.StringUtil) GradleCommandLineOptionsConverter(org.jetbrains.plugins.gradle.service.execution.cmd.GradleCommandLineOptionsConverter) ExternalSystemTaskExecutionSettings(com.intellij.openapi.externalSystem.model.execution.ExternalSystemTaskExecutionSettings) ParsedCommandLine(org.gradle.cli.ParsedCommandLine) ExternalSystemUtil(com.intellij.openapi.externalSystem.util.ExternalSystemUtil) List(java.util.List) ExternalConfigPathAware(com.intellij.openapi.externalSystem.model.project.ExternalConfigPathAware) ExternalSystemNode(com.intellij.openapi.externalSystem.view.ExternalSystemNode) NotificationSource(com.intellij.openapi.externalSystem.service.notification.NotificationSource) ExternalProjectsView(com.intellij.openapi.externalSystem.view.ExternalProjectsView) AnActionEvent(com.intellij.openapi.actionSystem.AnActionEvent) GradleConstants(org.jetbrains.plugins.gradle.util.GradleConstants) NotNull(org.jetbrains.annotations.NotNull) ExternalTaskExecutionInfo(com.intellij.openapi.externalSystem.model.execution.ExternalTaskExecutionInfo) ExternalTaskExecutionInfo(com.intellij.openapi.externalSystem.model.execution.ExternalTaskExecutionInfo) ParsedCommandLine(org.gradle.cli.ParsedCommandLine) List(java.util.List) CommandLineParser(org.gradle.cli.CommandLineParser) GradleCommandLineOptionsConverter(org.jetbrains.plugins.gradle.service.execution.cmd.GradleCommandLineOptionsConverter) ExternalSystemTaskExecutionSettings(com.intellij.openapi.externalSystem.model.execution.ExternalSystemTaskExecutionSettings)

Example 3 with CommandLineArgumentException

use of org.gradle.cli.CommandLineArgumentException in project gradle by gradle.

the class ProviderStartParameterConverter method toStartParameter.

public StartParameter toStartParameter(ProviderOperationParameters parameters, Map<String, String> properties) {
    // Important that this is constructed on the client so that it has the right gradleHomeDir and other state internally
    StartParameter startParameter = new StartParameter();
    startParameter.setProjectDir(parameters.getProjectDir());
    if (parameters.getGradleUserHomeDir() != null) {
        startParameter.setGradleUserHomeDir(parameters.getGradleUserHomeDir());
    }
    List<InternalLaunchable> launchables = parameters.getLaunchables(null);
    if (launchables != null) {
        startParameter.setTaskRequests(unpack(launchables, parameters.getProjectDir()));
    } else if (parameters.getTasks() != null) {
        startParameter.setTaskNames(parameters.getTasks());
    }
    new PropertiesToStartParameterConverter().convert(properties, startParameter);
    List<String> arguments = parameters.getArguments();
    if (arguments != null) {
        DefaultCommandLineConverter converter = new DefaultCommandLineConverter();
        try {
            converter.convert(arguments, startParameter);
        } catch (CommandLineArgumentException e) {
            throw new InternalUnsupportedBuildArgumentException("Problem with provided build arguments: " + arguments + ". " + "\n" + e.getMessage() + "\nEither it is not a valid build option or it is not supported in the target Gradle version." + "\nNot all of the Gradle command line options are supported build arguments." + "\nExamples of supported build arguments: '--info', '-u', '-p'." + "\nExamples of unsupported build options: '--daemon', '-?', '-v'." + "\nPlease find more information in the javadoc for the BuildLauncher class.", e);
        }
    }
    if (parameters.isSearchUpwards() != null) {
        startParameter.setSearchUpwards(parameters.isSearchUpwards());
    }
    if (parameters.getBuildLogLevel() != null) {
        startParameter.setLogLevel(parameters.getBuildLogLevel());
    }
    return startParameter;
}
Also used : StartParameter(org.gradle.StartParameter) DefaultCommandLineConverter(org.gradle.initialization.DefaultCommandLineConverter) PropertiesToStartParameterConverter(org.gradle.launcher.cli.converter.PropertiesToStartParameterConverter) InternalUnsupportedBuildArgumentException(org.gradle.tooling.internal.protocol.exceptions.InternalUnsupportedBuildArgumentException) CommandLineArgumentException(org.gradle.cli.CommandLineArgumentException) InternalLaunchable(org.gradle.tooling.internal.protocol.InternalLaunchable)

Example 4 with CommandLineArgumentException

use of org.gradle.cli.CommandLineArgumentException in project gradle by gradle.

the class LoggingCommandLineConverter method convert.

public LoggingConfiguration convert(ParsedCommandLine commandLine, LoggingConfiguration loggingConfiguration) throws CommandLineArgumentException {
    for (Map.Entry<String, LogLevel> entry : logLevelMap.entrySet()) {
        if (commandLine.hasOption(entry.getKey())) {
            loggingConfiguration.setLogLevel(entry.getValue());
        }
    }
    for (Map.Entry<String, ShowStacktrace> entry : showStacktraceMap.entrySet()) {
        if (commandLine.hasOption(entry.getKey())) {
            loggingConfiguration.setShowStacktrace(entry.getValue());
        }
    }
    if (commandLine.hasOption(CONSOLE)) {
        String value = commandLine.option(CONSOLE).getValue();
        String consoleValue = StringUtils.capitalize(value.toLowerCase(Locale.ENGLISH));
        try {
            ConsoleOutput consoleOutput = ConsoleOutput.valueOf(consoleValue);
            loggingConfiguration.setConsoleOutput(consoleOutput);
        } catch (IllegalArgumentException e) {
            throw new CommandLineArgumentException(String.format("Unrecognized value '%s' for %s.", value, CONSOLE));
        }
    }
    return loggingConfiguration;
}
Also used : ConsoleOutput(org.gradle.api.logging.configuration.ConsoleOutput) CommandLineArgumentException(org.gradle.cli.CommandLineArgumentException) BiMap(com.google.common.collect.BiMap) HashBiMap(com.google.common.collect.HashBiMap) Map(java.util.Map) LogLevel(org.gradle.api.logging.LogLevel) ShowStacktrace(org.gradle.api.logging.configuration.ShowStacktrace)

Example 5 with CommandLineArgumentException

use of org.gradle.cli.CommandLineArgumentException 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

CommandLineArgumentException (org.gradle.cli.CommandLineArgumentException)6 RunManagerEx (com.intellij.execution.RunManagerEx)2 RunnerAndConfigurationSettings (com.intellij.execution.RunnerAndConfigurationSettings)2 ExternalTaskExecutionInfo (com.intellij.openapi.externalSystem.model.execution.ExternalTaskExecutionInfo)2 NotificationData (com.intellij.openapi.externalSystem.service.notification.NotificationData)2 Project (com.intellij.openapi.project.Project)2 Map (java.util.Map)2 CommandLineParser (org.gradle.cli.CommandLineParser)2 ParsedCommandLine (org.gradle.cli.ParsedCommandLine)2 NotNull (org.jetbrains.annotations.NotNull)2 ExecuteGradleTaskHistoryService (org.jetbrains.plugins.gradle.service.task.ExecuteGradleTaskHistoryService)2 GradleRunTaskDialog (org.jetbrains.plugins.gradle.service.task.GradleRunTaskDialog)2 BiMap (com.google.common.collect.BiMap)1 HashBiMap (com.google.common.collect.HashBiMap)1 DefaultRunExecutor (com.intellij.execution.executors.DefaultRunExecutor)1 AnActionEvent (com.intellij.openapi.actionSystem.AnActionEvent)1 CommonDataKeys (com.intellij.openapi.actionSystem.CommonDataKeys)1 Presentation (com.intellij.openapi.actionSystem.Presentation)1 ExternalSystemAction (com.intellij.openapi.externalSystem.action.ExternalSystemAction)1 ExternalSystemActionUtil (com.intellij.openapi.externalSystem.action.ExternalSystemActionUtil)1