From 85800fa567e1112c7c1b387c96128d7f471d345b Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Fri, 21 Feb 2014 15:20:26 +0800 Subject: [PATCH] Add sourcemap support using lessc to css command Although Moodle doesn't officially support lessc as a compiler, it is very useful for development because it supports source mapping whilst recess does not. --- config-dist.json | 2 ++ lib/commands/css.py | 11 +++++++++++ lib/css.py | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 67 insertions(+), 2 deletions(-) diff --git a/config-dist.json b/config-dist.json index 39d2d6e..70b31b5 100644 --- a/config-dist.json +++ b/config-dist.json @@ -169,6 +169,8 @@ "java": "/usr/bin/java", // Path to recess "recess": "/usr/local/bin/recess", + // Path to lessc + "lessc": "/usr/local/bin/lessc", // Debug level of MDK. 'debug', 'info', 'warning', 'error' or 'critical'. "debug": "info", diff --git a/lib/commands/css.py b/lib/commands/css.py index 5cac58c..ffb6c00 100644 --- a/lib/commands/css.py +++ b/lib/commands/css.py @@ -62,6 +62,14 @@ class CssCommand(Command): } ), ( + ['-d', '--debug'], + { + 'action': 'store_true', + 'dest': 'debug', + 'help': 'produce an unminified debugging version with source maps' + } + ), + ( ['-w', '--watch'], { 'action': 'store_true', @@ -113,6 +121,9 @@ class CssCommand(Command): if args.compile: logging.info('Compiling theme \'%s\' on %s' % (args.theme, M.get('identifier'))) processor = css.Css(M) + processor.setDebug(args.debug) + if args.debug: + processor.setCompiler('lessc') processor.compile(theme=args.theme, sheets=args.sheets) # Setting up watchdog. This code should be improved when we will have more than a compile option. diff --git a/lib/css.py b/lib/css.py index 9bf57e7..7ab04b3 100644 --- a/lib/css.py +++ b/lib/css.py @@ -35,9 +35,18 @@ class Css(object): _M = None + _debug = False + _compiler = 'recess' + def __init__(self, M): self._M = M + def setCompiler(self, compiler): + self._compiler = compiler + + def setDebug(self, debug): + self._debug = debug + def compile(self, theme='bootstrapbase', sheets=None): """Compile LESS sheets contained within a theme""" @@ -71,7 +80,13 @@ class Css(object): continue try: - compiler = Recess(source, os.path.join(source, sheet), os.path.join(dest, destSheet)) + if self._compiler == 'recess': + compiler = Recess(source, os.path.join(source, sheet), os.path.join(dest, destSheet)) + elif self._compiler == 'lessc': + compiler = Lessc(self.getThemeDir(), os.path.join(source, sheet), os.path.join(dest, destSheet)) + + compiler.setDebug(self._debug) + compiler.execute() except CssCompileFailed: logging.warning('Failed compilation of %s' % (sheet)) @@ -88,14 +103,18 @@ class Css(object): def getThemeLessPath(self, theme): return os.path.join(self.getThemePath(theme), 'less') + def getThemeDir(self): + return os.path.join(self._M.get('path'), 'theme') + def getThemePath(self, theme): - return os.path.join(self._M.get('path'), 'theme', theme) + return os.path.join(self.getThemeDir(), theme) class Compiler(object): """LESS compiler abstract""" _compress = True + _debug = False _cwd = None _source = None _dest = None @@ -111,6 +130,9 @@ class Compiler(object): def setCompress(self, compress): self._compress = compress + def setDebug(self, debug): + self._debug = debug + class Recess(Compiler): """Recess compiler""" @@ -135,5 +157,35 @@ class Recess(Compiler): f.write(out) +class Lessc(Compiler): + """Lessc compiler""" + + def execute(self): + executable = C.get('lessc') + if not executable: + raise Exception('Could not find executable path') + + cmd = [executable] + + sourcePath = os.path.relpath(self._source, self._cwd) + sourceDir = os.path.dirname(sourcePath) + + if self._debug: + cmd.append('--source-map-rootpath=' + sourceDir) + cmd.append('--source-map-map-inline') + self.setCompress(False) + + if self._compress: + cmd.append('--compress') + + """Append the source and destination""" + cmd.append(sourcePath) + cmd.append(os.path.relpath(self._dest, self._cwd)) + + (code, out, err) = process(cmd, self._cwd) + if code != 0 or len(out) != 0: + raise CssCompileFailed('Error during compile') + + class CssCompileFailed(Exception): pass -- 2.11.0