-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
generate_gruntfile.py
388 lines (348 loc) · 13.4 KB
/
generate_gruntfile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
p = app.Plone # noqa
from zope.site.hooks import setSite
setSite(p)
from zope.site.hooks import getSite
import os
import uuid
portal = getSite()
import json
from plone.subrequest import subrequest
from Products.CMFPlone.interfaces import (
IBundleRegistry,
IResourceRegistry)
from zope.component import getUtility
from plone.registry.interfaces import IRegistry
registry = getUtility(IRegistry)
bundles = registry.collectionOfInterface(IBundleRegistry, prefix="plone.bundles") # noqa
resources = registry.collectionOfInterface(IResourceRegistry, prefix="plone.resources") # noqa
lessvariables = registry.records['plone.lessvariables'].value
gruntfile_template = """
module.exports = function(grunt) {{
'use strict';
grunt.initConfig({{
pkg: grunt.file.readJSON('package.json'),
less: {{
{less}
}},
requirejs: {{
{requirejs}
}},
sed: {{
{sed}
}},
uglify: {{
{uglify}
}},
watch: {{
scripts: {{
files: {files},
tasks: ['requirejs', 'less', 'sed', 'uglify']
}}
}}
}});
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-requirejs');
grunt.loadNpmTasks('grunt-contrib-less');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-sed');
grunt.registerTask('default', ['watch']);
grunt.registerTask('compile', ['requirejs', 'less', 'sed', 'uglify']);
}}
"""
sed_config = """
{name}: {{
path: '{path}',
pattern: '{pattern}',
replacement: '{destination}',
}},
"""
requirejs_config = """
"{bkey}": {{
options: {{
baseUrl: '/',
generateSourceMaps: false,
preserveLicenseComments: false,
paths: {paths},
shim: {shims},
wrapShim: true,
name: '{name}',
exclude: ['jquery'],
out: '{out}',
optimize: "none"
}}
}},
"""
uglify_config = """
"{bkey}": {{
options: {{
sourceMap: true,
sourceMapName: '{src}.min.js.map',
sourceMapIncludeSources: false
}},
files: {{
'{src}.min.js': ['{src}.js']
}}
}},
"""
less_config = """
"{name}": {{
options: {{
paths: {less_paths},
strictMath: false,
sourceMap: true,
outputSourceFiles: true,
strictImports: true,
sourceMapURL: "{sourcemap_url}",
relativeUrls: true,
plugins: [
new require('less-plugin-inline-urls'),
],
modifyVars: {{
{globalVars}
}}
}},
files: {{
{files}
}}
}}
"""
from plone.resource.file import FilesystemFile
from Products.Five.browser.resource import FileResource
from Products.Five.browser.resource import DirectoryResource
from plone.resource.directory import FilesystemResourceDirectory
from Products.CMFCore.FSFile import FSFile
temp_resource_folder = 'temp_resources'
def resource_to_dir(resource, file_type='.js'):
if resource.__module__ == 'Products.Five.metaclass':
try:
return resource.chooseContext().path
except:
try:
return resource.context.path
except:
try:
if callable(resource):
file_name = uuid.uuid4().hex
try:
os.mkdir(temp_resource_folder)
except OSError:
pass
full_file_name = temp_resource_folder + '/' + file_name + file_type
temp_file = open(full_file_name, 'w')
temp_file.write(resource().encode('utf-8'))
temp_file.close()
return os.getcwd() + '/' + full_file_name
else:
print "Missing resource type"
return None
except:
print "Missing resource type"
return None
elif isinstance(resource, FilesystemFile):
return resource.path
elif isinstance(resource, FileResource):
return resource.chooseContext().path
elif isinstance(resource, DirectoryResource):
return resource.context.path
elif isinstance(resource, FilesystemResourceDirectory):
return resource.directory
elif isinstance(resource, FSFile):
return resource._filepath
else:
print "Missing resource type"
return None
# REQUIRE JS CONFIGURATION
paths = {}
shims = {}
for requirejs, script in resources.items():
if script.js:
# Main resource js file
resource_file = portal.unrestrictedTraverse(script.js, None)
src = None
if resource_file:
local_file = resource_to_dir(resource_file)
else:
# In case is not found on traverse we dump it from request to file
response = subrequest(portal.absolute_url() + '/' + script.js)
local_file = None
if response.status == 200:
js_body = response.getBody()
file_name = uuid.uuid4().hex
try:
os.mkdir(temp_resource_folder)
except OSError:
pass
local_file = temp_resource_folder + '/' + file_name + '.js'
temp_file = open(local_file, 'w')
temp_file.write(js_body.encode('utf-8'))
temp_file.close()
if local_file:
# Extract .js
paths[requirejs] = local_file[:-3]
exports = script.export
deps = script.deps
inits = script.init
if exports != '' or deps != '' or inits != '':
shims[requirejs] = {}
if exports != '' and exports is not None:
shims[requirejs]['exports'] = exports
if deps != '' and deps is not None:
shims[requirejs]['deps'] = deps.split(',')
if inits != '' and inits is not None:
shims[requirejs]['init'] = inits
else:
print "No file found: " + script.js
if script.url:
# Resources available under name-url name
paths[requirejs + '-url'] = resource_to_dir(portal.unrestrictedTraverse(script.url)) # noqa
# LESS CONFIGURATION
globalVars = {}
globalVars["sitePath"] = "'/'"
globalVars["isPlone"] = "false"
globalVars["isMockup"] = "false"
less_vars_params = {
'site_url': 'LOCAL',
}
# Storing variables to use them on further vars
for name, value in lessvariables.items():
less_vars_params[name] = value
for name, value in lessvariables.items():
t = value.format(**less_vars_params)
if 'LOCAL' in t:
t_object = portal.unrestrictedTraverse(str(t.replace('LOCAL/', '').replace('\\"', '')), None) # noqa
if t_object:
t_file = resource_to_dir(t_object)
t_file = t_file.replace(os.getcwd() + '/', '')
globalVars[name] = "'%s/'" % t_file
else:
print "No file found: " + str(t.replace('LOCAL/', '').replace('\\"', '')) # noqa
else:
globalVars[name] = t
# Path to search for less
less_paths = []
# To replace later with sed
less_directories = {}
for name, value in resources.items():
for css in value.css:
# less vars can't have dots on it
local_src = portal.unrestrictedTraverse(css, None)
extension = css.split('.')[-1]
if local_src:
local_file = resource_to_dir(local_src, file_type=extension)
else:
# In case is not found on traverse we dump it from request to file
response = subrequest(portal.absolute_url() + '/' + css)
local_file = None
if response.status == 200:
css_body = response.getBody()
file_name = uuid.uuid4().hex
try:
os.mkdir(temp_resource_folder)
except OSError:
pass
local_file = temp_resource_folder + '/' + file_name + '.js'
temp_file = open(local_file, 'w')
temp_file.write(css_body.encode('utf-8'))
temp_file.close()
if local_file:
less_directories[css.rsplit('/', 1)[0]] = local_file.rsplit('/', 1)[0].replace(os.getcwd() + '/', '') # noqa
# local_file = local_file.replace(os.getcwd(), '')
# relative = ''
# for i in range(len(local_file.split('/'))):
# relative += '../'
# globalVars[name.replace('.', '_')] = "'%s'" % local_file # noqa
globalVars[name.replace('.', '_')] = "'%s'" % local_file.split('/')[-1] # noqa
if '/'.join(local_file.split('/')[:-1]) not in less_paths:
less_paths.append('/'.join(local_file.split('/')[:-1]))
else:
print "No file found: " + css
globalVars_string = ""
for g, src in globalVars.items():
globalVars_string += "\"%s\": \"%s\",\n" % (g, src)
# BUNDLE LOOP
require_configs = ""
uglify_configs = ""
less_files = ""
less_final_config = ""
sourceMap_url = ""
sed_config_final = ""
watch_files = []
sed_count = 0
for bkey, bundle in bundles.items():
if bundle.compile:
for resource in bundle.resources:
res_obj = resources[resource]
if res_obj.js:
js_object = portal.unrestrictedTraverse(res_obj.js, None)
if js_object:
main_js_path = resource_to_dir(js_object)
target_dir = '/'.join(bundle.jscompilation.split('/')[:-1])
target_name = bundle.jscompilation.split('/')[-1]
if '.min.js' in target_name:
# minified version in new resource registry
target_name = target_name.rpartition('.min.js')[0]
else:
target_name = target_name.rpartition('.js')[0]
target_path = resource_to_dir(portal.unrestrictedTraverse(target_dir)) # noqa
watch_files.append(main_js_path)
rc = requirejs_config.format(
bkey=resource,
paths=json.dumps(paths),
shims=json.dumps(shims),
name=main_js_path,
out=target_path + '/' + target_name + '.js'
)
require_configs += rc
uc = uglify_config.format(
bkey=resource,
src=target_path + '/' + target_name # noqa
)
uglify_configs += uc
if res_obj.css:
for css_file in res_obj.css:
css = portal.unrestrictedTraverse(css_file, None)
if css:
# We count how many folders to bundle to plone
elements = len(css_file.split('/'))
relative_paths = '../' * (elements - 1)
main_css_path = resource_to_dir(css)
target_dir = '/'.join(bundle.csscompilation.split('/')[:-1]) # noqa
target_name = bundle.csscompilation.split('/')[-1]
target_path = resource_to_dir(portal.unrestrictedTraverse(target_dir)) # noqa
less_file = "\"%s/%s\": \"%s\"," % (target_path, target_name, main_css_path) # noqa
sourceMap_url = target_name + '.map'
less_files += less_file
watch_files.append(main_css_path)
# replace urls
for webpath, direc in less_directories.items():
sed_config_final += sed_config.format(
path=target_path + '/' + target_name,
name='sed' + str(sed_count),
pattern=direc,
destination=relative_paths + webpath)
sed_count += 1
# replace the final missing paths
sed_config_final += sed_config.format(
path=target_path + '/' + target_name,
name='sed' + str(sed_count),
pattern=os.getcwd(),
destination='')
sed_count += 1
else:
print "No file found: " + script.js
less_files += '\n'
less_final_config = less_config.format(
name=bkey,
globalVars=globalVars_string,
files=less_files,
less_paths=json.dumps(less_paths),
sourcemap_url=sourceMap_url)
gruntfile = open('Gruntfile.js', 'w')
gruntfile.write(gruntfile_template.format(
less=less_final_config,
requirejs=require_configs,
uglify=uglify_configs,
sed=sed_config_final,
files=json.dumps(watch_files))
)
gruntfile.close()