You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by ol...@apache.org on 2011/07/09 13:12:25 UTC

svn commit: r1144639 - in /maven/archetype/trunk/maven-archetype-plugin/src/main/java/org/apache/maven/archetype/ui: ArchetypeSelectorUtils.java DefaultArchetypeSelectionQueryer.java DefaultArchetypeSelector.java

Author: olamy
Date: Sat Jul  9 11:12:25 2011
New Revision: 1144639

URL: http://svn.apache.org/viewvc?rev=1144639&view=rev
Log:
[ARCHETYPE-371] Add a command line argument to filter/limit the archetypes returned by mvn archetype:generate
filtering supported tru prompter too

Added:
    maven/archetype/trunk/maven-archetype-plugin/src/main/java/org/apache/maven/archetype/ui/ArchetypeSelectorUtils.java   (with props)
Modified:
    maven/archetype/trunk/maven-archetype-plugin/src/main/java/org/apache/maven/archetype/ui/DefaultArchetypeSelectionQueryer.java
    maven/archetype/trunk/maven-archetype-plugin/src/main/java/org/apache/maven/archetype/ui/DefaultArchetypeSelector.java

Added: maven/archetype/trunk/maven-archetype-plugin/src/main/java/org/apache/maven/archetype/ui/ArchetypeSelectorUtils.java
URL: http://svn.apache.org/viewvc/maven/archetype/trunk/maven-archetype-plugin/src/main/java/org/apache/maven/archetype/ui/ArchetypeSelectorUtils.java?rev=1144639&view=auto
==============================================================================
--- maven/archetype/trunk/maven-archetype-plugin/src/main/java/org/apache/maven/archetype/ui/ArchetypeSelectorUtils.java (added)
+++ maven/archetype/trunk/maven-archetype-plugin/src/main/java/org/apache/maven/archetype/ui/ArchetypeSelectorUtils.java Sat Jul  9 11:12:25 2011
@@ -0,0 +1,104 @@
+package org.apache.maven.archetype.ui;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.commons.lang.StringUtils;
+import org.apache.maven.archetype.catalog.Archetype;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @author Olivier Lamy
+ * @since 2.1
+ */
+public class ArchetypeSelectorUtils
+{
+    private static String extractGroupIdFromFilter( String filter )
+    {
+        return StringUtils.contains( filter, ':' ) ? StringUtils.substringBefore( filter, ":" ) : null;
+    }
+
+    private static String extractArtifactIdFromFilter( String filter )
+    {
+        // if no : the full text is considered as artifactId content
+        return StringUtils.contains( filter, ':' ) ? StringUtils.substringAfter( filter, ":" ) : filter;
+    }
+
+    /**
+     * apply some filtering on archetypes.
+     * currently only on artifactId contains filter
+     *
+     * @param archetypesPerCatalog
+     * @return
+     */
+    public static Map<String, List<Archetype>> getFilteredArchetypesByCatalog(
+        Map<String, List<Archetype>> archetypesPerCatalog, String filter )
+    {
+        if ( archetypesPerCatalog == null || archetypesPerCatalog.isEmpty() )
+        {
+            return Collections.emptyMap();
+        }
+        Map<String, List<Archetype>> filtered =
+            new LinkedHashMap<String, List<Archetype>>( archetypesPerCatalog.size() );
+
+        for ( Map.Entry<String, List<Archetype>> entry : archetypesPerCatalog.entrySet() )
+        {
+            List<Archetype> archetypes = new ArrayList<Archetype>();
+            for ( Archetype archetype : entry.getValue() )
+            {
+                String groupId = ArchetypeSelectorUtils.extractGroupIdFromFilter( filter );
+                String artifactId = ArchetypeSelectorUtils.extractArtifactIdFromFilter( filter );
+                if ( groupId == null )
+                {
+                    if ( StringUtils.contains( archetype.getArtifactId(), artifactId ) )
+                    {
+                        archetypes.add( archetype );
+                    }
+                }
+                else if ( artifactId == null )
+                {
+                    if ( StringUtils.contains( archetype.getGroupId(), groupId ) )
+                    {
+                        archetypes.add( archetype );
+                    }
+                }
+                else
+                {
+                    if ( StringUtils.contains( archetype.getGroupId(), groupId ) && StringUtils.contains(
+                        archetype.getArtifactId(), artifactId ) )
+                    {
+                        archetypes.add( archetype );
+                    }
+                }
+            }
+            if ( !archetypes.isEmpty() )
+            {
+                filtered.put( entry.getKey(), archetypes );
+            }
+        }
+
+        return filtered;
+    }
+
+}

