Skip to content

Commit 8439d18

Browse files
committed
Add handling for default value in lists
1 parent dca79c6 commit 8439d18

File tree

8 files changed

+54
-37
lines changed

8 files changed

+54
-37
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# Version 2.6.2 (2017-10-19)
2+
3+
* [new] Add basic prompt mode (-DbasicPrompt) on generate goal, for the case where ConsoleUI doesn't work properly.
4+
* [chg] If an archetype named "web" exists, set it as the default project type.
5+
16
# Version 2.6.2 (2017-08-10)
27

38
* [chg] Add a fallback to hard-coded archetype list for project generation.

src/license/THIRD-PARTY.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# Please fill the missing licenses for dependencies :
1414
#
1515
#
16-
#Thu Oct 19 14:13:45 CEST 2017
16+
#Thu Oct 19 15:02:05 CEST 2017
1717
classworlds--classworlds--1.1-alpha-2=
1818
commons-collections--commons-collections--3.1=
1919
dom4j--dom4j--1.6.1=

src/main/java/org/seedstack/maven/GenerateMojo.java

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,11 @@ public void execute() throws MojoExecutionException, MojoFailureException {
123123
getLog().info("Resolved version " + archetypeVersion);
124124
}
125125
try {
126-
// We have a list of possible types, let the user choose
126+
// We have a list of possible types, let the user choose (if a "web" choice exists, set it as default)
127127
List<Value> list = new ArrayList<>(findProjectTypes(archetypeGroupId, archetypeVersion, remoteCatalog));
128128
Collections.sort(list);
129129
list.add(new Value("custom archetype", "custom"));
130-
type = getPrompter().promptList("Choose the project type", list);
130+
type = getPrompter().promptList("Choose the project type", list, "web");
131131

132132
// If the user wants to input a custom archetype
133133
if ("custom".equals(type)) {
@@ -271,21 +271,6 @@ public void execute() throws MojoExecutionException, MojoFailureException {
271271
}
272272
}
273273

274-
private Thread setupBasicPromptHintHook() {
275-
Thread basicPromptHook = new Thread(new Runnable() {
276-
@Override
277-
public void run() {
278-
if (!basicMode) {
279-
getLog().info("Hint: if you experience prompting issues, try the basic prompt instead by adding \"-DbasicPrompt\" to your command line");
280-
}
281-
}
282-
}, "basic-prompt-hook");
283-
Runtime.getRuntime().addShutdownHook(basicPromptHook);
284-
return basicPromptHook;
285-
}
286-
287-
288-
289274
private Thread setupInquiryCancelHook(final File projectDir, final File questionFile, final HashMap<String, Object> vars) {
290275
Thread shutdownRender = new Thread(new Runnable() {
291276
@Override

src/main/java/org/seedstack/maven/components/inquirer/impl/AbstractInquirer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public Object ask(Question question) throws InquirerException {
8585
case CHECKBOX:
8686
return coerce(getPrompter().promptCheckbox(message, question.getValues()), type);
8787
case LIST:
88-
return coerce(getPrompter().promptList(message, question.getValues()), type);
88+
return coerce(getPrompter().promptList(message, question.getValues(), defaultValue), type);
8989
case CHOICE:
9090
return coerce(getPrompter().promptChoice(message, question.getValues()), type);
9191
default:

src/main/java/org/seedstack/maven/components/prompter/Prompter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
public interface Prompter {
1515
String promptChoice(String message, List<Value> values) throws PromptException;
1616

17-
String promptList(String message, List<Value> values) throws PromptException;
17+
String promptList(String message, List<Value> values, String defaultValue) throws PromptException;
1818

1919
Set<String> promptCheckbox(String message, List<Value> values) throws PromptException;
2020

src/main/java/org/seedstack/maven/components/prompter/Value.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,24 @@ public static List<String> convertValues(List<Value> values) {
9696
return result;
9797
}
9898

99+
@Override
100+
public boolean equals(Object o) {
101+
if (this == o) return true;
102+
if (o == null || getClass() != o.getClass()) return false;
103+
104+
Value value = (Value) o;
105+
106+
return name.equals(value.name);
107+
}
108+
109+
@Override
110+
public int hashCode() {
111+
return name.hashCode();
112+
}
113+
99114
@Override
100115
public int compareTo(Value o) {
101-
if (o == null) {
102-
throw new NullPointerException("Attempt to compare to null value");
103-
}
104-
return this.label.compareTo(o.label);
116+
return this.name.compareTo(o.name);
105117
}
106118
}
107119

src/main/java/org/seedstack/maven/components/prompter/impl/BasicPrompter.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,14 @@ public String promptChoice(String message, List<Value> groups) throws PromptExce
3333
throw new UnsupportedOperationException("Basic prompter cannot handle complex choices");
3434
}
3535

36-
@Override
37-
public String promptList(String message, List<Value> values) throws PromptException {
38-
return promptList(message, values, null);
39-
}
40-
41-
private String promptList(String message, List<Value> values, String defaultValue) throws PromptException {
36+
public String promptList(String message, List<Value> values, String defaultValue) throws PromptException {
4237
List<String> strings = Value.convertValues(values);
43-
String formattedMessage = formatMessage(message, strings, null);
38+
String formattedMessage = formatMessage(message, strings, defaultValue);
4439
String line;
4540
do {
4641
writePrompt(formattedMessage);
4742
line = readLine();
48-
if (line == null && defaultValue != null) {
43+
if ((line == null || line.isEmpty()) && defaultValue != null) {
4944
line = defaultValue;
5045
}
5146
} while (line == null || !strings.contains(line));

src/main/java/org/seedstack/maven/components/prompter/impl/ConsoleUIPrompter.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,38 @@ public String promptChoice(String message, List<Value> values) throws PromptExce
5757
}
5858

5959
@Override
60-
public String promptList(String message, List<Value> values) throws PromptException {
60+
public String promptList(String message, List<Value> values, String defaultValue) throws PromptException {
6161
ConsolePrompt consolePrompt = new ConsolePrompt();
6262
PromptBuilder promptBuilder = consolePrompt.getPromptBuilder();
6363
ListPromptBuilder listPromptBuilder = promptBuilder.createListPrompt()
6464
.name("dummy")
6565
.message(message);
66+
67+
// Put default value first
68+
Value defaultValueObject = null;
69+
if (defaultValue != null && !defaultValue.isEmpty()) {
70+
for (Value value : values) {
71+
if (value.getName().equals(defaultValue)) {
72+
defaultValueObject = value;
73+
listPromptBuilder.newItem()
74+
.name(value.getName())
75+
.text(value.getLabel())
76+
.add();
77+
break;
78+
}
79+
}
80+
}
81+
82+
// Put other values
6683
for (Value value : values) {
67-
listPromptBuilder.newItem()
68-
.name(value.getName())
69-
.text(value.getLabel())
70-
.add();
84+
if (!value.equals(defaultValueObject)) {
85+
listPromptBuilder.newItem()
86+
.name(value.getName())
87+
.text(value.getLabel())
88+
.add();
89+
}
7190
}
91+
7292
listPromptBuilder.addPrompt();
7393
return ((ListResult) extractAnswer(consolePrompt, promptBuilder)).getSelectedId();
7494
}

0 commit comments

Comments
 (0)