changeset 140:e605201414d0

Display export status on a console Use a new console to display export status information. Exporting includes building using maven, so display the output to help users diagnose any issues that come up.
author Omair Majid <omajid@redhat.com>
date Thu, 05 Jun 2014 14:39:49 -0400
parents 9a72cb1423af
children bfe6a252c0fe
files com.redhat.thermostat.tools.eclipse.plugin/META-INF/MANIFEST.MF com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/wizards/ExportPluginWizard.java
diffstat 2 files changed, 82 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/com.redhat.thermostat.tools.eclipse.plugin/META-INF/MANIFEST.MF	Thu Jun 05 13:16:42 2014 -0400
+++ b/com.redhat.thermostat.tools.eclipse.plugin/META-INF/MANIFEST.MF	Thu Jun 05 14:39:49 2014 -0400
@@ -23,7 +23,9 @@
  org.eclipse.jface.databinding,
  com.ibm.icu,
  org.eclipse.m2e.core;bundle-version="1.4.0",
- org.eclipse.m2e.core.ui;bundle-version="1.4.0"
+ org.eclipse.m2e.core.ui;bundle-version="1.4.0",
+ org.eclipse.debug.ui;bundle-version="3.9.0",
+ org.eclipse.ui.console;bundle-version="3.5.200"
 Bundle-RequiredExecutionEnvironment: JavaSE-1.7
 Bundle-ActivationPolicy: lazy
 Export-Package: com.redhat.thermostat.tools.eclipse.plugin;x-friends:="com.redhat.thermostat.tools.eclipse.plugin.tests",
--- a/com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/wizards/ExportPluginWizard.java	Thu Jun 05 13:16:42 2014 -0400
+++ b/com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/wizards/ExportPluginWizard.java	Thu Jun 05 14:39:49 2014 -0400
@@ -12,10 +12,21 @@
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.Status;
 import org.eclipse.core.runtime.jobs.Job;
+import org.eclipse.debug.core.DebugPlugin;
+import org.eclipse.debug.core.ILaunchManager;
+import org.eclipse.debug.core.IStreamListener;
+import org.eclipse.debug.core.Launch;
+import org.eclipse.debug.core.model.IProcess;
+import org.eclipse.debug.core.model.IStreamMonitor;
 import org.eclipse.jface.viewers.IStructuredSelection;
 import org.eclipse.jface.wizard.Wizard;
 import org.eclipse.ui.IExportWizard;
 import org.eclipse.ui.IWorkbench;
+import org.eclipse.ui.console.ConsolePlugin;
+import org.eclipse.ui.console.IConsole;
+import org.eclipse.ui.console.IConsoleManager;
+import org.eclipse.ui.console.MessageConsole;
+import org.eclipse.ui.console.MessageConsoleStream;
 
 import com.redhat.thermostat.tools.eclipse.plugin.Activator;
 import com.redhat.thermostat.tools.eclipse.plugin.Unzipper;
@@ -65,10 +76,17 @@
         protected IStatus run(IProgressMonitor monitor) {
             monitor.beginTask("Exporting plugin", IProgressMonitor.UNKNOWN);
 
+            MessageConsole console = getConsole("Exporting " + project.getName());
+            MessageConsoleStream stream = console.newMessageStream();
+
             try {
                 // mvn clean package
-                new MavenRunner().run(project, "package");
-            } catch (CoreException e) {
+                boolean mavenWorked = new MavenRunner().run(stream, project, "package");
+                if (!mavenWorked) {
+                    stream.write("Faild to run maven");
+                    return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Unable to run 'mvn clean package'");
+                }
+            } catch (CoreException | IOException e) {
                 e.printStackTrace();
                 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Unable to run 'mvn clean package'", e);
             }
@@ -106,7 +124,7 @@
                 ZipFile pluginZip = new ZipFile(new File(distributionTargetDir, zipFile));
                 File installationDirectory = new File(targetLocation, "plugins");
                 new Unzipper().unzip(pluginZip, installationDirectory);
-                System.out.println("Done installing " + project.getName() + " to " + installationDirectory);
+                stream.write("Done installing " + project.getName() + " to " + installationDirectory + "\n");
             } catch (IOException e) {
                 e.printStackTrace();
                 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Unable to install plugin in target location", e);
@@ -116,31 +134,77 @@
 
             return Status.OK_STATUS;
         }
+
+        private MessageConsole getConsole(String name) {
+            IConsoleManager consoleManager = ConsolePlugin.getDefault().getConsoleManager();
+            MessageConsole console = getOrCreateConsole(consoleManager, name);
+            consoleManager.showConsoleView(console);
+            return console;
+        }
+
+        private MessageConsole getOrCreateConsole(IConsoleManager consoleManager, String name) {
+            IConsole[] consoles = consoleManager.getConsoles();
+            for (IConsole console : consoles) {
+                if (console.getName().equals(name)) {
+                    return (MessageConsole) console;
+                }
+            }
+
+            MessageConsole console = new MessageConsole(name, null);
+            consoleManager.addConsoles(new IConsole[] { console });
+            return console;
+        }
+
     }
 
     private static class MavenRunner {
 
-        public void run(IProject project, String goal) throws CoreException {
+        /** Returns true if completed successfully */
+        public boolean run(MessageConsoleStream stream, IProject project, String goal) throws CoreException, IOException {
             try {
-                System.out.println("Starting packaging");
-                ProcessBuilder processBuilder = new ProcessBuilder("mvn", "clean", goal);
-                processBuilder.directory(project.getLocation().toFile());
-                processBuilder.redirectError(ProcessBuilder.Redirect.PIPE);
-                processBuilder.redirectOutput(ProcessBuilder.Redirect.PIPE);
-                Process process = processBuilder.start();
+                stream.write("Building " + project.getName() + "\n");
+                ConsoleRedirector consoleRedirector = new ConsoleRedirector(stream);
+
+                String[] command = new String[] { "mvn", "clean", goal };
+
+                Process p = DebugPlugin.exec(command, project.getLocation().toFile());
+                Launch launch = new Launch(null, ILaunchManager.RUN_MODE, null);
+                IProcess launchProcess = DebugPlugin.newProcess(launch, p, "Export Thermostat Plugin");
 
-                // FIXME expose process result to user for feedback.
+                launchProcess.getStreamsProxy().getOutputStreamMonitor().addListener(consoleRedirector);
+                launchProcess.getStreamsProxy().getErrorStreamMonitor().addListener(consoleRedirector);
 
-                boolean result = process.waitFor(5, TimeUnit.MINUTES);
+                boolean result = p.waitFor(1, TimeUnit.HOURS);
                 if (!result) {
-                    throw new AssertionError("An error invoking maven. Maybe try 'mvn clean package' on command line?");
+                    p.destroyForcibly();
+                    return false;
                 }
-                System.out.println("Done packaging");
-            } catch (IOException | InterruptedException e) {
+
+                return (p.exitValue() == 0);
+
+            } catch (InterruptedException e) {
                 IStatus status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Error running maven", e);
                 throw new CoreException(status);
             }
         }
 
     }
+
+    private static class ConsoleRedirector implements IStreamListener {
+
+        private MessageConsoleStream stream;
+
+        public ConsoleRedirector(MessageConsoleStream stream) {
+            this.stream = stream;
+        }
+
+        @Override
+        public void streamAppended(String text, IStreamMonitor monitor) {
+            try {
+                stream.write(text);
+            } catch (IOException e) {
+                throw new RuntimeException(e);
+            }
+        }
+    }
 }