Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resources Plugin -- How To include all contents in a directory?

I have a grails app that contains a whole mess of individual javascript files in a series of nested directories. I want to manage them via the resources plugin, but don't want to have to explicitly register each one.

Web Dir Structure

webapp
  app
    controller
      controller1.js
      controller2.js
      ...
    model
      model1.js
      ...
    view
      view1.js

what would be great is to just declare in my AppResources.groovy file:

resource url: 'app/**/*.js'

but that doesn't work -- throws a null pointer. I've tried:

resource url: 'app/**' but with no luck

I've thought that i would put some code in the config file that would recurse through the directory structure, but that doesn't seem to be working. Here's what I've tried:

def iterClos = {
        it.eachDir( iterClos );
        it.eachFile {
            resource url: ${it.canonicalPath};

        }

    }

    iterClos( new File("$grails.app.context/app") )

Unfortunately that failed as well.

Does anyone have any ideas how I could achieve this?

like image 423
John Gordon Avatar asked May 09 '12 17:05

John Gordon


1 Answers

problem solved.

As it turns out, the idea of running code to recuse through my javascript directories works. I just had the code incorrect. Here is the code that dynamically loads my javascript files:

--AppResources.groovy

import org.codehaus.groovy.grails.web.context.ServletContextHolder as SCH

modules = {
    core {
        resource url: '/resources/css/app.css', disposition: 'head'
        resource url: '/resources/css/myapp.css', disposition: 'head'
        resource url: '/extjs/ext-all-debug.js', dispostion: 'head'

        getFilesForPath('/app').each {
          resource url: it
        }
    }
}

def getFilesForPath(path) {

    def webFileCachePaths = []

    def servletContext = SCH.getServletContext()

    //context isn't present when testing in integration mode. -jg
    if(!servletContext) return webFileCachePaths

    def realPath = servletContext.getRealPath('/')

    def appDir = new File("$realPath/$path")

    appDir.eachFileRecurse {File file ->
        if (file.isDirectory() || file.isHidden()) return
        webFileCachePaths << file.path.replace(realPath, '')
    }

    webFileCachePaths
}

The above will cause the Resource plugin to track my javascript files. Here's what the html looks like when Resources is in debug mode:

<script src="/myapp/extjs/ext-all-debug.js?_debugResources=y&n=1336614540164" type="text/javascript" ></script>
<script src="/myapp/app/controller/LogController.js?_debugResources=y&n=1336614540164" type="text/javascript" ></script>
<script src="/myapp/app/controller/LoginController.js?_debugResources=y&n=1336614540164" type="text/javascript" ></script>
<script src="/myapp/app/controller/ProfileController.js?_debugResources=y&n=1336614540164" type="text/javascript" ></script>

...

Being new to Grails, it is a very welcome fact that one can put executable code within a config file!

like image 55
John Gordon Avatar answered Sep 23 '22 17:09

John Gordon