2 * Copyright 2004-2005 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * Gant script that evaluates all installed scripts to create help output
20 * @author Graeme Rocher
25 import org
.codehaus
.groovy
.grails
.commons
.GrailsClassUtils as GCU
27 Ant
.property(environment
:"env")
28 grailsHome
= Ant
.antProject
.properties
."env.GRAILS_HOME"
30 includeTargets
<< new File ( "${grailsHome}/scripts/Init.groovy" )
32 class HelpEvaluatingCategory
{
33 static defaultTask
= ""
34 static target(Object obj
, Map args
, Closure callable
) {
35 def e
= args
.find
{ e
-> e
.key
== "default" }?
.value
40 static getDefaultTask(Object obj
) {
46 File
getHelpFile(File script
) {
47 File helpDir
= new File(grailsTmp
, "help")
48 if (!helpDir
.exists()) helpDir
.mkdir()
49 String scriptname
= script
.getName()
50 return new File(helpDir
, scriptname
.substring(0, scriptname
.lastIndexOf('.')) + ".txt")
53 boolean shouldGenerateHelp(File script
) {
54 File file
= getHelpFile(script
)
55 return (!file
.exists() || file
.lastModified() < script
.lastModified() )
59 target ( 'default' : "Prints out the help for each script") {
60 Ant
.mkdir(dir
:grailsTmp
)
62 resolveResources("file:${grailsHome}/scripts/**.groovy").each
{ if (!it
.file
.name
.startsWith('_')) scripts
<< it
.file
}
63 resolveResources("file:${basedir}/scripts/*.groovy").each
{ if (!it
.file
.name
.startsWith('_')) scripts
<< it
.file
}
65 if(new File("${basedir}/plugins").exists()) {
66 resolveResources("file:${basedir}/plugins/*/scripts/*.groovy").each
{ if (!it
.file
.name
.startsWith('_')) scripts
<< it
.file
}
69 if(new File("${userHome}/.grails/scripts/").exists()) {
70 resolveResources("file:${userHome}/.grails/scripts/*.groovy").each
{ if (!it
.file
.name
.startsWith('_')) scripts
<< it
.file
}
77 def fileName
= GCU
.getNameFromScript(args
) + ".groovy"
78 def file
= scripts
.find
{ it
.name
== fileName
}
82 Usage (optionals marked with *):
85 def gcl
= new GroovyClassLoader()
86 use(HelpEvaluatingCategory
.class) {
87 if (shouldGenerateHelp(file
)) {
89 def script
= gcl
.parseClass(file
).newInstance()
90 script
.binding
= binding
93 def scriptName
= GCU
.getScriptName(file
.name
)
95 helpText
= "grails ${scriptName} -- ${getDefaultTask()}"
96 File helpFile
= getHelpFile(file
)
97 if(!helpFile
.exists())
98 helpFile
.createNewFile()
99 helpFile
.write(helpText
)
102 println
"Warning: Error caching created help for ${file}: ${t.message}"
106 helpText
= getHelpFile(file
).text
113 Usage (optionals marked with *):
114 grails [environment]* [target] [arguments]*
118 grails create-app books
120 Available Targets (type grails help 'target-name' for more info):"""
122 scripts
.unique
{ it
.name
}. sort
{ it
.name
}.each
{ file
->
123 def scriptName
= GCU
.getScriptName(file
.name
)
124 println
"grails ${scriptName}"
129 target( showHelp
: "Show help for a particular command") {
130 def gcl
= new GroovyClassLoader()
131 use(HelpEvaluatingCategory
.class) {
132 if (shouldGenerateHelp(file
)) {
134 def script
= gcl
.parseClass(file
).newInstance()
135 script
.binding
= binding
138 def scriptName
= GCU
.getScriptName(file
.name
)
140 helpText
= "grails ${scriptName} -- ${getDefaultTask()}"
141 getHelpFile(file
).write(helpText
)
144 println
"Error creating help for ${file}: ${t.message}"
145 t
.printStackTrace(System
.out
)
148 helpText
= getHelpFile(file
).text