Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Ruiz2012-02-23 15:25:05 -0500
committerSergey Prigogin2012-02-23 15:25:05 -0500
commit3115bb4e228e7d1903aaba91178d7ec9742c190e (patch)
tree9a4c4a8c743dc5769b8b1408a6f3656873d29593 /codan/org.eclipse.cdt.codan.ui
parentac7ee03d2515bbfb234d5022db341dc07bd3627c (diff)
downloadorg.eclipse.cdt-3115bb4e228e7d1903aaba91178d7ec9742c190e.tar.gz
org.eclipse.cdt-3115bb4e228e7d1903aaba91178d7ec9742c190e.tar.xz
org.eclipse.cdt-3115bb4e228e7d1903aaba91178d7ec9742c190e.zip
Initial take on external-tool-based checkers.
Diffstat (limited to 'codan/org.eclipse.cdt.codan.ui')
-rw-r--r--codan/org.eclipse.cdt.codan.ui/META-INF/MANIFEST.MF4
-rw-r--r--codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/internal/ui/CodanUIActivator.java14
-rw-r--r--codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/ui/CodanEditorUtility.java5
-rw-r--r--codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/ui/externaltool/CommandInvoker.java113
-rw-r--r--codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/ui/externaltool/ConsolePrinter.java54
-rw-r--r--codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/ui/externaltool/ConsolePrinterImpl.java81
6 files changed, 267 insertions, 4 deletions
diff --git a/codan/org.eclipse.cdt.codan.ui/META-INF/MANIFEST.MF b/codan/org.eclipse.cdt.codan.ui/META-INF/MANIFEST.MF
index af09ae68a7..f915a5f0dd 100644
--- a/codan/org.eclipse.cdt.codan.ui/META-INF/MANIFEST.MF
+++ b/codan/org.eclipse.cdt.codan.ui/META-INF/MANIFEST.MF
@@ -13,7 +13,8 @@ Require-Bundle: org.eclipse.ui,
org.eclipse.jface.text,
org.eclipse.ui.ide,
org.eclipse.cdt.ui,
- org.eclipse.core.filesystem
+ org.eclipse.core.filesystem,
+ org.eclipse.ui.console;bundle-version="3.5.100"
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Bundle-ActivationPolicy: lazy
Export-Package: org.eclipse.cdt.codan.internal.ui;x-friends:="org.eclipse.cdt.codan.ui.cxx",
@@ -23,5 +24,6 @@ Export-Package: org.eclipse.cdt.codan.internal.ui;x-friends:="org.eclipse.cdt.co
org.eclipse.cdt.codan.internal.ui.views;x-internal:=true,
org.eclipse.cdt.codan.internal.ui.widgets;x-internal:=true,
org.eclipse.cdt.codan.ui,
+ org.eclipse.cdt.codan.ui.externaltool,
org.eclipse.cdt.codan.ui.handlers
diff --git a/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/internal/ui/CodanUIActivator.java b/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/internal/ui/CodanUIActivator.java
index fcd4c9049b..20800ee401 100644
--- a/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/internal/ui/CodanUIActivator.java
+++ b/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/internal/ui/CodanUIActivator.java
@@ -83,7 +83,7 @@ public class CodanUIActivator extends AbstractUIPlugin {
}
/**
- * Logs an internal error with the specified throwable
+ * Logs an internal error with the specified {@code Throwable}.
*
* @param e
* the exception to be logged
@@ -103,6 +103,18 @@ public class CodanUIActivator extends AbstractUIPlugin {
}
/**
+ * Logs an internal error with the specified message and {@code Throwable}.
+ *
+ * @param message
+ * the error message to log
+ * @param e
+ * the exception to be logged
+ */
+ public static void log(String message, Throwable e) {
+ log(new Status(IStatus.ERROR, PLUGIN_ID, 1, message, e));
+ }
+
+ /**
* @return
*/
public IPreferenceStore getCorePreferenceStore() {
diff --git a/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/ui/CodanEditorUtility.java b/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/ui/CodanEditorUtility.java
index 8c5c40e8c5..7cb3830985 100644
--- a/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/ui/CodanEditorUtility.java
+++ b/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/ui/CodanEditorUtility.java
@@ -158,9 +158,10 @@ public class CodanEditorUtility {
}
/**
- * @return
+ * Returns the active workbench page.
+ * @return the active workbench page, or {@code null} if none can be found.
*/
- private static IWorkbenchPage getActivePage() {
+ public static IWorkbenchPage getActivePage() {
IWorkbenchWindow activeWorkbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
if (activeWorkbenchWindow == null)
return null;
diff --git a/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/ui/externaltool/CommandInvoker.java b/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/ui/externaltool/CommandInvoker.java
new file mode 100644
index 0000000000..5140080c4b
--- /dev/null
+++ b/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/ui/externaltool/CommandInvoker.java
@@ -0,0 +1,113 @@
+/*******************************************************************************
+ * Copyright (c) 2012 Google, Inc.
+ * 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 - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.cdt.codan.ui.externaltool;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.util.List;
+
+import org.eclipse.cdt.codan.core.externaltool.ICommandInvoker;
+import org.eclipse.cdt.codan.core.externaltool.IOutputParser;
+import org.eclipse.cdt.codan.core.externaltool.InvocationFailure;
+import org.eclipse.cdt.codan.internal.ui.CodanUIActivator;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.ui.PartInitException;
+
+/**
+ * Invokes an external tool command.
+ *
+ * @author alruiz@google.com (Alex Ruiz)
+ */
+public class CommandInvoker implements ICommandInvoker {
+ private static final String[] ENVIRONMENT_VARIABLE_SETTINGS = {};
+
+ @Override
+ public void buildAndLaunchCommand(IProject project, String externalToolName,
+ IPath executablePath, String[] args, IPath workingDirectory, boolean shouldDisplayOutput,
+ List<IOutputParser> parsers) throws InvocationFailure, Throwable {
+ ConsolePrinter consolePrinter = consolePrinter(externalToolName, shouldDisplayOutput);
+ String command = buildCommand(executablePath, args);
+ Process process = null;
+ try {
+ consolePrinter.clear();
+ consolePrinter.println(command);
+ consolePrinter.println();
+ try {
+ process = invoke(command, workingDirectory);
+ } catch (IOException e) {
+ throw new InvocationFailure("Unable to start " + externalToolName, e); //$NON-NLS-1$
+ }
+ processStream(process.getInputStream(), parsers, consolePrinter);
+ processStream(process.getErrorStream(), parsers, consolePrinter);
+ } finally {
+ if (process != null) {
+ process.destroy();
+ }
+ consolePrinter.close();
+ }
+ }
+
+ private ConsolePrinter consolePrinter(String externalToolName, boolean shouldDisplayOutput) {
+ if (shouldDisplayOutput) {
+ try {
+ return ConsolePrinterImpl.createOrFindConsole(externalToolName);
+ } catch (PartInitException e) {
+ CodanUIActivator.log("Unable to create/find console", e); //$NON-NLS-1$
+ }
+ }
+ return ConsolePrinter.NullImpl;
+ }
+
+ private String buildCommand(IPath executablePath, String[] args) {
+ StringBuilder builder = new StringBuilder();
+ builder.append(executablePath.toOSString());
+ for (String arg : args) {
+ builder.append(" ").append(arg); //$NON-NLS-1$
+ }
+ return builder.toString();
+ }
+
+ private Process invoke(String command, IPath workingDirectory) throws IOException {
+ Runtime runtime = Runtime.getRuntime();
+ if (workingDirectory == null) {
+ return runtime.exec(command);
+ }
+ return runtime.exec(command, ENVIRONMENT_VARIABLE_SETTINGS, workingDirectory.toFile());
+ }
+
+ private void processStream(InputStream inputStream, List<IOutputParser> parsers,
+ ConsolePrinter consolePrinter) throws IOException, InvocationFailure {
+ Reader reader = null;
+ try {
+ reader = new InputStreamReader(inputStream);
+ BufferedReader bufferedReader = new BufferedReader(reader);
+ String line = null;
+ while ((line = bufferedReader.readLine()) != null) {
+ consolePrinter.println(line);
+ for (IOutputParser parser : parsers) {
+ if (parser.parse(line)) {
+ break;
+ }
+ }
+ }
+ } finally {
+ if (reader != null) {
+ try {
+ reader.close();
+ } catch (IOException ignored) {}
+ }
+ }
+ }
+}
diff --git a/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/ui/externaltool/ConsolePrinter.java b/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/ui/externaltool/ConsolePrinter.java
new file mode 100644
index 0000000000..41ac71312b
--- /dev/null
+++ b/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/ui/externaltool/ConsolePrinter.java
@@ -0,0 +1,54 @@
+/*******************************************************************************
+ * Copyright (c) 2012 Google, Inc.
+ * 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 - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.cdt.codan.ui.externaltool;
+
+/**
+ * Prints the output of an external tool to an Eclipse console. It uses the name of the external
+ * tool as the console ID.
+ *
+ * @author alruiz@google.com (Alex Ruiz)
+ */
+interface ConsolePrinter {
+ ConsolePrinter NullImpl = new ConsolePrinter() {
+ @Override
+ public void clear() {}
+
+ @Override
+ public void println(String message) {}
+
+ @Override
+ public void println() {}
+
+ @Override
+ public void close() {}
+ };
+
+ /**
+ * Clears the contents of the console.
+ */
+ void clear();
+
+ /**
+ * Prints the specified message to the console, followed by a line separator string.
+ * @param message the message to print.
+ */
+ void println(String message);
+
+ /**
+ * Prints a line separator to the console.
+ */
+ void println();
+
+ /**
+ * Closes the output stream of the console.
+ */
+ void close();
+}
diff --git a/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/ui/externaltool/ConsolePrinterImpl.java b/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/ui/externaltool/ConsolePrinterImpl.java
new file mode 100644
index 0000000000..abaf246d10
--- /dev/null
+++ b/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/ui/externaltool/ConsolePrinterImpl.java
@@ -0,0 +1,81 @@
+/*******************************************************************************
+ * Copyright (c) 2012 Google, Inc.
+ * 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 - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.cdt.codan.ui.externaltool;
+
+import static org.eclipse.ui.console.IConsoleConstants.ID_CONSOLE_VIEW;
+
+import java.io.IOException;
+
+import org.eclipse.cdt.codan.ui.CodanEditorUtility;
+import org.eclipse.ui.IWorkbenchPage;
+import org.eclipse.ui.PartInitException;
+import org.eclipse.ui.console.ConsolePlugin;
+import org.eclipse.ui.console.IConsole;
+import org.eclipse.ui.console.IConsoleManager;
+import org.eclipse.ui.console.IConsoleView;
+import org.eclipse.ui.console.MessageConsole;
+import org.eclipse.ui.console.MessageConsoleStream;
+
+/**
+ * Default implementation of <code>{@link ConsolePrinter}</code>.
+ *
+ * @author alruiz@google.com (Alex Ruiz)
+ */
+class ConsolePrinterImpl implements ConsolePrinter {
+ private final MessageConsole console;
+ private final MessageConsoleStream out;
+
+ static ConsolePrinter createOrFindConsole(String externalToolName)
+ throws PartInitException {
+ MessageConsole console = findConsole(externalToolName);
+ IWorkbenchPage page = CodanEditorUtility.getActivePage();
+ if (page != null) {
+ IConsoleView view = (IConsoleView) page.showView(ID_CONSOLE_VIEW);
+ view.display(console);
+ }
+ return new ConsolePrinterImpl(console);
+ }
+
+ private static MessageConsole findConsole(String externalToolName) {
+ IConsoleManager consoleManager = ConsolePlugin.getDefault().getConsoleManager();
+ for (IConsole console : consoleManager.getConsoles()) {
+ if (externalToolName.equals(console.getName()) && console instanceof MessageConsole) {
+ return (MessageConsole) console;
+ }
+ }
+ MessageConsole console = new MessageConsole(externalToolName, null);
+ consoleManager.addConsoles(new IConsole[] { console });
+ return console;
+ }
+
+ private ConsolePrinterImpl(MessageConsole console) {
+ this.console = console;
+ out = console.newMessageStream();
+ }
+
+ public void clear() {
+ console.clearConsole();
+ }
+
+ public void println(String s) {
+ out.println(s);
+ }
+
+ public void println() {
+ out.println();
+ }
+
+ public void close() {
+ try {
+ out.close();
+ } catch (IOException ignored) {}
+ }
+}

Back to the top