OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 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 |
| 4 # found in the LICENSE file. |
| 5 |
| 6 import argparse |
| 7 import itertools |
| 8 import os |
| 9 import platform |
| 10 import re |
| 11 import subprocess |
| 12 import sys |
| 13 import tempfile |
| 14 |
| 15 |
| 16 _HERE_PATH = os.path.dirname(__file__) |
| 17 _SRC_PATH = os.path.normpath(os.path.join(_HERE_PATH, '..', '..', '..')) |
| 18 _CWD = os.getcwd() |
| 19 |
| 20 sys.path.append(os.path.join(_SRC_PATH, 'third_party', 'node')) |
| 21 import node |
| 22 import node_modules |
| 23 |
| 24 |
| 25 _RESOURCES_PATH = os.path.join(_SRC_PATH, 'ui', 'webui', 'resources') |
| 26 |
| 27 |
| 28 _CR_ELEMENTS_PATH = os.path.join(_RESOURCES_PATH, 'cr_elements') |
| 29 |
| 30 |
| 31 _CSS_RESOURCES_PATH = os.path.join(_RESOURCES_PATH, 'css') |
| 32 |
| 33 |
| 34 _HTML_RESOURCES_PATH = os.path.join(_RESOURCES_PATH, 'html') |
| 35 |
| 36 |
| 37 _JS_RESOURCES_PATH = os.path.join(_RESOURCES_PATH, 'js') |
| 38 |
| 39 |
| 40 _POLYMER_PATH = os.path.join( |
| 41 _SRC_PATH, 'third_party', 'polymer', 'v1_0', 'components-chromium') |
| 42 |
| 43 |
| 44 _VULCANIZE_BASE_ARGS = [ |
| 45 '--exclude', 'crisper.js', |
| 46 |
| 47 # These files are already combined and minified. |
| 48 '--exclude', 'chrome://resources/html/polymer.html', |
| 49 '--exclude', 'web-animations-next-lite.min.js', |
| 50 |
| 51 # These files are dynamically created by C++. |
| 52 '--exclude', 'load_time_data.js', |
| 53 '--exclude', 'strings.js', |
| 54 '--exclude', 'text_defaults.css', |
| 55 '--exclude', 'text_defaults_md.css', |
| 56 |
| 57 '--inline-css', |
| 58 '--inline-scripts', |
| 59 '--strip-comments', |
| 60 ] |
| 61 |
| 62 |
| 63 _URL_MAPPINGS = [ |
| 64 ('chrome://resources/cr_elements/', _CR_ELEMENTS_PATH), |
| 65 ('chrome://resources/css/', _CSS_RESOURCES_PATH), |
| 66 ('chrome://resources/html/', _HTML_RESOURCES_PATH), |
| 67 ('chrome://resources/js/', _JS_RESOURCES_PATH), |
| 68 ('chrome://resources/polymer/v1_0/', _POLYMER_PATH) |
| 69 ] |
| 70 |
| 71 |
| 72 _VULCANIZE_REDIRECT_ARGS = list(itertools.chain.from_iterable(map( |
| 73 lambda m: ['--redirect', '"%s|%s"' % (m[0], m[1])], _URL_MAPPINGS))) |
| 74 |
| 75 |
| 76 _REQUEST_LIST_FILE = 'request_list.txt' |
| 77 |
| 78 |
| 79 _PAK_UNPACK_FOLDER = 'flattened' |
| 80 |
| 81 |
| 82 def _run_node(cmd_parts, stdout=None): |
| 83 cmd = " ".join([node.GetBinaryPath()] + cmd_parts) |
| 84 process = subprocess.Popen( |
| 85 cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) |
| 86 stdout, stderr = process.communicate() |
| 87 |
| 88 if stderr: |
| 89 print >> sys.stderr, '%s failed: %s' % (cmd, stderr) |
| 90 raise |
| 91 |
| 92 return stdout |
| 93 |
| 94 |
| 95 def _undo_mapping(mappings, url): |
| 96 for (redirect_url, file_path) in mappings: |
| 97 if url.startswith(redirect_url): |
| 98 return url.replace(redirect_url, file_path + os.sep) |
| 99 return url |
| 100 |
| 101 |
| 102 # Get a list of all files that were bundled with Vulcanize and update the |
| 103 # depfile accordingly such that Ninja knows when to trigger re-vulcanization. |
| 104 def _update_dep_file(in_folder, args): |
| 105 in_path = os.path.join(_CWD, in_folder) |
| 106 out_path = os.path.join(_CWD, args.out_folder) |
| 107 |
| 108 # Prior call to vulcanize already generated the deps list, grab it from there. |
| 109 request_list = open(os.path.join( |
| 110 out_path, _REQUEST_LIST_FILE), 'r').read().splitlines() |
| 111 |
| 112 # Undo the URL mappings applied by vulcanize to get file paths relative to |
| 113 # current working directory. |
| 114 url_mappings = _URL_MAPPINGS + [ |
| 115 ('/', os.path.relpath(in_path, _CWD)), |
| 116 ('chrome://%s/' % args.host, os.path.relpath(in_path, _CWD)), |
| 117 ] |
| 118 |
| 119 dependencies = map( |
| 120 lambda url: _undo_mapping(url_mappings, url), request_list) |
| 121 |
| 122 # If the input was a .pak file, the generated depfile should not list files |
| 123 # already in the .pak file. |
| 124 filtered_dependencies = dependencies |
| 125 if (args.input_type == 'PAK_FILE'): |
| 126 filter_url = os.path.join(args.out_folder, _PAK_UNPACK_FOLDER) |
| 127 filtered_dependencies = filter( |
| 128 lambda url: not url.startswith(filter_url), dependencies) |
| 129 |
| 130 with open(os.path.join(_CWD, args.depfile), 'w') as f: |
| 131 f.write(os.path.join( |
| 132 args.out_folder, args.html_out_file) + ': ' + ' '.join( |
| 133 filtered_dependencies)) |
| 134 |
| 135 |
| 136 def _vulcanize(in_folder, out_folder, host, html_in_file, |
| 137 html_out_file, js_out_file): |
| 138 in_path = os.path.normpath(os.path.join(_CWD, in_folder)) |
| 139 out_path = os.path.join(_CWD, out_folder) |
| 140 |
| 141 html_out_path = os.path.join(out_path, html_out_file) |
| 142 js_out_path = os.path.join(out_path, js_out_file) |
| 143 |
| 144 output = _run_node( |
| 145 [node_modules.PathToVulcanize()] + |
| 146 _VULCANIZE_BASE_ARGS + _VULCANIZE_REDIRECT_ARGS + |
| 147 ['--out-request-list', os.path.join(out_path, _REQUEST_LIST_FILE), |
| 148 '--redirect', '"/|%s"' % in_path, |
| 149 '--redirect', '"chrome://%s/|%s"' % (host, in_path), |
| 150 # TODO(dpapad): Figure out why vulcanize treats the input path |
| 151 # differently on Windows VS Linux/Mac. |
| 152 os.path.join( |
| 153 in_path if platform.system() == 'Windows' else os.sep, |
| 154 html_in_file)]) |
| 155 |
| 156 with tempfile.NamedTemporaryFile(mode='wt+', delete=False) as tmp: |
| 157 # Grit includes are not supported, use HTML imports instead. |
| 158 tmp.write(output.replace( |
| 159 '<include src="', '<include src-disabled="')) |
| 160 |
| 161 try: |
| 162 _run_node([node_modules.PathToCrisper(), |
| 163 '--source', tmp.name, |
| 164 '--script-in-head', 'false', |
| 165 '--html', html_out_path, |
| 166 '--js', js_out_path]) |
| 167 |
| 168 # TODO(tsergeant): Remove when JS resources are minified by default: |
| 169 # crbug.com/619091. |
| 170 _run_node([node_modules.PathToUglifyJs(), js_out_path, |
| 171 '--comments', '"/Copyright|license|LICENSE|\<\/?if/"', |
| 172 '--output', js_out_path]) |
| 173 finally: |
| 174 os.remove(tmp.name) |
| 175 |
| 176 |
| 177 def _css_build(out_folder, files): |
| 178 out_path = os.path.join(_CWD, out_folder) |
| 179 paths = map(lambda f: os.path.join(out_path, f), files) |
| 180 |
| 181 _run_node([node_modules.PathToPolymerCssBuild()] + paths) |
| 182 |
| 183 |
| 184 def main(): |
| 185 parser = argparse.ArgumentParser() |
| 186 parser.add_argument('--depfile') |
| 187 parser.add_argument('--host') |
| 188 parser.add_argument('--html_in_file') |
| 189 parser.add_argument('--html_out_file') |
| 190 parser.add_argument('--input') |
| 191 parser.add_argument('--input_type') |
| 192 parser.add_argument('--js_out_file') |
| 193 parser.add_argument('--out_folder') |
| 194 args = parser.parse_args() |
| 195 args.input = os.path.normpath(args.input) |
| 196 |
| 197 vulcanize_input_folder = args.input |
| 198 |
| 199 # If a .pak file was specified, unpack that file first and pass the output to |
| 200 # vulcanize. |
| 201 if (args.input_type == 'PAK_FILE'): |
| 202 import unpack_pak |
| 203 input_folder = os.path.join(_CWD, args.input) |
| 204 output_folder = os.path.join(args.out_folder, _PAK_UNPACK_FOLDER) |
| 205 unpack_pak.unpack(args.input, output_folder) |
| 206 vulcanize_input_folder = output_folder |
| 207 |
| 208 _vulcanize(vulcanize_input_folder, args.out_folder, args.host, |
| 209 args.html_in_file, args.html_out_file, args.js_out_file) |
| 210 _css_build(args.out_folder, files=[args.html_out_file]) |
| 211 |
| 212 _update_dep_file(vulcanize_input_folder, args) |
| 213 |
| 214 |
| 215 if __name__ == '__main__': |
| 216 main() |
OLD | NEW |