World's Simplest GroovyBuilder

Here's a GroovyBuilder for generating NetBeans projects such as the ones I showed in the past days:
package generators

import groovy.util.BuilderSupport

class NBProjectGenerator extends BuilderSupport {

    protected void setParent(Object parent, Object child) {
    }

    protected Object createNode(Object name) {
        return null
    }

    //Here we receive the path to a folder that should be created:
    protected Object createNode(Object name, Object value) {
        def folder = new File(value)
        folder.mkdirs()
    }

    //Here we create a new file in a specified location
    //and with a specified content:
    protected Object createNode(Object name, Map attrs) {
        new File(attrs.get("loc") + File.separator + attrs.get("name")).write(attrs.get("file"))
    }

    protected Object createNode(Object name, Map attrs, Object value) {
        return null
    }
    
}

It could be used for generating any kind of source structure, not only NetBeans projects at all, with folders and files being the two only items of concern. This is how it would be used to create a new NetBeans module:

NBProjectGenerator nbProjectGenerator = new NBProjectGenerator()
nbProjectGenerator.generate() {
    //Root folder:
    folderProp(folder)
    //Source folder:
    srcProp(src)
    //NetBeans project folder:
    nbprojectProp(nbproject)
    //Main package:
    pkgsProp(pkgs)
    //Ant build.xml script:
    buildXmlProp(loc:folder, file:build_xml, name:"build.xml")
    //Build-impl.xml script:
    buildImplXmlProp(loc:nbproject, file:build_impl_xml, name:"build-impl.xml")
    //Bundle.properties:
    bundleProp(loc:pkgs, file:bundle_properties, name:"Bundle.properties")
    //Layer file:
    layerXmlProp(loc:pkgs, file:layer_xml, name:"layer.xml")
    //Manifest file:
    manifestMfProp(loc:folder, file:manifest_mf, name:"manifest.mf")
    //Platform properties:
    platformProp(loc:nbproject, file:platform_properties, name:"platform.properties")
    //Project properties:
    projectProp(loc:nbproject, file:project_properties, name:"project.properties")
    //Project XML file:
    projectXmlProp(loc:nbproject, file:project_xml, name:"project.xml")
}

Using the very same GroovyBuilder, you can generate a NetBeans Platform application. Only remember to exclude several of the statements above, since a NetBeans Platform application doesn't have its own build.xml, for example:

NBProjectGenerator nbProjectGenerator = new NBProjectGenerator()
nbProjectGenerator.generate() {
    //Root folder:
    folderProp(folder)
    //NetBeans project folder:
    nbprojectProp(nbproject)
    //Platform properties:
    platformProp(loc:nbproject, file:platform_properties, name:"platform.properties")
    //Project properties:
    projectProp(loc:nbproject, file:project_properties, name:"project.properties")
    //Project XML file:
    projectXmlProp(loc:nbproject, file:project_xml, name:"project.xml")
}

Something similar could be done for NetBeans web applications too, for example. The only thing that needs to be done is for the values to be retrieved somehow. Right now I have them hardcoded in my Groovy script (as shown in the blog entries of the last two days). However, I could access a file on disk via Groovy and set that as the value that is passed to the GroovyBuilder. And I could create another GroovyBuilder to simplify that in the same way as the above. I'd then be accessing two GroovyBuilders from within the same Groovy script and then generating NetBeans projects very quickly, efficiently, and transparently. The Groovy script itself would end up being pretty short and with no superfluous, repetitive boilerplate code, because that's moved to the GroovyBuilder class. All that's pretty neat!

Comments:

One could create a Grails cmd line script so we could generate a full fledged Netbeans 6.5 Grails project from a naked grails app.

prompt> grails netbeans

And voilà!

Posted by JS on July 30, 2008 at 06:03 AM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
12
13
14
18
19
21
22
23
24
25
26
27
28
29
30
   
       
Today