blob: c04a7faefbd50dbbc7da86bd727a0c649b377f9a [file] [log] [blame]
Avi Drissmandfd880852022-09-15 20:11:091# Copyright 2022 The Chromium Authors
dpapad5d67ad172022-01-24 10:04:052# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
dpapadb0e74d32022-05-10 18:39:315# Genaretes a wrapper TS file around a source HTML file holding either
6# 1) a Polymer element template or
7# 2) an <iron-iconset-svg> definitions
8#
9# Note: The HTML file must be named either 'icons.html' or be suffixed with
10# '_icons.html' for this tool to treat them as #2. Consequently, files holding
11# Polymer element templates should not use such naming to be treated as #1.
12#
13# In case #1 the wrapper exports a getTemplate() function that can be used at
14# runtime to import the template. This is useful for implementing Web Components
15# using JS modules, where all the HTML needs to reside in a JS file (no more
16# HTML imports).
17#
18# In case #2 the wrapper adds the <iron-iconset-svg> element to <head>, so that
19# it can be used by <iron-icon> instances.
dpapad5d67ad172022-01-24 10:04:0520
21import argparse
dpapad5d67ad172022-01-24 10:04:0522import io
dpapadd55b8f72022-06-29 19:37:3223import shutil
24import sys
25import tempfile
dpapad5d67ad172022-01-24 10:04:0526from os import path, getcwd, makedirs
27
dpapadd55b8f72022-06-29 19:37:3228_HERE_PATH = path.dirname(__file__)
29_SRC_PATH = path.normpath(path.join(_HERE_PATH, '..', '..'))
dpapad5d67ad172022-01-24 10:04:0530_CWD = getcwd()
31
dpapadd55b8f72022-06-29 19:37:3232sys.path.append(path.join(_SRC_PATH, 'third_party', 'node'))
33import node
34
dpapadbc6fc932022-05-28 00:47:5735# Template for non-Polymer elements.
dpapad24071ff2022-09-02 01:07:5436_NON_POLYMER_ELEMENT_TEMPLATE = """import {getTrustedHTML} from '%(scheme)s//resources/js/static_types.js';
dpapadbc6fc932022-05-28 00:47:5737export function getTemplate() {
dpapad24071ff2022-09-02 01:07:5438 return getTrustedHTML`<!--_html_template_start_-->%(content)s<!--_html_template_end_-->`;
dpapadbc6fc932022-05-28 00:47:5739}"""
40
41# Template for Polymer elements.
dpapad24071ff2022-09-02 01:07:5442_ELEMENT_TEMPLATE = """import {html} from '%(scheme)s//resources/polymer/v3_0/polymer/polymer_bundled.min.js';
dpapadb0e74d32022-05-10 18:39:3143export function getTemplate() {
dpapad24071ff2022-09-02 01:07:5444 return html`<!--_html_template_start_-->%(content)s<!--_html_template_end_-->`;
dpapadb0e74d32022-05-10 18:39:3145}"""
46
dpapad24071ff2022-09-02 01:07:5447_ICONS_TEMPLATE = """import '%(scheme)s//resources/polymer/v3_0/iron-iconset-svg/iron-iconset-svg.js';
48import {html} from '%(scheme)s//resources/polymer/v3_0/polymer/polymer_bundled.min.js';
dpapadb0e74d32022-05-10 18:39:3149
dpapad24071ff2022-09-02 01:07:5450const template = html`%(content)s`;
dpapadb0e74d32022-05-10 18:39:3151document.head.appendChild(template.content);
52"""
dpapad5d67ad172022-01-24 10:04:0553
54
55def main(argv):
56 parser = argparse.ArgumentParser()
57 parser.add_argument('--in_folder', required=True)
58 parser.add_argument('--out_folder', required=True)
59 parser.add_argument('--in_files', required=True, nargs="*")
dpapadd55b8f72022-06-29 19:37:3260 parser.add_argument('--minify', action='store_true')
dpapad0fb37802022-07-13 01:00:5261 parser.add_argument('--use_js', action='store_true')
dpapadbc6fc932022-05-28 00:47:5762 parser.add_argument('--template',
63 choices=['polymer', 'native'],
64 default='polymer')
dpapad24071ff2022-09-02 01:07:5465 parser.add_argument('--scheme',
66 choices=['chrome', 'relative'],
67 default='chrome')
dpapadbc6fc932022-05-28 00:47:5768
dpapad5d67ad172022-01-24 10:04:0569 args = parser.parse_args(argv)
70
71 in_folder = path.normpath(path.join(_CWD, args.in_folder))
72 out_folder = path.normpath(path.join(_CWD, args.out_folder))
dpapad0fb37802022-07-13 01:00:5273 extension = '.js' if args.use_js else '.ts'
dpapad5d67ad172022-01-24 10:04:0574
75 results = []
76
dpapadd55b8f72022-06-29 19:37:3277 # The folder to be used to read the HTML files to be wrapped.
78 wrapper_in_folder = in_folder
79
80 if args.minify:
81 # Minify the HTML files with html-minifier before generating the wrapper
82 # .ts files.
83 # Note: Passing all HTML files to html-minifier all at once because
84 # passing them individually takes a lot longer.
85 # Storing the output in a temporary folder, which is used further below when
86 # creating the final wrapper files.
87 tmp_out_dir = tempfile.mkdtemp(dir=out_folder)
88 try:
89 wrapper_in_folder = tmp_out_dir
90
91 # Using the programmatic Node API to invoke html-minifier, because the
92 # built-in command line API does not support explicitly specifying
93 # multiple files to be processed, and only supports specifying an input
94 # folder, which would lead to potentially processing unnecessary HTML
95 # files that are not part of the build (stale), or handled by other
96 # html_to_wrapper targets.
97 node.RunNode(
98 [path.join(_HERE_PATH, 'html_minifier.js'), in_folder, tmp_out_dir] +
99 args.in_files)
100 except RuntimeError as err:
101 shutil.rmtree(tmp_out_dir)
102 raise err
103
104 # Wrap the input files (minified or not) with an enclosing .ts file.
dpapad5d67ad172022-01-24 10:04:05105 for in_file in args.in_files:
dpapadd55b8f72022-06-29 19:37:32106 wrapper_in_file = path.join(wrapper_in_folder, in_file)
107
108 with io.open(wrapper_in_file, encoding='utf-8', mode='r') as f:
dpapadb0e74d32022-05-10 18:39:31109 html_content = f.read()
110
111 wrapper = None
dpapadbc6fc932022-05-28 00:47:57112 template = _ELEMENT_TEMPLATE \
113 if args.template == 'polymer' else _NON_POLYMER_ELEMENT_TEMPLATE
dpapadb0e74d32022-05-10 18:39:31114
115 filename = path.basename(in_file)
116 if filename == 'icons.html' or filename.endswith('_icons.html'):
117 template = _ICONS_TEMPLATE
118
dpapad24071ff2022-09-02 01:07:54119 wrapper = template % {
120 'content': html_content,
121 'scheme': 'chrome:' if args.scheme == 'chrome' else '',
122 }
dpapad5d67ad172022-01-24 10:04:05123
124 out_folder_for_file = path.join(out_folder, path.dirname(in_file))
125 makedirs(out_folder_for_file, exist_ok=True)
126 with io.open(path.join(out_folder, in_file) + extension, mode='wb') as f:
dpapadb0e74d32022-05-10 18:39:31127 f.write(wrapper.encode('utf-8'))
dpapadd55b8f72022-06-29 19:37:32128
129 if args.minify:
130 # Delete the temporary folder that was holding minified HTML files, no
131 # longer needed.
132 shutil.rmtree(tmp_out_dir)
133
dpapad5d67ad172022-01-24 10:04:05134 return
135
136
137if __name__ == '__main__':
138 main(sys.argv[1:])