Search in sources :

Example 1 with ParsedCommandLine

use of org.gradle.cli.ParsedCommandLine 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 2 with ParsedCommandLine

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

the class GradleWrapperMain method main.

public static void main(String[] args) throws Exception {
    File wrapperJar = wrapperJar();
    File propertiesFile = wrapperProperties(wrapperJar);
    File rootDir = rootDir(wrapperJar);
    CommandLineParser parser = new CommandLineParser();
    parser.allowUnknownOptions();
    parser.option(GRADLE_USER_HOME_OPTION, GRADLE_USER_HOME_DETAILED_OPTION).hasArgument();
    parser.option(GRADLE_QUIET_OPTION, GRADLE_QUIET_DETAILED_OPTION);
    SystemPropertiesCommandLineConverter converter = new SystemPropertiesCommandLineConverter();
    converter.configure(parser);
    ParsedCommandLine options = parser.parse(args);
    Properties systemProperties = System.getProperties();
    systemProperties.putAll(converter.convert(options, new HashMap<String, String>()));
    File gradleUserHome = gradleUserHome(options);
    addSystemProperties(gradleUserHome, rootDir);
    Logger logger = logger(options);
    WrapperExecutor wrapperExecutor = WrapperExecutor.forWrapperPropertiesFile(propertiesFile);
    wrapperExecutor.execute(args, new Install(logger, new Download(logger, "gradlew", wrapperVersion()), new PathAssembler(gradleUserHome)), new BootstrapMainStarter());
}
Also used : HashMap(java.util.HashMap) ParsedCommandLine(org.gradle.cli.ParsedCommandLine) CommandLineParser(org.gradle.cli.CommandLineParser) Properties(java.util.Properties) File(java.io.File) SystemPropertiesCommandLineConverter(org.gradle.cli.SystemPropertiesCommandLineConverter)

Example 3 with ParsedCommandLine

use of org.gradle.cli.ParsedCommandLine in project liferay-ide by liferay.

the class AbstractLiferayGradleTaskAction method _buildTaskExecutionInfo.

private ExternalTaskExecutionInfo _buildTaskExecutionInfo(Project project, @NotNull String projectPath, @NotNull String fullCommandLine) {
    CommandLineParser gradleCmdParser = new CommandLineParser();
    GradleCommandLineOptionsConverter commandLineConverter = new GradleCommandLineOptionsConverter();
    commandLineConverter.configure(gradleCmdParser);
    ParsedCommandLine parsedCommandLine = gradleCmdParser.parse(ParametersListUtil.parse(fullCommandLine, true));
    try {
        Map<String, List<String>> optionsMap = commandLineConverter.convert(parsedCommandLine, new HashMap<>());
        List<String> systemProperties = optionsMap.remove("system-prop");
        String vmOptions = systemProperties == null ? "" : StringUtil.join(systemProperties, entry -> "-D" + entry, " ");
        String scriptParameters = StringUtil.join(optionsMap.entrySet(), entry -> {
            List<String> values = entry.getValue();
            String longOptionName = entry.getKey();
            if ((values != null) && !values.isEmpty()) {
                return StringUtil.join(values, entry1 -> "--" + longOptionName + ' ' + entry1, " ");
            } else {
                return "--" + longOptionName;
            }
        }, " ");
        ExternalSystemTaskExecutionSettings settings = new ExternalSystemTaskExecutionSettings();
        settings.setExternalProjectPath(projectPath);
        settings.setExternalSystemIdString(GradleConstants.SYSTEM_ID.toString());
        settings.setScriptParameters(scriptParameters);
        settings.setTaskNames(parsedCommandLine.getExtraArguments());
        settings.setVmOptions(vmOptions);
        return new ExternalTaskExecutionInfo(settings, DefaultRunExecutor.EXECUTOR_ID);
    } catch (CommandLineArgumentException clae) {
        NotificationData notificationData = new NotificationData("<b>Command-line arguments cannot be parsed</b>", "<i>" + _taskName + "</i> \n" + clae.getMessage(), NotificationCategory.WARNING, NotificationSource.TASK_EXECUTION);
        notificationData.setBalloonNotification(true);
        ExternalSystemNotificationManager externalSystemNotificationManager = ExternalSystemNotificationManager.getInstance(project);
        externalSystemNotificationManager.showNotification(GradleConstants.SYSTEM_ID, notificationData);
        return null;
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) RunnerAndConfigurationSettings(com.intellij.execution.RunnerAndConfigurationSettings) Presentation(com.intellij.openapi.actionSystem.Presentation) NotificationCategory(com.intellij.openapi.externalSystem.service.notification.NotificationCategory) HashMap(java.util.HashMap) NotificationData(com.intellij.openapi.externalSystem.service.notification.NotificationData) CommandLineArgumentException(org.gradle.cli.CommandLineArgumentException) CommandLineParser(org.gradle.cli.CommandLineParser) Map(java.util.Map) RunManager(com.intellij.execution.RunManager) Project(com.intellij.openapi.project.Project) CommonDataKeys(com.intellij.openapi.actionSystem.CommonDataKeys) ParametersListUtil(com.intellij.util.execution.ParametersListUtil) DefaultRunExecutor(com.intellij.execution.executors.DefaultRunExecutor) ExternalSystemNotificationManager(com.intellij.openapi.externalSystem.service.notification.ExternalSystemNotificationManager) StringUtil(com.intellij.openapi.util.text.StringUtil) GradleCommandLineOptionsConverter(org.jetbrains.plugins.gradle.service.execution.cmd.GradleCommandLineOptionsConverter) ExternalSystemTaskExecutionSettings(com.intellij.openapi.externalSystem.model.execution.ExternalSystemTaskExecutionSettings) AnAction(com.intellij.openapi.actionSystem.AnAction) Icon(javax.swing.Icon) ParsedCommandLine(org.gradle.cli.ParsedCommandLine) ExternalSystemUtil(com.intellij.openapi.externalSystem.util.ExternalSystemUtil) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) CoreUtil(com.liferay.ide.idea.util.CoreUtil) NotificationSource(com.intellij.openapi.externalSystem.service.notification.NotificationSource) 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) GradleCommandLineOptionsConverter(org.jetbrains.plugins.gradle.service.execution.cmd.GradleCommandLineOptionsConverter) ParsedCommandLine(org.gradle.cli.ParsedCommandLine) List(java.util.List) CommandLineParser(org.gradle.cli.CommandLineParser) CommandLineArgumentException(org.gradle.cli.CommandLineArgumentException) ExternalSystemNotificationManager(com.intellij.openapi.externalSystem.service.notification.ExternalSystemNotificationManager) ExternalSystemTaskExecutionSettings(com.intellij.openapi.externalSystem.model.execution.ExternalSystemTaskExecutionSettings) NotificationData(com.intellij.openapi.externalSystem.service.notification.NotificationData)

