GRAILS-1019: Allowing expressions to be used with the 'disabled' attribute for g...
[grails.git] / scripts / Help.groovy
blob1f190b634ed5dbdf113eb82e77a48318e76773c6
1 /*
2 * Copyright 2004-2005 the original author or authors.
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
17 /**
18 * Gant script that evaluates all installed scripts to create help output
20 * @author Graeme Rocher
22 * @since 0.4
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
36 if(e) {
37 defaultTask = e
40 static getDefaultTask(Object obj) {
41 return defaultTask
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)
61 def scripts = []
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 }
73 def helpText = ""
76 if(args) {
77 def fileName = GCU.getNameFromScript(args) + ".groovy"
78 def file = scripts.find { it.name == fileName }
81 println """
82 Usage (optionals marked with *):
83 grails [environment]*
84 """
85 def gcl = new GroovyClassLoader()
86 use(HelpEvaluatingCategory.class) {
87 if (shouldGenerateHelp(file)) {
88 try {
89 def script = gcl.parseClass(file).newInstance()
90 script.binding = binding
91 script.run()
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)
101 catch(Throwable t) {
102 println "Warning: Error caching created help for ${file}: ${t.message}"
103 println helpText
105 } else {
106 helpText = getHelpFile(file).text
108 println helpText
111 else {
112 println """
113 Usage (optionals marked with *):
114 grails [environment]* [target] [arguments]*
116 Examples:
117 grails dev run-app
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)) {
133 try {
134 def script = gcl.parseClass(file).newInstance()
135 script.binding = binding
136 script.run()
138 def scriptName = GCU.getScriptName(file.name)
140 helpText = "grails ${scriptName} -- ${getDefaultTask()}"
141 getHelpFile(file).write(helpText)
143 catch(Throwable t) {
144 println "Error creating help for ${file}: ${t.message}"
145 t.printStackTrace(System.out)
147 } else {
148 helpText = getHelpFile(file).text
150 println helpText