diff --git a/grails-console/src/main/groovy/grails/ui/command/GrailsApplicationCommandRunner.groovy b/grails-console/src/main/groovy/grails/ui/command/GrailsApplicationCommandRunner.groovy index 2e47275318..af1cc45aef 100644 --- a/grails-console/src/main/groovy/grails/ui/command/GrailsApplicationCommandRunner.groovy +++ b/grails-console/src/main/groovy/grails/ui/command/GrailsApplicationCommandRunner.groovy @@ -15,6 +15,7 @@ */ package grails.ui.command +import grails.build.logging.GrailsConsole import groovy.transform.CompileStatic import org.springframework.beans.factory.config.AutowireCapableBeanFactory import org.springframework.context.ConfigurableApplicationContext @@ -35,6 +36,8 @@ import org.grails.build.parsing.CommandLineParser @CompileStatic class GrailsApplicationCommandRunner extends DevelopmentGrails { + static GrailsConsole console = GrailsConsole.getInstance() + String commandName protected GrailsApplicationCommandRunner(String commandName, Class... sources) { @@ -44,6 +47,7 @@ class GrailsApplicationCommandRunner extends DevelopmentGrails { @Override ConfigurableApplicationContext run(String... args) { + ConfigurableApplicationContext ctx = null ApplicationCommand command = ApplicationContextCommandRegistry.findCommand(commandName) if (command) { Object skipBootstrap = command.hasProperty('skipBootstrap')?.getProperty(command) @@ -51,25 +55,23 @@ class GrailsApplicationCommandRunner extends DevelopmentGrails { System.setProperty(Settings.SETTING_SKIP_BOOTSTRAP, skipBootstrap.toString()) } - ConfigurableApplicationContext ctx = null try { ctx = super.run(args) } catch (Throwable e) { - System.err.println("Context failed to load: $e.message") - System.exit(1) + console.error("Context failed to load: $e.message") } try { + console.addStatus("Command :$command.name") CommandLine commandLine = new CommandLineParser().parse(args) ctx.autowireCapableBeanFactory.autowireBeanProperties(command, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false) command.applicationContext = ctx boolean result = command.handle(new ExecutionContext(commandLine)) - result ? System.exit(0) : System.exit(1) + result ? console.addStatus('EXECUTE SUCCESSFUL') : console.error('EXECUTE FAILED', '') } catch (Throwable e) { - System.err.println("Command execution error: $e.message") - System.exit(1) + console.error("Command execution error: $e.message") } finally { try { @@ -80,10 +82,9 @@ class GrailsApplicationCommandRunner extends DevelopmentGrails { } } else { - System.err.println("Command not found for name: $commandName") - System.exit(1) + console.error("Command not found for name: $commandName") } - null + ctx } /** @@ -98,16 +99,15 @@ class GrailsApplicationCommandRunner extends DevelopmentGrails { applicationClass = Thread.currentThread().contextClassLoader.loadClass(args.last()) } catch (Throwable e) { - System.err.println('Application class not found') - System.exit(1) + console.error('Application class not found') + System.exit(0) } GrailsApplicationCommandRunner runner = new GrailsApplicationCommandRunner(args[0], applicationClass) runner.run(args.init() as String[]) } else { - System.err.println('Missing application class name and script name arguments') - System.exit(1) + console.error('Missing application class name and script name arguments') } } diff --git a/grails-console/src/main/groovy/grails/ui/script/GrailsApplicationScriptRunner.groovy b/grails-console/src/main/groovy/grails/ui/script/GrailsApplicationScriptRunner.groovy index e7b02f57ba..22b4363271 100644 --- a/grails-console/src/main/groovy/grails/ui/script/GrailsApplicationScriptRunner.groovy +++ b/grails-console/src/main/groovy/grails/ui/script/GrailsApplicationScriptRunner.groovy @@ -20,6 +20,7 @@ import org.codehaus.groovy.control.CompilerConfiguration import org.codehaus.groovy.control.customizers.ImportCustomizer import org.springframework.context.ConfigurableApplicationContext +import grails.build.logging.GrailsConsole import grails.config.Config import grails.core.GrailsApplication import grails.persistence.support.PersistenceContextInterceptor @@ -35,6 +36,8 @@ import grails.util.BuildSettings @CompileStatic class GrailsApplicationScriptRunner extends DevelopmentGrails { + static GrailsConsole console = GrailsConsole.getInstance() + List scripts private GrailsApplicationScriptRunner(List scripts, Class... sources) { @@ -64,19 +67,20 @@ class GrailsApplicationScriptRunner extends DevelopmentGrails { Collection interceptors = ctx.getBeansOfType(PersistenceContextInterceptor).values() try { - scripts.each { + for (File script in scripts) { try { + console.addStatus("Script :$script.name") for (i in interceptors) { i.init() } - sh.evaluate(it) + sh.evaluate(script) for (i in interceptors) { i.destroy() } + console.updateStatus('EXECUTE SUCCESSFUL') } catch (Throwable e) { - System.err.println("Script execution error: $e.message") - System.exit(1) + console.error("Script execution error: $e.message") } } } @@ -106,8 +110,8 @@ class GrailsApplicationScriptRunner extends DevelopmentGrails { applicationClass = Thread.currentThread().contextClassLoader.loadClass(args.last()) } catch (Throwable ignored) { - System.err.println('Application class not found') - System.exit(1) + console.error('Application class not found') + System.exit(0) } String[] scriptNames = args.init() as String[] List scripts = [] @@ -117,16 +121,16 @@ class GrailsApplicationScriptRunner extends DevelopmentGrails { scripts.add(script) } else { - System.err.println("Specified script [${scriptName}] not found") - System.exit(1) + console.error("Specified script [${scriptName}] not found") + System.exit(0) } } new GrailsApplicationScriptRunner(scripts, applicationClass).run(args) } else { - System.err.println('Missing application class name and script name arguments') - System.exit(1) + console.error('Missing application class name and script name arguments') + System.exit(0) } }