Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2029)

Unified Diff: chrome/browser/resources/vulcanize_gn.py

Issue 2569283002: WebUI: Add GN rules for Vulcanize. (Closed)
Patch Set: Update after node/node_modules renaming. Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/resources/vulcanize_gn.py
diff --git a/chrome/browser/resources/vulcanize_gn.py b/chrome/browser/resources/vulcanize_gn.py
new file mode 100755
index 0000000000000000000000000000000000000000..89a78d544a6f281756b33942fae9329374491d19
--- /dev/null
+++ b/chrome/browser/resources/vulcanize_gn.py
@@ -0,0 +1,196 @@
+#!/usr/bin/env python
+# Copyright 2016 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+import argparse
+import itertools
+import os
+import re
+import subprocess
+import sys
+import tempfile
+
+_HERE_PATH = os.path.join(os.path.dirname(__file__))
+_SRC_PATH = os.path.normpath(os.path.join(_HERE_PATH, '..', '..', '..'))
+
+sys.path.append(os.path.join(_SRC_PATH, 'third_party', 'node'))
+import node
+import node_modules
+
+_NODE_BINARY = node.GetBinaryPath()
+
+
+_RESOURCES_PATH = os.path.join(_SRC_PATH, 'ui', 'webui', 'resources')
+
Dan Beam 2017/01/20 23:49:30 i think we could do one of two things: a) add one
dpapad 2017/01/21 02:32:15 Added one more line.
+_CR_ELEMENTS_PATH = os.path.join(_RESOURCES_PATH, 'cr_elements')
+
+
+_CSS_RESOURCES_PATH = os.path.join(_RESOURCES_PATH, 'css')
+
+
+_HTML_RESOURCES_PATH = os.path.join(_RESOURCES_PATH, 'html')
+
+
+_JS_RESOURCES_PATH = os.path.join(_RESOURCES_PATH, 'js')
+
+
+_POLYMER_PATH = os.path.join(
+ _SRC_PATH, 'third_party', 'polymer', 'v1_0', 'components-chromium')
+
+
+_VULCANIZE_BASE_ARGS = [
+ '--exclude', 'crisper.js',
+
+ # These files are already combined and minified.
+ '--exclude', 'chrome://resources/html/polymer.html',
+ '--exclude', 'web-animations-next-lite.min.js',
+
+ # These files are dynamically created by C++.
+ '--exclude', 'load_time_data.js',
+ '--exclude', 'strings.js',
+ '--exclude', 'text_defaults.css',
+ '--exclude', 'text_defaults_md.css',
+
+ '--inline-css',
+ '--inline-scripts',
+ '--strip-comments',
+]
+
+
+_URL_MAPPINGS = [
+ ('chrome://resources/cr_elements/', _CR_ELEMENTS_PATH),
+ ('chrome://resources/css/', _CSS_RESOURCES_PATH),
+ ('chrome://resources/html/', _HTML_RESOURCES_PATH),
+ ('chrome://resources/js/', _JS_RESOURCES_PATH),
+ ('chrome://resources/polymer/v1_0/', _POLYMER_PATH)
+]
+
+
+_VULCANIZE_REDIRECT_ARGS = list(itertools.chain.from_iterable(map(
+ lambda m: ['--redirect', '%s|%s' % (m[0], m[1])], _URL_MAPPINGS)))
+
+
+def _run_cmd(cmd_parts, stdout=None):
+ cmd = "'" + "' '".join(cmd_parts) + "'"
+ process = subprocess.Popen(
+ cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
+ stdout, stderr = process.communicate()
+
+ if stderr:
+ print >> sys.stderr, '%s failed: %s' % (cmd, stderr)
+ raise
+
+ return stdout
+
+
+def _undo_mapping(mappings, url):
+ for tup in mappings:
Dan Beam 2017/01/20 23:49:30 I don't think |tup| is an explanatory variable nam
dpapad 2017/01/21 02:32:15 Done, sort of. No need to use enumerate().
+ res = re.match(tup[0], url)
Dan Beam 2017/01/20 23:49:30 don't use re, use url.startswith()
dpapad 2017/01/21 02:32:15 Done.
+ if res:
+ return url.replace(tup[0], tup[1] + '/');
+ return url
+
+
+# Get a list of all files that were bundled with Vulcanize and update the
+# depfile accordingly such that Ninja knows when to trigger re-vulcanization.
+def _update_dep_file(in_folder, out_folder, host, depfile, html_out_file):
+ in_path = os.path.join(os.getcwd(), in_folder)
+ out_path = os.path.join(os.getcwd(), 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.txt'), 'r').read().splitlines()
+
+ # Undo the URL mappings applied by vulcanize to get file paths relative to
+ # current working directory.
+ url_mappings = _URL_MAPPINGS + [
+ ('/', os.path.relpath(in_path, os.getcwd())),
+ ('chrome://%s/' % host, os.path.relpath(in_path, os.getcwd())),
+ ]
+
+ dependencies = map(
+ lambda url: _undo_mapping(url_mappings, url), request_list)
+
+ f = open(os.path.join(os.getcwd(), depfile), 'w')
+ f.write(os.path.join(
+ out_folder, html_out_file + ': ') + ' '.join(dependencies))
+ f.close()
Dan Beam 2017/01/20 23:49:30 nit: use with, which will auto-close the file for
dpapad 2017/01/21 02:32:15 Done, used with. Did not create a temp out_path va
+
+
+def _vulcanize(in_folder, out_folder, host, html_in_file,
+ html_out_file, js_out_file):
+ in_path = os.path.join(os.getcwd(), in_folder)
+ out_path = os.path.join(os.getcwd(), out_folder)
+
+ html_out_path = os.path.join(out_path, html_out_file)
+ js_out_path = os.path.join(out_path, js_out_file)
+
+ output = _run_cmd(
+ [_NODE_BINARY, node_modules.PathToVulcanize()] +
+ _VULCANIZE_BASE_ARGS + _VULCANIZE_REDIRECT_ARGS +
+ ['--out-request-list', os.path.join(out_path, 'request_list.txt'),
Dan Beam 2017/01/20 23:49:30 can we make a tempfile instead of using request_li
dpapad 2017/01/21 02:32:15 Per offline discussion, I am keeping this file und
+ '--redirect', '/|%s' % in_path,
+ '--redirect', 'chrome://%s/|%s' % (host, in_path),
+ os.path.join('/', html_in_file)])
+
+ with tempfile.NamedTemporaryFile(mode='wt+', delete=False) as tmp:
+ # Grit includes are not supported, use HTML imports instead.
+ tmp.write(output.replace(
+ '<include src="', '<include src-disabled="'))
+
+ try:
+ _run_cmd([_NODE_BINARY, node_modules.PathToCrisper(),
+ '--source', tmp.name,
+ '--script-in-head', 'false',
+ '--html', html_out_path,
+ '--js', js_out_path])
+
+ # TODO(tsergeant): Remove when JS resources are minified by default:
+ # crbug.com/619091.
+ _run_cmd([_NODE_BINARY, node_modules.PathToUglifyJs(), js_out_path,
+ '--comments', '/Copyright|license|LICENSE|\<\/?if/',
+ '--output', js_out_path])
+ finally:
+ os.remove(tmp.name)
+
+
+def _css_build(out_folder, files):
+ out_path = os.path.join(os.getcwd(), out_folder)
+ paths = map(lambda f: os.path.join(out_path, f), files)
+
+ _run_cmd([_NODE_BINARY, node_modules.PathToPolymerCssBuild()] + paths)
+
+
+def main():
+ parser = argparse.ArgumentParser()
+ parser.add_argument('--host')
+ parser.add_argument('--html_in_file')
+ parser.add_argument('--html_out_file')
+ parser.add_argument('--input_type')
+ parser.add_argument('--input')
+ parser.add_argument('--js_out_file')
+ parser.add_argument('--out_folder')
+ parser.add_argument('--depfile')
Dan Beam 2017/01/20 23:49:30 nit: move to top? (alpha)
dpapad 2017/01/21 02:32:15 Done.
+ args = parser.parse_args()
+
+ 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'):
+ import unpack_pak
+ input_folder = os.path.join(os.getcwd(), args.input)
+ output_folder = os.path.join(args.out_folder, 'flattened');
+ unpack_pak.unpack(args.input, output_folder)
+ vulcanize_input_folder = output_folder
+
+ _vulcanize(vulcanize_input_folder, args.out_folder, args.host,
+ args.html_in_file, args.html_out_file, args.js_out_file);
+ _css_build(args.out_folder, files=[args.html_out_file])
+
+ _update_dep_file(vulcanize_input_folder, args.out_folder, args.host,
+ args.depfile, args.html_out_file)
+
Dan Beam 2017/01/20 23:49:30 \n\n
dpapad 2017/01/21 02:32:15 Done.
+if __name__ == '__main__':
+ main()
« chrome/browser/resources/unpack_pak.py ('K') | « chrome/browser/resources/vulcanize.gni ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698