Mercurial > hg > thermostat-tools-eclipse
view com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/wizards/ExportPluginWizard.java @ 137:2d1213657085
Add progress and cancellation support to export
author | Omair Majid <omajid@redhat.com> |
---|---|
date | Thu, 05 Jun 2014 12:32:42 -0400 |
parents | 31d0ab4467ad |
children | 9a72cb1423af |
line wrap: on
line source
package com.redhat.thermostat.tools.eclipse.plugin.wizards; import java.io.File; import java.io.FilenameFilter; import java.io.IOException; import java.util.concurrent.TimeUnit; import java.util.zip.ZipFile; import org.eclipse.core.resources.IProject; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Status; import org.eclipse.core.runtime.jobs.Job; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.wizard.Wizard; import org.eclipse.ui.IExportWizard; import org.eclipse.ui.IWorkbench; import com.redhat.thermostat.tools.eclipse.plugin.Activator; import com.redhat.thermostat.tools.eclipse.plugin.Unzipper; public class ExportPluginWizard extends Wizard implements IExportWizard { private IStructuredSelection selection; private ExportPluginPage exportPage; @Override public void init(IWorkbench workbench, IStructuredSelection selection) { setWindowTitle("Export Thermostat Plugin"); setNeedsProgressMonitor(true); this.selection = selection; } @Override public void addPages() { exportPage = new ExportPluginPage(selection); addPage(exportPage); } @Override public boolean performFinish() { IProject project = exportPage.getProject(); String targetLocation = exportPage.getInstallationLocation(); Job export = new ExportPlugin(project, targetLocation); export.schedule(); return true; } private static class ExportPlugin extends Job { private IProject project; private String targetLocation; public ExportPlugin(IProject project, String targetLocation) { super("Exporting Thermostat plugin"); this.project = project; this.targetLocation = targetLocation; } @Override protected IStatus run(IProgressMonitor monitor) { monitor.beginTask("Exporting plugin", IProgressMonitor.UNKNOWN); try { // mvn clean package new MavenRunner().run(project, "package"); } catch (CoreException e) { e.printStackTrace(); return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Unable to run 'mvn clean package'", e); } if (monitor.isCanceled()) { return Status.CANCEL_STATUS; } monitor.worked(1); final String distributionModuleName = project.getName() + "-distribution"; // TODO unzip and extract to thermostatLocation File projectDir = project.getLocation().toFile(); // FIXME don't hardcode distribution project name File distributionDir = new File(projectDir, distributionModuleName); File distributionTargetDir = new File(distributionDir, "target"); String[] files = distributionTargetDir.list(new FilenameFilter() { @Override public boolean accept(File dir, String name) { return name.matches("\\Q" + distributionModuleName + "\\E.*\\.zip"); } }); String zipFile = files[0]; if (monitor.isCanceled()) { return Status.CANCEL_STATUS; } monitor.worked(1); try { 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); } catch (IOException e) { e.printStackTrace(); return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Unable to install plugin in target location", e); } monitor.worked(1); return Status.OK_STATUS; } } private static class MavenRunner { public void run(IProject project, String goal) throws CoreException { 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(); // FIXME make this non-blocking. Add tons of feedback. boolean result = process.waitFor(5, TimeUnit.MINUTES); if (!result) { throw new AssertionError("An error invoking maven. Maybe try 'mvn clean package' on command line?"); } System.out.println("Done packaging"); } catch (IOException | InterruptedException e) { IStatus status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Error running maven", e); throw new CoreException(status); } } } }