1515 */
1616package grails.ui.script
1717
18+ import grails.config.Config
19+ import grails.core.GrailsApplication
20+ import grails.persistence.support.PersistenceContextInterceptor
1821import grails.ui.support.DevelopmentGrailsApplication
1922import groovy.transform.CompileStatic
23+ import org.codehaus.groovy.control.CompilerConfiguration
24+ import org.codehaus.groovy.control.customizers.ImportCustomizer
2025import org.springframework.context.ConfigurableApplicationContext
2126/**
2227 * Used to run Grails scripts within the context of a Grails application
@@ -26,15 +31,15 @@ import org.springframework.context.ConfigurableApplicationContext
2631 */
2732@CompileStatic
2833class GrailsApplicationScriptRunner extends DevelopmentGrailsApplication {
29- File script
34+ List< File > scripts
3035
31- private GrailsApplicationScriptRunner (File script , Object ... sources ) {
36+ private GrailsApplicationScriptRunner (List< File > scripts , Object ... sources ) {
3237 super (sources)
33- this . script = script
38+ this . scripts = scripts
3439 }
3540
3641 @Override
37- ConfigurableApplicationContext run (String ... args ) {
42+ ConfigurableApplicationContext run (String ...args ) {
3843 ConfigurableApplicationContext ctx
3944 try {
4045 ctx = super . run(args)
@@ -45,39 +50,77 @@ class GrailsApplicationScriptRunner extends DevelopmentGrailsApplication {
4550
4651 def binding = new Binding ()
4752 binding. setVariable(" ctx" , ctx)
48- try {
49- new GroovyShell (binding). evaluate(script)
50- } catch (Throwable e) {
51- System . err. println (" Script execution error: $e. message " )
52- System . exit(1 )
53+
54+ Config config = ctx. getBean(' grailsApplication' , GrailsApplication ). config
55+ String defaultPackageKey = ' grails.codegen.defaultPackage'
56+ GroovyShell sh
57+ CompilerConfiguration configuration = CompilerConfiguration . DEFAULT
58+ if (config. containsProperty(defaultPackageKey)) {
59+ ImportCustomizer importCustomizer = new ImportCustomizer ()
60+ importCustomizer. addStarImports config. getProperty(defaultPackageKey, String )
61+ configuration. addCompilationCustomizers(importCustomizer)
5362 }
54- finally {
63+ sh = new GroovyShell (binding, configuration)
64+
65+ Collection<PersistenceContextInterceptor > interceptors = ctx. getBeansOfType(PersistenceContextInterceptor ). values()
66+
67+ try {
68+ scripts. each {
69+ try {
70+ for (i in interceptors) {
71+ i. init()
72+ }
73+ sh. evaluate(it)
74+ for (i in interceptors) {
75+ i. destroy()
76+ }
77+ } catch (Throwable e) {
78+ System . err. println (" Script execution error: $e. message " )
79+ System . exit(1 )
80+ }
81+ }
82+ } finally {
5583 try {
84+ for (i in interceptors) {
85+ i. destroy()
86+ }
5687 ctx?. close()
5788 } catch (Throwable e) {
5889 // ignore
5990 }
6091 }
92+
93+
6194 return ctx
6295 }
6396 /**
6497 * Main method to run an existing Application class
6598 *
66- * @param args The first argument is the Application class name
99+ * @param args The last argument is the Application class name. All other args are script names
67100 */
68101 public static void main (String [] args ) {
69102 if (args. size() > 1 ) {
70- def applicationClass = Thread . currentThread(). contextClassLoader. loadClass(args[0 ])
71- File script = new File (args[1 ]);
72- if (script. exists()) {
73- new GrailsApplicationScriptRunner (script, applicationClass). run(args[1 .. -1 ] as String [])
74- }
75- else {
76- System . err. println (" Specified script [$script ] does not exist" )
103+ Class applicationClass
104+ try {
105+ applicationClass = Thread . currentThread(). contextClassLoader. loadClass(args. last())
106+ } catch (Throwable e) {
107+ System . err. println (" Application class not found" )
77108 System . exit(1 )
78109 }
79- }
80- else {
110+ String [] scriptNames = args. init() as String []
111+ List<File > scripts = []
112+ scriptNames. each { String scriptName ->
113+ File script = new File (scriptName)
114+ if (script. exists()) {
115+ scripts. add(script)
116+ } else {
117+ System . err. println (" Specified script [${ scriptName} ] not found" )
118+ System . exit(1 )
119+ }
120+ }
121+
122+ new GrailsApplicationScriptRunner (scripts, applicationClass). run(args)
123+ } else {
81124 System . err. println (" Missing application class name and script name arguments" )
82125 System . exit(1 )
83126 }
0 commit comments