Propchange: maven/archetype/trunk/maven-archetype-plugin/src/main/java/org/apache/maven/archetype/ui/ArchetypeSelectorUtils.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/archetype/trunk/maven-archetype-plugin/src/main/java/org/apache/maven/archetype/ui/ArchetypeSelectorUtils.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Modified: maven/archetype/trunk/maven-archetype-plugin/src/main/java/org/apache/maven/archetype/ui/DefaultArchetypeSelectionQueryer.java
URL: http://svn.apache.org/viewvc/maven/archetype/trunk/maven-archetype-plugin/src/main/java/org/apache/maven/archetype/ui/DefaultArchetypeSelectionQueryer.java?rev=1144639&r1=1144638&r2=1144639&view=diff
==============================================================================
--- maven/archetype/trunk/maven-archetype-plugin/src/main/java/org/apache/maven/archetype/ui/DefaultArchetypeSelectionQueryer.java (original)
+++ maven/archetype/trunk/maven-archetype-plugin/src/main/java/org/apache/maven/archetype/ui/DefaultArchetypeSelectionQueryer.java Sat Jul  9 11:12:25 2011
@@ -19,6 +19,7 @@ package org.apache.maven.archetype.ui;
  * under the License.
  */
 
+import org.apache.commons.lang.math.NumberUtils;
 import org.apache.maven.archetype.catalog.Archetype;
 import org.apache.maven.artifact.versioning.ArtifactVersion;
 import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
@@ -36,12 +37,16 @@ import java.util.Set;
 import java.util.SortedMap;
 import java.util.TreeMap;
 
-/** @plexus.component */
+/**
+ * @plexus.component
+ */
 public class DefaultArchetypeSelectionQueryer
     extends AbstractLogEnabled
     implements ArchetypeSelectionQueryer
 {
-    /** @plexus.requirement role-hint="archetype" */
+    /**
+     * @plexus.requirement role-hint="archetype"
+     */
     private Prompter prompter;
 
     public boolean confirmSelection( ArchetypeDefinition archetypeDefinition )
@@ -51,7 +56,7 @@ public class DefaultArchetypeSelectionQu
             "Confirm archetype selection: \n" + archetypeDefinition.getGroupId() + "/" + archetypeDefinition.getName()
                 + "\n";
 
-        String answer = prompter.prompt( query, Arrays.asList( new String[] { "Y", "N" } ), "Y" );
+        String answer = prompter.prompt( query, Arrays.asList( new String[]{ "Y", "N" } ), "Y" );
 
         return "Y".equalsIgnoreCase( answer );
     }
@@ -95,8 +100,9 @@ public class DefaultArchetypeSelectionQu
 
                 String answer = String.valueOf( counter );
 
-                query.append( answer + ": " + catalog + " -> " + archetype.getGroupId() + ":" + archetype.getArtifactId() + " ("
-                    + description + ")\n" );
+                query.append(
+                    answer + ": " + catalog + " -> " + archetype.getGroupId() + ":" + archetype.getArtifactId() + " ("
+                        + description + ")\n" );
 
                 answers.add( answer );
 
@@ -114,16 +120,30 @@ public class DefaultArchetypeSelectionQu
 
         }
 
-        query.append( "Choose a number: " );
+        query.append( "Choose a number or apply filter (format part of groupId:part if artifactId ) : " );
 
         String answer;
         if ( defaultSelection == 0 )
         {
-            answer = prompter.prompt( query.toString(), answers );
+            answer = prompter.prompt( query.toString() );
         }
         else
         {
-            answer = prompter.prompt( query.toString(), answers, Integer.toString( defaultSelection ) );
+            answer = prompter.prompt( query.toString(), Integer.toString( defaultSelection ) );
+        }
+
+        if ( !NumberUtils.isNumber( answer ) )
+        {
+            // not a number so apply filter and ask again
+
+            Map<String, List<Archetype>> filteredCatalogs =
+                ArchetypeSelectorUtils.getFilteredArchetypesByCatalog( catalogs, answer );
+            return selectArchetype( filteredCatalogs, defaultDefinition );
+        }
+
+        if ( !answer.contains( answer ) )
+        {
+            return selectArchetype( catalogs, defaultDefinition );
         }
 
         Archetype selection = archetypeAnswerMap.get( answer );

