changeset 196:154b8df04829

processing: Add Compass and Sass processors. The Sass processor is similar to the Less processor, i.e. it tries to be part of the structured pipeline processing by using the mapfile produced by the Sass compiler in order to provide a list of dependencies. The Compass processor is completely acting outside of the pipeline, so the server won't know what's up to date and what's not. It's expected that the user will run `compass watch` to keep things up to date. However, it will require to pass the server's cache directory to put things in, so we'll need to add some easy way to get that path for the user.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 11 Jan 2015 23:08:49 -0800
parents b4724e577a8c
children 57eec8a67095
files piecrust/plugins/builtin.py piecrust/processing/base.py piecrust/processing/compass.py piecrust/processing/sass.py
diffstat 4 files changed, 294 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/piecrust/plugins/builtin.py	Sun Jan 11 23:04:55 2015 -0800
+++ b/piecrust/plugins/builtin.py	Sun Jan 11 23:08:49 2015 -0800
@@ -4,6 +4,8 @@
 from piecrust.commands.builtin.info import (
         RootCommand, ShowConfigCommand,
         FindCommand, ShowSourcesCommand, ShowRoutesCommand, ShowPathsCommand)
+from piecrust.commands.builtin.plugins import (
+        PluginsCommand)
 from piecrust.commands.builtin.scaffolding import (
         PrepareCommand,
         DefaultPrepareTemplatesCommandExtension,
@@ -20,8 +22,10 @@
 from piecrust.importing.piecrust import PieCrust1Importer
 from piecrust.plugins.base import PieCrustPlugin
 from piecrust.processing.base import CopyFileProcessor
+from piecrust.processing.compass import CompassProcessor
 from piecrust.processing.less import LessProcessor
 from piecrust.processing.requirejs import RequireJSProcessor
+from piecrust.processing.sass import SassProcessor
 from piecrust.processing.sitemap import SitemapProcessor
 from piecrust.sources.base import DefaultPageSource
 from piecrust.sources.posts import (
@@ -52,7 +56,8 @@
                 ShowPathsCommand(),
                 BakeCommand(),
                 ShowRecordCommand(),
-                ServeCommand()]
+                ServeCommand(),
+                PluginsCommand()]
 
     def getCommandExtensions(self):
         return [
@@ -88,7 +93,9 @@
     def getProcessors(self):
         return [
                 CopyFileProcessor(),
+                CompassProcessor(),
                 LessProcessor(),
+                SassProcessor(),
                 RequireJSProcessor(),
                 SitemapProcessor()]
 
--- a/piecrust/processing/base.py	Sun Jan 11 23:04:55 2015 -0800
+++ b/piecrust/processing/base.py	Sun Jan 11 23:08:49 2015 -0800
@@ -134,6 +134,9 @@
         self.skip_patterns = make_re(self.skip_patterns)
         self.force_patterns = make_re(self.force_patterns)
 
+    def addSkipPatterns(self, patterns):
+        self.skip_patterns += make_re(patterns)
+
     def filterProcessors(self, authorized_names):
         self.processors = list(filter(
             lambda p: p.PROCESSOR_NAME in authorized_names,
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/piecrust/processing/compass.py	Sun Jan 11 23:08:49 2015 -0800
@@ -0,0 +1,132 @@
+import os
+import os.path
+import logging
+import platform
+import subprocess
+from piecrust.processing.base import Processor, PRIORITY_FIRST
+from piecrust.uriutil import multi_replace
+
+
+logger = logging.getLogger(__name__)
+
+
+class CompassProcessor(Processor):
+    PROCESSOR_NAME = 'compass'
+
+    STATE_UNKNOWN = 0
+    STATE_INACTIVE = 1
+    STATE_ACTIVE = 2
+
+    def __init__(self):
+        super(CompassProcessor, self).__init__()
+        # Using a high priority is needed to get to the `.scss` files before
+        # the Sass processor.
+        self.priority = PRIORITY_FIRST
+        self.is_bypassing_structured_processing = True
+        self.is_delegating_dependency_check = False
+        self._state = self.STATE_UNKNOWN
+
+    def initialize(self, app):
+        super(CompassProcessor, self).initialize(app)
+
+    def onPipelineStart(self, pipeline):
+        super(CompassProcessor, self).onPipelineStart(pipeline)
+        self._maybeActivate(pipeline)
+
+    def onPipelineEnd(self, pipeline):
+        super(CompassProcessor, self).onPipelineEnd(pipeline)
+        self._maybeRunCompass(pipeline)
+
+    def matches(self, path):
+        if self._state != self.STATE_ACTIVE:
+            return False
+
+        _, ext = os.path.splitext(path)
+        return ext == '.scss' or ext == '.sass'
+
+    def getDependencies(self, path):
+        raise Exception("Compass processor should handle dependencies by "
+                        "itself.")
+
+    def getOutputFilenames(self, filename):
+        raise Exception("Compass processor should handle outputs by itself.")
+
+    def process(self, path, out_dir):
+        if path.startswith(self.app.theme_dir):
+            if not self._runInTheme:
+                logger.debug("Scheduling Compass execution in theme directory "
+                             "after the pipeline is done.")
+                self._runInTheme = True
+        else:
+            if not self._runInSite:
+                logger.debug("Scheduling Compass execution after the pipeline "
+                             "is done.")
+                self._runInSite = True
+
+    def _maybeActivate(self, pipeline):
+        if self._state != self.STATE_UNKNOWN:
+            return
+
+        config = self.app.config.get('compass')
+        if config is None or not config.get('enable'):
+            logger.debug("Compass processing is disabled (set "
+                         "`compass/enable` to `true` to enable it).")
+            self._state = self.STATE_INACTIVE
+            return
+
+        logger.debug("Activating Compass processing for SCSS/SASS files.")
+        self._state = self.STATE_ACTIVE
+
+        bin_path = config.get('bin', 'compass')
+
+        config_path = config.get('config_path', 'config.rb')
+        config_path = os.path.join(self.app.root_dir, config_path)
+        if not os.path.exists(config_path):
+            raise Exception("Can't find Compass configuration file: %s" %
+                            config_path)
+        self._args = '%s compile --config "%s"' % (bin_path, config_path)
+
+        frameworks = config.get('frameworks', [])
+        if not isinstance(frameworks, list):
+            frameworks = frameworks.split(',')
+        for f in frameworks:
+            self._args += ' --load %s' % f
+
+        custom_args = config.get('options')
+        if custom_args:
+            self._args += ' ' + custom_args
+
+        out_dir = pipeline.out_dir
+        tmp_dir = os.path.join(pipeline.tmp_dir, 'compass')
+        self._args = multi_replace(
+                self._args,
+                {'%out_dir%': out_dir,
+                    '%tmp_dir%': tmp_dir})
+
+        self._runInSite = False
+        self._runInTheme = False
+
+    def _maybeRunCompass(self, pipeline):
+        if self._state != self.STATE_ACTIVE:
+            return
+
+        logger.debug("Running Compass with:")
+        logger.debug(self._args)
+
+        prev_cwd = os.getcwd()
+        os.chdir(self.app.root_dir)
+        try:
+            retcode = subprocess.call(self._args, shell=True)
+        except FileNotFoundError as ex:
+            logger.error("Tried running Compass with command: %s" %
+                         self._args)
+            raise Exception("Error running Compass. "
+                            "Did you install it?") from ex
+        finally:
+            os.chdir(prev_cwd)
+
+        if retcode != 0:
+            raise Exception("Error occured in Compass. Please check "
+                            "log messages above for more information.")
+        return True
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/piecrust/processing/sass.py	Sun Jan 11 23:08:49 2015 -0800
@@ -0,0 +1,151 @@
+import os
+import os.path
+import json
+import hashlib
+import logging
+import platform
+import subprocess
+from piecrust.processing.base import SimpleFileProcessor
+from piecrust.processing.tree import FORCE_BUILD
+
+
+logger = logging.getLogger(__name__)
+
+
+class SassProcessor(SimpleFileProcessor):
+    PROCESSOR_NAME = 'sass'
+
+    def __init__(self):
+        super(SassProcessor, self).__init__(
+                extensions={'scss': 'css', 'sass': 'css'})
+        self._conf = None
+        self._map_dir = None
+
+    def initialize(self, app):
+        super(SassProcessor, self).initialize(app)
+
+    def onPipelineStart(self, pipeline):
+        super(SassProcessor, self).onPipelineStart(pipeline)
+        self._map_dir = os.path.join(pipeline.tmp_dir, 'sass')
+        if not os.path.isdir(self._map_dir):
+            os.makedirs(self._map_dir)
+
+        # Ignore include-only Sass files.
+        pipeline.addSkipPatterns(['_*.scss', '_*.sass'])
+
+    def getDependencies(self, path):
+        if _is_include_only(path):
+            raise Exception("Include only Sass files should be ignored!")
+
+        map_path = self._getMapPath(path)
+        try:
+            with open(map_path, 'r') as f:
+                dep_map = json.load(f)
+        except IOError:
+            # Map file not found... rebuild.
+            logger.debug("No map file found for Sass file '%s' at '%s'. "
+                         "Rebuilding." % (path, map_path))
+            return FORCE_BUILD
+
+        if dep_map.get('version') != 3:
+            logger.warning("Unknown Sass map version. Rebuilding.")
+            return FORCE_BUILD
+
+        sources = dep_map.get('sources', [])
+        deps = list(map(_clean_scheme, sources))
+        return deps
+
+    def _doProcess(self, in_path, out_path):
+        self._ensureInitialized()
+
+        if _is_include_only(in_path):
+            raise Exception("Include only Sass files should be ignored!")
+
+        sourcemap = 'none'
+        if self.app.cache.enabled:
+            sourcemap = 'file'
+
+        args = [self._conf['bin'],
+                '--sourcemap=%s' % sourcemap,
+                '--style', self._conf['style']]
+
+        cache_dir = self._conf['cache_dir']
+        if cache_dir:
+            args += ['--cache-location', cache_dir]
+        else:
+            args += ['--no-cache']
+
+        for lp in self._conf['load_paths']:
+            args += ['-I', lp]
+
+        args += self._conf['options']
+        args += [in_path, out_path]
+        logger.debug("Processing Sass file: %s" % args)
+
+        # On Windows, we need to run the process in a shell environment
+        # otherwise it looks like `PATH` isn't taken into account.
+        shell = (platform.system() == 'Windows')
+        try:
+            retcode = subprocess.call(args, shell=shell)
+        except FileNotFoundError as ex:
+            logger.error("Tried running Sass processor with command: %s" %
+                         args)
+            raise Exception("Error running Sass processor. "
+                            "Did you install it?") from ex
+
+        # The sourcemap is generated next to the CSS file... there doesn't
+        # seem to be any option to override that, sadly... so we need to move
+        # it to the cache directory.
+        if self.app.cache.enabled:
+            src_map_file = out_path + '.map'
+            dst_map_file = self._getMapPath(in_path)
+            try:
+                os.rename(src_map_file, dst_map_file)
+            except OSError:
+                pass
+
+        if retcode != 0:
+            raise Exception("Error occured in Sass compiler. Please check "
+                            "log messages above for more information.")
+
+        return True
+
+    def _ensureInitialized(self):
+        if self._conf is not None:
+            return
+
+        self._conf = self.app.config.get('sass') or {}
+        self._conf.setdefault('bin', 'scss')
+        self._conf.setdefault('style', 'nested')
+        self._conf.setdefault('load_paths', [])
+        if not isinstance(self._conf['load_paths'], list):
+            raise Exception("The `sass/load_paths` configuration setting "
+                            "must be an array of paths.")
+        self._conf.setdefault('options', [])
+        if not isinstance(self._conf['options'], list):
+            raise Exception("The `sass/options` configuration setting "
+                            "must be an array of arguments.")
+
+        cache_dir = None
+        if self.app.cache.enabled:
+            cache_dir = os.path.join(self.app.cache_dir, 'sass')
+        self._conf.setdefault('cache_dir', cache_dir)
+
+    def _getMapPath(self, path):
+        map_name = "%s_%s.map" % (
+                os.path.basename(path),
+                hashlib.md5(path.encode('utf8')).hexdigest())
+        map_path = os.path.join(self._map_dir, map_name)
+        return map_path
+
+
+def _clean_scheme(p):
+    if p.startswith('file://'):
+        return p[7:]
+    return p
+
+
+def _is_include_only(path):
+    name = os.path.basename(path)
+    return len(name) > 0 and name[0] == '_'
+