Commit 3d4d9244 authored by rbpotter's avatar rbpotter Committed by Commit Bot

Grit: Add resource_path attribute for constructing resource maps

This overrides the file path for the resource (i.e. the |name| property
in the GritResourceMap struct) when constructing the resource map, if it
is provided.

Bug: 1132403
Change-Id: I7ef57c6b037fe6b5c4019da8bc0927e4fac9645b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2454631
Commit-Queue: Rebekah Potter <rbpotter@chromium.org>
Reviewed-by: default avatardpapad <dpapad@chromium.org>
Reviewed-by: default avatarLei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/master@{#815547}
parent 7f27d9d6
......@@ -137,6 +137,10 @@ def _IsSubpath(path1, path2):
return path1_abs == common
def _GetItemPath(item):
resource_path = item.attrs.get('resource_path', '')
if resource_path:
return resource_path
path = item.GetInputPath().replace("\\", "/")
# Handle the case where the file resides within the output folder,
......
......@@ -33,6 +33,8 @@ class FormatResourceMapUnittest(unittest.TestCase):
</structures>
<includes first_id="10000">
<include type="foo" file="abc" name="IDS_FIRSTPRESENT" />
<include type="foo" file="rst" resource_path="new_path/rst_resource"
name="IDS_WITHRESOURCEPATH" />
<if expr="False">
<include type="foo" file="def" name="IDS_MISSING" />
</if>
......@@ -44,7 +46,7 @@ class FormatResourceMapUnittest(unittest.TestCase):
</if>
<include type="foo" file="mno" name="IDS_THIRDPRESENT" />
<include type="foo" file="opq" name="IDS_FOURTHPRESENT"
skip_in_resource_map="true" />
skip_in_resource_map="true" />
</includes>
</release>''', run_gatherers=True)
output = util.StripBlankLinesAndComments(''.join(
......@@ -70,6 +72,7 @@ extern const size_t kTheRcHeaderSize;''', output)
const GritResourceMap kTheRcHeader[] = {
{"IDC_KLONKMENU", IDC_KLONKMENU},
{"IDS_FIRSTPRESENT", IDS_FIRSTPRESENT},
{"IDS_WITHRESOURCEPATH", IDS_WITHRESOURCEPATH},
{"IDS_LANGUAGESPECIFIC", IDS_LANGUAGESPECIFIC},
{"IDS_THIRDPRESENT", IDS_THIRDPRESENT},
};
......@@ -84,6 +87,7 @@ const size_t kTheRcHeaderSize = base::size(kTheRcHeader);''', output)
const GritResourceMap kTheRcHeader[] = {
{"grit/testdata/klonk.rc", IDC_KLONKMENU},
{"abc", IDS_FIRSTPRESENT},
{"new_path/rst_resource", IDS_WITHRESOURCEPATH},
{"ghi", IDS_LANGUAGESPECIFIC},
{"mno", IDS_THIRDPRESENT},
};
......
......@@ -48,6 +48,8 @@ class IncludeNode(base.Node):
"""Attributes:
translateable: False if the node has contents that should not be
translated.
resource_path: If provided, is used to populate the |name|
property of the generated GritResourceMap struct.
preprocess: Takes the same code path as flattenhtml, but it
disables any processing/inlining outside of <if>
and <include>.
......@@ -69,6 +71,7 @@ class IncludeNode(base.Node):
'use_base_dir': 'true',
'skip_minify': 'false',
'skip_in_resource_map': 'false',
'resource_path': '',
}
def GetInputPath(self):
......
......@@ -15,6 +15,7 @@ import zlib
if __name__ == '__main__':
sys.path.append(os.path.join(os.path.dirname(__file__), '../..'))
import grit.format.resource_map
from grit.node import misc
from grit.node import include
from grit.node import empty
......@@ -129,6 +130,37 @@ class IncludeNodeUnittest(unittest.TestCase):
self.assertIn(b'in the middle...', result)
self.assertNotIn(b'should be removed', result)
def testAcceptsResourcePath(self):
root = util.ParseGrdForUnittest(
'''
<outputs>
<output filename="grit/test1_resources.h" type="rc_header">
<emit emit_type='prepend'></emit>
</output>
<output filename="grit/test1_resources_map.cc"
type="resource_file_map_source" />
<output filename="grit/test1_resources_map.h"
type="resource_map_header" />
</outputs>
<release seq="3">
<includes>
<include name="TEST1_TEXT" file="test1_text.txt"
resource_path="foo/renamed1_text.txt"
compress="false" type="BINDATA"/>
</includes>
</release>''',
base_dir=util.PathFromRoot('grit/testdata'))
inc, = root.GetChildrenOfType(include.IncludeNode)
formatter = grit.format.resource_map.GetFormatter(
'resource_file_map_source')
formatted = formatter(root,
lang='en',
output_dir=util.PathFromRoot('grit/testdata'))
found = False
for segment in formatted:
found = found or 'foo/renamed1_text.txt' in segment
self.assertNotIn('test1_text.txt', segment)
self.assertTrue(found)
if __name__ == '__main__':
unittest.main()
......@@ -57,7 +57,8 @@ GRD_BEGIN_TEMPLATE = '<?xml version="1.0" encoding="UTF-8"?>\n'\
GRD_INCLUDE_TEMPLATE = ' <include name="{name}" ' \
'file="${{root_gen_dir}}/{path_from_gen}" ' \
'use_base_dir="false" type="BINDATA" />\n'
'resource_path="{path}" use_base_dir="false" ' \
'type="BINDATA" />\n'
GRD_END_TEMPLATE = ' </includes>\n'\
' </release>\n'\
......@@ -85,6 +86,7 @@ def main(argv):
filepath = os.path.join(base_dir, filename).replace('\\', '/')
rebased_path = os.path.relpath(filepath, args.root_gen_dir)
grd_file.write(GRD_INCLUDE_TEMPLATE.format(name=name,
path=filename,
path_from_gen=rebased_path))
grd_file.write(GRD_END_TEMPLATE)
......
......@@ -12,12 +12,12 @@
</outputs>
<release seq="1">
<includes>
<include name="IDR_TEST_TEST_HTML" file="${root_gen_dir}/preprocessed/test.html" use_base_dir="false" type="BINDATA" />
<include name="IDR_TEST_TEST_JS" file="${root_gen_dir}/preprocessed/test.js" use_base_dir="false" type="BINDATA" />
<include name="IDR_TEST_DIR_ELEMENT_IN_DIR_JS" file="${root_gen_dir}/preprocessed/dir/element_in_dir.js" use_base_dir="false" type="BINDATA" />
<include name="IDR_TEST_TEST_UI_JS" file="${root_gen_dir}/preprocessed/test_ui.js" use_base_dir="false" type="BINDATA" />
<include name="IDR_TEST_TEST_PROXY_JS" file="${root_gen_dir}/preprocessed/test_proxy.js" use_base_dir="false" type="BINDATA" />
<include name="IDR_TEST_DIR_ANOTHER_ELEMENT_IN_DIR_JS" file="${root_gen_dir}/preprocessed/dir/another_element_in_dir.js" use_base_dir="false" type="BINDATA" />
<include name="IDR_TEST_TEST_HTML" file="${root_gen_dir}/preprocessed/test.html" resource_path="test.html" use_base_dir="false" type="BINDATA" />
<include name="IDR_TEST_TEST_JS" file="${root_gen_dir}/preprocessed/test.js" resource_path="test.js" use_base_dir="false" type="BINDATA" />
<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="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="BINDATA" />
<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="BINDATA" />
<include name="IDR_TEST_DIR_ANOTHER_ELEMENT_IN_DIR_JS" file="${root_gen_dir}/preprocessed/dir/another_element_in_dir.js" resource_path="dir/another_element_in_dir.js" use_base_dir="false" type="BINDATA" />
</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