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);
}
}
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);
}
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;
}
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;
}
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;
}
Aggregations