Commits

David Carr  committed ef07584

Initial import of key files for the plugin

  • Participants
  • Parent commits 2207d0e

Comments (0)

Files changed (5)

File PageResourcesGrailsPlugin.groovy

+import org.grails.plugin.resources.page.PageResourcesInterceptor
+
+class PageResourcesGrailsPlugin {
+    def version = "0.1"
+    def grailsVersion = "1.3 > *"
+    def dependsOn = [resources: "1.2-RC1"]
+    def pluginExcludes = [
+        "grails-app/views/error.gsp"
+    ]
+
+    def title = "Page Resources Plugin"
+    def author = "David M. Carr"
+    def authorEmail = "david@carrclan.us"
+    def description = '''\
+Enhances the resources plugin by allowing for creation of "page" resource modules using convention over configuration.
+'''
+
+    def documentation = "http://grails.org/plugin/page-resources"
+
+    def license = "APACHE"
+
+    // Location of the plugin's issue tracker.
+//    def issueManagement = [ system: "JIRA", url: "http://jira.grails.org/browse/GPMYPLUGIN" ]
+
+    // Online location of the plugin's browseable source code.
+//    def scm = [ url: "http://svn.grails-plugins.codehaus.org/browse/grails-plugins/" ]
+
+    def doWithSpring = {
+        pageResourcesInterceptor(PageResourcesInterceptor)
+    }
+}

File application.properties

+#Grails Metadata file
+#Fri Jul 13 09:12:29 EDT 2012
+app.grails.version=2.1.0
+app.name=page-resources

File grails-app/conf/BuildConfig.groovy

+grails.project.class.dir = "target/classes"
+grails.project.test.class.dir = "target/test-classes"
+grails.project.test.reports.dir = "target/test-reports"
+grails.project.target.level = 1.6
+
+grails.project.dependency.resolution = {
+    inherits("global")
+    log "warn" // log level of Ivy resolver, either 'error', 'warn', 'info', 'debug' or 'verbose'
+    repositories {
+        grailsCentral()
+    }
+    dependencies {
+    }
+    plugins {
+        compile(":resources:1.2-RC1")
+        build(":tomcat:$grailsVersion",
+              ":release:2.0.3") {
+            export = false
+        }
+    }
+}

File grails-app/conf/ZZZPageResources.groovy

+import org.codehaus.groovy.grails.commons.ApplicationHolder
+import org.slf4j.LoggerFactory
+
+def log = LoggerFactory.getLogger('org.grails.plugin.resource.page.PageResources')
+def application = ApplicationHolder.application
+
+modules = {
+    // This file should be named such that it comes alphabetically after any *Resources files that contain explicit
+    // page module definitions.  Otherwise, it will overrride the other definition.
+
+    def rootDir = application.parentContext.getResource("/").getFile()
+    def pagesDir = new File(rootDir, 'pages')
+    int prefixLen = rootDir.path.length()
+    def dependModuleName = 'defaultPageDependencies'
+    def dependModule = delegate._modules.find { it.name == dependModuleName }
+    if (pagesDir.isDirectory()) {
+        pagesDir.eachDirRecurse { File moduleDir ->
+            String moduleName = moduleDir.path.substring(prefixLen + 1).replaceAll(/[\/\\]/, '_')
+            def module = delegate._modules.find { it.name == moduleName }
+            if (module) {
+                if (log.isDebugEnabled()) {
+                    log.debug("Page module ${moduleName} already defined; using pre-existing definition")
+                }
+            } else {
+                File[] files = moduleDir.listFiles(
+                    [accept: { File f -> f.isFile() && f.name ==~ /.*\.(js|less|css)/}] as FileFilter
+                ).sort()
+                if (files.length > 0) {
+                    if (log.isInfoEnabled()) {
+                        log.info("Defining page module: ${moduleName}")
+                    }
+                    delegate.invokeMethod(moduleName, {
+                        if (dependModule) {
+                            dependsOn(dependModuleName)
+                        }
+                        defaultBundle(false)
+                        files.each { file ->
+                            def url = file.path.substring(prefixLen)
+                            resource(url: url)
+                        }
+                    })
+                }
+            }
+        }
+    }
+}

File src/groovy/org/grails/plugin/resources/page/PageResourcesInterceptor.groovy

+package org.grails.plugin.resources.page
+
+import org.grails.plugin.resource.ResourceProcessor
+import org.grails.plugin.resource.ResourceTagLib
+import org.slf4j.LoggerFactory
+import org.springframework.web.servlet.ModelAndView
+import org.springframework.web.servlet.handler.HandlerInterceptorAdapter
+
+import javax.annotation.Resource
+import javax.servlet.http.HttpServletRequest
+import javax.servlet.http.HttpServletResponse
+
+class PageResourcesInterceptor extends HandlerInterceptorAdapter {
+    def log = LoggerFactory.getLogger(PageResourcesInterceptor.class)
+
+    @Resource
+    ResourceProcessor resourceProcessor
+
+    @Resource
+    ResourceTagLib resourceTagLib
+
+    @Override
+    void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) {
+        def viewName = modelAndView?.viewName
+        if (viewName) {
+            String moduleName = 'pages' + viewName.replaceAll('/', '_')
+            def module = resourceProcessor.getModule(moduleName)
+            if (module) {
+                if (log.isDebugEnabled()) {
+                    log.debug("Automatically requiring page module ${moduleName} for view ${viewName}")
+                }
+                resourceTagLib.require(module: moduleName)
+            }
+        }
+    }
+}