001package com.ganteater.ae.maven.plugin;
002
003import java.io.File;
004import java.io.IOException;
005import java.util.List;
006
007import javax.swing.JOptionPane;
008import javax.swing.WindowConstants;
009
010import org.apache.commons.lang.StringUtils;
011import org.apache.commons.lang3.exception.ExceptionUtils;
012import org.apache.maven.plugin.MojoExecutionException;
013import org.apache.maven.plugin.MojoFailureException;
014import org.apache.maven.plugins.annotations.Mojo;
015import org.apache.maven.plugins.annotations.ResolutionScope;
016
017import com.ganteater.ae.AEWorkspace;
018import com.ganteater.ae.CommandException;
019import com.ganteater.ae.ConfigurationException;
020import com.ganteater.ae.TaskCancelingException;
021import com.ganteater.ae.desktop.DesktopWorkspace;
022import com.ganteater.ae.desktop.ui.AEFrame;
023import com.ganteater.ae.desktop.ui.OptionPane;
024
025@Mojo(name = "run", requiresProject = false, requiresDependencyCollection = ResolutionScope.TEST, requiresDependencyResolution = ResolutionScope.TEST)
026public class AE extends AEBase {
027
028        @Override
029        public void execute() throws MojoExecutionException, MojoFailureException {
030
031                if (plugin != null && operationsDependencies != null) {
032                        List<File> dependenciesToScan = DependencyScanner.filter(plugin.getArtifacts(), operationsDependencies);
033                        DependencyScanner scanner = new DependencyScanner(dependenciesToScan);
034                        operations = scanner.scan();
035                }
036
037                AEFrame frame = new AEFrame() {
038
039                        private static final long serialVersionUID = 1L;
040
041                        @Override
042                        protected DesktopWorkspace createWorkspace() {
043                                return new DesktopWorkspace(this) {
044                                        @Override
045                                        public void refreshTaskPath() throws IOException {
046                                                super.refreshTaskPath();
047                                                getRecipesPanel().refreshTaskPathTable();
048                                        }
049
050                                        @Override
051                                        protected File findAlternativeConfiguration(String baseDir) {
052                                                File confFile = null;
053                                                if (StringUtils.equals(project.getPackaging(), "pom")) {
054                                                        confFile = super.findAlternativeConfiguration(aeDir);
055                                                }
056
057                                                if (confFile == null) {
058                                                        confFile = super.selectConfiguration();
059                                                }
060
061                                                return confFile;
062                                        }
063                                };
064                        }
065
066                };
067                AEWorkspace workspace = frame.getWorkspace();
068
069                File file = aeDir == null ? project.getBasedir() : new File(project.getBasedir(), aeDir);
070                if(file != null) {
071                        workspace.setStartDir(file);
072                }
073                workspace.setOperationsClasses(operations);
074
075                importVariables(workspace);
076
077                try {
078                        startRecipe(frame, workspace);
079
080                } catch (Exception e) {
081                        throw new MojoExecutionException("Anteater failed.", e);
082                }
083
084                frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
085                try {
086                        for (;;) {
087                                if (!frame.isVisible()) {
088                                        break;
089                                }
090                                Thread.sleep(500);
091                        }
092                } catch (InterruptedException e) {
093                        Thread.currentThread().interrupt();
094                }
095        }
096
097        private void startRecipe(AEFrame frame, AEWorkspace workspace) throws MojoExecutionException, CommandException {
098                try {
099                        frame.start();
100                        runRecipes(workspace, true);
101
102                } catch (TaskCancelingException e) {
103                        System.exit(0);
104                } catch (ConfigurationException e) {
105                        frame.setAlwaysOnTop(true);
106                        OptionPane.showMessageDialog(frame, ExceptionUtils.getRootCauseMessage(e), "Configuration Error",
107                                        JOptionPane.ERROR_MESSAGE);
108                        e.printStackTrace();
109                        System.exit(0);
110                }
111        }
112
113}