views:

65

answers:

2

I'm implementing an ArtefactHandler and I want to be able to create artefacts from Scripts (to support a legacy format). I don't want to make all Scripts into artefacts, but just those in a particular subdirectory of grails-app, say grails-app/foo/.

I'm stuck at trying to figure out the path of the artefact from my ArtefactHandler's isArtefactClass method. Is there a way to get the path to the original source of the class, or otherwise determine if it's contained in grails-app/foo?

+1  A: 

Grails doesn't actually care where on the filesystem your classes are. That's just smoke and mirrors. As such, there is currently no support for location based artefacts, but there is a ticket for it: http://jira.codehaus.org/browse/GRAILS-2174

In the meantime, the simplest solution is to force your scripts to have a certain naming convention or possibly introduce some kind of marker annotation. I am unsure how to introspect script classes though looking for annotations.

Luke Daley
This is the official answer, I have a hack below.
noah
A: 

Luke has the official answer above, but here is the hack I came up with:

if (Script.class.isAssignableFrom(clazz)
    && ((DefaultGrailsApplication) ApplicationHolder.getApplication()).
        getResourceLoader().loadGroovySource(clazz.getName()).
        getPath().contains("grails-app/foo")) {
    return true;
}

Which probably has many problems. Firstly it relies on DefaultGrailsApplication being the implementation of GrailsApplication, so we're violating encapsulation. Secondly, it looks like loadGroovySource returns null for the built in artefacts (like taglibs) so this may not work on things like plugins and anything distributed in binary form.

EDIT: Looks like it wont even work in production. Back to the drawing board...

noah