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 re | |
7 import subprocess | 8 import subprocess |
8 import sys | 9 import sys |
9 import tempfile | 10 import tempfile |
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 |
(...skipping 25 matching lines...) Expand all Loading... | |
42 '--redirect', 'chrome://resources/cr_elements/|%s' % _CR_ELEMENTS_PATH, | 43 '--redirect', 'chrome://resources/cr_elements/|%s' % _CR_ELEMENTS_PATH, |
43 '--redirect', 'chrome://resources/css/|%s' % _CSS_RESOURCES_PATH, | 44 '--redirect', 'chrome://resources/css/|%s' % _CSS_RESOURCES_PATH, |
44 '--redirect', 'chrome://resources/html/|%s' % _HTML_RESOURCES_PATH, | 45 '--redirect', 'chrome://resources/html/|%s' % _HTML_RESOURCES_PATH, |
45 '--redirect', 'chrome://resources/js/|%s' % _JS_RESOURCES_PATH, | 46 '--redirect', 'chrome://resources/js/|%s' % _JS_RESOURCES_PATH, |
46 '--redirect', 'chrome://resources/polymer/v1_0/|%s' % _POLYMER_PATH, | 47 '--redirect', 'chrome://resources/polymer/v1_0/|%s' % _POLYMER_PATH, |
47 | 48 |
48 '--strip-comments', | 49 '--strip-comments', |
49 ] | 50 ] |
50 | 51 |
51 | 52 |
53 def _strip_js_comments(filename): | |
54 start_comment = re.compile(r'^ */\*\*?$') | |
55 end_comment = re.compile(r'^ *\*\*?/$') | |
56 single_line_comment = re.compile(r'^ *//') | |
57 keep_comment = re.compile(r'Copyright|license|LICENSE|\<\/?if') | |
michaelpg
2016/08/17 07:39:56
ugh, grit works inside comments? what about
//
tsergeant
2016/08/17 07:57:52
The commented-if is used in one place in util.js.
| |
58 | |
59 with open(filename) as f: | |
60 contents = f.readlines() | |
61 | |
62 out_lines = [] | |
63 inside_comment = False | |
64 | |
65 for line in contents: | |
66 if not inside_comment: | |
67 if start_comment.match(line): | |
68 inside_comment = True | |
69 elif not single_line_comment.match(line) or keep_comment.search(line): | |
70 out_lines.append(line) | |
71 elif end_comment.match(line): | |
72 inside_comment = False | |
73 | |
74 with open(filename, 'w') as f: | |
75 f.writelines(out_lines) | |
76 | |
77 | |
52 def _vulcanize(directory, host, html_in_file, html_out_file='vulcanized.html', | 78 def _vulcanize(directory, host, html_in_file, html_out_file='vulcanized.html', |
53 js_out_file='crisper.js', extra_args=None): | 79 js_out_file='crisper.js', extra_args=None): |
54 def _run_cmd(cmd_parts, stdout=None): | 80 def _run_cmd(cmd_parts, stdout=None): |
55 cmd = "'" + "' '".join(cmd_parts) + "'" | 81 cmd = "'" + "' '".join(cmd_parts) + "'" |
56 process = subprocess.Popen( | 82 process = subprocess.Popen( |
57 cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) | 83 cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) |
58 stdout, stderr = process.communicate() | 84 stdout, stderr = process.communicate() |
59 | 85 |
60 if stderr: | 86 if stderr: |
61 print >> sys.stderr, '%s failed: %s' % (cmd, stderr) | 87 print >> sys.stderr, '%s failed: %s' % (cmd, stderr) |
(...skipping 16 matching lines...) Expand all Loading... | |
78 with tempfile.NamedTemporaryFile(mode='wt+', delete=False) as tmp: | 104 with tempfile.NamedTemporaryFile(mode='wt+', delete=False) as tmp: |
79 # Grit includes are not supported, use HTML imports instead. | 105 # Grit includes are not supported, use HTML imports instead. |
80 tmp.write(output.replace( | 106 tmp.write(output.replace( |
81 '<include src="', '<include src-disabled="')) | 107 '<include src="', '<include src-disabled="')) |
82 | 108 |
83 try: | 109 try: |
84 _run_cmd(['crisper', '--source', tmp.name, | 110 _run_cmd(['crisper', '--source', tmp.name, |
85 '--script-in-head', 'false', | 111 '--script-in-head', 'false', |
86 '--html', html_out_path, | 112 '--html', html_out_path, |
87 '--js', js_out_path]) | 113 '--js', js_out_path]) |
114 | |
115 _strip_js_comments(js_out_path) | |
88 finally: | 116 finally: |
89 os.remove(tmp.name) | 117 os.remove(tmp.name) |
90 | 118 |
91 | 119 |
92 def main(): | 120 def main(): |
93 _vulcanize(directory='md_downloads', host='downloads', | 121 _vulcanize(directory='md_downloads', host='downloads', |
94 html_in_file='downloads.html') | 122 html_in_file='downloads.html') |
95 | 123 |
96 # Already loaded by history.html: | 124 # Already loaded by history.html: |
97 history_extra_args = ['--exclude', 'chrome://resources/html/util.html', | 125 history_extra_args = ['--exclude', 'chrome://resources/html/util.html', |
98 '--exclude', 'chrome://history/constants.html'] | 126 '--exclude', 'chrome://history/constants.html'] |
99 _vulcanize(directory='md_history', host='history', html_in_file='app.html', | 127 _vulcanize(directory='md_history', host='history', html_in_file='app.html', |
100 html_out_file='app.vulcanized.html', js_out_file='app.crisper.js', | 128 html_out_file='app.vulcanized.html', js_out_file='app.crisper.js', |
101 extra_args=history_extra_args) | 129 extra_args=history_extra_args) |
102 | 130 |
103 | 131 |
104 if __name__ == '__main__': | 132 if __name__ == '__main__': |
105 main() | 133 main() |
OLD | NEW |