3 # Copyright (c) 2015 The Chromium Authors. All rights reserved.
4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file.
7 """Generate ResourceRewriter.java which overwrites the given package's
15 from util
import build_utils
17 # Import jinja2 from third_party/jinja2
18 sys
.path
.append(os
.path
.abspath(os
.path
.join(os
.path
.dirname(__file__
),
25 RESOURCE_REWRITER
="""/* AUTO-GENERATED FILE. DO NOT MODIFY. */
27 package {{ package }};
29 * Helper class used to fix up resource ids.
31 class ResourceRewriter {
33 * Rewrite the R 'constants' for the WebView.
35 public static void rewriteRValues(final int packageId) {
36 {% for res_package in res_packages %}
37 {{ res_package }}.R.onResourcesLoaded(packageId);
44 """Parses command line options.
47 An Namespace from argparse.parse_args()
49 parser
= argparse
.ArgumentParser(prog
='generate_resource_rewriter')
51 parser
.add_argument('--package-name',
53 help='The package name of ResourceRewriter.')
54 parser
.add_argument('--dep-packages',
56 help='A list of packages whose resource id will be'
57 'overwritten in ResourceRewriter.')
58 parser
.add_argument('--output-dir',
60 help='A output directory of generated'
61 ' ResourceRewriter.java')
63 return parser
.parse_args(args
)
66 def CreateResourceRewriter(package
, res_packages
, output_dir
):
67 output_dir
= os
.path
.join(output_dir
, *package
.split('.'))
68 build_utils
.MakeDirectory(output_dir
)
69 java_path
= os
.path
.join(output_dir
, 'ResourceRewriter.java')
70 template
= jinja2
.Template(RESOURCE_REWRITER
,
73 output
= template
.render(package
=package
, res_packages
=res_packages
)
74 with
open(java_path
, 'w') as f
:
79 options
= ParseArgs(sys
.argv
[1:])
81 CreateResourceRewriter(
83 build_utils
.ParseGypList(options
.dep_packages
),
87 if __name__
== '__main__':