Modified: maven/archetype/trunk/maven-archetype-plugin/src/main/java/org/apache/maven/archetype/ui/DefaultArchetypeSelector.java
URL: http://svn.apache.org/viewvc/maven/archetype/trunk/maven-archetype-plugin/src/main/java/org/apache/maven/archetype/ui/DefaultArchetypeSelector.java?rev=1144639&r1=1144638&r2=1144639&view=diff
==============================================================================
--- maven/archetype/trunk/maven-archetype-plugin/src/main/java/org/apache/maven/archetype/ui/DefaultArchetypeSelector.java (original)
+++ maven/archetype/trunk/maven-archetype-plugin/src/main/java/org/apache/maven/archetype/ui/DefaultArchetypeSelector.java Sat Jul  9 11:12:25 2011
@@ -78,7 +78,7 @@ public class DefaultArchetypeSelector
         if ( StringUtils.isNotBlank( request.getFilter() ) )
         {
             // applying some filtering depending on filter parameter
-            archetypes = getFilteredArchetypesByCatalog( archetypes, request );
+            archetypes = ArchetypeSelectorUtils.getFilteredArchetypesByCatalog( archetypes, request.getFilter() );
         }
 
         if ( definition.isDefined() && StringUtils.isEmpty( request.getArchetypeRepository() ) )
@@ -175,73 +175,6 @@ public class DefaultArchetypeSelector
         definition.updateRequest( request );
     }
 
-    /**
-     * apply some filtering on archetypes.
-     * currently only on artifactId contains filter
-     *
-     * @param archetypesPerCatalog
-     * @return
-     */
-    private Map<String, List<Archetype>> getFilteredArchetypesByCatalog(
-        Map<String, List<Archetype>> archetypesPerCatalog, ArchetypeGenerationRequest request )
-    {
-        if ( archetypesPerCatalog == null || archetypesPerCatalog.isEmpty() )
-        {
-            return Collections.emptyMap();
-        }
-        Map<String, List<Archetype>> filtered =
-            new LinkedHashMap<String, List<Archetype>>( archetypesPerCatalog.size() );
-
-        for ( Map.Entry<String, List<Archetype>> entry : archetypesPerCatalog.entrySet() )
-        {
-            List<Archetype> archetypes = new ArrayList<Archetype>();
-            for ( Archetype archetype : entry.getValue() )
-            {
-                String groupId = extractGroupIdFromFilter( request.getFilter() );
-                String artifactId = extractArtifactIdFromFilter( request.getFilter() );
-                if ( groupId == null )
-                {
-                    if ( StringUtils.contains( archetype.getArtifactId(), artifactId ) )
-                    {
-                        archetypes.add( archetype );
-                    }
-                }
-                else if ( artifactId == null )
-                {
-                    if ( StringUtils.contains( archetype.getGroupId(), groupId ) )
-                    {
-                        archetypes.add( archetype );
-                    }
-                }
-                else
-                {
-                    if ( StringUtils.contains( archetype.getGroupId(), groupId ) && StringUtils.contains(
-                        archetype.getArtifactId(), artifactId ) )
-                    {
-                        archetypes.add( archetype );
-                    }
-                }
-            }
-            if ( !archetypes.isEmpty() )
-            {
-                filtered.put( entry.getKey(), archetypes );
-            }
-        }
-
-        return filtered;
-    }
-
-    private String extractGroupIdFromFilter( String filter )
-    {
-        return StringUtils.contains( filter, ':' ) ? StringUtils.substringBefore( filter, ":" ) : null;
-    }
-
-    private String extractArtifactIdFromFilter( String filter )
-    {
-        // if no : the full text is considered as artifactId content
-        return StringUtils.contains( filter, ':' ) ? StringUtils.substringAfter( filter, ":" ) : filter;
-    }
-
 
     private Map<String, List<Archetype>> getArchetypesByCatalog( String catalogs )
     {