1 | |
package org.apache.maven.archetype.ui.generation; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import org.apache.commons.lang.math.NumberUtils; |
23 | |
import org.apache.maven.archetype.catalog.Archetype; |
24 | |
import org.apache.maven.archetype.ui.ArchetypeDefinition; |
25 | |
import org.apache.maven.artifact.versioning.ArtifactVersion; |
26 | |
import org.apache.maven.artifact.versioning.DefaultArtifactVersion; |
27 | |
import org.codehaus.plexus.components.interactivity.Prompter; |
28 | |
import org.codehaus.plexus.components.interactivity.PrompterException; |
29 | |
import org.codehaus.plexus.logging.AbstractLogEnabled; |
30 | |
|
31 | |
import java.util.ArrayList; |
32 | |
import java.util.Arrays; |
33 | |
import java.util.HashMap; |
34 | |
import java.util.HashSet; |
35 | |
import java.util.List; |
36 | |
import java.util.Map; |
37 | |
import java.util.Set; |
38 | |
import java.util.SortedMap; |
39 | |
import java.util.TreeMap; |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | 10 | public class DefaultArchetypeSelectionQueryer |
45 | |
extends AbstractLogEnabled |
46 | |
implements ArchetypeSelectionQueryer |
47 | |
{ |
48 | |
|
49 | |
|
50 | |
|
51 | |
private Prompter prompter; |
52 | |
|
53 | |
public boolean confirmSelection( ArchetypeDefinition archetypeDefinition ) |
54 | |
throws PrompterException |
55 | |
{ |
56 | 0 | String query = |
57 | |
"Confirm archetype selection: \n" + archetypeDefinition.getGroupId() + "/" + archetypeDefinition.getName() |
58 | |
+ "\n"; |
59 | |
|
60 | 0 | String answer = prompter.prompt( query, Arrays.asList( new String[]{ "Y", "N" } ), "Y" ); |
61 | |
|
62 | 0 | return "Y".equalsIgnoreCase( answer ); |
63 | |
} |
64 | |
|
65 | |
public Archetype selectArchetype( Map<String, List<Archetype>> catalogs ) |
66 | |
throws PrompterException |
67 | |
{ |
68 | 2 | return selectArchetype( catalogs, null ); |
69 | |
} |
70 | |
|
71 | |
public Archetype selectArchetype( Map<String, List<Archetype>> catalogs, ArchetypeDefinition defaultDefinition ) |
72 | |
throws PrompterException |
73 | |
{ |
74 | 5 | Archetype selection = null; |
75 | 5 | Map<String, List<Archetype>> filteredCatalogs = catalogs; |
76 | |
|
77 | |
do |
78 | |
{ |
79 | 6 | StringBuilder query = new StringBuilder( "Choose archetype:\n" ); |
80 | |
|
81 | 6 | Set<String> archetypeKeys = new HashSet<String>(); |
82 | 6 | List<String> answers = new ArrayList<String>(); |
83 | 6 | Map<String, Archetype> archetypeAnswerMap = new HashMap<String, Archetype>(); |
84 | |
|
85 | 6 | int counter = 0; |
86 | 6 | int defaultSelection = 0; |
87 | |
|
88 | 6 | for ( Map.Entry<String, List<Archetype>> entry : filteredCatalogs.entrySet() ) |
89 | |
{ |
90 | 6 | String catalog = entry.getKey(); |
91 | |
|
92 | 6 | for ( Archetype archetype : entry.getValue() ) |
93 | |
{ |
94 | 11 | String archetypeKey = archetype.getGroupId() + ":" + archetype.getArtifactId(); |
95 | |
|
96 | 11 | if ( !archetypeKeys.add( archetypeKey ) ) |
97 | |
{ |
98 | 0 | continue; |
99 | |
} |
100 | |
|
101 | 11 | counter++; |
102 | |
|
103 | 11 | String description = archetype.getDescription(); |
104 | 11 | if ( description == null ) |
105 | |
{ |
106 | 11 | description = "-"; |
107 | |
} |
108 | |
|
109 | 11 | String answer = String.valueOf( counter ); |
110 | |
|
111 | 11 | query.append( answer + ": " + catalog + " -> " + archetype.getGroupId() + ":" |
112 | |
+ archetype.getArtifactId() + " (" + description + ")\n" ); |
113 | |
|
114 | 11 | answers.add( answer ); |
115 | |
|
116 | 11 | archetypeAnswerMap.put( answer, archetype ); |
117 | |
|
118 | |
|
119 | 11 | if ( defaultDefinition != null && archetype.getGroupId().equals( defaultDefinition.getGroupId() ) |
120 | |
&& archetype.getArtifactId().equals( defaultDefinition.getArtifactId() ) ) |
121 | |
{ |
122 | 2 | defaultSelection = counter; |
123 | |
} |
124 | 11 | } |
125 | 6 | } |
126 | |
|
127 | 6 | if ( counter == 0 ) |
128 | |
{ |
129 | 0 | query.append( " Your filter doesn't match any archetype (hint: enter to return to initial list)\n" ); |
130 | |
} |
131 | |
|
132 | 6 | query.append( "Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): " ); |
133 | |
|
134 | |
String answer; |
135 | 6 | if ( defaultSelection == 0 ) |
136 | |
{ |
137 | 4 | answer = prompter.prompt( query.toString() ); |
138 | |
} |
139 | |
else |
140 | |
{ |
141 | 2 | answer = prompter.prompt( query.toString(), Integer.toString( defaultSelection ) ); |
142 | |
} |
143 | |
|
144 | 6 | if ( NumberUtils.isNumber( answer ) ) |
145 | |
{ |
146 | 5 | selection = archetypeAnswerMap.get( answer ); |
147 | |
} |
148 | |
else |
149 | |
{ |
150 | |
|
151 | 1 | filteredCatalogs = ArchetypeSelectorUtils.getFilteredArchetypesByCatalog( catalogs, answer ); |
152 | |
} |
153 | |
} |
154 | 6 | while ( selection == null ); |
155 | |
|
156 | 5 | return selectVersion( catalogs, selection.getGroupId(), selection.getArtifactId() ); |
157 | |
} |
158 | |
|
159 | |
private Archetype selectVersion( Map<String, List<Archetype>> catalogs, String groupId, String artifactId ) |
160 | |
throws PrompterException |
161 | |
{ |
162 | 5 | SortedMap<ArtifactVersion, Archetype> archetypeVersionsMap = new TreeMap<ArtifactVersion, Archetype>(); |
163 | |
|
164 | 5 | for ( Map.Entry<String, List<Archetype>> entry : catalogs.entrySet() ) |
165 | |
{ |
166 | 5 | for ( Archetype archetype : entry.getValue() ) |
167 | |
{ |
168 | 10 | if ( !groupId.equals( archetype.getGroupId() ) || !artifactId.equals( archetype.getArtifactId() ) ) |
169 | |
{ |
170 | 0 | continue; |
171 | |
} |
172 | |
|
173 | 5 | ArtifactVersion version = new DefaultArtifactVersion( archetype.getVersion() ); |
174 | |
|
175 | |
|
176 | 5 | if ( !archetypeVersionsMap.containsKey( version ) ) |
177 | |
{ |
178 | 5 | archetypeVersionsMap.put( version, archetype ); |
179 | |
} |
180 | 5 | } |
181 | |
} |
182 | |
|
183 | 5 | if ( archetypeVersionsMap.size() == 1 ) |
184 | |
{ |
185 | 5 | return archetypeVersionsMap.values().iterator().next(); |
186 | |
} |
187 | |
|
188 | |
|
189 | 0 | StringBuilder query = new StringBuilder( "Choose version: \n" ); |
190 | |
|
191 | 0 | List<String> answers = new ArrayList<String>(); |
192 | 0 | Map<String, Archetype> answerMap = new HashMap<String, Archetype>(); |
193 | |
|
194 | 0 | int counter = 1; |
195 | 0 | String mapKey = null; |
196 | |
|
197 | 0 | for ( Map.Entry<ArtifactVersion, Archetype> entry : archetypeVersionsMap.entrySet() ) |
198 | |
{ |
199 | 0 | ArtifactVersion version = entry.getKey(); |
200 | 0 | Archetype archetype = entry.getValue(); |
201 | |
|
202 | 0 | mapKey = String.valueOf( counter ); |
203 | |
|
204 | 0 | query.append( mapKey + ": " + version + "\n" ); |
205 | |
|
206 | 0 | answers.add( mapKey ); |
207 | |
|
208 | 0 | answerMap.put( mapKey, archetype ); |
209 | |
|
210 | 0 | counter++; |
211 | 0 | } |
212 | |
|
213 | 0 | query.append( "Choose a number: " ); |
214 | |
|
215 | 0 | Archetype archetype = null; |
216 | |
|
217 | |
do |
218 | |
{ |
219 | 0 | String answer = prompter.prompt( query.toString(), answers, mapKey ); |
220 | |
|
221 | 0 | archetype = answerMap.get( answer ); |
222 | |
} |
223 | 0 | while ( archetype == null ); |
224 | |
|
225 | 0 | return archetype; |
226 | |
} |
227 | |
|
228 | |
public void setPrompter( Prompter prompter ) |
229 | |
{ |
230 | 5 | this.prompter = prompter; |
231 | 5 | } |
232 | |
} |