Created
May 29, 2018 04:29
-
-
Save eliasfeijo/41e344c387a8fa199e061f451e4cf51e to your computer and use it in GitHub Desktop.
Grails 2.x run-script with arguments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2005-2018 the original author or authors. | |
* 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. | |
*/ | |
/* | |
* This script is almost a copy from grails-2.5.5/scripts/RunScript.groovy | |
* I made this because the original one does not support arguments, which is sad | |
* | |
* Paste this script inside your application's scripts directory (grails-app/scripts) | |
* Usage example: | |
* cd myProjectDir | |
* echo "println(params)" > scripts/HelloScript.groovy | |
* grails run-single-script scripts/HelloScript.groovy foo bar=baz | |
* The output will produce the following line: | |
* [foo, bar=baz] | |
*/ | |
includeTargets << grailsScript("_GrailsBootstrap") | |
def description = | |
'''\n Run a single script file within grails Context\ | |
\n Arguments are binded inside the script file as the 'params' List variable\ | |
''' | |
target(default: description) { | |
depends(checkVersion, configureProxy, compile, bootstrap) | |
if (!argsMap.params) { | |
event('StatusError', ['ERROR: Missing arguments (scriptFile)']) | |
System.exit 1 | |
} | |
def persistenceInterceptor = | |
appCtx.containsBean('persistenceInterceptor') ? | |
appCtx.persistenceInterceptor : null | |
persistenceInterceptor?.init() | |
try { | |
def scriptFile = argsMap.params[0] | |
def params = argsMap.params.clone() | |
params.removeAt(0) // Removes scriptFile from params argument | |
event('StatusUpdate', ["Running script $scriptFile ..."]) | |
executeScript(scriptFile, params, classLoader) | |
event('StatusUpdate', ["Script $scriptFile complete!"]) | |
} finally { | |
persistenceInterceptor?.flush() | |
persistenceInterceptor?.destroy() | |
} | |
} | |
def executeScript(scriptFile, params, classLoader){ | |
File script = new File("${basedir}/${scriptFile}") | |
if (!script.exists()) { | |
event('StatusError', ["Designated script doesn't exist: $scriptFile"]) | |
return | |
} | |
def mapBinding = [:] | |
mapBinding.ctx = appCtx | |
mapBinding.grailsApplication = grailsApp | |
mapBinding.params = params | |
def shell = new GroovyShell(classLoader, new Binding(mapBinding)) | |
shell.evaluate script.getText('UTF-8') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment