Grails Goodness: Extending IntegrateWith Command
We can extend the integrate-with
command in Grails to generate files for a custom IDE or build system. We must add a _Events.groovy
file to our Grails projects and then write an implementation for the eventIntegrateWithStart
event. Inside the event we must define a new closure with our code to generate files. The name of the closure must have the following pattern: binding.integrateCustomIdentifier
. The value for CustomIdentifier can be used as an argument for the integrate-with
command.
Suppose we want to extend integrate-with
to generate a simple Sublime Text project file. First we create a template Sublime Text project file where we define folders for a Grails application. We create the folder src/ide-support/sublimetext
and add the file grailsProject.sublimetext-project
with the following contents:
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | { "folders" : [ { "name" : "Domain classes" , "path" : "grails-app/domain" }, { "name" : "Controllers" , "path" : "grails-app/controllers" }, { "name" : "Taglibs" , "path" : "grails-app/taglib" }, { "name" : "Views" , "path" : "grails-app/views" }, { "name" : "Services" , "path" : "grails-app/services" }, { "name" : "Configuration" , "path" : "grails-app/conf" }, { "name" : "grails-app/i18n" , "path" : "grails-app/i18n" }, { "name" : "grails-app/utils" , "path" : "grails-app/utils" }, { "name" : "grails-app/migrations" , "path" : "grails-app/migrations" }, { "name" : "web-app" , "path" : "web-app" }, { "name" : "Scripts" , "path" : "scripts" }, { "name" : "Sources:groovy" , "path" : "src/groovy" }, { "name" : "Sources:java" , "path" : "src/java" }, { "name" : "Tests:integration" , "path" : "test/integration" }, { "name" : "Tests:unit" , "path" : "test/unit" }, { "name" : "All files" , "follow_symlinks" : true , "path" : "." } ] } |
Next we create the file scripts/_Events.groovy
:
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 | includeTargets << grailsScript( "_GrailsInit" ) eventIntegrateWithStart = { // Usage: integrate-with --sublimeText binding.integrateSublimeText = { // Copy template file. ant.copy(todir: basedir) { fileset(dir: "src/ide-support/sublimetext/" ) } // Move template file to real project file with name of Grails application. ant.move(file: "$basedir/grailsProject.sublime-project" , tofile: "$basedir/${grailsAppName}.sublime-project" , overwrite: true ) grailsConsole.updateStatus "Created SublimeText project file" } } |
We are done and can now run the integrate-with
command with the new argument sublimeText
:
1 2 3 | $ grails integrate-with --sublimeText | Created SublimeText project file. $ |
If we open the project in Sublime Text we see our folder structure for a Grails application:
Code written with Grails 2.3.7.
Reference: | Grails Goodness: Extending IntegrateWith Command from our JCG partner Hubert Ikkink at the JDriven blog. |