From 8e3b5829b60cab6f436e8571762330d3daeffad8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= Date: Mon, 1 Jan 2024 08:46:06 +0100 Subject: [PATCH] feat: update to `ExecutionCoordinator` --- ...ngCommandExecutionCoordinatorResolver.java | 36 ------------------- .../cloud/spring/SpringCommandManager.java | 20 ++++++----- .../spring/config/CloudSpringConfig.java | 9 +++-- .../spring/ApplicationIntegrationTest.java | 13 ++----- 4 files changed, 19 insertions(+), 59 deletions(-) delete mode 100644 cloud-spring/src/main/java/org/incendo/cloud/spring/SpringCommandExecutionCoordinatorResolver.java diff --git a/cloud-spring/src/main/java/org/incendo/cloud/spring/SpringCommandExecutionCoordinatorResolver.java b/cloud-spring/src/main/java/org/incendo/cloud/spring/SpringCommandExecutionCoordinatorResolver.java deleted file mode 100644 index 0ad6a06..0000000 --- a/cloud-spring/src/main/java/org/incendo/cloud/spring/SpringCommandExecutionCoordinatorResolver.java +++ /dev/null @@ -1,36 +0,0 @@ -// -// MIT License -// -// Copyright (c) 2023 Incendo -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in all -// copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -// SOFTWARE. -// -package org.incendo.cloud.spring; - -import cloud.commandframework.CommandTree; -import cloud.commandframework.execution.CommandExecutionCoordinator; -import java.util.function.Function; -import org.apiguardian.api.API; -import org.checkerframework.checker.nullness.qual.NonNull; - -@API(status = API.Status.STABLE, since = "1.0.0") -public interface SpringCommandExecutionCoordinatorResolver extends Function<@NonNull CommandTree, - @NonNull CommandExecutionCoordinator> { - -} diff --git a/cloud-spring/src/main/java/org/incendo/cloud/spring/SpringCommandManager.java b/cloud-spring/src/main/java/org/incendo/cloud/spring/SpringCommandManager.java index 0988daf..ea45baf 100644 --- a/cloud-spring/src/main/java/org/incendo/cloud/spring/SpringCommandManager.java +++ b/cloud-spring/src/main/java/org/incendo/cloud/spring/SpringCommandManager.java @@ -25,14 +25,16 @@ import cloud.commandframework.CommandManager; import cloud.commandframework.arguments.suggestion.SuggestionFactory; +import cloud.commandframework.arguments.suggestion.Suggestions; import cloud.commandframework.context.CommandInput; import cloud.commandframework.exceptions.ArgumentParseException; import cloud.commandframework.exceptions.InvalidCommandSenderException; import cloud.commandframework.exceptions.InvalidSyntaxException; import cloud.commandframework.exceptions.NoPermissionException; import cloud.commandframework.exceptions.NoSuchCommandException; -import cloud.commandframework.execution.FilteringCommandSuggestionProcessor; +import cloud.commandframework.execution.ExecutionCoordinator; import cloud.commandframework.keys.CloudKey; +import cloud.commandframework.util.StringUtils; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -70,27 +72,24 @@ public class SpringCommandManager extends CommandManager implements Comple /** * Creates a new command manager. * - * @param commandExecutionCoordinatorResolver the resolver for the execution coordinator + * @param executionCoordinator the execution coordinator * @param commandPermissionHandler the permission handler * @param commandRegistrationHandler the registration handler * @param commandSenderMapper the mapper for the custom command sender type * @param applicationContext the application context */ public SpringCommandManager( - final @NonNull SpringCommandExecutionCoordinatorResolver commandExecutionCoordinatorResolver, + final @NonNull ExecutionCoordinator executionCoordinator, final @NonNull SpringCommandPermissionHandler commandPermissionHandler, final @NonNull SpringCommandRegistrationHandler commandRegistrationHandler, final @NonNull CommandSenderMapper commandSenderMapper, final @NonNull ApplicationContext applicationContext ) { - super(commandExecutionCoordinatorResolver, commandRegistrationHandler); + super(executionCoordinator, commandRegistrationHandler); this.commandPermissionHandler = commandPermissionHandler; this.commandSenderMapper = commandSenderMapper; this.suggestionFactory = super.suggestionFactory().mapped(CloudCompletionProposal::fromSuggestion); this.parameterInjectorRegistry().registerInjectionService(new SpringInjectionService<>(applicationContext)); - this.commandSuggestionProcessor(new FilteringCommandSuggestionProcessor<>( - FilteringCommandSuggestionProcessor.Filter.startsWith(true).andTrimBeforeLastSpace() - )); this.registerDefaultExceptionHandlers(); } @@ -125,7 +124,12 @@ void commandExecutionEvent(final @NonNull CommandExecutionEvent event) { strings.addAll(completionContext.getWords()); final String input = String.join(" ", strings); - return this.suggestionFactory().suggestImmediately(this.commandSenderMapper.map(null), input).stream() + final Suggestions suggestions = + this.suggestionFactory.suggestImmediately(this.commandSenderMapper.map(null), input); + return suggestions.list() + .stream() + .map(suggestion -> suggestion.withSuggestion(StringUtils.trimBeforeLastSpace(suggestion.suggestion(), + suggestions.commandInput()))) .map(suggestion -> (CompletionProposal) suggestion) .toList(); } diff --git a/cloud-spring/src/main/java/org/incendo/cloud/spring/config/CloudSpringConfig.java b/cloud-spring/src/main/java/org/incendo/cloud/spring/config/CloudSpringConfig.java index 2d78cb9..4ac8bae 100644 --- a/cloud-spring/src/main/java/org/incendo/cloud/spring/config/CloudSpringConfig.java +++ b/cloud-spring/src/main/java/org/incendo/cloud/spring/config/CloudSpringConfig.java @@ -23,11 +23,10 @@ // package org.incendo.cloud.spring.config; -import cloud.commandframework.execution.CommandExecutionCoordinator; +import cloud.commandframework.execution.ExecutionCoordinator; import org.apiguardian.api.API; import org.checkerframework.checker.nullness.qual.NonNull; import org.incendo.cloud.spring.CommandSenderMapper; -import org.incendo.cloud.spring.SpringCommandExecutionCoordinatorResolver; import org.incendo.cloud.spring.SpringCommandPermissionHandler; import org.incendo.cloud.spring.SpringCommandSender; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; @@ -50,9 +49,9 @@ public class CloudSpringConfig { } @Bean - @ConditionalOnMissingBean(SpringCommandExecutionCoordinatorResolver.class) - @NonNull SpringCommandExecutionCoordinatorResolver commandExecutionCoordinatorResolver() { - return CommandExecutionCoordinator.simpleCoordinator()::apply; + @ConditionalOnMissingBean(ExecutionCoordinator.class) + @NonNull ExecutionCoordinator executionCoordinator() { + return ExecutionCoordinator.simpleCoordinator(); } @Bean diff --git a/cloud-spring/src/test/java/org/incendo/cloud/spring/ApplicationIntegrationTest.java b/cloud-spring/src/test/java/org/incendo/cloud/spring/ApplicationIntegrationTest.java index 134499a..06a75d1 100644 --- a/cloud-spring/src/test/java/org/incendo/cloud/spring/ApplicationIntegrationTest.java +++ b/cloud-spring/src/test/java/org/incendo/cloud/spring/ApplicationIntegrationTest.java @@ -28,7 +28,7 @@ import cloud.commandframework.CommandProperties; import cloud.commandframework.context.CommandContext; import cloud.commandframework.context.StandardCommandContextFactory; -import cloud.commandframework.execution.AsynchronousCommandExecutionCoordinator; +import cloud.commandframework.execution.ExecutionCoordinator; import cloud.commandframework.internal.CommandNode; import org.checkerframework.checker.nullness.qual.NonNull; import org.junit.jupiter.api.DisplayName; @@ -61,13 +61,6 @@ void test() { assertThat(this.springCommandManager).isNotNull(); } - @Test - @DisplayName("Verify that the command execution coordinator was overridden") - void testCommandExecutionCoordinator() { - assertThat(this.springCommandManager.commandExecutor().commandExecutionCoordinator()) - .isInstanceOf(AsynchronousCommandExecutionCoordinator.class); - } - @Test @DisplayName("Verify that the command bean was registered") void testCommandRegistration() { @@ -108,8 +101,8 @@ static class TestConfig { } @Bean - @NonNull SpringCommandExecutionCoordinatorResolver commandExecutionCoordinatorResolver() { - return AsynchronousCommandExecutionCoordinator.builder().withAsynchronousParsing().build()::apply; + @NonNull ExecutionCoordinator executionCoordinator() { + return ExecutionCoordinator.asyncCoordinator(); } @Bean