16
16
17
17
package org .springframework .boot .cli .command ;
18
18
19
+ import groovy .lang .Closure ;
19
20
import groovy .lang .GroovyClassLoader ;
20
21
import groovy .lang .Script ;
21
22
22
23
import java .io .File ;
23
24
import java .util .List ;
25
+ import java .util .Map ;
24
26
import java .util .ServiceLoader ;
25
27
26
28
import joptsimple .OptionSet ;
@@ -87,6 +89,8 @@ protected void run(OptionSet options) throws Exception {
87
89
options , this , repositoryConfiguration );
88
90
89
91
this .compiler = new GroovyCompiler (configuration );
92
+ this .compiler
93
+ .addCompilationCustomizers (new ScriptCompilationCustomizer ());
90
94
loader = this .compiler .getLoader ();
91
95
Thread .currentThread ().setContextClassLoader (loader );
92
96
@@ -102,7 +106,25 @@ protected void run(OptionSet options) throws Exception {
102
106
if (this .compiler != null && files .length > 0 ) {
103
107
Class <?>[] classes = this .compiler .compile (files );
104
108
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 )) {
106
128
((Script ) type .newInstance ()).run ();
107
129
}
108
130
}
@@ -121,7 +143,6 @@ protected void run(OptionSet options) throws Exception {
121
143
}
122
144
123
145
}
124
-
125
146
}
126
147
127
148
private static class InitGroovyCompilerConfigurationAdapter extends
@@ -138,4 +159,8 @@ public GroovyCompilerScope getScope() {
138
159
}
139
160
}
140
161
162
+ public static interface Commands {
163
+ Map <String , Closure <?>> getCommands ();
164
+ }
165
+
141
166
}
0 commit comments