Chromium Code Reviews| Index: chrome/browser/resources/vulcanize_gn.py |
| diff --git a/chrome/browser/resources/vulcanize_gn.py b/chrome/browser/resources/vulcanize_gn.py |
| index 9189330863c0d3d6734c2da8ac7b9669d6c06cea..a33b05130b08bcd7354788de21836b57ea04eb8f 100755 |
| --- a/chrome/browser/resources/vulcanize_gn.py |
| +++ b/chrome/browser/resources/vulcanize_gn.py |
| @@ -15,7 +15,7 @@ import tempfile |
| _HERE_PATH = os.path.dirname(__file__) |
| _SRC_PATH = os.path.normpath(os.path.join(_HERE_PATH, '..', '..', '..')) |
| -_CWD = os.getcwd() |
| +_CWD = os.getcwd() # NOTE(dbeam): this is typically out/<gn_name>/. |
| sys.path.append(os.path.join(_SRC_PATH, 'third_party', 'node')) |
| import node |
| @@ -96,6 +96,7 @@ def _undo_mapping(mappings, url): |
| for (redirect_url, file_path) in mappings: |
| if url.startswith(redirect_url): |
| return url.replace(redirect_url, file_path + os.sep) |
| + # TODO(dbeam): can we make this stricter? |
| return url |
| @@ -106,8 +107,22 @@ def _update_dep_file(in_folder, args): |
| out_path = os.path.join(_CWD, args.out_folder) |
| # Prior call to vulcanize already generated the deps list, grab it from there. |
| - request_list = open(os.path.join( |
| - out_path, _REQUEST_LIST_FILE), 'r').read().splitlines() |
| + request_list_path = os.path.join(out_path, _REQUEST_LIST_FILE) |
| + request_list = open(request_list_path, 'r').read().splitlines() |
| + |
| + if platform.system() == 'Windows': |
| + # TODO(dbeam): UGH. For some reason Vulcanize is interpreting the target |
| + # file path as a URL and using the drive letter (e.g. D:\) as a protocol. |
| + # This is a little insane, but we're fixing here by normalizing case (which |
| + # really shouldn't matter, these are all file paths and generally are all |
| + # lower case) and writing from / to \ (file path) and then back again. This |
| + # is compounded by NodeJS having a bug in url.resolve() that handles |
| + # chrome:// protocol URLs poorly as well as us using startswith() to strip |
| + # file paths (which isn't crazy awesome either). Don't remove unless you |
| + # really really know what you're doing. |
| + norm = lambda u: u.lower().replace('/', '\\') |
| + request_list = [norm(u).replace(norm(in_path), '').replace('\\', '/') |
| + for u in request_list] |
| # Undo the URL mappings applied by vulcanize to get file paths relative to |
| # current working directory. |
| @@ -116,21 +131,18 @@ def _update_dep_file(in_folder, args): |
| ('chrome://%s/' % args.host, os.path.relpath(in_path, _CWD)), |
| ] |
| - dependencies = map( |
| - lambda url: _undo_mapping(url_mappings, url), request_list) |
| + deps = [_undo_mapping(url_mappings, u) for u in request_list] |
| + deps = map(os.path.normpath, deps) |
| # If the input was a .pak file, the generated depfile should not list files |
| # already in the .pak file. |
| - filtered_dependencies = dependencies |
| - if (args.input_type == 'PAK_FILE'): |
| + if args.input.endswith('.pak'): |
| filter_url = os.path.join(args.out_folder, _PAK_UNPACK_FOLDER) |
| - filtered_dependencies = filter( |
| - lambda url: not url.startswith(filter_url), dependencies) |
| + deps = [d for d in deps if not d.startswith(filter_url)] |
| with open(os.path.join(_CWD, args.depfile), 'w') as f: |
| - f.write(os.path.join( |
| - args.out_folder, args.html_out_file) + ': ' + ' '.join( |
| - filtered_dependencies)) |
| + deps_file_header = os.path.join(args.out_folder, args.html_out_file) |
| + f.write(deps_file_header + ': ' + ' '.join(deps)) |
| def _vulcanize(in_folder, args): |
| @@ -157,6 +169,8 @@ def _vulcanize(in_folder, args): |
| if args.insert_in_head: |
| assert '<head>' in output |
| + # NOTE(dbeam): Vulcanize eats <base> tags after processing. This undoes |
| + # that by adding a <base> tag to the (post-processed) generated output. |
| output = output.replace('<head>', '<head>' + args.insert_in_head) |
| with tempfile.NamedTemporaryFile(mode='wt+', delete=False) as tmp: |
| @@ -180,30 +194,36 @@ def _vulcanize(in_folder, args): |
| def _css_build(out_folder, files): |
| out_path = os.path.join(_CWD, out_folder) |
| - paths = map(lambda f: os.path.join(out_path, f), files) |
| + paths = [os.path.join(out_path, f) for f in files] |
| _run_node([node_modules.PathToPolymerCssBuild()] + paths) |
| -def main(): |
| +def main(argv): |
| parser = argparse.ArgumentParser() |
| parser.add_argument('--depfile') |
|
dpapad
2017/02/07 03:00:32
This is required too. If it needs to be optional t
Dan Beam
2017/02/07 03:02:39
made required
|
| - parser.add_argument('--host') |
| - parser.add_argument('--html_in_file') |
| - parser.add_argument('--html_out_file') |
| - parser.add_argument('--input') |
| - parser.add_argument('--input_type') |
| + parser.add_argument('--host', required=True) |
| + parser.add_argument('--html_in_file', required=True) |
| + parser.add_argument('--html_out_file', required=True) |
| + parser.add_argument('--input', required=True) |
| parser.add_argument('--insert_in_head') |
| - parser.add_argument('--js_out_file') |
| - parser.add_argument('--out_folder') |
| - args = parser.parse_args() |
| + parser.add_argument('--js_out_file', required=True) |
| + parser.add_argument('--out_folder', required=True) |
| + args = parser.parse_args(argv) |
| + |
| + # NOTE(dbeam): on Windows, GN can send dirs/like/this. When joined, you might |
| + # get dirs/like/this\file.txt. This looks odd to windows. Normalize to right |
| + # the slashes. |
| + if args.depfile: |
| + args.depfile = os.path.normpath(args.depfile) |
| args.input = os.path.normpath(args.input) |
| + args.out_folder = os.path.normpath(args.out_folder) |
| vulcanize_input_folder = args.input |
| # If a .pak file was specified, unpack that file first and pass the output to |
| # vulcanize. |
| - if (args.input_type == 'PAK_FILE'): |
| + if args.input.endswith('.pak'): |
| import unpack_pak |
| input_folder = os.path.join(_CWD, args.input) |
| output_folder = os.path.join(args.out_folder, _PAK_UNPACK_FOLDER) |
| @@ -213,8 +233,9 @@ def main(): |
| _vulcanize(vulcanize_input_folder, args) |
| _css_build(args.out_folder, files=[args.html_out_file]) |
| - _update_dep_file(vulcanize_input_folder, args) |
| + if args.depfile: |
| + _update_dep_file(vulcanize_input_folder, args) |
| if __name__ == '__main__': |
| - main() |
| + main(sys.argv[1:]) |