Skip to content

Commit

Permalink
Grails Console: use GrailsConsoleLogger when running application comm…
Browse files Browse the repository at this point in the history
…ands or scripts
  • Loading branch information
rainboyan committed May 22, 2023
1 parent 352dc7f commit e1f963e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand All @@ -44,32 +47,31 @@ 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)
if (skipBootstrap instanceof Boolean && !System.getProperty(Settings.SETTING_SKIP_BOOTSTRAP)) {
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 {
Expand All @@ -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
}

/**
Expand All @@ -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')
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -35,6 +36,8 @@ import grails.util.BuildSettings
@CompileStatic
class GrailsApplicationScriptRunner extends DevelopmentGrails {

static GrailsConsole console = GrailsConsole.getInstance()

List<File> scripts

private GrailsApplicationScriptRunner(List<File> scripts, Class<?>... sources) {
Expand Down Expand Up @@ -64,19 +67,20 @@ class GrailsApplicationScriptRunner extends DevelopmentGrails {
Collection<PersistenceContextInterceptor> 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")
}
}
}
Expand Down Expand Up @@ -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<File> scripts = []
Expand All @@ -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)
}
}

Expand Down

0 comments on commit e1f963e

Please sign in to comment.