grails g:if
我们可以在Grails中扩展integrate-with
命令,以生成用于自定义IDE或构建系统的文件。 我们必须将_Events.groovy
文件添加到我们的Grails项目中,然后为eventIntegrateWithStart
事件编写一个实现。 在事件内部,我们必须使用代码定义一个新的闭包以生成文件。 闭包的名称必须具有以下模式: binding.integrate CustomIdentifier
。 CustomIdentifier的值可用作integrate-with
命令的参数。
假设我们要扩展“ integrate-with
以生成一个简单的Sublime Text项目文件。 首先,我们创建一个模板Sublime Text项目文件,在其中为Grails应用程序定义文件夹。 我们创建文件夹src/ide-support/sublimetext
并添加文件grailsProject.sublimetext-project
,其内容如下:
{
"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": "."
}
]
}
接下来,我们创建文件scripts/_Events.groovy
:
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"
}
}
我们已经完成,现在可以integrate-with
带有新参数sublimeText
的integrate-with
命令运行:
$ grails integrate-with --sublimeText
| Created SublimeText project file.
$
如果在Sublime Text中打开项目,我们将看到Grails应用程序的文件夹结构:
用Grails 2.3.7编写的代码。
翻译自: https://www.javacodegeeks.com/2014/04/grails-goodness-extending-integratewith-command.html
grails g:if