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

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

Issue 2569283002: WebUI: Add GN rules for Vulcanize. (Closed)
Patch Set: Update paths, fix presubmit warnings. 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_new.py
diff --git a/chrome/browser/resources/vulcanize_new.py b/chrome/browser/resources/vulcanize_new.py
new file mode 100755
index 0000000000000000000000000000000000000000..39296cebe5246f89d62823964954e949eab91bb8
--- /dev/null
+++ b/chrome/browser/resources/vulcanize_new.py
@@ -0,0 +1,164 @@
+#!/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 os
+import subprocess
+import sys
+import tempfile
+import argparse
+
+# See //docs/vulcanize.md for instructions on installing prerequistes and
+# running the vulcanize build.
+
+_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.get_binary()
+
+_RESOURCES_PATH = os.path.join(_SRC_PATH, 'ui', 'webui', 'resources')
+
+_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',
+
+ '--redirect', 'chrome://resources/cr_elements/|%s' % _CR_ELEMENTS_PATH,
+ '--redirect', 'chrome://resources/css/|%s' % _CSS_RESOURCES_PATH,
+ '--redirect', 'chrome://resources/html/|%s' % _HTML_RESOURCES_PATH,
+ '--redirect', 'chrome://resources/js/|%s' % _JS_RESOURCES_PATH,
+ '--redirect', 'chrome://resources/polymer/v1_0/|%s' % _POLYMER_PATH,
+
+ '--strip-comments',
+]
+
+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 _vulcanize(in_folder, out_folder, host, html_in_file,
+ html_out_file, js_out_file, extra_args=None):
+ #print 'Vulcanizing %s/%s' % (in_folder, html_in_file)
+
+ in_path = os.path.join(os.getcwd(), in_folder)
+ out_path = os.path.join(os.getcwd(), out_folder)
+
+ """print('------------------------');
+ print('cwd: ' + os.getcwd())
+ print('in_folder: ' + in_folder)
+ print('in_path: ' + in_path + ' ' + os.path.abspath(in_path))
+ print('out_path: ' + out_path)
+ print('------------------------')"""
+
+ html_in_path = os.path.join(in_path, html_in_file)
+ html_out_path = os.path.join(out_path, html_out_file)
+ js_out_path = os.path.join(out_path, js_out_file)
+ extra_args = extra_args or []
+
+ output = _run_cmd([_NODE_BINARY, node_modules.get_vulcanize_binary()] +
+ _VULCANIZE_BASE_ARGS + extra_args +
+ ['--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.get_crisper_binary(),
+ '--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.
+ # Commented out because it blows up with "for of" loops.
+ _run_cmd([_NODE_BINARY, node_modules.get_uglifyjs_binary(), 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.get_polymer_css_build_binary()] + paths)
+
+def main():
+ parser = argparse.ArgumentParser()
+ parser.add_argument('--host')
+ parser.add_argument('--htmlInFile')
+ parser.add_argument('--htmlOutFile')
+ parser.add_argument('--inputType')
+ parser.add_argument('--input')
+ parser.add_argument('--jsOutFile')
+ parser.add_argument('--outFolder')
+ parser.add_argument('--depfile')
+ 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.inputType == 'PAK_FILE'):
+ import unpack_pak
+ input_folder = os.path.join(os.getcwd(), args.input)
+ output_folder = os.path.join(args.outFolder, 'flattened');
+ unpack_pak.unpack(args.input, output_folder)
+ vulcanize_input_folder = output_folder
+ # assert inputType == "FOLDER"
+
+ _vulcanize(vulcanize_input_folder, args.outFolder, args.host, args.htmlInFile,
+ args.htmlOutFile, args.jsOutFile);
+ _css_build(args.outFolder, files=[args.htmlOutFile])
+
+ # TODO(dpapad): Get a list of files from vulcanize.
+ f = open(os.path.join(os.getcwd(), args.depfile), 'w')
+
+ # Temporary hack, until vulcanize --list-files works.
+ if (args.host == 'downloads'):
+ dummy_input =
+ './../../chrome/browser/resources/md_downloads/i18n_setup.html'
+ else:
+ dummy_input = './../../chrome/browser/resources/settings/i18n_setup.html'
+
+ f.write(os.path.join(args.outFolder, 'vulcanized.html: ') + dummy_input);
+ f.close()
+
+if __name__ == '__main__':
+ main()

Powered by Google App Engine
This is Rietveld 408576698