Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding support to compile less files inside a plugin #7

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion LesscssResourcesGrailsPlugin.groovy
Original file line number Diff line number Diff line change
@@ -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
Expand Down
34 changes: 33 additions & 1 deletion grails-app/resourceMappers/LesscssResourceMapper.groovy
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
}