Skip to content

Commit

Permalink
Grails Console: Fixes BUG: The run-script command does not work if gr…
Browse files Browse the repository at this point in the history
…ails.codegen.defaultPackage is set

This is happening because Grails is attempting to apply compilation customizers to CompilerConfiguration.DEFAULT, but as of Groovy 2.5.7 the implementation of CompilerConfiguration provided by CompilerConfiguration.DEFAULT is immutable. see https://issues.apache.org/jira/browse/GROOVY-9100
  • Loading branch information
rainboyan committed May 22, 2023
1 parent f8962c9 commit eb5bc0b
Showing 1 changed file with 7 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import grails.config.Config
import grails.core.GrailsApplication
import grails.persistence.support.PersistenceContextInterceptor
import grails.ui.support.DevelopmentGrails
import grails.util.BuildSettings

/**
* Used to run Grails scripts within the context of a Grails application
Expand All @@ -43,25 +44,19 @@ class GrailsApplicationScriptRunner extends DevelopmentGrails {

@Override
ConfigurableApplicationContext run(String... args) {
ConfigurableApplicationContext ctx
try {
ctx = super.run(args)
}
catch (Throwable e) {
System.err.println("Context failed to load: $e.message")
System.exit(1)
}
ConfigurableApplicationContext ctx = super.run(args)

Binding binding = new Binding()
binding.setVariable('ctx', ctx)

Config config = ctx.getBean('grailsApplication', GrailsApplication).config
String defaultPackageKey = 'grails.codegen.defaultPackage'
String defaultPackageName = config.getProperty(defaultPackageKey, String)
GroovyShell sh
CompilerConfiguration configuration = CompilerConfiguration.DEFAULT
if (config.containsProperty(defaultPackageKey)) {
CompilerConfiguration configuration = new CompilerConfiguration()
if (defaultPackageName) {
ImportCustomizer importCustomizer = new ImportCustomizer()
importCustomizer.addStarImports config.getProperty(defaultPackageKey, String)
importCustomizer.addStarImports(defaultPackageName)
configuration.addCompilationCustomizers(importCustomizer)
}
sh = new GroovyShell(binding, configuration)
Expand Down Expand Up @@ -117,7 +112,7 @@ class GrailsApplicationScriptRunner extends DevelopmentGrails {
String[] scriptNames = args.init() as String[]
List<File> scripts = []
scriptNames.each { String scriptName ->
File script = new File(scriptName)
File script = new File(BuildSettings.BASE_DIR, "src/main/scripts/${scriptName}.groovy")
if (script.exists()) {
scripts.add(script)
}
Expand Down

0 comments on commit eb5bc0b

Please sign in to comment.