001package com.ganteater.ae.maven.plugin;
002
003import java.io.File;
004import java.util.Enumeration;
005import java.util.List;
006
007import org.apache.commons.lang.StringUtils;
008import org.apache.maven.plugin.MojoExecutionException;
009import org.apache.maven.plugin.MojoFailureException;
010import org.apache.maven.plugins.annotations.Mojo;
011import org.apache.maven.plugins.annotations.Parameter;
012import org.apache.maven.plugins.annotations.ResolutionScope;
013
014import com.ganteater.ae.AEWorkspace;
015import com.ganteater.ae.CommandServer;
016import com.ganteater.ae.ConfigConstants;
017import com.ganteater.ae.RecipeRunner;
018
019@Mojo(name = "do", requiresProject = false, requiresDependencyCollection = ResolutionScope.TEST, requiresDependencyResolution = ResolutionScope.TEST)
020public class AEConsole extends AEBase {
021
022
023        @Parameter(defaultValue = "0")
024        protected int port;
025
026        @Override
027        public void execute() throws MojoExecutionException, MojoFailureException {
028                List<File> dependenciesToScan = DependencyScanner.filter(plugin.getArtifacts(), operationsDependencies);
029                DependencyScanner scanner = new DependencyScanner(dependenciesToScan);
030                operations = scanner.scan();
031
032                File file = aeDir == null ? project.getBasedir() : new File(project.getBasedir(), aeDir);
033                AEPluginWorkspace workspace = new AEPluginWorkspace(file) {
034                        @Override
035                        protected File findAlternativeConfiguration(String baseDir) {
036                                File confFile = null;
037                                loadCustomProperties();
038
039                                if (StringUtils.equals(project.getPackaging(), "pom")) {
040                                        confFile = super.findAlternativeConfiguration(aeDir);
041                                }
042
043                                if (confFile == null) {
044                                        confFile = super.selectConfiguration();
045                                }
046
047                                return confFile;
048                        }
049                };
050                workspace.setOperationsClasses(operations);
051
052                getLog().info("Giant anteater looking for food ...");
053                getLog().info("Loading configuration ...");
054                workspace.loadConfiguration(true);
055
056                importVariables(workspace);
057
058                try {
059                        if (port > 0) {
060                                CommandServer.openCommandPort(workspace, port);
061                        }
062                        workspace.runSetupNodes();
063                } catch (Exception e) {
064                        throw new MojoExecutionException("Execution failed.", e);
065                }
066
067                runRecipes(workspace, false);
068        }
069
070        protected String[] defaultRecipes(AEPluginWorkspace workspace) {
071                String name = "Recipe name";
072                String savedValue = workspace.getDefaultUserConfiguration(".inputValue." + name, null);
073                String recipeName = workspace.inputValue(name, null, savedValue, null, null);
074                workspace.setDefaultUserConfiguration(".inputValue." + name, recipeName);
075                return new String[] { recipeName };
076        }
077
078        protected void runRecipes(AEWorkspace workspace, boolean async) throws MojoExecutionException {
079                if (recipes == null) {
080                        String sysRecipes = System.getProperty(ConfigConstants.RECIPES_PARAM_NAME);
081                        if (sysRecipes != null) {
082                                recipes = StringUtils.split(sysRecipes, ',');
083                        }
084                }
085
086                if (recipes == null) {
087                        String[] testsList = workspace.getTestsList();
088                        recipes = workspace.inputMultiChoice("Recipe", testsList, null, null);
089                }
090
091                for (String name : recipes) {
092                        System.out.println("Recipe: [" + name + "]");
093                        RecipeRunner runTask = workspace.runTask(name, false);
094
095                        if (runTask == null) {
096                                System.out.println("Recipe: [" + name + "] not found.");
097
098                                Enumeration<Object> keys = workspace.getTestsDescList().keys();
099                                System.out.println("Recipe list:");
100                                while (keys.hasMoreElements()) {
101                                        Object taskName = keys.nextElement();
102                                        System.out.println("\t" + taskName);
103                                }
104                        }
105                }
106
107                workspace.close();
108        }
109
110}