Skip to content

Commit 1e75c0a

Browse files
author
Dave Syer
committed
Extend programming model for script commands
1 parent b5db4d3 commit 1e75c0a

File tree

17 files changed

+468
-280
lines changed

17 files changed

+468
-280
lines changed

spring-boot-cli/src/main/java/org/springframework/boot/cli/SpringCli.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,15 @@ public class SpringCli {
5252

5353
private String displayName = CLI_APP + " ";
5454

55+
private InitCommand init;
56+
5557
/**
5658
* Create a new {@link SpringCli} implementation with the default set of commands.
5759
*/
5860
public SpringCli() {
5961
try {
60-
new InitCommand(this).run();
62+
this.init = new InitCommand(this);
63+
this.init.run();
6164
}
6265
catch (Exception e) {
6366
throw new IllegalStateException("Cannot init with those args", e);
@@ -66,6 +69,10 @@ public SpringCli() {
6669
this.commands.add(new HintCommand());
6770
}
6871

72+
public InitCommand getInitCommand() {
73+
return this.init;
74+
}
75+
6976
/**
7077
* Set the command available to the CLI. Primarily used to support testing. NOTE: The
7178
* 'help' command will be automatically provided in addition to this list.

spring-boot-cli/src/main/java/org/springframework/boot/cli/command/InitCommand.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616

1717
package org.springframework.boot.cli.command;
1818

19+
import groovy.lang.Closure;
1920
import groovy.lang.GroovyClassLoader;
2021
import groovy.lang.Script;
2122

2223
import java.io.File;
2324
import java.util.List;
25+
import java.util.Map;
2426
import java.util.ServiceLoader;
2527

2628
import joptsimple.OptionSet;
@@ -87,6 +89,8 @@ protected void run(OptionSet options) throws Exception {
8789
options, this, repositoryConfiguration);
8890

8991
this.compiler = new GroovyCompiler(configuration);
92+
this.compiler
93+
.addCompilationCustomizers(new ScriptCompilationCustomizer());
9094
loader = this.compiler.getLoader();
9195
Thread.currentThread().setContextClassLoader(loader);
9296

@@ -102,7 +106,25 @@ protected void run(OptionSet options) throws Exception {
102106
if (this.compiler != null && files.length > 0) {
103107
Class<?>[] classes = this.compiler.compile(files);
104108
for (Class<?> type : classes) {
105-
if (Script.class.isAssignableFrom(type)) {
109+
Command script = ScriptCommand.command(type);
110+
if (script != null) {
111+
this.cli.register(script);
112+
}
113+
else if (CommandFactory.class.isAssignableFrom(type)) {
114+
for (Command command : ((CommandFactory) type.newInstance())
115+
.getCommands(this.cli)) {
116+
this.cli.register(command);
117+
}
118+
}
119+
else if (Commands.class.isAssignableFrom(type)) {
120+
Map<String, Closure<?>> commands = ((Commands) type.newInstance())
121+
.getCommands();
122+
for (String command : commands.keySet()) {
123+
this.cli.register(new ScriptCommand(command, commands
124+
.get(command)));
125+
}
126+
}
127+
else if (Script.class.isAssignableFrom(type)) {
106128
((Script) type.newInstance()).run();
107129
}
108130
}
@@ -121,7 +143,6 @@ protected void run(OptionSet options) throws Exception {
121143
}
122144

123145
}
124-
125146
}
126147

127148
private static class InitGroovyCompilerConfigurationAdapter extends
@@ -138,4 +159,8 @@ public GroovyCompilerScope getScope() {
138159
}
139160
}
140161

162+
public static interface Commands {
163+
Map<String, Closure<?>> getCommands();
164+
}
165+
141166
}

0 commit comments

Comments
 (0)