Skip to content

Instantly share code, notes, and snippets.

@jwill
Created May 2, 2010 00:37
Show Gist options
  • Save jwill/386792 to your computer and use it in GitHub Desktop.
Save jwill/386792 to your computer and use it in GitHub Desktop.
RemoveAll script for Grails
import grails.util.GrailsNameUtils
import org.codehaus.groovy.grails.scaffolding.*
import org.codehaus.groovy.grails.commons.GrailsDomainClass
/**
* Gant script for deleting domain class, crud views, and tests
*
* @author jwill
*/
includeTargets << grailsScript("_GrailsBootstrap")
includeTargets << grailsScript("_GrailsCreateArtifacts")
target ('default': "Removes the CRUD views for a specified domain class") {
depends( checkVersion, parseArguments, packageApp )
promptForName(type: "Domain Class")
generateForName = argsMap["params"][0]
removeCrudViewsAndDomainAndTests()
}
target(removeCrudViewsAndDomainAndTests: "Removes domain and crud views.") {
depends(loadApp)
def name = generateForName
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) {
removeTests(domainClass, basedir)
removeViews(domainClass, basedir)
removeDomainClass(domainClass, basedir)
event("StatusFinal", ["Finished deletion of views and domain class ${domainClass.fullName}"])
}
else {
event("StatusFinal", ["No domain class found for name ${name}. Please try again and enter a valid domain class name"])
}
}
public void removeDomainClass(GrailsDomainClass domainClass, String destdir) {
if (!destdir)
throw new IllegalArgumentException("Argument [destdir] not specified")
event("StatusUpdate", ["Removing domain class ${domainClass.fullName}"])
def view = new File("${destdir}/grails-app/domain/${domainClass.name}.groovy")
if (view.exists())
view.delete()
event("StatusUpdate", ["Removed views and domain class for ${domainClass.fullName}"])
}
public void removeTests(GrailsDomainClass domainClass, String destdir) {
if (!destdir)
throw new IllegalArgumentException("Argument [destdir] not specified")
event("StatusUpdate", ["Removing tests for domain class ${domainClass.fullName}"])
def view = new File("${destdir}/test/unit/${domainClass.name}Tests.groovy")
if (view.exists())
view.delete()
event("StatusUpdate", ["Removed tests for domain class ${domainClass.fullName}"])
}
public void removeViews(GrailsDomainClass domainClass, String destdir) {
def templateGenerator = new DefaultGrailsTemplateGenerator(classLoader)
if (!destdir)
throw new IllegalArgumentException("Argument [destdir] not specified")
def templateNames = templateGenerator.getTemplateNames()
event("StatusUpdate", ["Removing views for domain class ${domainClass.fullName}"])
for(t in templateNames) {
def view = new File("${destdir}/grails-app/views/${domainClass.propertyName}/${t}.gsp")
if (view.exists())
view.delete()
}
// removes directory as well if empty
def viewsDir = new File("${destdir}/grails-app/views/${domainClass.propertyName}")
if (viewsDir.list().size() == 0)
viewsDir.delete()
event("StatusUpdate", ["Removed views and domain class for ${domainClass.fullName}"])
}
@jwill
Copy link
Author

jwill commented May 2, 2010

Place this in either the scripts directory of your app for local access or in the scripts directory of your Grails install for system-wide access to the script.
To run:
grails remove-all DomainClass

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment