Skip to content

Commit 29b141a

Browse files
committed
Restore default name of command groups as in v3
This commit restores the default name of command groups as in v3. Utils.splitCamelCase copied verbatim from v3. Resolves #1308
1 parent 8fa7e9c commit 29b141a

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

spring-shell-core/src/main/java/org/springframework/shell/core/command/annotation/support/CommandFactoryBean.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,16 @@ public Command getObject() {
8282
description = description.isEmpty() ? "N/A" : description;
8383
String help = command.help();
8484
String group = command.group();
85-
group = group.isEmpty() ? this.method.getDeclaringClass().getSimpleName() + " Commands" : group;
85+
if (group.isEmpty()) {
86+
String simpleName = Utils.splitCamelCase(this.method.getDeclaringClass().getSimpleName());
87+
if (!simpleName.endsWith(" Commands")) {
88+
group = simpleName + " Commands";
89+
}
90+
else {
91+
group = simpleName;
92+
}
93+
94+
}
8695
boolean hidden = command.hidden();
8796
String[] aliases = command.alias();
8897
String availabilityProviderBeanName = command.availabilityProvider();

spring-shell-core/src/main/java/org/springframework/shell/core/utils/Utils.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.springframework.shell.core.command.CommandRegistry;
3131
import org.springframework.shell.core.command.ExitStatus;
3232
import org.springframework.util.Assert;
33+
import org.springframework.util.StringUtils;
3334

3435
/**
3536
* Some text utilities.
@@ -39,6 +40,17 @@
3940
*/
4041
public class Utils {
4142

43+
/**
44+
* Split a CamelCase class name into separate words. Used to derive command group
45+
* names. For example, "MyCommands" becomes "My Commands".
46+
* @param className the simple class name
47+
* @return the split string
48+
*/
49+
public static String splitCamelCase(String className) {
50+
String[] tokens = className.split("(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])");
51+
return StringUtils.arrayToDelimitedString(tokens, " ");
52+
}
53+
4254
/**
4355
* Turn CamelCaseText into gnu-style-lowercase.
4456
*/

spring-shell-core/src/test/java/org/springframework/shell/core/UtilsTests.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@
3333
*/
3434
class UtilsTests {
3535

36+
@Test
37+
void testSplitCamelCase() {
38+
assertThat(Utils.splitCamelCase("MyCommands")).isEqualTo("My Commands");
39+
assertThat(Utils.splitCamelCase("MyAppCommands")).isEqualTo("My App Commands");
40+
}
41+
3642
@Test
3743
public void testUnCamelify() throws Exception {
3844
assertThat(Utils.unCamelify("HelloWorld")).isEqualTo("hello-world");

0 commit comments

Comments
 (0)