OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2015 The Chromium Authors. All rights reserved. | 2 # Copyright 2016 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 import os | 6 import os |
7 import subprocess | 7 import subprocess |
8 import sys | 8 import sys |
9 import tempfile | 9 import tempfile |
10 | 10 |
11 # See //docs/vulcanize.md for instructions on installing prerequistes and | |
12 # running the vulcanize build. | |
11 | 13 |
12 _HERE_PATH = os.path.join(os.path.dirname(__file__)) | 14 _HERE_PATH = os.path.join(os.path.dirname(__file__)) |
13 | 15 _SRC_PATH = os.path.normpath(os.path.join(_HERE_PATH, '..', '..', '..')) |
14 _HTML_IN_PATH = os.path.join(_HERE_PATH, 'downloads.html') | |
15 _HTML_OUT_PATH = os.path.join(_HERE_PATH, 'vulcanized.html') | |
16 _JS_OUT_PATH = os.path.join(_HERE_PATH, 'crisper.js') | |
17 | |
18 _SRC_PATH = os.path.normpath(os.path.join(_HERE_PATH, '..', '..', '..', '..')) | |
19 | 16 |
20 _RESOURCES_PATH = os.path.join(_SRC_PATH, 'ui', 'webui', 'resources') | 17 _RESOURCES_PATH = os.path.join(_SRC_PATH, 'ui', 'webui', 'resources') |
21 | 18 |
22 _CR_ELEMENTS_PATH = os.path.join(_RESOURCES_PATH, 'cr_elements') | 19 _CR_ELEMENTS_PATH = os.path.join(_RESOURCES_PATH, 'cr_elements') |
23 _CSS_RESOURCES_PATH = os.path.join(_RESOURCES_PATH, 'css') | 20 _CSS_RESOURCES_PATH = os.path.join(_RESOURCES_PATH, 'css') |
24 _HTML_RESOURCES_PATH = os.path.join(_RESOURCES_PATH, 'html') | 21 _HTML_RESOURCES_PATH = os.path.join(_RESOURCES_PATH, 'html') |
25 _JS_RESOURCES_PATH = os.path.join(_RESOURCES_PATH, 'js') | 22 _JS_RESOURCES_PATH = os.path.join(_RESOURCES_PATH, 'js') |
26 | |
27 _POLYMER_PATH = os.path.join( | 23 _POLYMER_PATH = os.path.join( |
28 _SRC_PATH, 'third_party', 'polymer', 'v1_0', 'components-chromium') | 24 _SRC_PATH, 'third_party', 'polymer', 'v1_0', 'components-chromium') |
29 | 25 |
30 _VULCANIZE_ARGS = [ | 26 _VULCANIZE_BASE_ARGS = [ |
31 '--exclude', 'crisper.js', | 27 '--exclude', 'crisper.js', |
32 | 28 |
33 # These files are already combined and minified. | 29 # These files are already combined and minified. |
34 '--exclude', 'chrome://resources/html/polymer.html', | 30 '--exclude', 'chrome://resources/html/polymer.html', |
35 '--exclude', 'web-animations-next-lite.min.js', | 31 '--exclude', 'web-animations-next-lite.min.js', |
36 | 32 |
37 # These files are dynamically created by C++. | 33 # These files are dynamically created by C++. |
38 '--exclude', 'load_time_data.js', | 34 '--exclude', 'load_time_data.js', |
39 '--exclude', 'strings.js', | 35 '--exclude', 'strings.js', |
40 '--exclude', 'text_defaults.css', | 36 '--exclude', 'text_defaults.css', |
41 '--exclude', 'text_defaults_md.css', | 37 '--exclude', 'text_defaults_md.css', |
42 | 38 |
43 '--inline-css', | 39 '--inline-css', |
44 '--inline-scripts', | 40 '--inline-scripts', |
45 | 41 |
46 '--redirect', 'chrome://downloads/|%s' % _HERE_PATH, | |
47 '--redirect', 'chrome://resources/cr_elements/|%s' % _CR_ELEMENTS_PATH, | 42 '--redirect', 'chrome://resources/cr_elements/|%s' % _CR_ELEMENTS_PATH, |
48 '--redirect', 'chrome://resources/css/|%s' % _CSS_RESOURCES_PATH, | 43 '--redirect', 'chrome://resources/css/|%s' % _CSS_RESOURCES_PATH, |
49 '--redirect', 'chrome://resources/html/|%s' % _HTML_RESOURCES_PATH, | 44 '--redirect', 'chrome://resources/html/|%s' % _HTML_RESOURCES_PATH, |
50 '--redirect', 'chrome://resources/js/|%s' % _JS_RESOURCES_PATH, | 45 '--redirect', 'chrome://resources/js/|%s' % _JS_RESOURCES_PATH, |
51 '--redirect', 'chrome://resources/polymer/v1_0/|%s' % _POLYMER_PATH, | 46 '--redirect', 'chrome://resources/polymer/v1_0/|%s' % _POLYMER_PATH, |
52 | 47 |
53 '--strip-comments', | 48 '--strip-comments', |
54 ] | 49 ] |
55 | 50 |
56 def main(): | 51 |
52 def vulcanize(directory, host, html_in_file, html_out_file, js_out_file, | |
Dan Beam
2016/08/10 18:17:41
nit: this is not public-ish to those that import t
Dan Beam
2016/08/10 18:17:41
can you name some or all of these params?
tsergeant
2016/08/11 01:28:26
I'm not sure what you mean? They are named?
Dan Beam
2016/08/11 02:29:49
sorry, keyword=arguments
https://docs.python.org/3
tsergeant
2016/08/11 03:28:00
Right, got it. I've changed the output files to ke
| |
53 extra_args = []): | |
Dan Beam
2016/08/10 18:17:41
python style guide asks you to use something like
tsergeant
2016/08/11 01:28:26
Done.
| |
57 def _run_cmd(cmd_parts, stdout=None): | 54 def _run_cmd(cmd_parts, stdout=None): |
58 cmd = "'" + "' '".join(cmd_parts) + "'" | 55 cmd = "'" + "' '".join(cmd_parts) + "'" |
59 process = subprocess.Popen( | 56 process = subprocess.Popen( |
60 cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) | 57 cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) |
61 stdout, stderr = process.communicate() | 58 stdout, stderr = process.communicate() |
62 | 59 |
63 if stderr: | 60 if stderr: |
64 print >> sys.stderr, '%s failed: %s' % (cmd, stderr) | 61 print >> sys.stderr, '%s failed: %s' % (cmd, stderr) |
65 raise | 62 raise |
66 | 63 |
67 return stdout | 64 return stdout |
68 | 65 |
69 output = _run_cmd(['vulcanize'] + _VULCANIZE_ARGS + [_HTML_IN_PATH]) | 66 print 'Vulcanizing %s' % directory |
67 | |
68 target_path = os.path.join(_HERE_PATH, directory) | |
69 html_in_path = os.path.join(target_path, html_in_file) | |
70 html_out_path = os.path.join(target_path, html_out_file) | |
71 js_out_path = os.path.join(target_path, js_out_file) | |
72 | |
73 output = _run_cmd(['vulcanize'] + _VULCANIZE_BASE_ARGS + extra_args + | |
74 ['--redirect', 'chrome://%s/|%s' % (host, target_path), | |
75 html_in_path]) | |
70 | 76 |
71 with tempfile.NamedTemporaryFile(mode='wt+', delete=False) as tmp: | 77 with tempfile.NamedTemporaryFile(mode='wt+', delete=False) as tmp: |
72 tmp.write(output.replace( | 78 tmp.write(output.replace( |
73 '<include src="', '<include src="../../../../ui/webui/resources/js/')) | 79 '<include src="', '<include src="../../../../ui/webui/resources/js/')) |
Dan Beam
2016/08/10 18:17:41
sooooooo are we continuing this hack?
../../../..
tsergeant
2016/08/11 01:28:26
Hmmmmmmmmm.
At the moment, the only place this is
tsergeant
2016/08/11 03:28:00
After discussing this: I'm leaving it this way for
michaelpg
2016/08/11 07:42:14
I think the right way to do this is to run grit *b
tsergeant
2016/08/15 05:54:18
Grit output is platform specific (due to <if> tags
| |
74 | 80 |
75 try: | 81 try: |
76 _run_cmd(['crisper', '--source', tmp.name, | 82 _run_cmd(['crisper', '--source', tmp.name, |
77 '--script-in-head', 'false', | 83 '--script-in-head', 'false', |
78 '--html', _HTML_OUT_PATH, | 84 '--html', html_out_path, |
79 '--js', _JS_OUT_PATH]) | 85 '--js', js_out_path]) |
86 | |
87 # TODO(tsergeant): Remove when JS resources are minified by default: | |
88 # crbug.com/619091. | |
89 _run_cmd(['uglifyjs', js_out_path, | |
tsergeant
2016/08/10 06:40:34
Uglify-with-beautify is...a little strange. Stripp
Dan Beam
2016/08/10 18:17:41
I don't really care what we use, but either way we
| |
90 '--beautify', 'indent-level=2,quote_style=3', | |
91 '--comments', '/Copyright|license|LICENSE|\<\/?if/', | |
92 '--output', js_out_path]) | |
80 finally: | 93 finally: |
81 os.remove(tmp.name) | 94 os.remove(tmp.name) |
82 | 95 |
83 | 96 |
97 def main(): | |
98 vulcanize('md_downloads', 'downloads', 'downloads.html', 'vulcanized.html', | |
99 'crisper.js') | |
100 | |
101 # Already loaded by history.html: | |
102 history_extra_args = ['--exclude', 'chrome://resources/html/util.html', | |
103 '--exclude', 'chrome://history/constants.html'] | |
104 vulcanize('md_history', 'history', 'app.html', 'app.vulcanized.html', | |
105 'app.crisper.js', history_extra_args) | |
106 | |
Dan Beam
2016/08/10 18:17:41
\n\n between top-levels
tsergeant
2016/08/11 01:28:26
Done.
| |
84 if __name__ == '__main__': | 107 if __name__ == '__main__': |
85 main() | 108 main() |
OLD | NEW |