comparison piecrust/processing/requirejs.py @ 117:6827dcc9d3fb

Changes to the asset processing pipeline: * Add semi-functional RequireJS processor. * Processors now match on the relative path. * Support for processors that add more processors of their own. * A couple of related fixes.
author Ludovic Chabant <ludovic@chabant.com>
date Tue, 28 Oct 2014 08:20:38 -0700
parents
children e5f048799d61
comparison
equal deleted inserted replaced
116:1c13f3389fcb 117:6827dcc9d3fb
1 import os
2 import os.path
3 import json
4 import hashlib
5 import logging
6 import platform
7 import subprocess
8 from piecrust.processing.base import Processor, PRIORITY_FIRST
9 from piecrust.processing.tree import FORCE_BUILD
10
11
12 logger = logging.getLogger(__name__)
13
14
15 class RequireJSProcessor(Processor):
16 PROCESSOR_NAME = 'requirejs'
17
18 def __init__(self):
19 super(RequireJSProcessor, self).__init__()
20 self.is_bypassing_structured_processing = True
21 self._conf = None
22
23 def initialize(self, app):
24 super(RequireJSProcessor, self).initialize(app)
25
26 self._conf = app.config.get('requirejs')
27 if self._conf is None:
28 return
29
30 if 'build_path' not in self._conf:
31 raise Exception("You need to specify `requirejs/build_path` "
32 "for RequireJS.")
33 self._conf.setdefault('bin', 'r.js')
34 self._conf.setdefault('out_path', self._conf['build_path'])
35
36 def onPipelineStart(self, pipeline):
37 super(RequireJSProcessor, self).onPipelineStart(pipeline)
38
39 logger.debug("Adding Javascript suppressor to build pipeline.")
40 skip = _JavascriptSkipProcessor(self._conf['build_path'])
41 pipeline.processors.append(skip)
42
43 def matches(self, path):
44 return path == self._conf['build_path']
45
46 def getDependencies(self, path):
47 return FORCE_BUILD
48
49 def process(self, path, out_dir):
50 args = [self._conf['bin'], '-o', path]
51 shell = (platform.system() == 'Windows')
52 cwd = self.app.root_dir
53 logger.debug("Running RequireJS: %s" % ' '.join(args))
54 try:
55 retcode = subprocess.call(args, shell=shell, cwd=cwd)
56 except FileNotFoundError as ex:
57 logger.error("Tried running RequireJS processor "
58 "with command: %s" % args)
59 raise Exception("Error running RequireJS. "
60 "Did you install it?") from ex
61 if retcode != 0:
62 raise Exception("Error occured in RequireJS compiler. "
63 "Please check log messages above for "
64 "more information.")
65 return True
66
67
68 class _JavascriptSkipProcessor(Processor):
69 PROCESSOR_NAME = 'requirejs_javascript_skip'
70
71 def __init__(self, except_path=None):
72 super(_JavascriptSkipProcessor, self).__init__()
73 self.priority = PRIORITY_FIRST
74 self.is_bypassing_structured_processing = True
75 self._except_path = except_path
76
77 def matches(self, path):
78 _, ext = os.path.splitext(path)
79 return ext == '.js' and path != self._except_path
80
81 def process(self, in_path, out_path):
82 return False
83