Browse files

assetgen: add js.template support for #11 and bump version.

  • Loading branch information...
1 parent afe3fda commit 6fdd1f997c27577bd6eb73b1baba2e4f0f53acd5 @tav committed Oct 25, 2012
Showing with 34 additions and 2 deletions.
  1. +32 −1 assetgen/main.py
  2. +1 −1 assetgen/version.py
  3. +1 −0 setup.py
View
33 assetgen/main.py
@@ -29,8 +29,11 @@
except ImportError:
from pickle import dump, load
+from mako.exceptions import RichTraceback
+from mako.template import Template
from requests import get as get_url
from simplejson import dump as encode_json, dumps as enc_json, loads as dec_json
+from simplejson import JSONEncoderForHTML
from tavutil.env import run_command
from tavutil.optcomplete import autocomplete
from tavutil.scm import is_git, SCMConfig
@@ -409,6 +412,9 @@ def extend_opts(xs, opt):
else:
xs.extend(opt)
+def jsliteral(v, enc=JSONEncoderForHTML().encode):
+ return enc(v)
+
def mismatch(s1, s2, source, existing):
log.error(
"Mixed %s/%s source files are not compatible with source maps (%s + %s)"
@@ -423,6 +429,14 @@ def __init__(self, *args):
super(JSAsset, self).__init__(*args)
sources = self.sources
get_spec = self.spec.get
+ tmpl = get_spec('template')
+ if tmpl:
+ self.template_encoding = get_spec('template.source.encoding', 'utf-8')
+ self.template = Template(
+ tmpl, output_encoding=get_spec('template.output.encoding', 'utf-8')
+ )
+ else:
+ self.template = None
if get_spec('sourcemaps'):
ts = cs = js = None
for source in sources:
@@ -473,6 +487,20 @@ def __init__(self, *args):
break
self.ts = ts
+ def apply_template(self, source):
+ if self.template_encoding != 'ascii':
+ source = unicode(source, self.template_encoding)
+ try:
+ return self.template.render(jsliteral=jsliteral, source=source)
+ except Exception, err:
+ traceback = RichTraceback()
+ for (filename, lineno, function, line) in traceback.traceback:
+ print "File %s, line %s, in %s" % (filename, lineno, function)
+ print line, "\n"
+ print "%s: %s" % (str(traceback.error.__class__.__name__), traceback.error)
+ print
+ raise err
+
def sourcemap(self, js, sm_path, sm_id, src_map):
data = dec_json(read(sm_path))
if self.sm_root:
@@ -565,7 +593,10 @@ def generate(self):
do(['tsc', tempts])
out(read(tempjs))
else:
- out(read(source))
+ if self.template:
+ out(self.apply_template(read(source)))
+ else:
+ out(read(source))
self.uglify(''.join(output), get_spec)
def uglify(self, output, get_spec):
View
2 assetgen/version.py
@@ -1,4 +1,4 @@
# Public Domain (-) 2010-2012 The Assetgen Authors.
# See the Assetgen UNLICENSE file for details.
-__release__ = '0.3.1'
+__release__ = '0.3.2'
View
1 setup.py
@@ -29,6 +29,7 @@
"assetgen = assetgen.main:main"
]),
install_requires=[
+ "Mako >= 0.7.2",
"PyYAML >= 3.09",
"requests >= 0.14.1",
"simplejson >= 2.1.6",

0 comments on commit 6fdd1f9

Please sign in to comment.