-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathbuild.py
268 lines (214 loc) · 7.36 KB
/
build.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
#!/usr/bin/env python3
import os
import re
import subprocess
import zipfile
from glob import glob
from urllib.request import urlretrieve
from mimetypes import guess_type as mime_type
from shutil import rmtree as rmt, copytree, copy, move, which
osp = os.path
pj = osp.join
os.chdir(osp.split(osp.abspath(__file__))[0])
def rmtree(path):
if osp.exists(path):
rmt(path)
def mkdirs(path):
try: os.makedirs(path)
finally: return osp.exists(path)
app_dir = osp.abspath('./src')
app = {
'name': 'Imagus',
'url': 'https://www.reddit.com/r/imagus/',
'version': '0.9.8.74'
}
browsers = {
"crx": {
"manifest": "manifest.json",
"locales": "_locales",
"file_ext": ".crx"
}
}
if not which('java'):
raise SystemExit('java must be installed for minification!');
minifiers = {
'closure-compiler': {
'file': 'closure-compiler-v20200224.jar',
'url': 'https://dl.google.com/closure-compiler/compiler-20200224.zip',
},
'yuicompressor': {
'file': 'yuicompressor-2.4.7/build/yuicompressor-2.4.7.jar',
'url': 'https://github.com/downloads/yui/yuicompressor/yuicompressor-2.4.7.zip',
},
'htmlcompressor': {
'file': 'htmlcompressor-1.5.3.jar',
'url': 'https://github.com/serg472/htmlcompressor/releases/download/1.5.3/htmlcompressor-1.5.3.jar',
}
}
bin_dir = pj('./bin')
if not os.path.isdir(bin_dir):
os.makedirs(bin_dir)
for minifier_name, minifier in minifiers.items():
jar_path = pj(bin_dir, os.path.basename(minifier['file']))
if os.path.isfile(jar_path):
minifiers[minifier_name] = jar_path
continue
file_path = pj(bin_dir, os.path.basename(minifier['url']))
print(minifier['url'] + '...')
urlretrieve(minifier['url'], filename=file_path)
if file_path.endswith('.zip'):
with zipfile.ZipFile(file_path) as zf:
with open(jar_path, 'wb') as jf:
jf.write(zf.read(minifier['file']))
os.remove(file_path)
if not os.path.isfile(jar_path):
params['-min'] = False
break
minifiers[minifier_name] = jar_path
tmp_dir = osp.abspath('.tmp')
compress_js = 'WHITESPACE_ONLY' #SIMPLE_OPTIMIZATIONS
compress_css = True
compress_html = True
tmp_dir_js = osp.abspath('.tmp_js')
if compress_js:
tmp_dir_js_min = osp.abspath('.tmp_js_min')
try:
rmtree(tmp_dir)
rmtree(tmp_dir_js)
rmtree(tmp_dir_js_min)
except:
pass
if not mkdirs(tmp_dir) or not mkdirs(tmp_dir_js) or compress_js and not mkdirs(tmp_dir_js_min):
raise SystemExit("Temp directories couldn't be created!")
copy(pj(app_dir, 'includes', 'app.js'), pj(tmp_dir_js, 'app.js'))
copy(pj(app_dir, 'js', 'app_bg.js'), pj(tmp_dir_js, 'app_bg.js'))
tmp = ['background.html', 'options.html']
if compress_html:
subprocess.call(
[
'java', '-jar', minifiers['htmlcompressor'],
'--type', 'html',
'--remove-quotes',
'--simple-doctype',
'--remove-style-attr',
'--remove-script-attr',
'--compress-css',
'-o', tmp_dir
] + [pj(app_dir, x) for x in tmp]
)
else:
for i in tmp:
copy(pj(app_dir, i), pj(tmp_dir, i))
if compress_css:
tmp = [pj(app_dir, 'css'), pj(tmp_dir, 'css')]
mkdirs(tmp[1])
copy(pj(tmp[0], 'options.css'), tmp[1])
copy(pj(tmp[0], 'sp_ddg.css'), tmp[1])
tmp = [
osp.relpath(pj(tmp[1], 'options.css'), '.'),
osp.relpath(pj(tmp[0], 'options.css'), '.')
]
subprocess.call([
'java', '-jar', minifiers['yuicompressor'],
'--charset', 'utf-8',
'--type', 'css',
'-o', tmp[0],
tmp[1]
])
else:
copytree(pj(app_dir, 'css'), pj(tmp_dir, 'css'), copy_function=copy)
if compress_js:
import itertools
tmp = pj(tmp_dir, 'includes', '')
if mkdirs(tmp):
subprocess.call([
'java', '-jar', minifiers['closure-compiler'],
'--charset=utf-8',
'--warning_level=QUIET',
'--strict_mode_input',
'--language_in=ECMASCRIPT_NEXT',
'--rewrite_polyfills=false',
'--compilation_level=' + compress_js,
'--js="' + pj(app_dir, 'includes', 'content.js') + '"',
'--js_output_file="' + tmp + 'content.js"'
])
subprocess.call(
[
'java', '-jar', minifiers['closure-compiler'],
'--charset=utf-8',
'--warning_level=QUIET',
'--strict_mode_input',
'--language_in=ECMASCRIPT_NEXT',
'--rewrite_polyfills=false',
'--compilation_level=' + compress_js,
'--module_output_path_prefix', pj(tmp_dir_js_min, ''),
] +
list(itertools.chain(
*[['--js="' + pj(tmp_dir_js, x + '.js') + '"', '--module', x + ':1'] for x in ['app_bg', 'app']]
))
)
rmtree(tmp_dir_js)
move(tmp_dir_js_min, tmp_dir_js)
else:
raise SystemExit('Failed to create folder!')
tmp = pj(tmp_dir, 'js', '')
if mkdirs(tmp):
js_compress_list = ['background', 'options', 'SieveUI', 'sp_gim']
subprocess.call(
[
'java', '-jar', minifiers['closure-compiler'],
'--charset=utf-8',
'--warning_level=QUIET',
'--strict_mode_input',
'--language_in=ECMASCRIPT_NEXT',
'--rewrite_polyfills=false',
'--compilation_level=' + compress_js,
'--module_output_path_prefix', tmp
] +
list(itertools.chain(
*[['--js="' + pj(app_dir, 'js', x + '.js') + '"', '--module', x + ':1'] for x in js_compress_list]
))
)
try:
# https://github.com/ampproject/amphtml/issues/22463
os.remove(pj(tmp_dir, 'js', '$weak$.js'))
except:
pass
else:
raise SystemExit('Failed to create folder!')
else:
copytree(pj(app_dir, 'includes'), pj(tmp_dir, 'includes'), copy_function=copy)
copytree(pj(app_dir, 'js'), pj(tmp_dir, 'js'), copy_function=copy)
package_name = osp.abspath(app['name'].lower() + '-' + app['version'])
copy(pj(app_dir, 'icon.png'), pj(tmp_dir, 'icon.png'))
copy(pj(app_dir, 'icon-18.png'), pj(tmp_dir, 'icon-18.png'))
copy(pj(app_dir, 'defaults.jsn'), pj(tmp_dir, 'defaults.jsn'))
copy(pj(app_dir, 'locales.jsn'), pj(tmp_dir, 'locales.jsn'))
copy(pj(app_dir, 'sieve.jsn'), pj(tmp_dir, 'sieve.jsn'))
# Chrome
tmp = [
pj(tmp_dir, browsers['crx']['locales']),
pj(tmp_dir, browsers['crx']['manifest']),
package_name + '.zip'
]
copytree(pj(app_dir, browsers['crx']['locales']), tmp[0], copy_function=copy)
copy(pj(app_dir, browsers['crx']['manifest']), tmp[1])
move(pj(tmp_dir_js, 'app.js'), pj(tmp_dir, 'includes', 'app.js'))
move(pj(tmp_dir_js, 'app_bg.js'), pj(tmp_dir, 'js', 'app_bg.js'))
with zipfile.ZipFile(tmp[2], 'w', zipfile.ZIP_DEFLATED) as z:
for root, dirs, files in os.walk(tmp_dir):
for file in files:
fn = pj(root, file)
wargs = [fn, fn[len(tmp_dir):]]
mime = mime_type(fn)[0]
if mime and re.search(r'^image/(?!svg)', mime):
wargs.append(zipfile.ZIP_STORED)
z.write(*wargs)
rmtree(tmp[0])
os.remove(tmp[1])
print(package_name + '.zip')
try:
rmtree(tmp_dir)
rmtree(tmp_dir_js)
except:
pass