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