Skip to main content
summaryrefslogtreecommitdiffstats
path: root/codan
diff options
context:
space:
mode:
authorAlena Laskavaia2015-03-04 15:14:16 -0500
committerElena Laskavaia2015-03-05 15:18:31 -0500
commit7ecd20efa9233a7aeb53b5096ef4a4e77c55af67 (patch)
tree075e1885926d42eece70c923319314fdf380b326 /codan
parentf3457c7a55b271335fd5ad491d2ecbcd16890c2f (diff)
downloadorg.eclipse.cdt-7ecd20efa9233a7aeb53b5096ef4a4e77c55af67.tar.gz
org.eclipse.cdt-7ecd20efa9233a7aeb53b5096ef4a4e77c55af67.tar.xz
org.eclipse.cdt-7ecd20efa9233a7aeb53b5096ef4a4e77c55af67.zip
codan: refactored external tool invoke to be more useful
Change-Id: Ide621e5541febe8fda784605ef4d5e23476669d4 Signed-off-by: Alena Laskavaia <elaskavaia.cdt@gmail.com>
Diffstat (limited to 'codan')
-rw-r--r--codan/org.eclipse.cdt.codan.core.cxx/src/org/eclipse/cdt/codan/core/cxx/internal/externaltool/Command.java10
-rw-r--r--codan/org.eclipse.cdt.codan.core.cxx/src/org/eclipse/cdt/codan/core/cxx/internal/externaltool/ExternalToolInvoker.java117
2 files changed, 77 insertions, 50 deletions
diff --git a/codan/org.eclipse.cdt.codan.core.cxx/src/org/eclipse/cdt/codan/core/cxx/internal/externaltool/Command.java b/codan/org.eclipse.cdt.codan.core.cxx/src/org/eclipse/cdt/codan/core/cxx/internal/externaltool/Command.java
index d5e9ef8d86..0ca2b04094 100644
--- a/codan/org.eclipse.cdt.codan.core.cxx/src/org/eclipse/cdt/codan/core/cxx/internal/externaltool/Command.java
+++ b/codan/org.eclipse.cdt.codan.core.cxx/src/org/eclipse/cdt/codan/core/cxx/internal/externaltool/Command.java
@@ -18,10 +18,16 @@ import org.eclipse.core.runtime.IPath;
class Command {
private final IPath path;
private final String[] args;
+ private final String[] env;
Command(IPath path, String[] args) {
+ this(path, args, new String[] {});
+ }
+
+ Command(IPath path, String[] args, String[] env) {
this.path = path;
this.args = args;
+ this.env = env;
}
IPath getPath() {
@@ -31,4 +37,8 @@ class Command {
String[] getArgs() {
return args;
}
+
+ String[] getEnv() {
+ return env;
+ }
}
diff --git a/codan/org.eclipse.cdt.codan.core.cxx/src/org/eclipse/cdt/codan/core/cxx/internal/externaltool/ExternalToolInvoker.java b/codan/org.eclipse.cdt.codan.core.cxx/src/org/eclipse/cdt/codan/core/cxx/internal/externaltool/ExternalToolInvoker.java
index 14b97cc9ed..bfc7156caf 100644
--- a/codan/org.eclipse.cdt.codan.core.cxx/src/org/eclipse/cdt/codan/core/cxx/internal/externaltool/ExternalToolInvoker.java
+++ b/codan/org.eclipse.cdt.codan.core.cxx/src/org/eclipse/cdt/codan/core/cxx/internal/externaltool/ExternalToolInvoker.java
@@ -10,6 +10,9 @@
*******************************************************************************/
package org.eclipse.cdt.codan.core.cxx.internal.externaltool;
+import java.io.IOException;
+import java.io.OutputStream;
+
import org.eclipse.cdt.codan.core.cxx.externaltool.ArgsSeparator;
import org.eclipse.cdt.codan.core.cxx.externaltool.ConfigurationSettings;
import org.eclipse.cdt.codan.core.cxx.externaltool.InvocationFailure;
@@ -22,80 +25,94 @@ import org.eclipse.cdt.core.resources.IConsole;
import org.eclipse.cdt.internal.core.ConsoleOutputSniffer;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.core.runtime.SubProgressMonitor;
/**
* Invokes an external tool to perform checks on a single file.
*/
public class ExternalToolInvoker {
private static final String DEFAULT_CONTEXT_MENU_ID = "org.eclipse.cdt.ui.CDTBuildConsole"; //$NON-NLS-1$
- private static final String[] ENV = {};
private static final NullProgressMonitor NULL_PROGRESS_MONITOR = new NullProgressMonitor();
-
private final CommandBuilder commandBuilder = new CommandBuilder();
/**
* Invokes an external tool.
+ *
* @param parameters the parameters to pass to the external tool executable.
* @param settings user-configurable settings.
- * @param argsSeparator separates the arguments to pass to the external tool executable. These
+ * @param argsSeparator separates the arguments to pass to the external tool
+ * executable. These
* arguments are stored in a single {@code String}.
* @param parsers parse the output of the external tool.
- * @throws InvocationFailure if the external tool could not be invoked or if the external tool
- * itself reports that it cannot be executed (e.g. due to a configuration error).
+ * @throws InvocationFailure if the external tool could not be invoked or if
+ * the external tool
+ * itself reports that it cannot be executed (e.g. due to a
+ * configuration error).
* @throws Throwable if something else goes wrong.
*/
- public void invoke(InvocationParameters parameters, ConfigurationSettings settings,
- ArgsSeparator argsSeparator, IConsoleParser[] parsers)
- throws InvocationFailure, Throwable {
+ public void invoke(InvocationParameters parameters, ConfigurationSettings settings, ArgsSeparator argsSeparator,
+ IConsoleParser[] parsers) throws InvocationFailure, Throwable {
Command command = commandBuilder.buildCommand(parameters, settings, argsSeparator);
- try {
- launchCommand(command, parsers, parameters, settings);
- } finally {
- shutDown(parsers);
- }
+ launchCommand(command, parsers, parameters, settings);
}
- private void launchCommand(Command command, IConsoleParser[] parsers,
- InvocationParameters parameters, ConfigurationSettings settings)
- throws InvocationFailure, CoreException {
+ private void launchCommand(Command command, IConsoleParser[] parsers, InvocationParameters parameters, ConfigurationSettings settings)
+ throws InvocationFailure, CoreException {
IProject project = parameters.getActualFile().getProject();
- IConsole c = startConsole(project, settings.getExternalToolName());
- ConsoleOutputSniffer sniffer =
- new ConsoleOutputSniffer(c.getOutputStream(), c.getErrorStream(), parsers);
- ICommandLauncher launcher = commandLauncher(project);
- Process p = launcher.execute(command.getPath(), command.getArgs(), ENV,
- parameters.getWorkingDirectory(), NULL_PROGRESS_MONITOR);
- if (p == null) {
- String format = "Unable to launch external tool '%s'. Cause unknown."; //$NON-NLS-1$
- throw new InvocationFailure(String.format(format, settings.getExternalToolName()));
- }
- try {
- p.getOutputStream().close();
- } catch (Throwable ignored) {}
- try {
- launcher.waitAndRead(sniffer.getOutputStream(), sniffer.getErrorStream(), NULL_PROGRESS_MONITOR);
- } finally {
- p.destroy();
- }
- }
-
- private IConsole startConsole(IProject project, String externalToolName) {
- IConsole console = CCorePlugin.getDefault().getConsole(null, DEFAULT_CONTEXT_MENU_ID, externalToolName, null);
- console.start(project);
- return console;
- }
-
- private ICommandLauncher commandLauncher(IProject project) {
- ICommandLauncher launcher = new CommandLauncher();
- launcher.showCommand(true);
- launcher.setProject(project);
- return launcher;
+ final String toolName = settings.getExternalToolName();
+ final IPath workingDirectory = parameters.getWorkingDirectory();
+ final IPath commandPath = command.getPath();
+ final String[] commandArgs = command.getArgs();
+ final String[] commandEnv = command.getEnv();
+ launchOnBuildConsole(project, parsers, toolName, commandPath, commandArgs, commandEnv, workingDirectory, NULL_PROGRESS_MONITOR);
}
- private void shutDown(IConsoleParser[] parsers) {
- for (IConsoleParser parser : parsers) {
- parser.shutdown();
+ /* TODO: extract this in some sort of public API call
+ */
+ public void launchOnBuildConsole(IProject project, IConsoleParser[] parsers, final String toolName, final IPath commandPath,
+ final String[] commandArgs, final String[] commandEnv, final IPath workingDirectory, final IProgressMonitor monitor)
+ throws CoreException, InvocationFailure {
+ monitor.beginTask("Launching " + toolName, 100);
+ IConsole c = CCorePlugin.getDefault().getConsole(null, DEFAULT_CONTEXT_MENU_ID, toolName, null);
+ ConsoleOutputSniffer sniffer = new ConsoleOutputSniffer(c.getOutputStream(), c.getErrorStream(), parsers);
+ final OutputStream out = sniffer.getOutputStream();
+ final OutputStream err = sniffer.getErrorStream();
+ try {
+ ICommandLauncher launcher = new CommandLauncher();
+ launcher.showCommand(true);
+ launcher.setProject(project);
+ Process p = launcher.execute(commandPath, commandArgs, commandEnv, workingDirectory, new SubProgressMonitor(monitor, 50));
+ if (p == null) {
+ String format = "Unable to launch external tool '%s': %s"; //$NON-NLS-1$
+ throw new InvocationFailure(String.format(format, commandPath, launcher.getErrorMessage()));
+ }
+ try {
+ // this is process input stream which we don't need
+ p.getOutputStream().close();
+ } catch (Throwable ignored) {
+ // ignore
+ }
+ try {
+ launcher.waitAndRead(out, err, new SubProgressMonitor(monitor, 50));
+ } finally {
+ p.destroy();
+ }
+ } finally {
+ // closing sniffer's streams will shut down the parsers as well
+ try {
+ out.close();
+ } catch (IOException e) {
+ // ignore
+ }
+ try {
+ err.close();
+ } catch (IOException e) {
+ // ignore
+ }
+ monitor.done();
}
}
}

Back to the top