/
InteractiveMode.groovy
359 lines (315 loc) · 12.6 KB
/
InteractiveMode.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
* Copyright 2011 SpringSource
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.codehaus.groovy.grails.cli.interactive
import grails.build.logging.GrailsConsole
import grails.util.BuildSettings
import grails.util.BuildSettingsHolder
import grails.util.Environment
import groovy.transform.CompileStatic
import org.codehaus.groovy.grails.cli.fork.ForkedGrailsProcess
import org.codehaus.groovy.grails.cli.fork.testing.ForkedGrailsTestRunner
import java.awt.Desktop
import org.codehaus.groovy.grails.cli.GrailsScriptRunner
import org.codehaus.groovy.grails.cli.ScriptExitException
import org.codehaus.groovy.grails.cli.ScriptNotFoundException
import org.codehaus.groovy.grails.cli.parsing.CommandLine
import org.codehaus.groovy.grails.cli.parsing.ParseException
import org.codehaus.groovy.grails.cli.support.MetaClassRegistryCleaner
import org.codehaus.groovy.grails.cli.api.BaseSettingsApi
/**
* Provides the implementation of interactive mode in Grails.
*
* @author Graeme Rocher
* @since 2.0
*/
class InteractiveMode {
static final List FIXED_OPEN_OPTIONS = Collections.unmodifiableList([ 'test-report', 'dep-report' ])
/** Use this to split strings on unescaped whitespace. */
static final String ARG_SPLIT_PATTERN = /(?<!\\)\s+/
static InteractiveMode current
@Delegate GrailsConsole console = GrailsConsole.getInstance()
GrailsScriptRunner scriptRunner
BuildSettings settings
boolean interactiveModeActive = false
def grailsServer
/** Options supported by the 'open' command. */
def openOptions
protected MetaClassRegistryCleaner registryCleaner = MetaClassRegistryCleaner.createAndRegister()
protected GrailsInteractiveCompletor interactiveCompletor
InteractiveMode(BuildSettings settings, GrailsScriptRunner scriptRunner) {
this.scriptRunner = scriptRunner
this.settings = settings
this.interactiveCompletor = new GrailsInteractiveCompletor(settings, scriptRunner.availableScripts)
BuildSettingsHolder.settings = settings
GroovySystem.getMetaClassRegistry().addMetaClassRegistryChangeEventListener(registryCleaner)
// Initialise the command options map supported by the 'open' command.
openOptions = [
'test-report': [
path: new File(settings.testReportsDir, "html/index.html").absolutePath,
description: "Opens the current test report (if it exists)" ],
'dep-report': [
path: new File(settings.projectTargetDir, "dependency-report/index.html").absolutePath,
description: "Opens the current dependency report (if it exists)"] ]
}
void setGrailsServer(grailsServer) {
addStatus "Application loaded in interactive mode. Type 'stop-app' to shutdown."
this.grailsServer = grailsServer
}
static boolean isActive() {
getCurrent() != null && getCurrent().interactiveModeActive
}
void run() {
current = this
System.setProperty("grails.disable.exit", "true") // you can't exit completely in interactive mode from a script
console.reader.addCompleter(interactiveCompletor)
interactiveModeActive = true
System.setProperty(Environment.INTERACTIVE_MODE_ENABLED, "true")
new BaseSettingsApi(settings, false).enableUaa()
String originalGrailsEnv = System.getProperty(Environment.KEY)
String originalGrailsEnvDefault = System.getProperty(Environment.DEFAULT)
addStatus("Enter a script name to run. Use TAB for completion: ")
Runtime.addShutdownHook {
final files = settings.projectWorkDir.listFiles()
if (files) {
final toDelete = files.findAll { File f -> f.name.endsWith("-resume") && f.directory }
toDelete*.delete()
}
}
startBackgroundTestRunner()
while (interactiveModeActive) {
String scriptName = showPrompt()
if (scriptName == null) {
goodbye()
}
try {
def trimmed = scriptName.trim()
if (trimmed) {
if (trimmed.startsWith("create-app")) {
error "You cannot create an application in interactive mode."
}
else if (trimmed.startsWith("install-plugin")) {
error "You cannot install a plugin in interactive mode."
}
else if (trimmed.startsWith("uninstall-plugin")) {
error "You cannot uninstall a plugin in interactive mode."
}
else if ("quit".equals(trimmed)) {
quit()
} else if ('stop-app'.equals(trimmed)) {
stopApp()
} else if ("exit".equals(trimmed)) {
exit()
}
else if ("restart-daemon".equals(trimmed)) {
restartDaemon()
}
else if ("start-daemon".equals(trimmed)) {
restartDaemon()
}
else if (scriptName.startsWith("open ")) {
open scriptName
}
else if ("!".equals(trimmed)) {
bang()
}
else if (scriptName.startsWith("!")) {
execute scriptName
}
else {
try {
parseAndExecute(scriptName)
} catch (ParseException e) {
error "Invalid command: $e.message"
}
}
}
}
catch (ScriptExitException e) {
// do nothing. just return to consuming input
}
catch (ScriptNotFoundException e) {
error "Script not found for name $scriptName"
} catch (Throwable e) {
error "Error running script $scriptName: $e.message", e
}
finally {
// Reset Environment after each script run
if (originalGrailsEnv != null) {
System.setProperty(Environment.KEY, originalGrailsEnv)
} else {
System.clearProperty(Environment.KEY)
}
if (originalGrailsEnvDefault != null) {
System.setProperty(Environment.DEFAULT, originalGrailsEnvDefault)
} else {
System.clearProperty(Environment.DEFAULT)
}
if (grailsServer == null) {
try {
registryCleaner.clean()
} catch (ignored) {
// ignore
}
}
}
}
interactiveModeActive = false
System.setProperty(Environment.INTERACTIVE_MODE_ENABLED, "true")
}
boolean backgroundTestRunnerStarted = false
@CompileStatic
protected void startBackgroundTestRunner() {
if (backgroundTestRunnerStarted || console.isWindows()) return
backgroundTestRunnerStarted = true
Thread.start {
// start a background JVM ready to run tests
if (settings.forkSettings.test) {
final runner = new ForkedGrailsTestRunner(settings)
if (settings.forkSettings.test instanceof Map) {
runner.configure((Map) settings.forkSettings.test)
}
if (runner.isForkingReserveEnabled()) {
runner.forkReserve()
}
else if (runner.daemon) {
runner.restartDaemon()
}
}
}
}
@CompileStatic
void restartDaemon() {
final runner = new ForkedGrailsTestRunner(settings)
if (settings.forkSettings.test instanceof Map) {
runner.configure((Map) settings.forkSettings.test)
}
runner.restartDaemon()
}
protected void quit() {
exit true
}
protected void goodbye() {
updateStatus "Goodbye"
System.exit(0)
}
protected void stopApp() {
if (settings.forkSettings?.get('run')) {
parseAndExecute 'stop-app'
} else if (grailsServer) {
try {
updateStatus "Stopping Grails server"
grailsServer.stop()
} catch (e) {
error "Error stopping server: $e.message", e
}
finally {
grailsServer = null
}
}
}
protected void exit(boolean shouldStopApp = false) {
if (shouldStopApp) {
stopApp()
}
goodbye()
}
protected void open(String scriptName) {
if (!Desktop.isDesktopSupported()) {
error "The Desktop API isn't supported for this platform"
return
}
String fileName = scriptName[5..-1].trim()
try {
final Desktop desktop = Desktop.getDesktop()
final args = fileName.split(ARG_SPLIT_PATTERN)
for (arg in args) {
arg = unescape(arg)
// Is this arg one of the fixed options for 'open'?
def fixedOption = openOptions.find { option, value ->
// No match if a file matching the name of the
// option exists.
arg == option && !new File(option).exists()
}
File file = new File(fixedOption ? fixedOption.value.path : arg)
if (file.exists()) {
desktop.open file
}
else {
error "File $arg does not exist"
}
}
} catch (e) {
error "Could not open file $fileName: $e.message"
}
}
protected void bang() {
def history = console.reader.history
//move one step back to !
history.previous()
if (!history.previous()) {
error "! not valid. Can not repeat without history"
return
}
//another step to previous command
String scriptName = history.current()
if (scriptName.startsWith("!")) {
error "Can not repeat command: $scriptName"
}
else {
try {
parseAndExecute(scriptName)
} catch (ParseException e) {
error "Invalid command: $e.message"
}
}
}
protected void execute(String scriptName) {
try {
def args = scriptName[1..-1].split(ARG_SPLIT_PATTERN).collect { unescape(it) }
def process = new ProcessBuilder(args).redirectErrorStream(true).start()
log process.inputStream.getText('UTF-8')
} catch (e) {
error "Error occurred executing process: $e.message"
}
}
void parseAndExecute(String scriptName) throws ParseException {
def parser = GrailsScriptRunner.getCommandLineParser()
def commandLine = parser.parseString(scriptName)
prepareConsole(commandLine)
if (commandLine.hasOption(CommandLine.DEBUG_FORK)) {
System.setProperty(ForkedGrailsProcess.DEBUG_FORK, "true")
}
else {
System.setProperty(ForkedGrailsProcess.DEBUG_FORK, "false")
}
scriptRunner.executeScriptWithCaching(commandLine)
}
void prepareConsole(commandLine) {
final console = GrailsConsole.instance
console.stacktrace = commandLine.hasOption(CommandLine.STACKTRACE_ARGUMENT)
console.verbose = commandLine.hasOption(CommandLine.VERBOSE_ARGUMENT)
}
/**
* Removes '\' escape characters from the given string.
*/
protected unescape(String str) {
return str.replace('\\', '')
}
void refresh() {
final scripts = scriptRunner.getAvailableScripts()
interactiveCompletor.setStrings(new TreeSet<String>(GrailsInteractiveCompletor.getScriptNames(scripts).toList()))
}
}