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.RecipeRunner;
017
018@Mojo(name = "do", requiresProject = false, requiresDependencyCollection = ResolutionScope.TEST, requiresDependencyResolution = ResolutionScope.TEST)
019public class AEConsole extends AEBase {
020
021        @Parameter(defaultValue = "0")
022        protected int port;
023
024        @Override
025        public void execute() throws MojoExecutionException, MojoFailureException {
026                List<File> dependenciesToScan = DependencyScanner.filter(plugin.getArtifacts(), operationsDependencies);
027                DependencyScanner scanner = new DependencyScanner(dependenciesToScan);
028                operations = scanner.scan();
029
030                AEPluginWorkspace workspace = new AEPluginWorkspace(basedir);
031                workspace.setOperationsClasses(operations);
032
033                getLog().info("Giant anteater looking for food ...");
034                getLog().info("Loading configuration ...");
035                workspace.loadConfiguration(true);
036
037                importVariables(workspace);
038
039                try {
040                        if (port > 0) {
041                                CommandServer.openCommandPort(workspace, port);
042                        }
043                        workspace.runSetupNodes();
044                } catch (Exception e) {
045                        throw new MojoExecutionException("Execution failed.", e);
046                }
047
048                runRecipes(workspace, false);
049        }
050
051        protected String[] defaultRecipes(AEPluginWorkspace workspace) {
052                String name = "Recipe name";
053                String savedValue = workspace.getDefaultUserConfiguration(".inputValue." + name, null);
054                String recipeName = workspace.inputValue(name, null, savedValue, null, null);
055                workspace.setDefaultUserConfiguration(".inputValue." + name, recipeName);
056                return new String[] { recipeName };
057        }
058
059        protected void runRecipes(AEWorkspace workspace, boolean async) throws MojoExecutionException {
060                if (recipes == null) {
061                        String sysRecipes = System.getProperty("recipes");
062                        if (sysRecipes != null) {
063                                recipes = StringUtils.split(sysRecipes, ',');
064                        }
065                }
066
067                if (recipes == null) {
068                        String[] testsList = workspace.getTestsList();
069                        recipes = workspace.inputMultiChoice("Recipe", testsList, null, null);
070                }
071
072                for (String name : recipes) {
073                        System.out.println("Recipe: [" + name + "]");
074                        RecipeRunner runTask = workspace.runTask(name, false);
075
076                        if (runTask == null) {
077                                System.out.println("Recipe: [" + name + "] not found.");
078
079                                Enumeration<Object> keys = workspace.getTestsDescList().keys();
080                                System.out.println("Recipe list:");
081                                while (keys.hasMoreElements()) {
082                                        Object taskName = keys.nextElement();
083                                        System.out.println("\t" + taskName);
084                                }
085                        }
086                }
087
088                workspace.close();
089        }
090
091}