Mercurial > piecrust2
comparison piecrust/processing/browserify.py @ 1038:7487e2df8a56
bake: Add support for Browserify.
| author | Ludovic Chabant <ludovic@chabant.com> |
|---|---|
| date | Tue, 16 Jan 2018 08:41:46 -0800 |
| parents | |
| children | 717ac3c4ee77 |
comparison
equal
deleted
inserted
replaced
| 1037:89d94955b818 | 1038:7487e2df8a56 |
|---|---|
| 1 import os | |
| 2 import os.path | |
| 3 import logging | |
| 4 import platform | |
| 5 import subprocess | |
| 6 from piecrust.processing.base import Processor, PRIORITY_FIRST, FORCE_BUILD | |
| 7 | |
| 8 | |
| 9 logger = logging.getLogger(__name__) | |
| 10 | |
| 11 | |
| 12 class BrowserifyProcessor(Processor): | |
| 13 PROCESSOR_NAME = 'browserify' | |
| 14 | |
| 15 def __init__(self): | |
| 16 super(BrowserifyProcessor, self).__init__() | |
| 17 self.priority = PRIORITY_FIRST | |
| 18 self.is_bypassing_structured_processing = True | |
| 19 self._conf = None | |
| 20 | |
| 21 def initialize(self, app): | |
| 22 super(BrowserifyProcessor, self).initialize(app) | |
| 23 | |
| 24 self._conf = app.config.get('browserify') | |
| 25 if self._conf is None: | |
| 26 return | |
| 27 | |
| 28 if self._conf is True: | |
| 29 self._conf = {} | |
| 30 | |
| 31 self._conf.setdefault('bin', 'browserify') | |
| 32 | |
| 33 def matches(self, path): | |
| 34 return self._conf is not None and os.path.splitext(path)[1] == '.js' | |
| 35 | |
| 36 def getDependencies(self, path): | |
| 37 return FORCE_BUILD | |
| 38 | |
| 39 def process(self, path, out_dir): | |
| 40 _, fname = os.path.split(path) | |
| 41 out_path = os.path.join(out_dir, fname) | |
| 42 | |
| 43 args = [self._conf['bin'], path, '-o', out_path] | |
| 44 cwd = self.app.root_dir | |
| 45 logger.debug("Running Browserify: %s" % ' '.join(args)) | |
| 46 try: | |
| 47 retcode = subprocess.call(args, cwd=cwd) | |
| 48 except FileNotFoundError as ex: | |
| 49 logger.error("Tried running Browserify processor " | |
| 50 "with command: %s" % args) | |
| 51 raise Exception("Error running Browserify. " | |
| 52 "Did you install it?") from ex | |
| 53 if retcode != 0: | |
| 54 raise Exception("Error occured in Browserify compiler. " | |
| 55 "Please check log messages above for " | |
| 56 "more information.") | |
| 57 return True |
