OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2016 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 |
(...skipping 30 matching lines...) Expand all Loading... |
41 | 41 |
42 '--redirect', 'chrome://resources/cr_elements/|%s' % _CR_ELEMENTS_PATH, | 42 '--redirect', 'chrome://resources/cr_elements/|%s' % _CR_ELEMENTS_PATH, |
43 '--redirect', 'chrome://resources/css/|%s' % _CSS_RESOURCES_PATH, | 43 '--redirect', 'chrome://resources/css/|%s' % _CSS_RESOURCES_PATH, |
44 '--redirect', 'chrome://resources/html/|%s' % _HTML_RESOURCES_PATH, | 44 '--redirect', 'chrome://resources/html/|%s' % _HTML_RESOURCES_PATH, |
45 '--redirect', 'chrome://resources/js/|%s' % _JS_RESOURCES_PATH, | 45 '--redirect', 'chrome://resources/js/|%s' % _JS_RESOURCES_PATH, |
46 '--redirect', 'chrome://resources/polymer/v1_0/|%s' % _POLYMER_PATH, | 46 '--redirect', 'chrome://resources/polymer/v1_0/|%s' % _POLYMER_PATH, |
47 | 47 |
48 '--strip-comments', | 48 '--strip-comments', |
49 ] | 49 ] |
50 | 50 |
| 51 def _run_cmd(cmd_parts, stdout=None): |
| 52 cmd = "'" + "' '".join(cmd_parts) + "'" |
| 53 process = subprocess.Popen( |
| 54 cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) |
| 55 stdout, stderr = process.communicate() |
| 56 |
| 57 if stderr: |
| 58 print >> sys.stderr, '%s failed: %s' % (cmd, stderr) |
| 59 raise |
| 60 |
| 61 return stdout |
51 | 62 |
52 def _vulcanize(directory, host, html_in_file, html_out_file='vulcanized.html', | 63 def _vulcanize(directory, host, html_in_file, html_out_file='vulcanized.html', |
53 js_out_file='crisper.js', extra_args=None): | 64 js_out_file='crisper.js', extra_args=None): |
54 def _run_cmd(cmd_parts, stdout=None): | 65 print 'Vulcanizing %s/%s' % (directory, html_in_file) |
55 cmd = "'" + "' '".join(cmd_parts) + "'" | |
56 process = subprocess.Popen( | |
57 cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) | |
58 stdout, stderr = process.communicate() | |
59 | |
60 if stderr: | |
61 print >> sys.stderr, '%s failed: %s' % (cmd, stderr) | |
62 raise | |
63 | |
64 return stdout | |
65 | |
66 print 'Vulcanizing %s' % directory | |
67 | 66 |
68 target_path = os.path.join(_HERE_PATH, directory) | 67 target_path = os.path.join(_HERE_PATH, directory) |
69 html_in_path = os.path.join(target_path, html_in_file) | 68 html_in_path = os.path.join(target_path, html_in_file) |
70 html_out_path = os.path.join(target_path, html_out_file) | 69 html_out_path = os.path.join(target_path, html_out_file) |
71 js_out_path = os.path.join(target_path, js_out_file) | 70 js_out_path = os.path.join(target_path, js_out_file) |
72 extra_args = extra_args or [] | 71 extra_args = extra_args or [] |
73 | 72 |
74 output = _run_cmd(['vulcanize'] + _VULCANIZE_BASE_ARGS + extra_args + | 73 output = _run_cmd(['vulcanize'] + _VULCANIZE_BASE_ARGS + extra_args + |
75 ['--redirect', 'chrome://%s/|%s' % (host, target_path), | 74 ['--redirect', 'chrome://%s/|%s' % (host, target_path), |
76 html_in_path]) | 75 html_in_path]) |
77 | 76 |
78 with tempfile.NamedTemporaryFile(mode='wt+', delete=False) as tmp: | 77 with tempfile.NamedTemporaryFile(mode='wt+', delete=False) as tmp: |
79 # Grit includes are not supported, use HTML imports instead. | 78 # Grit includes are not supported, use HTML imports instead. |
80 tmp.write(output.replace( | 79 tmp.write(output.replace( |
81 '<include src="', '<include src-disabled="')) | 80 '<include src="', '<include src-disabled="')) |
82 | 81 |
83 try: | 82 try: |
84 _run_cmd(['crisper', '--source', tmp.name, | 83 _run_cmd(['crisper', '--source', tmp.name, |
85 '--script-in-head', 'false', | 84 '--script-in-head', 'false', |
86 '--html', html_out_path, | 85 '--html', html_out_path, |
87 '--js', js_out_path]) | 86 '--js', js_out_path]) |
88 | 87 |
89 # TODO(tsergeant): Remove when JS resources are minified by default: | 88 # TODO(tsergeant): Remove when JS resources are minified by default: |
90 # crbug.com/619091. | 89 # crbug.com/619091. |
91 _run_cmd(['uglifyjs', js_out_path, | 90 _run_cmd(['uglifyjs', js_out_path, |
92 '--beautify', 'indent-level=2,quote_style=3', | 91 '--beautify', 'indent-level=2,quote_style=3', |
93 '--comments', '/Copyright|license|LICENSE|\<\/?if/', | 92 '--comments', '/Copyright|license|LICENSE|\<\/?if/', |
94 '--output', js_out_path]) | 93 '--output', js_out_path]) |
95 _run_cmd(['polymer-css-build', html_out_path]) | |
96 finally: | 94 finally: |
97 os.remove(tmp.name) | 95 os.remove(tmp.name) |
98 | 96 |
99 | 97 |
| 98 def _css_build(directory, files): |
| 99 target_path = os.path.join(_HERE_PATH, directory) |
| 100 paths = map(lambda f: os.path.join(target_path, f), files) |
| 101 |
| 102 _run_cmd(['polymer-css-build'] + paths) |
| 103 |
| 104 |
100 def main(): | 105 def main(): |
101 _vulcanize(directory='md_downloads', host='downloads', | 106 _vulcanize(directory='md_downloads', host='downloads', |
102 html_in_file='downloads.html') | 107 html_in_file='downloads.html') |
| 108 _css_build(directory='md_downloads', files=['vulcanized.html']) |
103 | 109 |
104 # Already loaded by history.html: | 110 # Already loaded by history.html: |
105 history_extra_args = ['--exclude', 'chrome://resources/html/util.html', | 111 history_extra_args = ['--exclude', 'chrome://resources/html/util.html', |
106 '--exclude', 'chrome://history/constants.html'] | 112 '--exclude', 'chrome://history/constants.html'] |
107 _vulcanize(directory='md_history', host='history', html_in_file='app.html', | 113 _vulcanize(directory='md_history', host='history', html_in_file='app.html', |
108 html_out_file='app.vulcanized.html', js_out_file='app.crisper.js', | 114 html_out_file='app.vulcanized.html', js_out_file='app.crisper.js', |
109 extra_args=history_extra_args) | 115 extra_args=history_extra_args) |
110 | 116 |
| 117 # Ensures that no file transitively imported by app.vulcanized.html is |
| 118 # imported by lazy_load.vulcanized.html. |
| 119 lazy_load_extra_args = ['--exclude', 'chrome://history/app.html'] |
| 120 _vulcanize(directory='md_history', host='history', |
| 121 html_in_file='lazy_load.html', |
| 122 html_out_file='lazy_load.vulcanized.html', |
| 123 js_out_file='lazy_load.crisper.js', |
| 124 extra_args=history_extra_args + lazy_load_extra_args) |
| 125 _css_build(directory='md_history', files=['app.vulcanized.html', |
| 126 'lazy_load.vulcanized.html']) |
111 | 127 |
112 if __name__ == '__main__': | 128 if __name__ == '__main__': |
113 main() | 129 main() |
OLD | NEW |