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