4
4
5
5
from django .conf import settings
6
6
from django .core .management .base import BaseCommand
7
+ from django .template import Context , Template
7
8
from django .template .loaders .app_directories import get_app_template_dirs
8
9
9
10
from dashboard .templatetags .bundle import render
10
11
12
+ from app .bundle_context import context , templateTags
11
13
12
- def rmdir (loc ):
14
+
15
+ def rmdir (loc , depth = 1 ):
13
16
# drop both the bundled and the bundles before recreating
14
17
if os .path .exists (loc ) and os .path .isdir (loc ):
15
- print ('- Deleting assets from: %s' % loc )
16
- shutil .rmtree (loc )
18
+ # list all dirs/paths in the loc
19
+ files = os .listdir (loc )
20
+
21
+ print ('%s Deleting %s assets from: %s' % ('-' * depth , len (files ), loc ))
17
22
23
+ # delete files/dirs from the given loc leaving the loc dir intact
24
+ for path in files :
25
+ nextLoc = os .path .join (loc , path )
26
+ if os .path .isdir (nextLoc ):
27
+ rmdir (nextLoc , depth + 1 )
28
+ else :
29
+ os .remove (nextLoc )
18
30
19
31
def rmdirs (loc , kind ):
20
32
# base path of the assets
@@ -29,6 +41,11 @@ class Command(BaseCommand):
29
41
help = 'generates .js/.scss files from bundle template tags'
30
42
31
43
def handle (self , * args , ** options ):
44
+ """ This command will collect templates, render them with the bundleContext and run them through the bundle procedure"""
45
+
46
+ print ('\n Collect template files from all apps:' )
47
+
48
+ # get a list of all templates (rather than views to avoid segfault error that django-compressor was giving us on production)
32
49
template_dir_list = []
33
50
for template_dir in get_app_template_dirs ('templates' ):
34
51
if settings .BASE_DIR in template_dir :
@@ -41,29 +58,38 @@ def handle(self, *args, **options):
41
58
if ".html" in filename :
42
59
template_list .append (os .path .join (base_dir , filename ))
43
60
61
+ print ('\n - %s templates discovered' % len (template_list ))
62
+
44
63
# using regex to grab the bundle tags content from html
45
64
block_pattern = re .compile (r'({%\sbundle(.|\n)*?(?<={%\sendbundle\s%}))' )
46
65
open_pattern = re .compile (r'({%\s+bundle\s+(js|css|merge_js|merge_css)\s+?(file)?\s+?([^\s]*)?\s+?%})' )
47
66
close_pattern = re .compile (r'({%\sendbundle\s%})' )
48
- static_open_pattern = re . compile ( r'({%\sstatic\s["|\'])' )
49
- static_close_pattern = re . compile ( r'(\s?%}(\"|\')?\s?\/?>) ' )
67
+
68
+ print ( ' \n Clear bundle directories: \n ' )
50
69
51
70
# remove the previously bundled files
52
71
for ext in ['js' , 'scss' , 'css' ]:
53
72
rmdirs ('assets' , ext )
54
73
rmdirs ('static' , ext )
55
74
56
- print ('\n Start generating bundle files \n ' )
75
+ print ('\n Start generating bundled assets (using app.bundleContext as context): \n ' )
57
76
58
77
# store unique entries for count
59
78
rendered = dict ()
60
79
80
+ # get the tags and context from bundleContext
81
+ tags = templateTags ()
82
+ bundleContext = context ()
83
+ # load the bundleContext into a Context instance so that it can be fed to Template.render
84
+ bundleContext = Context (bundleContext )
85
+
86
+ # check every template for bundle tags
61
87
for template in template_list :
62
88
try :
63
- f = open (( '%s' % template ). replace ( '/' , os . sep ), 'r' , encoding = 'utf8' )
64
-
65
- t = f . read ()
66
- if re .search (block_pattern , t ) is not None :
89
+ # read the template file
90
+ t = open (( '%s' % template ). replace ( '/' , os . sep ), 'r' , encoding = 'utf8' ). read ()
91
+ # check for bundle tags
92
+ if re .search (block_pattern , t ):
67
93
for m in re .finditer (block_pattern , t ):
68
94
block = m .group (0 )
69
95
details = re .search (open_pattern , block )
@@ -76,15 +102,22 @@ def handle(self, *args, **options):
76
102
block = re .sub (open_pattern , '' , block )
77
103
block = re .sub (close_pattern , '' , block )
78
104
79
- # clean static helper if we havent ran this through parse
80
- block = re .sub (static_open_pattern , '' , block )
81
- block = re .sub (static_close_pattern , '>' , block )
105
+ # add static helper to the block
106
+ block = '{% ' + 'load %s' % ' ' .join (tags ) + ' %}\n ' + block
107
+
108
+ # create a template from the block
109
+ block = Template (block )
110
+
111
+ # render the template with bundleContext
112
+ block = block .render (bundleContext )
113
+
114
+ # clean static_url from the path (its not required but is included as legacy in most script/link inclusions)
115
+ block = block .replace (settings .STATIC_URL , "" )
82
116
83
117
# render the template (producing a bundle file)
84
118
rendered [render (block , kind , 'file' , name , True )] = True
85
-
86
119
except Exception as e :
87
120
print ('-- X - failed to parse %s: %s' % (template , e ))
88
121
pass
89
122
90
- print ('\n Generated %s bundle files ' % len (rendered ))
123
+ print ('\n \n ------------------- Generated %s bundled assets ------------------- \n \n ' % len (rendered ))
0 commit comments