forked from GoogleChromeLabs/text-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.py
More file actions
258 lines (210 loc) · 6.99 KB
/
Copy pathbuild.py
File metadata and controls
258 lines (210 loc) · 6.99 KB
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
#!/usr/bin/python3
import json
import os
import re
import shutil
import subprocess
import sys
import urllib.parse
import urllib.request
APP_NAME = 'Text'
IS_APP = True
BASE_DIR = os.path.dirname(sys.argv[0])
SOURCE_DIR = BASE_DIR
BUILD_DIR = os.path.join(BASE_DIR, 'build')
FILES = [
'index.html',
'_locales/en/messages.json',
'css/app.css',
'css/theme-dark.css',
'css/theme-default.css',
'css/theme-light.css',
'icon/16x16.png',
'icon/32x32.png',
'icon/48x48.png',
'icon/64x64.png',
'icon/96x96.png',
'icon/128x128.png',
'icon/256x256.png',
'images/arrow-down.svg',
'images/arrow-up.svg',
'images/check_no_box.png',
'images/check_no_box_white.png',
'images/close.svg',
'images/close-tab.svg',
'images/maximize.svg',
'images/menu.svg',
'images/search.svg',
'images/minimize.svg',
'lib/analytics/google-analytics-bundle.js',
'lib/CodeMirror/lib/codemirror.css',
'lib/jquery-1.8.3.min.js'
]
MANIFEST = 'manifest.json'
INDEX_HTML = 'index.html'
TARGET_JS = 'js/all.js'
TARGET_JS_INCLUDE = ('<script src="' + TARGET_JS + '" type="text/javascript">'
'</script>')
JS_INCLUDES = re.compile(r'(<!-- JS -->.*<!-- /JS -->)', flags=re.M | re.S)
JS_SRC = re.compile(r'<script src="([^"]*)" type="text/javascript">')
CLOSURE_URL = 'http://closure-compiler.appspot.com/compile'
BACKGROUND_EXTERNS = os.path.join(SOURCE_DIR, 'js/externs.js')
JS_EXTERNS = None
EXTERNS_URLS = [
'https://closure-compiler.googlecode.com' +
'/svn/trunk/contrib/externs/jquery-1.8.js',
'https://closure-compiler.googlecode.com' +
'/git/contrib/externs/google_analytics_api.js'
]
SKIP_JS_FILES = []
USE_LOCALIZED_NAME = False
COMPILATION_LEVEL = 'SIMPLE_OPTIMIZATIONS'
BACKGROUND_COMPILATION_LEVEL = 'ADVANCED_OPTIMIZATIONS'
debug_build = False
def parse_command_line():
global debug_build
for option in sys.argv[1:]:
if option == '-d':
debug_build = True
else:
raise Exception('Unknown command line option: ' + option)
def delete(*paths):
for path in paths:
if os.path.isdir(path):
print('Deleting', path)
shutil.rmtree(path, ignore_errors=True)
elif os.path.isfile(path):
print('Deleting', path)
os.remove(path)
def copy_files(src, dst, files):
for f in files:
print('Copying', f)
full_path = os.path.join(src, f)
target_path = os.path.join(dst, f)
os.makedirs(os.path.dirname(target_path), exist_ok=True)
shutil.copy(full_path, target_path)
def get_version():
version = subprocess.check_output(['git', 'describe'],
universal_newlines=True)
match = re.compile('v(\d+(?:\.\d+))(?:-(\d+)-g.*)?').match(version)
version = match.group(1)
if match.group(2):
version += '.' + match.group(2)
return version
def process_manifest(out_dir, version):
manifest = json.load(open(os.path.join(SOURCE_DIR, MANIFEST)))
if USE_LOCALIZED_NAME:
manifest['name'] = '__MSG_extName__'
manifest['file_handlers']['text']['title'] = '__MSG_extName__'
else:
manifest['name'] = APP_NAME
manifest['file_handlers']['text']['title'] = APP_NAME
manifest['version'] = version
if IS_APP:
background_js = manifest['app']['background']['scripts']
else:
background_js = manifest['background']['scripts']
background_libs = set(f for f in background_js if f.startswith('lib'))
background_js = set(background_js) - background_libs
background_libs.add('js/background.js')
if IS_APP:
manifest['app']['background']['scripts'] = list(background_libs)
else:
manifest['background']['scripts'] = list(background_libs)
json.dump(manifest, open(os.path.join(out_dir, MANIFEST), 'w'), indent=2)
return list(background_js)
def process_index(out_dir):
html = open(os.path.join(SOURCE_DIR, INDEX_HTML)).read()
match = JS_INCLUDES.search(html)
if not match:
print('Can\'t find JS includes in index.html.')
exit(1)
js_includes = match.group(1)
html = JS_INCLUDES.sub(TARGET_JS_INCLUDE, html)
open(os.path.join(out_dir, INDEX_HTML), 'w').write(html)
js_files = []
for match in JS_SRC.finditer(js_includes):
js_files.append(match.group(1))
return js_files
def print_errors(errors, js_files):
for error in errors:
if error['file'].lower().find('externs') >= 0:
filename = error['file']
else:
fileno = int(error['file'][6:])
filename = js_files[fileno]
if 'error' in error:
text = error['error']
else:
text = error['warning']
print(filename + ':' + str(error['lineno']) + ' ' + text)
print(error['line'])
def compile_js(out_path, js_files, level, externs):
print('Compiling JavaScript code.')
params = [
('compilation_level', level),
('language', 'ECMASCRIPT5'),
('output_format', 'json'),
('output_info', 'statistics'),
('output_info', 'warnings'),
('output_info', 'errors'),
('output_info', 'compiled_code')
]
if debug_build:
params.append(('formatting', 'pretty_print'))
js_code = ['/** @define {boolean} */\nvar DEBUG = true;']
else:
js_code = ['/** @define {boolean} */\nvar DEBUG = false;']
for js_file in js_files:
if os.path.basename(js_file) not in SKIP_JS_FILES:
js_code.append(open(os.path.join(SOURCE_DIR, js_file)).read())
if externs:
params.append(('js_externs', open(externs).read()))
for url in EXTERNS_URLS:
params.append(('externs_url', url))
for code in js_code:
params.append(('js_code', code))
params = bytes(urllib.parse.urlencode(params, encoding='utf8'), 'utf8')
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
print('Connecting', CLOSURE_URL)
out = urllib.request.urlopen(CLOSURE_URL, data=params)
result = json.loads(out.read().decode('utf8'))
if 'errors' in result and len(result['errors']):
print('Errors:')
print_errors(result['errors'], js_files)
print()
if 'warnings' in result and len(result['warnings']):
print('Warnings:')
print_errors(result['warnings'], js_files)
print()
print('Writing', out_path)
os.makedirs(os.path.dirname(out_path), exist_ok=True)
open(out_path, 'w').write(result['compiledCode'])
def main():
parse_command_line()
version = get_version()
dir_name = APP_NAME + '-' + version
if debug_build:
dir_name += '-dbg'
print(dir_name)
out_dir = os.path.join(BUILD_DIR, dir_name)
archive_path = out_dir + '.zip'
delete(out_dir, archive_path)
copy_files(SOURCE_DIR, out_dir, FILES)
background_js_files = process_manifest(out_dir, version)
compile_js(os.path.join(out_dir, 'js', 'background.js'),
background_js_files,
BACKGROUND_COMPILATION_LEVEL,
BACKGROUND_EXTERNS)
js_files = process_index(out_dir)
compile_js(os.path.join(out_dir, TARGET_JS),
js_files,
COMPILATION_LEVEL,
JS_EXTERNS)
print('Archiving', archive_path)
shutil.make_archive(out_dir, 'zip',
root_dir=os.path.abspath(BUILD_DIR),
base_dir=dir_name,
verbose=True)
if __name__ == '__main__':
main()