From c8a1d8830c2b9d383531904b7679890b502c8d75 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Tue, 14 Jan 2014 15:06:03 -0800 Subject: [PATCH] Refactor CLI internals for REPL shell Numerous updates to the Spring CLI, primarily for better embedded REPL shell support: * Refactor the CLI application to help separate concerts between the main CLI and the embedded shell. Both the CLI and embedded shell now delegate to a new `CommandRunner` to handle running commands. The runner can be configured differently depending depending on need. For example, the embedded shell adds the 'prompt' and 'clear' commands. * Most `Command` implementations have been moved to sub-packages so that they can be co-located with the classes that they use. * Option commands are now only used in the CLI, the embedded shell does not user them and details have been removed from the Command interface. * The REPL shell has been significantly refactored to: - Support CTRL-C to cancel the running process. This is supported when running external commands and most internal commands. - Fork a new JVM when running commands (primarily for CTRL-C support but also for potential memory and classpath issues) - Change the "continue" trigger from `<<` to `\` - Support command completion of files - Add ANSI color output - Provide 'help' support for internal commands (such as 'clear') - Remove the now redundant `stop` command Fixes gh-227 --- spring-boot-cli/.gitignore | 1 + spring-boot-cli/pom.xml | 7 +- ...liException.java => CommandException.java} | 34 +- .../boot/cli/CommandRunner.java | 286 ++++++++++++ .../boot/cli/NoArgumentsException.java | 28 ++ .../cli/NoHelpCommandArgumentsException.java | 32 ++ .../boot/cli/NoSuchCommandException.java | 9 +- .../springframework/boot/cli/SpringCli.java | 433 +----------------- .../boot/cli/command/AbstractCommand.java | 23 +- .../boot/cli/{ => command}/Command.java | 11 +- .../cli/{ => command}/CommandFactory.java | 6 +- .../cli/command/DefaultCommandFactory.java | 32 +- .../boot/cli/command/OptionHandler.java | 4 +- .../boot/cli/{ => command}/OptionHelp.java | 4 +- .../cli/command/OptionParsingCommand.java | 12 +- .../boot/cli/command/ShellCommand.java | 287 ------------ .../boot/cli/command/core/HelpCommand.java | 112 +++++ .../boot/cli/command/core/HintCommand.java | 111 +++++ .../command/{ => core}/VersionCommand.java | 9 +- .../cli/command/{ => grab}/CleanCommand.java | 8 +- .../cli/command/{ => grab}/GrabCommand.java | 9 +- .../cli/command/{ => run}/RunCommand.java | 11 +- .../run}/SpringApplicationRunner.java | 4 +- .../SpringApplicationRunnerConfiguration.java | 4 +- .../boot/cli/command/shell/AnsiString.java | 81 ++++ .../boot/cli/command/shell/ClearCommand.java | 44 ++ .../command/{ => shell}/CommandCompleter.java | 32 +- ...scapeAwareWhiteSpaceArgumentDelimiter.java | 103 +++++ .../ExitCommand.java} | 23 +- .../cli/command/shell/ForkProcessCommand.java | 103 +++++ .../command/{ => shell}/PromptCommand.java | 17 +- .../cli/command/shell/RunProcessCommand.java | 150 ++++++ .../boot/cli/command/shell/Shell.java | 261 +++++++++++ .../boot/cli/command/shell/ShellCommand.java | 39 ++ .../cli/command/shell/ShellExitException.java | 34 ++ .../cli/command/{ => test}/TestCommand.java | 11 +- .../test}/TestRunner.java | 4 +- .../test}/TestRunnerConfiguration.java | 4 +- .../boot/groovy/DelegateTestRunner.java | 4 +- ...framework.boot.cli.command.CommandFactory} | 0 .../cli/command/CustomCommandFactory.java | 9 +- .../springframework/boot/cli/CliTester.java | 10 +- ...gCliTests.java => CommandRunnerTests.java} | 89 ++-- .../boot/cli/GrabCommandIntegrationTests.java | 4 +- .../boot/cli/GrapesCleaner.java | 4 +- .../boot/cli/TestCommandIntegrationTests.java | 6 +- ...AwareWhiteSpaceArgumentDelimiterTests.java | 101 ++++ spring-boot-parent/pom.xml | 5 + 48 files changed, 1693 insertions(+), 922 deletions(-) create mode 100644 spring-boot-cli/.gitignore rename spring-boot-cli/src/main/java/org/springframework/boot/cli/{SpringCliException.java => CommandException.java} (71%) create mode 100644 spring-boot-cli/src/main/java/org/springframework/boot/cli/CommandRunner.java create mode 100644 spring-boot-cli/src/main/java/org/springframework/boot/cli/NoArgumentsException.java create mode 100644 spring-boot-cli/src/main/java/org/springframework/boot/cli/NoHelpCommandArgumentsException.java rename spring-boot-cli/src/main/java/org/springframework/boot/cli/{ => command}/Command.java (82%) rename spring-boot-cli/src/main/java/org/springframework/boot/cli/{ => command}/CommandFactory.java (86%) rename spring-boot-cli/src/main/java/org/springframework/boot/cli/{ => command}/OptionHelp.java (89%) delete mode 100644 spring-boot-cli/src/main/java/org/springframework/boot/cli/command/ShellCommand.java create mode 100644 spring-boot-cli/src/main/java/org/springframework/boot/cli/command/core/HelpCommand.java create mode 100644 spring-boot-cli/src/main/java/org/springframework/boot/cli/command/core/HintCommand.java rename spring-boot-cli/src/main/java/org/springframework/boot/cli/command/{ => core}/VersionCommand.java (77%) rename spring-boot-cli/src/main/java/org/springframework/boot/cli/command/{ => grab}/CleanCommand.java (94%) rename spring-boot-cli/src/main/java/org/springframework/boot/cli/command/{ => grab}/GrabCommand.java (85%) rename spring-boot-cli/src/main/java/org/springframework/boot/cli/command/{ => run}/RunCommand.java (92%) rename spring-boot-cli/src/main/java/org/springframework/boot/cli/{runner => command/run}/SpringApplicationRunner.java (98%) rename spring-boot-cli/src/main/java/org/springframework/boot/cli/{runner => command/run}/SpringApplicationRunnerConfiguration.java (91%) create mode 100644 spring-boot-cli/src/main/java/org/springframework/boot/cli/command/shell/AnsiString.java create mode 100644 spring-boot-cli/src/main/java/org/springframework/boot/cli/command/shell/ClearCommand.java rename spring-boot-cli/src/main/java/org/springframework/boot/cli/command/{ => shell}/CommandCompleter.java (78%) create mode 100644 spring-boot-cli/src/main/java/org/springframework/boot/cli/command/shell/EscapeAwareWhiteSpaceArgumentDelimiter.java rename spring-boot-cli/src/main/java/org/springframework/boot/cli/command/{StopCommand.java => shell/ExitCommand.java} (55%) create mode 100644 spring-boot-cli/src/main/java/org/springframework/boot/cli/command/shell/ForkProcessCommand.java rename spring-boot-cli/src/main/java/org/springframework/boot/cli/command/{ => shell}/PromptCommand.java (73%) create mode 100644 spring-boot-cli/src/main/java/org/springframework/boot/cli/command/shell/RunProcessCommand.java create mode 100644 spring-boot-cli/src/main/java/org/springframework/boot/cli/command/shell/Shell.java create mode 100644 spring-boot-cli/src/main/java/org/springframework/boot/cli/command/shell/ShellCommand.java create mode 100644 spring-boot-cli/src/main/java/org/springframework/boot/cli/command/shell/ShellExitException.java rename spring-boot-cli/src/main/java/org/springframework/boot/cli/command/{ => test}/TestCommand.java (84%) rename spring-boot-cli/src/main/java/org/springframework/boot/cli/{testrunner => command/test}/TestRunner.java (97%) rename spring-boot-cli/src/main/java/org/springframework/boot/cli/{testrunner => command/test}/TestRunnerConfiguration.java (88%) rename spring-boot-cli/src/main/resources/META-INF/services/{org.springframework.boot.cli.CommandFactory => org.springframework.boot.cli.command.CommandFactory} (100%) rename spring-boot-cli/src/test/java/org/springframework/boot/cli/{SpringCliTests.java => CommandRunnerTests.java} (63%) create mode 100644 spring-boot-cli/src/test/java/org/springframework/boot/cli/command/shell/EscapeAwareWhiteSpaceArgumentDelimiterTests.java diff --git a/spring-boot-cli/.gitignore b/spring-boot-cli/.gitignore new file mode 100644 index 0000000000..ea8c4bf7f3 --- /dev/null +++ b/spring-boot-cli/.gitignore @@ -0,0 +1 @@ +/target diff --git a/spring-boot-cli/pom.xml b/spring-boot-cli/pom.xml index 9946a4d83f..9240777b4c 100644 --- a/spring-boot-cli/pom.xml +++ b/spring-boot-cli/pom.xml @@ -14,7 +14,6 @@ ${basedir}/.. org.springframework.boot.cli.SpringCli default - 2.11 @@ -53,7 +52,6 @@ jline jline - ${jline.version} net.sf.jopt-simple @@ -320,7 +318,6 @@ - @@ -332,10 +329,10 @@ + message="Customizing homebrew for ${project.version} with checksum ${checksum} in ${repo} repo" /> diff --git a/spring-boot-cli/src/main/java/org/springframework/boot/cli/SpringCliException.java b/spring-boot-cli/src/main/java/org/springframework/boot/cli/CommandException.java similarity index 71% rename from spring-boot-cli/src/main/java/org/springframework/boot/cli/SpringCliException.java rename to spring-boot-cli/src/main/java/org/springframework/boot/cli/CommandException.java index d1143f51ef..bc350f0635 100644 --- a/spring-boot-cli/src/main/java/org/springframework/boot/cli/SpringCliException.java +++ b/spring-boot-cli/src/main/java/org/springframework/boot/cli/CommandException.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2013 the original author or authors. + * Copyright 2012-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,41 +23,41 @@ import java.util.Set; /** * Runtime exception wrapper that defines additional {@link Option}s that are understood - * by the {@link SpringCli}. + * by the {@link CommandRunner}. * * @author Phillip Webb */ -public class SpringCliException extends RuntimeException { +public class CommandException extends RuntimeException { private static final long serialVersionUID = 0L; private final EnumSet