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