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 generates a CRUD controller and matching views for a given domain class
20 * @author Graeme Rocher
25 import org
.codehaus
.groovy
.grails
.commons
.GrailsClassUtils as GCU
26 import groovy
.text
.SimpleTemplateEngine
27 import org
.codehaus
.groovy
.grails
.commons
.spring
.GrailsRuntimeConfigurator
;
28 import org
.codehaus
.groovy
.grails
.scaffolding
.*
29 import org
.springframework
.mock
.web
.MockServletContext
30 import org
.springframework
.core
.io
.Resource
31 import org
.codehaus
.groovy
.grails
.commons
.ApplicationHolder
;
34 Ant
.property(environment
:"env")
35 grailsHome
= Ant
.antProject
.properties
."env.GRAILS_HOME"
37 includeTargets
<< new File ( "${grailsHome}/scripts/Bootstrap.groovy" )
40 generateController
= true
42 target ('default': "Generates a CRUD interface (controller + views) for a domain class") {
43 depends( checkVersion
, packageApp
)
44 typeName
= "Domain Class"
49 target(generateAll
:"The implementation target") {
51 rootLoader
.addURL(classesDir
.toURI().toURL())
54 def name
= args
.trim()
55 name
= name
.indexOf('.') > -1 ? name
: GCU
.getClassNameRepresentation(name
)
56 def domainClass
= grailsApp
.getDomainClass(name
)
59 println
"Domain class not found in grails-app/domain, trying hibernate mapped classes..."
61 def config
= new GrailsRuntimeConfigurator(grailsApp
, appCtx
)
62 appCtx
= config
.configure(appCtx
.servletContext
)
68 domainClass
= grailsApp
.getDomainClass(name
)
72 def generator
= new DefaultGrailsTemplateGenerator()
74 event("StatusUpdate", ["Generating views for domain class ${domainClass.fullName}"])
75 generator
.generateViews(domainClass
, basedir
)
77 if(generateController
) {
78 event("StatusUpdate", ["Generating controller for domain class ${domainClass.fullName}"])
79 generator
.generateController(domainClass
, basedir
)
81 event("StatusFinal", ["Finished generation for domain class ${domainClass.fullName}"])
84 event("StatusFinal", ["No domain class found for name ${name}. Please try again and enter a valid domain class name"])