Commit 593dfd4d authored by rbpotter's avatar rbpotter Committed by Commit Bot

WebUI: Add support for resource_path_rewrites to generate_grd

Support more general resource path rewrites by adding the option to
pass such rewrites from the caller in the form:
"original_file_path|resource_path_for_grd"
Leverage this to rename rollup files, and remove the special-case logic
that was used for this purpose originally.

Bug: 1132403
Change-Id: Icffa7d85497ab000044fed1ebcd9542cb5b869bc
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2476623Reviewed-by: default avatardpapad <dpapad@chromium.org>
Commit-Queue: Rebekah Potter <rbpotter@chromium.org>
Cr-Commit-Position: refs/heads/master@{#817961}
parent d6ee6d27
......@@ -38,6 +38,7 @@ generate_grd("build_grd") {
if (optimize_webui) {
deps = [ ":build" ]
manifest_files = [ "$target_gen_dir/$build_manifest" ]
resource_path_rewrites = [ "bookmarks.rollup.js|bookmarks.js" ]
} else {
deps = [
":preprocess",
......
......@@ -47,6 +47,7 @@ generate_grd("build_grd") {
if (optimize_webui) {
deps = [ ":build" ]
manifest_files = [ "$target_gen_dir/$build_manifest" ]
resource_path_rewrites = [ "downloads.rollup.js|downloads.js" ]
} else {
deps = [
":preprocess",
......
......@@ -44,6 +44,7 @@ generate_grd("build_grd") {
if (optimize_webui) {
deps = [ ":build" ]
resource_path_rewrites = [ "extensions.rollup.js|extensions.js" ]
manifest_files = [ "$target_gen_dir/$build_manifest" ]
} else {
deps = [
......
......@@ -39,6 +39,7 @@ generate_grd("build_grd") {
input_files_base_dir = rebase_path(".", "//")
if (optimize_webui) {
deps = [ ":build" ]
resource_path_rewrites = [ "print_preview.rollup.js|print_preview.js" ]
manifest_files = [ "$target_gen_dir/$build_manifest" ]
} else {
deps = [
......
......@@ -70,6 +70,10 @@ generate_grd("build_grd") {
if (optimize_webui) {
deps = [ ":build" ]
manifest_files = [ "$target_gen_dir/$build_manifest" ]
resource_path_rewrites = [
"settings.rollup.js|settings.js",
"lazy_load.rollup.js|lazy_load.js",
]
} else {
deps = [
":preprocess",
......
......@@ -30,5 +30,9 @@ template("generate_grd") {
]
args += [ "--input-files" ] + invoker.input_files
}
if (defined(invoker.resource_path_rewrites)) {
args += [ "--resource-path-rewrites" ] + invoker.resource_path_rewrites
}
}
}
......@@ -73,19 +73,16 @@ GRD_END_TEMPLATE = ' </includes>\n'\
# Generates an <include .... /> row for the given file.
def _generate_include_row(grd_prefix, filename, pathname):
def _generate_include_row(grd_prefix, filename, pathname, \
resource_path_rewrites):
name_suffix = filename.upper().replace('/', '_').replace('.', '_'). \
replace('-', '_')
name = 'IDR_%s_%s' % (grd_prefix.upper(), name_suffix)
extension = os.path.splitext(filename)[1]
type = 'chrome_html' if extension == '.html' or extension == '.js' \
else 'BINDATA'
resource_path = filename
# Remove 'rollup' from *.rollup.js paths, except for shared.rollup.js.
# Possibly pass such replacements from the gni file, if this ends up not being
# sufficient for all cases.
if ('rollup' in resource_path and 'shared' not in resource_path):
resource_path = resource_path.replace('rollup.', '')
resource_path = resource_path_rewrites[filename] \
if filename in resource_path_rewrites else filename
return GRD_INCLUDE_TEMPLATE.format(
file=pathname,
path=resource_path,
......@@ -101,18 +98,26 @@ def main(argv):
parser.add_argument('--root-gen-dir', required=True)
parser.add_argument('--input-files', nargs="*")
parser.add_argument('--input-files-base-dir')
parser.add_argument('--resource-path-rewrites', nargs="*")
args = parser.parse_args(argv)
grd_file = open(os.path.normpath(os.path.join(_CWD, args.out_grd)), 'w')
grd_file.write(GRD_BEGIN_TEMPLATE.format(prefix=args.grd_prefix))
resource_path_rewrites = {}
if args.resource_path_rewrites != None:
for r in args.resource_path_rewrites:
[original, rewrite] = r.split("|")
resource_path_rewrites[original] = rewrite
if args.input_files != None:
assert(args.input_files_base_dir)
for filename in args.input_files:
filepath = os.path.join(
args.input_files_base_dir, filename).replace('\\', '/')
grd_file.write(_generate_include_row(
args.grd_prefix, filename, '${root_src_dir}/' + filepath))
args.grd_prefix, filename, '${root_src_dir}/' + filepath,
resource_path_rewrites))
for manifest_file in args.manifest_files:
manifest_path = os.path.normpath(os.path.join(_CWD, manifest_file))
......@@ -123,7 +128,8 @@ def main(argv):
filepath = os.path.join(base_dir, filename).replace('\\', '/')
rebased_path = os.path.relpath(filepath, args.root_gen_dir)
grd_file.write(_generate_include_row(
args.grd_prefix, filename, '${root_gen_dir}/' + rebased_path))
args.grd_prefix, filename, '${root_gen_dir}/' + rebased_path,
resource_path_rewrites))
grd_file.write(GRD_END_TEMPLATE)
return
......
......@@ -27,7 +27,7 @@ class GenerateGrdTest(unittest.TestCase):
return open(os.path.join(self._out_folder, file_name), 'rb').read()
def _run_test_(self, grd_expected, manifest_files, input_files=None,
input_files_base_dir=None):
input_files_base_dir=None, resource_path_rewrites=None):
assert not self._out_folder
self._out_folder = tempfile.mkdtemp(dir=_HERE_DIR)
args = [
......@@ -45,6 +45,9 @@ class GenerateGrdTest(unittest.TestCase):
]
args += input_files
if (resource_path_rewrites):
args += [ '--resource-path-rewrites' ] + resource_path_rewrites
generate_grd.main(args)
actual_grd = self._read_out_file('test_resources.grd')
......@@ -70,6 +73,19 @@ class GenerateGrdTest(unittest.TestCase):
[ 'images/test_svg.svg', 'test_html_in_src.html' ],
'test_src_dir')
def testSuccessWithRewrites(self):
self._run_test_(
'expected_grd_with_rewrites.grd',
[
os.path.join(pathToHere, 'tests', 'test_manifest_1.json'),
os.path.join(pathToHere, 'tests', 'test_manifest_2.json'),
],
input_files=None, input_files_base_dir=None,
resource_path_rewrites=[
'test.rollup.js|test.js',
'dir/another_element_in_dir.js|dir2/another_element_in_dir_renamed.js',
])
if __name__ == '__main__':
unittest.main()
......@@ -13,7 +13,7 @@
<release seq="1">
<includes>
<include name="IDR_TEST_TEST_HTML" file="${root_gen_dir}/preprocessed/test.html" resource_path="test.html" use_base_dir="false" type="chrome_html" />
<include name="IDR_TEST_TEST_ROLLUP_JS" file="${root_gen_dir}/preprocessed/test.rollup.js" resource_path="test.js" use_base_dir="false" type="chrome_html" />
<include name="IDR_TEST_TEST_ROLLUP_JS" file="${root_gen_dir}/preprocessed/test.rollup.js" resource_path="test.rollup.js" use_base_dir="false" type="chrome_html" />
<include name="IDR_TEST_SHARED_ROLLUP_JS" file="${root_gen_dir}/preprocessed/shared.rollup.js" resource_path="shared.rollup.js" use_base_dir="false" type="chrome_html" />
<include name="IDR_TEST_DIR_ELEMENT_IN_DIR_JS" file="${root_gen_dir}/preprocessed/dir/element_in_dir.js" resource_path="dir/element_in_dir.js" use_base_dir="false" type="chrome_html" />
<include name="IDR_TEST_DIR_TEST_SVG_SVG" file="${root_gen_dir}/preprocessed/dir/test_svg.svg" resource_path="dir/test_svg.svg" use_base_dir="false" type="BINDATA" />
......
......@@ -15,7 +15,7 @@
<include name="IDR_TEST_IMAGES_TEST_SVG_SVG" file="${root_src_dir}/test_src_dir/images/test_svg.svg" resource_path="images/test_svg.svg" use_base_dir="false" type="BINDATA" />
<include name="IDR_TEST_TEST_HTML_IN_SRC_HTML" file="${root_src_dir}/test_src_dir/test_html_in_src.html" resource_path="test_html_in_src.html" use_base_dir="false" type="chrome_html" />
<include name="IDR_TEST_TEST_HTML" file="${root_gen_dir}/preprocessed/test.html" resource_path="test.html" use_base_dir="false" type="chrome_html" />
<include name="IDR_TEST_TEST_ROLLUP_JS" file="${root_gen_dir}/preprocessed/test.rollup.js" resource_path="test.js" use_base_dir="false" type="chrome_html" />
<include name="IDR_TEST_TEST_ROLLUP_JS" file="${root_gen_dir}/preprocessed/test.rollup.js" resource_path="test.rollup.js" use_base_dir="false" type="chrome_html" />
<include name="IDR_TEST_SHARED_ROLLUP_JS" file="${root_gen_dir}/preprocessed/shared.rollup.js" resource_path="shared.rollup.js" use_base_dir="false" type="chrome_html" />
<include name="IDR_TEST_DIR_ELEMENT_IN_DIR_JS" file="${root_gen_dir}/preprocessed/dir/element_in_dir.js" resource_path="dir/element_in_dir.js" use_base_dir="false" type="chrome_html" />
<include name="IDR_TEST_DIR_TEST_SVG_SVG" file="${root_gen_dir}/preprocessed/dir/test_svg.svg" resource_path="dir/test_svg.svg" use_base_dir="false" type="BINDATA" />
......
<?xml version="1.0" encoding="UTF-8"?>
<grit latest_public_release="0" current_release="1" output_all_resource_defines="false">
<outputs>
<output filename="grit/test_resources.h" type="rc_header">
<emit emit_type='prepend'></emit>
</output>
<output filename="grit/test_resources_map.cc"
type="resource_file_map_source" />
<output filename="grit/test_resources_map.h"
type="resource_map_header" />
<output filename="test_resources.pak" type="data_package" />
</outputs>
<release seq="1">
<includes>
<include name="IDR_TEST_TEST_HTML" file="${root_gen_dir}/preprocessed/test.html" resource_path="test.html" use_base_dir="false" type="chrome_html" />
<include name="IDR_TEST_TEST_ROLLUP_JS" file="${root_gen_dir}/preprocessed/test.rollup.js" resource_path="test.js" use_base_dir="false" type="chrome_html" />
<include name="IDR_TEST_SHARED_ROLLUP_JS" file="${root_gen_dir}/preprocessed/shared.rollup.js" resource_path="shared.rollup.js" use_base_dir="false" type="chrome_html" />
<include name="IDR_TEST_DIR_ELEMENT_IN_DIR_JS" file="${root_gen_dir}/preprocessed/dir/element_in_dir.js" resource_path="dir/element_in_dir.js" use_base_dir="false" type="chrome_html" />
<include name="IDR_TEST_DIR_TEST_SVG_SVG" file="${root_gen_dir}/preprocessed/dir/test_svg.svg" resource_path="dir/test_svg.svg" use_base_dir="false" type="BINDATA" />
<include name="IDR_TEST_TEST_UI_JS" file="${root_gen_dir}/preprocessed/test_ui.js" resource_path="test_ui.js" use_base_dir="false" type="chrome_html" />
<include name="IDR_TEST_TEST_PROXY_JS" file="${root_gen_dir}/preprocessed/test_proxy.js" resource_path="test_proxy.js" use_base_dir="false" type="chrome_html" />
<include name="IDR_TEST_FILE_WITH_DASHES_JS" file="${root_gen_dir}/preprocessed/file-with-dashes.js" resource_path="file-with-dashes.js" use_base_dir="false" type="chrome_html" />
<include name="IDR_TEST_DIR_ANOTHER_ELEMENT_IN_DIR_JS" file="${root_gen_dir}/preprocessed/dir/another_element_in_dir.js" resource_path="dir2/another_element_in_dir_renamed.js" use_base_dir="false" type="chrome_html" />
</includes>
</release>
</grit>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment