| 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 |
| 11 |
| 11 # See //docs/vulcanize.md for instructions on installing prerequistes and | 12 # See //docs/vulcanize.md for instructions on installing prerequistes and |
| 12 # running the vulcanize build. | 13 # running the vulcanize build. |
| 13 | 14 |
| 14 _HERE_PATH = os.path.join(os.path.dirname(__file__)) | 15 _HERE_PATH = os.path.join(os.path.dirname(__file__)) |
| 15 _SRC_PATH = os.path.normpath(os.path.join(_HERE_PATH, '..', '..', '..')) | 16 _SRC_PATH = os.path.normpath(os.path.join(_HERE_PATH, '..', '..', '..')) |
| 16 | 17 |
| 18 sys.path.append(os.path.join(_SRC_PATH, 'third_party', 'node')) |
| 19 import node |
| 20 import node_modules |
| 21 |
| 22 |
| 17 _RESOURCES_PATH = os.path.join(_SRC_PATH, 'ui', 'webui', 'resources') | 23 _RESOURCES_PATH = os.path.join(_SRC_PATH, 'ui', 'webui', 'resources') |
| 18 | 24 |
| 19 _CR_ELEMENTS_PATH = os.path.join(_RESOURCES_PATH, 'cr_elements') | 25 _CR_ELEMENTS_PATH = os.path.join(_RESOURCES_PATH, 'cr_elements') |
| 20 _CSS_RESOURCES_PATH = os.path.join(_RESOURCES_PATH, 'css') | 26 _CSS_RESOURCES_PATH = os.path.join(_RESOURCES_PATH, 'css') |
| 21 _HTML_RESOURCES_PATH = os.path.join(_RESOURCES_PATH, 'html') | 27 _HTML_RESOURCES_PATH = os.path.join(_RESOURCES_PATH, 'html') |
| 22 _JS_RESOURCES_PATH = os.path.join(_RESOURCES_PATH, 'js') | 28 _JS_RESOURCES_PATH = os.path.join(_RESOURCES_PATH, 'js') |
| 23 _POLYMER_PATH = os.path.join( | 29 _POLYMER_PATH = os.path.join( |
| 24 _SRC_PATH, 'third_party', 'polymer', 'v1_0', 'components-chromium') | 30 _SRC_PATH, 'third_party', 'polymer', 'v1_0', 'components-chromium') |
| 25 | 31 |
| 26 _VULCANIZE_BASE_ARGS = [ | 32 _VULCANIZE_BASE_ARGS = [ |
| (...skipping 14 matching lines...) Expand all Loading... |
| 41 | 47 |
| 42 '--redirect', '"chrome://resources/cr_elements/|%s"' % _CR_ELEMENTS_PATH, | 48 '--redirect', '"chrome://resources/cr_elements/|%s"' % _CR_ELEMENTS_PATH, |
| 43 '--redirect', '"chrome://resources/css/|%s"' % _CSS_RESOURCES_PATH, | 49 '--redirect', '"chrome://resources/css/|%s"' % _CSS_RESOURCES_PATH, |
| 44 '--redirect', '"chrome://resources/html/|%s"' % _HTML_RESOURCES_PATH, | 50 '--redirect', '"chrome://resources/html/|%s"' % _HTML_RESOURCES_PATH, |
| 45 '--redirect', '"chrome://resources/js/|%s"' % _JS_RESOURCES_PATH, | 51 '--redirect', '"chrome://resources/js/|%s"' % _JS_RESOURCES_PATH, |
| 46 '--redirect', '"chrome://resources/polymer/v1_0/|%s"' % _POLYMER_PATH, | 52 '--redirect', '"chrome://resources/polymer/v1_0/|%s"' % _POLYMER_PATH, |
| 47 | 53 |
| 48 '--strip-comments', | 54 '--strip-comments', |
| 49 ] | 55 ] |
| 50 | 56 |
| 51 def _run_cmd(cmd_parts, stdout=None): | 57 |
| 52 cmd = " ".join(cmd_parts) | 58 def _run_node(cmd_parts, stdout=None): |
| 59 cmd = " ".join([node.GetBinaryPath()] + cmd_parts) |
| 53 process = subprocess.Popen( | 60 process = subprocess.Popen( |
| 54 cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) | 61 cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) |
| 55 stdout, stderr = process.communicate() | 62 stdout, stderr = process.communicate() |
| 56 | 63 |
| 57 if stderr: | 64 if stderr: |
| 58 print >> sys.stderr, '%s failed: %s' % (cmd, stderr) | 65 print >> sys.stderr, '%s failed: %s' % (cmd, stderr) |
| 59 raise | 66 raise |
| 60 | 67 |
| 61 return stdout | 68 return stdout |
| 62 | 69 |
| 70 |
| 63 def _vulcanize(directory, host, html_in_file, html_out_file='vulcanized.html', | 71 def _vulcanize(directory, host, html_in_file, html_out_file='vulcanized.html', |
| 64 js_out_file='crisper.js', extra_args=None): | 72 js_out_file='crisper.js', extra_args=None): |
| 65 print 'Vulcanizing %s/%s' % (directory, html_in_file) | 73 print 'Vulcanizing %s/%s' % (directory, html_in_file) |
| 66 | 74 |
| 67 target_path = os.path.join(_HERE_PATH, directory) | 75 target_path = os.path.join(_HERE_PATH, directory) |
| 68 html_in_path = os.path.join(target_path, html_in_file) | 76 html_in_path = os.path.join(target_path, html_in_file) |
| 69 html_out_path = os.path.join(target_path, html_out_file) | 77 html_out_path = os.path.join(target_path, html_out_file) |
| 70 js_out_path = os.path.join(target_path, js_out_file) | 78 js_out_path = os.path.join(target_path, js_out_file) |
| 71 extra_args = extra_args or [] | 79 extra_args = extra_args or [] |
| 72 | 80 |
| 73 output = _run_cmd(['vulcanize'] + _VULCANIZE_BASE_ARGS + extra_args + | 81 output = _run_node( |
| 74 ['--redirect', '"chrome://%s/|%s"' % (host, target_path), | 82 [node_modules.PathToVulcanize()] + _VULCANIZE_BASE_ARGS + extra_args + |
| 75 html_in_path]) | 83 ['--redirect', '"chrome://%s/|%s"' % (host, target_path), html_in_path]) |
| 76 | 84 |
| 77 with tempfile.NamedTemporaryFile(mode='wt+', delete=False) as tmp: | 85 with tempfile.NamedTemporaryFile(mode='wt+', delete=False) as tmp: |
| 78 # Grit includes are not supported, use HTML imports instead. | 86 # Grit includes are not supported, use HTML imports instead. |
| 79 tmp.write(output.replace( | 87 tmp.write(output.replace( |
| 80 '<include src="', '<include src-disabled="')) | 88 '<include src="', '<include src-disabled="')) |
| 81 | 89 |
| 82 try: | 90 try: |
| 83 _run_cmd(['crisper', '--source', tmp.name, | 91 _run_node([node_modules.PathToCrisper(), '--source', tmp.name, |
| 84 '--script-in-head', 'false', | 92 '--script-in-head', 'false', '--html', html_out_path, |
| 85 '--html', html_out_path, | 93 '--js', js_out_path]) |
| 86 '--js', js_out_path]) | |
| 87 | 94 |
| 88 # TODO(tsergeant): Remove when JS resources are minified by default: | 95 # TODO(tsergeant): Remove when JS resources are minified by default: |
| 89 # crbug.com/619091. | 96 # crbug.com/619091. |
| 90 _run_cmd(['uglifyjs', js_out_path, | 97 _run_node([node_modules.PathToUglifyJs(), js_out_path, |
| 91 '--comments', '"/Copyright|license|LICENSE|\<\/?if/"', | 98 '--comments', '"/Copyright|license|LICENSE|\<\/?if/"', |
| 92 '--output', js_out_path]) | 99 '--output', js_out_path]) |
| 93 finally: | 100 finally: |
| 94 os.remove(tmp.name) | 101 os.remove(tmp.name) |
| 95 | 102 |
| 96 | 103 |
| 97 def _css_build(directory, files): | 104 def _css_build(directory, files): |
| 98 target_path = os.path.join(_HERE_PATH, directory) | 105 target_path = os.path.join(_HERE_PATH, directory) |
| 99 paths = map(lambda f: os.path.join(target_path, f), files) | 106 paths = map(lambda f: os.path.join(target_path, f), files) |
| 100 | 107 |
| 101 _run_cmd(['polymer-css-build'] + paths) | 108 _run_node([node_modules.PathToPolymerCssBuild()] + paths) |
| 102 | 109 |
| 103 | 110 |
| 104 def main(): | 111 def main(): |
| 105 _vulcanize(directory='md_downloads', host='downloads', | 112 _vulcanize(directory='md_downloads', host='downloads', |
| 106 html_in_file='downloads.html') | 113 html_in_file='downloads.html') |
| 107 _css_build(directory='md_downloads', files=['vulcanized.html']) | 114 _css_build(directory='md_downloads', files=['vulcanized.html']) |
| 108 | 115 |
| 109 # Already loaded by history.html: | 116 # Already loaded by history.html: |
| 110 history_extra_args = ['--exclude', 'chrome://resources/html/util.html', | 117 history_extra_args = ['--exclude', 'chrome://resources/html/util.html', |
| 111 '--exclude', 'chrome://history/constants.html'] | 118 '--exclude', 'chrome://history/constants.html'] |
| 112 _vulcanize(directory='md_history', host='history', html_in_file='app.html', | 119 _vulcanize(directory='md_history', host='history', html_in_file='app.html', |
| 113 html_out_file='app.vulcanized.html', js_out_file='app.crisper.js', | 120 html_out_file='app.vulcanized.html', js_out_file='app.crisper.js', |
| 114 extra_args=history_extra_args) | 121 extra_args=history_extra_args) |
| 115 | 122 |
| 116 # Ensures that no file transitively imported by app.vulcanized.html is | 123 # Ensures that no file transitively imported by app.vulcanized.html is |
| 117 # imported by lazy_load.vulcanized.html. | 124 # imported by lazy_load.vulcanized.html. |
| 118 lazy_load_extra_args = ['--exclude', 'chrome://history/app.html'] | 125 lazy_load_extra_args = ['--exclude', 'chrome://history/app.html'] |
| 119 _vulcanize(directory='md_history', host='history', | 126 _vulcanize(directory='md_history', host='history', |
| 120 html_in_file='lazy_load.html', | 127 html_in_file='lazy_load.html', |
| 121 html_out_file='lazy_load.vulcanized.html', | 128 html_out_file='lazy_load.vulcanized.html', |
| 122 js_out_file='lazy_load.crisper.js', | 129 js_out_file='lazy_load.crisper.js', |
| 123 extra_args=history_extra_args + lazy_load_extra_args) | 130 extra_args=history_extra_args + lazy_load_extra_args) |
| 124 _css_build(directory='md_history', files=['app.vulcanized.html', | 131 _css_build(directory='md_history', files=['app.vulcanized.html', |
| 125 'lazy_load.vulcanized.html']) | 132 'lazy_load.vulcanized.html']) |
| 126 | 133 |
| 134 |
| 127 if __name__ == '__main__': | 135 if __name__ == '__main__': |
| 128 main() | 136 main() |
| OLD | NEW |