Search in sources :

Example 1 with CommandLineParser

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

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

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

Example 4 with CommandLineParser

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

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

the class InProcessGradleExecuter method executeBuild.

private BuildResult executeBuild(GradleInvocation invocation, StandardOutputListener outputListener, StandardOutputListener errorListener, BuildListenerImpl listener) {
    // Augment the environment for the execution
    System.setIn(connectStdIn());
    processEnvironment.maybeSetProcessDir(getWorkingDir());
    for (Map.Entry<String, String> entry : invocation.environmentVars.entrySet()) {
        processEnvironment.maybeSetEnvironmentVariable(entry.getKey(), entry.getValue());
    }
    Map<String, String> implicitJvmSystemProperties = getImplicitJvmSystemProperties();
    System.getProperties().putAll(implicitJvmSystemProperties);
    resetTempDirLocation();
    // TODO: Fix tests that rely on this being set before we process arguments like this...
    StartParameter startParameter = new StartParameter();
    startParameter.setCurrentDir(getWorkingDir());
    startParameter.setShowStacktrace(ShowStacktrace.ALWAYS);
    CommandLineParser parser = new CommandLineParser();
    ParametersConverter parametersConverter = new ParametersConverter();
    parametersConverter.configure(parser);
    final Parameters parameters = new Parameters(startParameter);
    parametersConverter.convert(parser.parse(getAllArgs()), parameters);
    BuildActionExecuter<BuildActionParameters> actionExecuter = GLOBAL_SERVICES.get(BuildActionExecuter.class);
    ListenerManager listenerManager = GLOBAL_SERVICES.get(ListenerManager.class);
    listenerManager.addListener(listener);
    try {
        // TODO: Reuse more of BuildActionsFactory
        BuildAction action = new ExecuteBuildAction(startParameter);
        BuildActionParameters buildActionParameters = createBuildActionParameters(startParameter);
        BuildRequestContext buildRequestContext = createBuildRequestContext(outputListener, errorListener);
        startMeasurement();
        actionExecuter.execute(action, buildRequestContext, buildActionParameters, GLOBAL_SERVICES);
        return new BuildResult(null, null);
    } catch (ReportedException e) {
        return new BuildResult(null, e.getCause());
    } finally {
        stopMeasurement();
        listenerManager.removeListener(listener);
    }
}
Also used : StartParameter(org.gradle.StartParameter) Parameters(org.gradle.launcher.cli.Parameters) DefaultBuildActionParameters(org.gradle.launcher.exec.DefaultBuildActionParameters) BuildActionParameters(org.gradle.launcher.exec.BuildActionParameters) DefaultBuildActionParameters(org.gradle.launcher.exec.DefaultBuildActionParameters) BuildActionParameters(org.gradle.launcher.exec.BuildActionParameters) ExecuteBuildAction(org.gradle.launcher.cli.ExecuteBuildAction) DefaultBuildRequestContext(org.gradle.initialization.DefaultBuildRequestContext) BuildRequestContext(org.gradle.initialization.BuildRequestContext) BuildResult(org.gradle.BuildResult) ParametersConverter(org.gradle.launcher.cli.ParametersConverter) CommandLineParser(org.gradle.cli.CommandLineParser) Map(java.util.Map) HashMap(java.util.HashMap) ExecuteBuildAction(org.gradle.launcher.cli.ExecuteBuildAction) BuildAction(org.gradle.internal.invocation.BuildAction) ListenerManager(org.gradle.internal.event.ListenerManager) ReportedException(org.gradle.initialization.ReportedException)

Aggregations

CommandLineParser (org.gradle.cli.CommandLineParser)5 ParsedCommandLine (org.gradle.cli.ParsedCommandLine)4 HashMap (java.util.HashMap)3 Map (java.util.Map)2 CommandLineArgumentException (org.gradle.cli.CommandLineArgumentException)2 RunManagerEx (com.intellij.execution.RunManagerEx)1 RunnerAndConfigurationSettings (com.intellij.execution.RunnerAndConfigurationSettings)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 ExternalSystemDataKeys (com.intellij.openapi.externalSystem.model.ExternalSystemDataKeys)1 ExternalSystemTaskExecutionSettings (com.intellij.openapi.externalSystem.model.execution.ExternalSystemTaskExecutionSettings)1 ExternalTaskExecutionInfo (com.intellij.openapi.externalSystem.model.execution.ExternalTaskExecutionInfo)1 ExternalConfigPathAware (com.intellij.openapi.externalSystem.model.project.ExternalConfigPathAware)1 ExternalSystemNotificationManager (com.intellij.openapi.externalSystem.service.notification.ExternalSystemNotificationManager)1 NotificationCategory (com.intellij.openapi.externalSystem.service.notification.NotificationCategory)1 NotificationData (com.intellij.openapi.externalSystem.service.notification.NotificationData)1