Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a11a038
commit 645828f
Showing
42 changed files
with
634 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# encoding: utf-8 | ||
|
||
import logging | ||
|
||
import click | ||
from webassets import script | ||
from webassets.exceptions import BundleError | ||
|
||
from ckan.lib import webassets_tools | ||
from ckan.cli import error_shout | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
@click.group(name=u'asset', short_help=u'WebAssets commands') | ||
def asset(): | ||
pass | ||
|
||
|
||
@asset.command(u'build', short_help=u'Builds all bundles.') | ||
def build(): | ||
u'''Builds bundles, regardless of whether they are changed or not.''' | ||
script.main(['build'], webassets_tools.env) | ||
click.secho(u'Compile assets: SUCCESS', fg=u'green', bold=True) | ||
|
||
|
||
@asset.command(u'watch', short_help=u'Watch changes in source files.') | ||
def watch(): | ||
u'''Start a daemon which monitors source files, and rebuilds bundles. | ||
This can be useful during development, if building is not | ||
instantaneous, and you are losing valuable time waiting for the | ||
build to finish while trying to access your site. | ||
''' | ||
script.main(['watch'], webassets_tools.env) | ||
|
||
|
||
@asset.command(u'clean', short_help=u'Clear cache.') | ||
def clean(): | ||
u'''Will clear out the cache, which after a while can grow quite large.''' | ||
try: | ||
script.main(['clean'], webassets_tools.env) | ||
except BundleError as e: | ||
return error_shout(e) | ||
click.secho(u'Clear cache: SUCCESS', fg=u'green', bold=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
# encoding: utf-8 | ||
|
||
import logging | ||
import os | ||
|
||
from markupsafe import Markup | ||
|
||
from webassets import Environment | ||
from webassets.loaders import YAMLLoader | ||
|
||
from ckan.common import config, g | ||
|
||
logger = logging.getLogger(__name__) | ||
env = None | ||
|
||
|
||
def create_library(name, path): | ||
"""Create WebAssets library(set of Bundles). | ||
""" | ||
config_path = os.path.join(path, u'webassets.yml') | ||
if not os.path.exists(config_path): | ||
return | ||
|
||
library = YAMLLoader(config_path).load_bundles() | ||
bundles = { | ||
'/'.join([name, key]): bundle | ||
for key, bundle | ||
in library.items() | ||
} | ||
|
||
# Unfortunately, you'll get an error attempting to register bundle | ||
# with the same name twice. For now, let's just pop existing | ||
# bundle and avoid name-conflicts | ||
# TODO: make PR into webassets with preferable solution | ||
for name, bundle in bundles.items(): | ||
env._named_bundles.pop(name, None) | ||
env.register(name, bundle) | ||
|
||
env.append_path(path) | ||
|
||
|
||
def webassets_init(): | ||
global env | ||
|
||
public = config.get(u'ckan.base_public_folder') | ||
|
||
public_folder = os.path.abspath(os.path.join( | ||
os.path.dirname(__file__), '..', public)) | ||
|
||
base_path = os.path.join(public_folder, u'base') | ||
static_path = os.path.join(public_folder, u'webassets') | ||
|
||
env = Environment() | ||
env.directory = static_path | ||
logger.warn(u'DEBUG: %s', config.get(u'debug', False)) | ||
env.debug = True | ||
env.url = u'/webassets/' | ||
|
||
env.append_path(base_path, u'/base/') | ||
|
||
logger.debug(u'Base path {0}'.format(base_path)) | ||
create_library(u'vendor', os.path.join( | ||
base_path, u'vendor')) | ||
|
||
create_library(u'base', os.path.join(base_path, u'javascript')) | ||
|
||
create_library(u'datapreview', os.path.join(base_path, u'datapreview')) | ||
|
||
create_library(u'css', os.path.join(base_path, u'css')) | ||
|
||
|
||
def _make_asset_collection(): | ||
return {u'style': [], u'script': [], u'included': set()} | ||
|
||
|
||
def include_asset(name): | ||
try: | ||
if not g.webassets: | ||
raise AttributeError(u'WebAssets not initialized yet') | ||
except AttributeError: | ||
g.webassets = _make_asset_collection() | ||
if name in g.webassets[u'included']: | ||
return | ||
|
||
try: | ||
bundle = env[name] | ||
except KeyError: | ||
logger.error(u'Trying to include unknown asset: <{}>'.format(name)) | ||
return | ||
|
||
deps = bundle.extra.get(u'preload', []) | ||
|
||
# Using DFS may lead to infinite recursion(unlikely, because | ||
# extensions rarely depends on each other), so there is a sense to | ||
# memoize visited routes. | ||
|
||
# TODO: consider infinite loop prevention for assets that depends | ||
# on each other | ||
for dep in deps: | ||
include_asset(dep) | ||
|
||
urls = bundle.urls() | ||
type_ = None | ||
for url in urls: | ||
link = url.split(u'?')[0] | ||
if link.endswith(u'.css'): | ||
type_ = u'style' | ||
break | ||
elif link.endswith(u'.js'): | ||
type_ = u'script' | ||
break | ||
else: | ||
logger.warn(u'Undefined asset type: {}'.format(urls)) | ||
return | ||
g.webassets[type_].extend(urls) | ||
g.webassets[u'included'].add(name) | ||
|
||
|
||
def _to_tag(url, type_): | ||
if type_ == u'style': | ||
return u'<link href="{}" rel="stylesheet"/>'.format(url) | ||
elif type_ == u'script': | ||
return u'<script src="{}" type="text/javascript"></script>'.format(url) | ||
return u'' | ||
|
||
|
||
def render_assets(type_): | ||
try: | ||
assets = g.webassets | ||
except AttributeError: | ||
return u'' | ||
|
||
if not assets: | ||
return u'' | ||
collection = assets[type_] | ||
tags = u'\n'.join([_to_tag(asset, type_) for asset in assets[type_]]) | ||
collection[:] = [] | ||
return Markup(tags) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.