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 argparse | 6 import argparse |
7 import itertools | 7 import itertools |
8 import os | 8 import os |
9 import platform | 9 import platform |
10 import re | 10 import re |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
151 | 151 |
152 # Grit includes are not supported, use HTML imports instead. | 152 # Grit includes are not supported, use HTML imports instead. |
153 output = output.replace('<include src="', '<include src-disabled="') | 153 output = output.replace('<include src="', '<include src-disabled="') |
154 | 154 |
155 if args.insert_in_head: | 155 if args.insert_in_head: |
156 assert '<head>' in output | 156 assert '<head>' in output |
157 # NOTE(dbeam): Vulcanize eats <base> tags after processing. This undoes | 157 # NOTE(dbeam): Vulcanize eats <base> tags after processing. This undoes |
158 # that by adding a <base> tag to the (post-processed) generated output. | 158 # that by adding a <base> tag to the (post-processed) generated output. |
159 output = output.replace('<head>', '<head>' + args.insert_in_head) | 159 output = output.replace('<head>', '<head>' + args.insert_in_head) |
160 | 160 |
161 with tempfile.NamedTemporaryFile(mode='wt+', delete=False) as tmp: | 161 crisper_input = tempfile.NamedTemporaryFile(mode='wt+', delete=False) |
162 tmp.write(output) | 162 crisper_input.write(output) |
163 crisper_input.close() | |
164 | |
165 crisper_output = tempfile.NamedTemporaryFile(mode='wt+', delete=False) | |
166 crisper_output.close() | |
163 | 167 |
164 try: | 168 try: |
165 node.RunNode([node_modules.PathToCrisper(), | 169 node.RunNode([node_modules.PathToCrisper(), |
166 '--source', tmp.name, | 170 '--source', crisper_input.name, |
167 '--script-in-head', 'false', | 171 '--script-in-head', 'false', |
172 '--only-split', | |
168 '--html', html_out_path, | 173 '--html', html_out_path, |
169 '--js', js_out_path]) | 174 '--js', crisper_output.name]) |
170 | 175 |
171 # Create an empty JS file if crisper did not create one. | 176 # Crisper by default inserts a <script> tag with the name of the --js file, |
172 if not os.path.isfile(js_out_path): | 177 # but since we are using a temporary file, need to manually insert a |
173 open(js_out_path, 'w').close() | 178 # <script> tag with the correct final filename (in combination with |
179 # --only-split flag). There is no way currently to manually specify the | |
180 # <script> tag's path, see https://github.com/PolymerLabs/crisper/issues/46. | |
181 with open(html_out_path, "r+") as f: | |
Dan Beam
2017/06/06 19:09:27
nit: rest of this file uses ' instead of "
dpapad
2017/06/06 19:30:46
Done.
| |
182 data = f.read() | |
183 newData = data.replace( | |
Dan Beam
2017/06/06 19:09:27
python_vars_like_this
dpapad
2017/06/06 19:30:46
Done.
| |
184 '</body></html>', | |
185 '<script src="' + args.js_out_file + '"></script></body></html>') | |
186 assert newData != data, 'Expected to find </body></html> token.' | |
187 f.seek(0) | |
188 f.write(newData) | |
189 f.truncate() | |
174 | 190 |
175 node.RunNode([node_modules.PathToUglifyJs(), js_out_path, | 191 node.RunNode([node_modules.PathToUglifyJs(), crisper_output.name, |
176 '--comments', '"/Copyright|license|LICENSE|\<\/?if/"', | 192 '--comments', '"/Copyright|license|LICENSE|\<\/?if/"', |
177 '--output', js_out_path]) | 193 '--output', js_out_path]) |
178 finally: | 194 finally: |
179 os.remove(tmp.name) | 195 if os.path.exists(crisper_input.name): |
196 os.remove(crisper_input.name) | |
197 if os.path.exists(crisper_output.name): | |
198 os.remove(crisper_output.name) | |
180 | 199 |
181 | 200 |
182 def main(argv): | 201 def main(argv): |
183 parser = argparse.ArgumentParser() | 202 parser = argparse.ArgumentParser() |
184 parser.add_argument('--depfile', required=True) | 203 parser.add_argument('--depfile', required=True) |
185 parser.add_argument('--exclude', nargs='*') | 204 parser.add_argument('--exclude', nargs='*') |
186 parser.add_argument('--host', required=True) | 205 parser.add_argument('--host', required=True) |
187 parser.add_argument('--html_in_file', required=True) | 206 parser.add_argument('--html_in_file', required=True) |
188 parser.add_argument('--html_out_file', required=True) | 207 parser.add_argument('--html_out_file', required=True) |
189 parser.add_argument('--input', required=True) | 208 parser.add_argument('--input', required=True) |
190 parser.add_argument('--insert_in_head') | 209 parser.add_argument('--insert_in_head') |
191 parser.add_argument('--js_out_file', required=True) | 210 parser.add_argument('--js_out_file', required=True) |
192 parser.add_argument('--out_folder', required=True) | 211 parser.add_argument('--out_folder', required=True) |
193 args = parser.parse_args(argv) | 212 args = parser.parse_args(argv) |
194 | 213 |
195 # NOTE(dbeam): on Windows, GN can send dirs/like/this. When joined, you might | 214 # NOTE(dbeam): on Windows, GN can send dirs/like/this. When joined, you might |
196 # get dirs/like/this\file.txt. This looks odd to windows. Normalize to right | 215 # get dirs/like/this\file.txt. This looks odd to windows. Normalize to right |
197 # the slashes. | 216 # the slashes. |
198 args.depfile = os.path.normpath(args.depfile) | 217 args.depfile = os.path.normpath(args.depfile) |
199 args.input = os.path.normpath(args.input) | 218 args.input = os.path.normpath(args.input) |
200 args.out_folder = os.path.normpath(args.out_folder) | 219 args.out_folder = os.path.normpath(args.out_folder) |
201 | 220 |
202 _vulcanize(args.input, args) | 221 _vulcanize(args.input, args) |
203 _update_dep_file(args.input, args) | 222 _update_dep_file(args.input, args) |
204 | 223 |
205 | 224 |
206 if __name__ == '__main__': | 225 if __name__ == '__main__': |
207 main(sys.argv[1:]) | 226 main(sys.argv[1:]) |
OLD | NEW |