Example 4 with ParsedCommandLine

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

Example 5 with ParsedCommandLine

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

the class BuildLogLevelMixIn method getBuildLogLevel.

public LogLevel getBuildLogLevel() {
    LoggingCommandLineConverter converter = new LoggingCommandLineConverter();
    CommandLineParser parser = new CommandLineParser().allowUnknownOptions().allowMixedSubcommandsAndOptions();
    converter.configure(parser);
    List<String> arguments = parameters.getArguments();
    ParsedCommandLine parsedCommandLine = parser.parse(arguments == null ? Collections.<String>emptyList() : arguments);
    // configure verbosely only if arguments do not specify any log level.
    if (parameters.getVerboseLogging() && !parsedCommandLine.hasAnyOption(converter.getLogLevelOptions())) {
        return LogLevel.DEBUG;
    }
    LoggingConfiguration loggingConfiguration = converter.convert(parsedCommandLine, new DefaultLoggingConfiguration());
    return loggingConfiguration.getLogLevel();
}
Also used : DefaultLoggingConfiguration(org.gradle.internal.logging.DefaultLoggingConfiguration) LoggingConfiguration(org.gradle.api.logging.configuration.LoggingConfiguration) DefaultLoggingConfiguration(org.gradle.internal.logging.DefaultLoggingConfiguration) LoggingCommandLineConverter(org.gradle.internal.logging.LoggingCommandLineConverter) ParsedCommandLine(org.gradle.cli.ParsedCommandLine) CommandLineParser(org.gradle.cli.CommandLineParser)

Aggregations

CommandLineParser (org.gradle.cli.CommandLineParser)5 ParsedCommandLine (org.gradle.cli.ParsedCommandLine)5 HashMap (java.util.HashMap)3 CommandLineArgumentException (org.gradle.cli.CommandLineArgumentException)3 RunnerAndConfigurationSettings (com.intellij.execution.RunnerAndConfigurationSettings)2 DefaultRunExecutor (com.intellij.execution.executors.DefaultRunExecutor)2 AnActionEvent (com.intellij.openapi.actionSystem.AnActionEvent)2 CommonDataKeys (com.intellij.openapi.actionSystem.CommonDataKeys)2 Presentation (com.intellij.openapi.actionSystem.Presentation)2 ExternalSystemTaskExecutionSettings (com.intellij.openapi.externalSystem.model.execution.ExternalSystemTaskExecutionSettings)2 ExternalTaskExecutionInfo (com.intellij.openapi.externalSystem.model.execution.ExternalTaskExecutionInfo)2 ExternalSystemNotificationManager (com.intellij.openapi.externalSystem.service.notification.ExternalSystemNotificationManager)2 NotificationCategory (com.intellij.openapi.externalSystem.service.notification.NotificationCategory)2 NotificationData (com.intellij.openapi.externalSystem.service.notification.NotificationData)2 NotificationSource (com.intellij.openapi.externalSystem.service.notification.NotificationSource)2 ExternalSystemUtil (com.intellij.openapi.externalSystem.util.ExternalSystemUtil)2 Project (com.intellij.openapi.project.Project)2 StringUtil (com.intellij.openapi.util.text.StringUtil)2 ParametersListUtil (com.intellij.util.execution.ParametersListUtil)2 List (java.util.List)2