mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-07-20 07:25:23 +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:
parent
f3457c7a55
commit
7ecd20efa9
2 changed files with 77 additions and 50 deletions
|
@ -18,10 +18,16 @@ import org.eclipse.core.runtime.IPath;
|
||||||
class Command {
|
class Command {
|
||||||
private final IPath path;
|
private final IPath path;
|
||||||
private final String[] args;
|
private final String[] args;
|
||||||
|
private final String[] env;
|
||||||
|
|
||||||
Command(IPath path, String[] args) {
|
Command(IPath path, String[] args) {
|
||||||
|
this(path, args, new String[] {});
|
||||||
|
}
|
||||||
|
|
||||||
|
Command(IPath path, String[] args, String[] env) {
|
||||||
this.path = path;
|
this.path = path;
|
||||||
this.args = args;
|
this.args = args;
|
||||||
|
this.env = env;
|
||||||
}
|
}
|
||||||
|
|
||||||
IPath getPath() {
|
IPath getPath() {
|
||||||
|
@ -31,4 +37,8 @@ class Command {
|
||||||
String[] getArgs() {
|
String[] getArgs() {
|
||||||
return args;
|
return args;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String[] getEnv() {
|
||||||
|
return env;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,6 +10,9 @@
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
package org.eclipse.cdt.codan.core.cxx.internal.externaltool;
|
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.ArgsSeparator;
|
||||||
import org.eclipse.cdt.codan.core.cxx.externaltool.ConfigurationSettings;
|
import org.eclipse.cdt.codan.core.cxx.externaltool.ConfigurationSettings;
|
||||||
import org.eclipse.cdt.codan.core.cxx.externaltool.InvocationFailure;
|
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.cdt.internal.core.ConsoleOutputSniffer;
|
||||||
import org.eclipse.core.resources.IProject;
|
import org.eclipse.core.resources.IProject;
|
||||||
import org.eclipse.core.runtime.CoreException;
|
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.NullProgressMonitor;
|
||||||
|
import org.eclipse.core.runtime.SubProgressMonitor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Invokes an external tool to perform checks on a single file.
|
* Invokes an external tool to perform checks on a single file.
|
||||||
*/
|
*/
|
||||||
public class ExternalToolInvoker {
|
public class ExternalToolInvoker {
|
||||||
private static final String DEFAULT_CONTEXT_MENU_ID = "org.eclipse.cdt.ui.CDTBuildConsole"; //$NON-NLS-1$
|
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 static final NullProgressMonitor NULL_PROGRESS_MONITOR = new NullProgressMonitor();
|
||||||
|
|
||||||
private final CommandBuilder commandBuilder = new CommandBuilder();
|
private final CommandBuilder commandBuilder = new CommandBuilder();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Invokes an external tool.
|
* Invokes an external tool.
|
||||||
|
*
|
||||||
* @param parameters the parameters to pass to the external tool executable.
|
* @param parameters the parameters to pass to the external tool executable.
|
||||||
* @param settings user-configurable settings.
|
* @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}.
|
* arguments are stored in a single {@code String}.
|
||||||
* @param parsers parse the output of the external tool.
|
* @param parsers parse the output of the external tool.
|
||||||
* @throws InvocationFailure if the external tool could not be invoked or if the external tool
|
* @throws InvocationFailure if the external tool could not be invoked or if
|
||||||
* itself reports that it cannot be executed (e.g. due to a configuration error).
|
* the external tool
|
||||||
|
* itself reports that it cannot be executed (e.g. due to a
|
||||||
|
* configuration error).
|
||||||
* @throws Throwable if something else goes wrong.
|
* @throws Throwable if something else goes wrong.
|
||||||
*/
|
*/
|
||||||
public void invoke(InvocationParameters parameters, ConfigurationSettings settings,
|
public void invoke(InvocationParameters parameters, ConfigurationSettings settings, ArgsSeparator argsSeparator,
|
||||||
ArgsSeparator argsSeparator, IConsoleParser[] parsers)
|
IConsoleParser[] parsers) throws InvocationFailure, Throwable {
|
||||||
throws InvocationFailure, Throwable {
|
|
||||||
Command command = commandBuilder.buildCommand(parameters, settings, argsSeparator);
|
Command command = commandBuilder.buildCommand(parameters, settings, argsSeparator);
|
||||||
try {
|
|
||||||
launchCommand(command, parsers, parameters, settings);
|
launchCommand(command, parsers, parameters, settings);
|
||||||
} finally {
|
|
||||||
shutDown(parsers);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void launchCommand(Command command, IConsoleParser[] parsers,
|
private void launchCommand(Command command, IConsoleParser[] parsers, InvocationParameters parameters, ConfigurationSettings settings)
|
||||||
InvocationParameters parameters, ConfigurationSettings settings)
|
|
||||||
throws InvocationFailure, CoreException {
|
throws InvocationFailure, CoreException {
|
||||||
IProject project = parameters.getActualFile().getProject();
|
IProject project = parameters.getActualFile().getProject();
|
||||||
IConsole c = startConsole(project, settings.getExternalToolName());
|
final String toolName = settings.getExternalToolName();
|
||||||
ConsoleOutputSniffer sniffer =
|
final IPath workingDirectory = parameters.getWorkingDirectory();
|
||||||
new ConsoleOutputSniffer(c.getOutputStream(), c.getErrorStream(), parsers);
|
final IPath commandPath = command.getPath();
|
||||||
ICommandLauncher launcher = commandLauncher(project);
|
final String[] commandArgs = command.getArgs();
|
||||||
Process p = launcher.execute(command.getPath(), command.getArgs(), ENV,
|
final String[] commandEnv = command.getEnv();
|
||||||
parameters.getWorkingDirectory(), NULL_PROGRESS_MONITOR);
|
launchOnBuildConsole(project, parsers, toolName, commandPath, commandArgs, commandEnv, workingDirectory, 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) {
|
/* TODO: extract this in some sort of public API call
|
||||||
IConsole console = CCorePlugin.getDefault().getConsole(null, DEFAULT_CONTEXT_MENU_ID, externalToolName, null);
|
*/
|
||||||
console.start(project);
|
public void launchOnBuildConsole(IProject project, IConsoleParser[] parsers, final String toolName, final IPath commandPath,
|
||||||
return console;
|
final String[] commandArgs, final String[] commandEnv, final IPath workingDirectory, final IProgressMonitor monitor)
|
||||||
}
|
throws CoreException, InvocationFailure {
|
||||||
|
monitor.beginTask("Launching " + toolName, 100);
|
||||||
private ICommandLauncher commandLauncher(IProject project) {
|
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();
|
ICommandLauncher launcher = new CommandLauncher();
|
||||||
launcher.showCommand(true);
|
launcher.showCommand(true);
|
||||||
launcher.setProject(project);
|
launcher.setProject(project);
|
||||||
return launcher;
|
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 {
|
||||||
private void shutDown(IConsoleParser[] parsers) {
|
// this is process input stream which we don't need
|
||||||
for (IConsoleParser parser : parsers) {
|
p.getOutputStream().close();
|
||||||
parser.shutdown();
|
} 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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue