From 3e6eb6fec89ecf5bd8f2cbc5309e53d5e4674228 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Tue, 14 Jan 2014 13:27:40 +0000 Subject: [PATCH] Remove support for InitCommand outside REPL In this commit we retain "init" as a command inside the ShellCommand but not on the bash command line. Seems to have an impact on performance so relevant to gh-212. --- .../springframework/boot/cli/SpringCli.java | 18 +++++------------- .../boot/cli/command/InitCommand.java | 2 +- .../boot/cli/command/ShellCommand.java | 2 +- .../boot/cli/command/InitCommandTests.java | 2 +- .../boot/cli/command/ScriptCommandTests.java | 2 +- 5 files changed, 9 insertions(+), 17 deletions(-) diff --git a/spring-boot-cli/src/main/java/org/springframework/boot/cli/SpringCli.java b/spring-boot-cli/src/main/java/org/springframework/boot/cli/SpringCli.java index c90ebbf573..7aec4592b9 100644 --- a/spring-boot-cli/src/main/java/org/springframework/boot/cli/SpringCli.java +++ b/spring-boot-cli/src/main/java/org/springframework/boot/cli/SpringCli.java @@ -24,10 +24,10 @@ import java.util.EnumSet; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.ServiceLoader; import java.util.Set; import org.springframework.boot.cli.command.AbstractCommand; -import org.springframework.boot.cli.command.InitCommand; /** * Spring Command Line Interface. This is the main entry-point for the Spring command line @@ -54,28 +54,20 @@ public class SpringCli { private String displayName = CLI_APP + " "; - private InitCommand init; - private Map commandMap = new HashMap(); /** * Create a new {@link SpringCli} implementation with the default set of commands. */ public SpringCli(String... args) { - try { - this.init = new InitCommand(this); - this.init.run(args); - } - catch (Exception e) { - throw new IllegalStateException("Cannot init with those args", e); + for (CommandFactory factory : ServiceLoader.load(CommandFactory.class)) { + for (Command command : factory.getCommands(this)) { + register(command); + } } addBaseCommands(); } - public InitCommand getInitCommand() { - return this.init; - } - /** * Set the command available to the CLI. Primarily used to support testing. NOTE: The * 'help' command will be automatically provided in addition to this list. diff --git a/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/InitCommand.java b/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/InitCommand.java index f323f8e47e..5443be79a5 100644 --- a/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/InitCommand.java +++ b/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/InitCommand.java @@ -154,7 +154,7 @@ public class InitCommand extends OptionParsingCommand { enhanced = true; } - if (this.cli.getCommands().isEmpty() || enhanced) { + if (enhanced) { for (CommandFactory factory : ServiceLoader.load(CommandFactory.class, loader)) { diff --git a/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/ShellCommand.java b/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/ShellCommand.java index a7f4c50a23..31d67e38ae 100644 --- a/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/ShellCommand.java +++ b/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/ShellCommand.java @@ -99,7 +99,7 @@ public class ShellCommand extends AbstractCommand { PromptCommand prompt = new PromptCommand(this); cli.register(prompt); - cli.register(cli.getInitCommand()); + cli.register(new InitCommand(cli)); } private ConsoleReader createConsoleReader() throws IOException { diff --git a/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/InitCommandTests.java b/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/InitCommandTests.java index e211e4a39a..72e98f321d 100644 --- a/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/InitCommandTests.java +++ b/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/InitCommandTests.java @@ -99,7 +99,7 @@ public class InitCommandTests { @Test public void runOptions() throws Exception { SpringCli cli = new SpringCli(); - InitCommand command = cli.getInitCommand(); + InitCommand command = new InitCommand(cli); command.run("src/test/resources/commands/options.groovy"); cli.find("foo").run("--foo=bar", "--bar=123"); assertTrue(this.output.toString().contains("Hello Foo: bar=123")); diff --git a/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/ScriptCommandTests.java b/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/ScriptCommandTests.java index d69aab8eee..818667248a 100644 --- a/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/ScriptCommandTests.java +++ b/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/ScriptCommandTests.java @@ -44,7 +44,7 @@ public class ScriptCommandTests { public void init() { this.classLoader = Thread.currentThread().getContextClassLoader(); this.cli = new SpringCli(); - this.init = this.cli.getInitCommand(); + this.init = new InitCommand(this.cli); executed = false; }