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