@@ -83,7 +83,6 @@ def __delitem__(self, key):
83
83
del self .env ._app .config [self ._transform_key (key )]
84
84
85
85
86
-
87
86
def get_static_folder (app_or_blueprint ):
88
87
"""Return the static folder of the given Flask app
89
88
instance, or module/blueprint.
@@ -213,19 +212,65 @@ def init_app(self, app):
213
212
pass
214
213
else :
215
214
import argparse
216
- from webassets .script import GenericArgparseImplementation
215
+ from webassets .script import GenericArgparseImplementation , CommandError
217
216
218
217
class CatchAllParser (object ):
219
218
def parse_known_args (self , app_args ):
220
219
return argparse .Namespace (), app_args
221
220
221
+ class FlaskArgparseInterface (GenericArgparseImplementation ):
222
+ """Subclass the CLI implementation to add a --parse-templates option."""
223
+
224
+ def _construct_parser (self , * a , ** kw ):
225
+ super (FlaskArgparseInterface , self ).\
226
+ _construct_parser (* a , ** kw )
227
+ self .parser .add_argument (
228
+ '--parse-templates' , action = 'store_true' ,
229
+ help = 'search project templates to find bundles' )
230
+
231
+ def _setup_assets_env (self , ns , log ):
232
+ env = super (FlaskArgparseInterface , self )._setup_assets_env (ns , log )
233
+ if env is not None :
234
+ if ns .parse_templates :
235
+ log .info ('Searching templates...' )
236
+ # Note that we exclude container bundles. By their very nature,
237
+ # they are guaranteed to have been created by solely referencing
238
+ # other bundles which are already registered.
239
+ env .add (* [b for b in self .load_from_templates (env )
240
+ if not b .is_container ])
241
+
242
+ if not len (env ):
243
+ raise CommandError (
244
+ 'No asset bundles were found. '
245
+ 'If you are defining assets directly within '
246
+ 'your templates, you want to use the '
247
+ '--parse-templates option.' )
248
+ return env
249
+
250
+ def load_from_templates (self , env ):
251
+ from webassets .ext .jinja2 import Jinja2Loader , AssetsExtension
252
+ from flask import current_app as app
253
+
254
+ # Use the application's Jinja environment to parse
255
+ jinja2_env = app .jinja_env
256
+
257
+ # Get the template directories of app and blueprints
258
+ template_dirs = [path .join (app .root_path , app .template_folder )]
259
+ for blueprint in app .blueprints :
260
+ template_dirs .append (
261
+ path .join (blueprint .root_path , blueprint .template_folder ))
262
+
263
+ return Jinja2Loader (env , template_dirs , [jinja2_env ]).load_bundles ()
264
+
222
265
class ManageAssets (script .Command ):
223
266
"""Manage assets."""
224
267
capture_all_args = True
225
268
226
- def __init__ (self , assets_env = None , impl = GenericArgparseImplementation ):
269
+ def __init__ (self , assets_env = None , impl = FlaskArgparseInterface ,
270
+ log = None ):
227
271
self .env = assets_env
228
272
self .implementation = impl
273
+ self .log = log
229
274
230
275
def create_parser (self , prog ):
231
276
return CatchAllParser ()
@@ -248,6 +293,7 @@ def run(self, args):
248
293
import sys , os .path
249
294
prog = '%s %s' % (os .path .basename (sys .argv [0 ]), sys .argv [1 ])
250
295
251
- return self .implementation (self .env , prog = prog ).main (args )
296
+ impl = self .implementation (self .env , prog = prog , log = self .log )
297
+ impl .main (args )
252
298
253
299
__all__ = __all__ + ('ManageAssets' ,)
0 commit comments