001package com.ganteater.ae.maven.plugin;
002
003import java.io.BufferedReader;
004import java.io.File;
005import java.io.IOException;
006import java.io.InputStream;
007import java.io.InputStreamReader;
008import java.nio.file.Files;
009import java.nio.file.Path;
010import java.nio.file.Paths;
011import java.util.ArrayList;
012import java.util.List;
013import java.util.stream.Collectors;
014import java.util.stream.Stream;
015
016import javax.swing.WindowConstants;
017
018import org.apache.commons.lang.StringUtils;
019import org.apache.commons.lang.SystemUtils;
020import org.apache.maven.plugin.MojoExecutionException;
021import org.apache.maven.plugin.MojoFailureException;
022import org.apache.maven.plugins.annotations.Mojo;
023import org.apache.maven.plugins.annotations.ResolutionScope;
024
025import com.ganteater.ae.AEWorkspace;
026import com.ganteater.ae.ConfigConstants;
027import com.ganteater.ae.ConfigurationException;
028import com.ganteater.ae.TaskCancelingException;
029import com.ganteater.ae.desktop.DesktopWorkspace;
030import com.ganteater.ae.desktop.ui.AEFrame;
031
032@Mojo(name = "run", requiresProject = false, requiresDependencyCollection = ResolutionScope.TEST, requiresDependencyResolution = ResolutionScope.TEST)
033public class AE extends AEBase {
034
035        @Override
036        public void execute() throws MojoExecutionException, MojoFailureException {
037
038                if (plugin != null && operationsDependencies != null) {
039                        List<File> dependenciesToScan = DependencyScanner.filter(plugin.getArtifacts(), operationsDependencies);
040                        DependencyScanner scanner = new DependencyScanner(dependenciesToScan);
041                        operations = scanner.scan();
042                }
043
044                AEFrame frame = new AEFrame() {
045
046                        private static final long serialVersionUID = 1L;
047
048                        @Override
049                        protected DesktopWorkspace createWorkspace() {
050                                return new DesktopWorkspace(this) {
051                                        @Override
052                                        public void refreshTaskPath() throws IOException {
053                                                super.refreshTaskPath();
054                                                getRecipesPanel().refreshTaskPathTable();
055                                        }
056
057                                        @Override
058                                        protected File findAlternativeConfiguration() {
059                                                File confFile = null;
060                                                if (StringUtils.equals(project.getPackaging(), "pom")) {
061                                                        File path = project.getBasedir();                                               
062                                                        if (path == null) {
063                                                                path = SystemUtils.getUserDir();
064                                                        }
065                                                        List<String> confFiles = findAEconfigs(path);
066
067                                                        String absolutePath = path.getAbsolutePath();
068                                                        String prefix = StringUtils.replace(absolutePath, basedir.getAbsolutePath(), "");
069                                                        final String replace = StringUtils.replace(basedir.getAbsolutePath(), prefix, "");
070                                                        List<String> collect = confFiles.stream().map(f -> {
071                                                                String pathConfFile = StringUtils.replace(f,
072                                                                                replace + File.separator + ConfigConstants.AE_CONFIG_XML, "");
073
074                                                                if (!new File(pathConfFile + File.separator + "pom.xml").exists()) {
075                                                                        pathConfFile = f;
076                                                                }
077
078                                                                return pathConfFile;
079                                                        }).collect(Collectors.toList());
080
081                                                        if (!collect.isEmpty()) {
082                                                                String project = inputSelectChoice("Projects", null,
083                                                                                collect.toArray(new String[collect.size()]), collect.get(0), false);
084                                                                File aeConfigFile = new File(project);
085                                                                File pomFile = new File(aeConfigFile.getParent(), "pom.xml");
086                                                                if (pomFile.exists()) {
087                                                                        aeConfigFile = aeConfigFile.getParentFile();
088                                                                }
089
090                                                                if (!StringUtils.endsWith(aeConfigFile.getAbsolutePath(),
091                                                                                ConfigConstants.AE_CONFIG_XML)) {
092                                                                        executeAnteater(aeConfigFile);
093                                                                        
094                                                                } else {
095                                                                        confFile = new File(project);
096                                                                }
097                                                        }
098                                                }
099
100                                                if (confFile == null) {
101                                                        confFile = super.selectConfiguration();
102                                                }
103
104                                                return confFile;
105                                        }
106                                };
107                        }
108
109                };
110                AEWorkspace workspace = frame.getWorkspace();
111
112                workspace.setStartDir(basedir);
113                workspace.setOperationsClasses(operations);
114
115                importVariables(workspace);
116
117                try {
118                        startRecipe(frame, workspace);
119
120                } catch (Exception e) {
121                        throw new MojoExecutionException("Anteater failed.", e);
122                }
123
124                frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
125                try {
126                        for (;;) {
127                                if (!frame.isVisible()) {
128                                        break;
129                                }
130                                Thread.sleep(500);
131                        }
132                } catch (InterruptedException e) {
133                        Thread.currentThread().interrupt();
134                }
135        }
136
137        private void startRecipe(AEFrame frame, AEWorkspace workspace) throws MojoExecutionException {
138                try {
139                        frame.start();
140                        runRecipes(workspace, true);
141
142                } catch (TaskCancelingException e) {
143                        System.exit(0);
144                } catch (ConfigurationException e) {
145                        e.printStackTrace();
146                        System.exit(0);
147                }
148        }
149
150}