1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

codan: refactored external tool invoke to be more useful

Change-Id: Ide621e5541febe8fda784605ef4d5e23476669d4
Signed-off-by: Alena Laskavaia <elaskavaia.cdt@gmail.com>
This commit is contained in:
Alena Laskavaia 2015-03-04 15:14:16 -05:00 committed by Elena Laskavaia
parent f3457c7a55
commit 7ecd20efa9
2 changed files with 77 additions and 50 deletions

View file

@ -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;
}
}

View file

@ -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()));
}
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);
}
/* 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 {
p.getOutputStream().close();
} catch (Throwable ignored) {}
try {
launcher.waitAndRead(sniffer.getOutputStream(), sniffer.getErrorStream(), NULL_PROGRESS_MONITOR);
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 {
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;
}
private void shutDown(IConsoleParser[] parsers) {
for (IConsoleParser parser : parsers) {
parser.shutdown();
// 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();
}
}
}