mirror of
https://github.com/scm-manager/scm-manager.git
synced 2026-02-06 06:39:15 +01:00
Sort CLI commands alphabetically (#2020)
Sort CLI commands alphabetically to show them in consistent order.
This commit is contained in:
@@ -26,10 +26,11 @@ package sonia.scm.cli;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Singleton;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Singleton
|
||||
public class CommandRegistry {
|
||||
@@ -41,17 +42,17 @@ public class CommandRegistry {
|
||||
this.commandCollector = commandCollector;
|
||||
}
|
||||
|
||||
public Set<RegisteredCommandNode> createCommandTree() {
|
||||
Set<RegisteredCommandNode> rootCommands = new HashSet<>();
|
||||
Set<RegisteredCommand> registeredCommands = commandCollector.collect();
|
||||
public List<RegisteredCommandNode> createCommandTree() {
|
||||
List<RegisteredCommandNode> rootCommands = new ArrayList<>();
|
||||
List<RegisteredCommand> sortedCommands = collectSortedCommands();
|
||||
|
||||
Map<Class<?>, RegisteredCommandNode> commandNodes = new HashMap<>();
|
||||
|
||||
for (RegisteredCommand command : registeredCommands) {
|
||||
for (RegisteredCommand command : sortedCommands) {
|
||||
commandNodes.put(command.getCommand(), new RegisteredCommandNode(command.getName(), command.getCommand()));
|
||||
}
|
||||
|
||||
for (RegisteredCommand command : registeredCommands) {
|
||||
for (RegisteredCommand command : sortedCommands) {
|
||||
RegisteredCommandNode node = commandNodes.get(command.getCommand());
|
||||
if (command.getParent() == null) {
|
||||
rootCommands.add(node);
|
||||
@@ -66,4 +67,11 @@ public class CommandRegistry {
|
||||
}
|
||||
return rootCommands;
|
||||
}
|
||||
|
||||
private List<RegisteredCommand> collectSortedCommands() {
|
||||
return commandCollector.collect()
|
||||
.stream()
|
||||
.sorted((a, b) -> a.getName().compareToIgnoreCase(b.getName()))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user