adding support to compile less files inside a plugin #7

Open
wants to merge 3 commits into
from
Jump to file or symbol
Failed to load files and symbols.
+34 −2
Diff settings

Always

Just for now

@@ -1,6 +1,6 @@
class LesscssResourcesGrailsPlugin {
// the plugin version
- def version = "0.6"
+ def version = "0.6.1-SNAPSHOT"
// the version or versions of Grails the plugin is designed for
def grailsVersion = "1.3.5 > *"
// the other plugins this plugin depends on
@@ -1,6 +1,8 @@
import com.asual.lesscss.LessEngine
import com.asual.lesscss.LessException
import org.grails.plugin.resource.mapper.MapperPhase
+import org.codehaus.groovy.grails.plugins.GrailsPluginUtils
+import grails.util.PluginBuildSettings
/**
* @author Paul Fairless
@@ -9,12 +11,16 @@ import org.grails.plugin.resource.mapper.MapperPhase
*/
import org.codehaus.groovy.grails.commons.ApplicationHolder as AH
import org.codehaus.groovy.grails.commons.GrailsResourceUtils
+import org.springframework.util.AntPathMatcher
class LesscssResourceMapper {
def phase = MapperPhase.GENERATION // need to run early so that we don't miss out on all the good stuff
static defaultExcludes = ['**/*.js','**/*.png','**/*.gif','**/*.jpg','**/*.jpeg','**/*.gz','**/*.zip']
static String LESS_FILE_EXTENSION = '.less'
+ static String PLUGIN_PREFIX = '/plugins/'
+ static final PATH_MATCHER = new AntPathMatcher()
+
def map(resource, config){
File originalFile = resource.processedFile
@@ -50,6 +56,32 @@ class LesscssResourceMapper {
}
private File getOriginalFileSystemFile(String sourcePath) {
- new File(GrailsResourceUtils.WEB_APP_DIR + sourcePath);
+ def resourceFile
+ if(isPlugin(sourcePath)){
+ def pluginFullName = getPluginFullName(sourcePath)
+ def resourcePath = getResourceRelativePath(pluginFullName, sourcePath)
+
+ def ourPluginInfo = GrailsPluginUtils.getPluginInfos().find{pluginInfo -> pluginFullName.contains(pluginInfo.name)}
+ resourceFile = new File(ourPluginInfo.pluginDir.path + '/' + GrailsResourceUtils.WEB_APP_DIR + '/' + resourcePath)
+
+ if(resourceFile.exists()){
+ return resourceFile
+ }
+ }
+
+ return new File(GrailsResourceUtils.WEB_APP_DIR + sourcePath)
+ }
+
+ private getPluginFullName(sourcePath){
+ def pluginMap = PATH_MATCHER.extractUriTemplateVariables(PLUGIN_PREFIX + '{plugin}/**', sourcePath)
+ pluginMap.get('plugin')
+ }
+
+ private getResourceRelativePath(pluginName, sourcePath){
+ PATH_MATCHER.extractPathWithinPattern(PLUGIN_PREFIX+ pluginName +'/**', sourcePath)
+ }
+
+ private isPlugin(String original){
+ PATH_MATCHER.match(PLUGIN_PREFIX + '**', original)
}
}