1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
/*******************************************************************************
* Copyright (c) 2012 Google, Inc and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Alex Ruiz (Google) - initial API and implementation
*******************************************************************************/
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;
import org.eclipse.cdt.codan.core.cxx.externaltool.InvocationParameters;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.CommandLauncher;
import org.eclipse.cdt.core.ICommandLauncher;
import org.eclipse.cdt.core.IConsoleParser;
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 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
* 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 Throwable if something else goes wrong.
*/
public void invoke(InvocationParameters parameters, ConfigurationSettings settings, ArgsSeparator argsSeparator,
IConsoleParser[] parsers) throws InvocationFailure, Throwable {
Command command = commandBuilder.buildCommand(parameters, settings, argsSeparator);
launchCommand(command, parsers, parameters, settings);
}
private void launchCommand(Command command, IConsoleParser[] parsers, InvocationParameters parameters, ConfigurationSettings settings)
throws InvocationFailure, CoreException {
IProject project = parameters.getActualFile().getProject();
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 {
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();
}
}
}
|