
Originally Posted by
pledbrook
First off, generate-controller, generate-views, and generate-all already allow you to generate scaffolding for a single class. Does your script do something the others don't? Second, you should really include the Grails script "_GrailsBootstrap" and depend on the "bootstrap" task.
Which version of Grails are you using?
Peter
Thanks for writing. You are right.. the scripts mentioned do actually do that stuff individually and since the post I have decided to reuse those precise scripts. I even switched to using the _GrailsBootstrap script and using it within my script. The only difference so far as I can tell.. is that I want to control through the arguments, whether or not to generate views and controllers independently.
First, my grails version is 1.3.4 and I am about to upgrade to 1.3.5 today.
Second. I have made an additional change. My new script (called SingleDomainGenerator.groovy) is in the scripts directory of an INLINE plugin called minimalist.
I also changed this call
Code:
...templateGenerator.generateViews(domainClass, '.')
to use a . rather than basedir
here is the script:
Code:
/*
* Copyright 2004-2005 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.codehaus.groovy.grails.scaffolding.*
import grails.util.GrailsNameUtils
/**
* Gant script that generates a CRUD controller and matching views for a given domain class
*
* @author Graeme Rocher
*
* @since 0.4
*/
includeTargets << grailsScript("_GrailsCreateArtifacts")
includeTargets << grailsScript("_GrailsGenerate")
includeTargets << grailsScript("_GrailsInit")
includeTargets << grailsScript("_GrailsBootstrap")
target(generateForOne: "Generates controllers and views for only one domain class.") {
depends(parseArguments,packageApp, loadApp)
echo "basedir = ${basedir}"
def name = argsMap["params"][0]
def generateViews = new Boolean(argsMap["params"][1])
def generateController = new Boolean(argsMap["params"][2])
echo "name : ${name} | generateviews: ${generateViews} | generate controllers : ${generateController}"
name = name.indexOf('.') > -1 ? name : GrailsNameUtils.getClassNameRepresentation(name)
def domainClass = grailsApp.getDomainClass(name)
if (!domainClass) {
println "Domain class not found in grails-app/domain, trying hibernate mapped classes..."
bootstrap()
domainClass = grailsApp.getDomainClass(name)
}
if (domainClass) {
generateForDomainClass(domainClass)
event("StatusFinal", ["Finished generation for domain class ${domainClass.fullName}"])
}
else {
event("StatusFinal", ["No domain class found for name ${name}. Please try again and enter a valid domain class name"])
exit(1)
}
}
target(uberGenerate: "Generates controllers and views for all domain classes.") {
depends(loadApp)
def domainClasses = grailsApp.domainClasses
if (!domainClasses) {
println "No domain classes found in grails-app/domain, trying hibernate mapped classes..."
bootstrap()
domainClasses = grailsApp.domainClasses
}
if (domainClasses) {
domainClasses.each { domainClass -> generateForDomainClass(domainClass) }
event("StatusFinal", ["Finished generation for domain classes"])
}
else {
event("StatusFinal", ["No domain classes found"])
}
}
def generateForDomainClass(domainClass) {
def templateGenerator = new DefaultGrailsTemplateGenerator()
//use a resource loader
if (generateViews) {
event("StatusUpdate", ["Generating views for domain class ${domainClass.fullName}"])
templateGenerator.generateViews(domainClass, '.')
event("GenerateViewsEnd", [domainClass.fullName])
}
if (generateController) {
event("StatusUpdate", ["Generating controller for domain class ${domainClass.fullName}"])
templateGenerator.generateController(domainClass, '.')
event("GenerateControllerEnd", [domainClass.fullName])
}
}
setDefaultTarget(generateForOne)
and here is the inlining of my plugins
Code:
grails.plugin.location.eventmanager = "../eventmanager"
grails.plugin.location.minimalist = "../minimalist"
and it now generates the controllers/views as expected. However the problem is that it is generating the views and controllers using the templates that are provided with Grails scaffolding by default.
IN the plugin minimalist that I have inlined, I have a the same views and controller file, specially modified for my project.
How can I get the script above to generate controllers and views using the minimalist controller /view templates rather than the default Grails templates ?
I know i have to use a resource loader ..but not sure how ...
thanks