Commit c073381e authored by dpapad's avatar dpapad Committed by Commit Bot

WebUI Polymer3: Add tool to auto-generate Polymer3 from v2 files.

This CL adds the core functionality of the polymer_modulizer() GN rule.
In follow up CLs, the js_modulizer() will be added, and some code will
be shared between the two tools.

Bug: 965770
Change-Id: I5f9e925c38d41d294e31877bfa7852e3fc5a88b6
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1663734
Commit-Queue: Demetrios Papadopoulos <dpapad@chromium.org>
Reviewed-by: default avatarRebekah Potter <rbpotter@chromium.org>
Cr-Commit-Position: refs/heads/master@{#672330}
parent e3c709bc
...@@ -2,17 +2,32 @@ ...@@ -2,17 +2,32 @@
# Use of this source code is governed by a BSD-style license that can be # Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file. # found in the LICENSE file.
template("js_html_template") { common_namespace_rewrites = [
action(target_name + "_js_html_template") { "Polymer.PaperRippleBehavior|PaperRippleBehavior",
"cr.ui.FocusOutlineManager|FocusOutlineManager",
# TODO(dpapad): Add more such rewrites as they get discovered.
]
template("polymer_modulizer") {
action(target_name + "_module") {
script = "//tools/polymer/polymer.py" script = "//tools/polymer/polymer.py"
inputs = [ inputs = [
invoker.js_file,
invoker.html_file, invoker.html_file,
] ]
if (invoker.html_type == "dom-module" || invoker.html_type == "v3-ready") {
inputs += [ invoker.js_file ]
}
output_js_file = "$target_gen_dir/" + invoker.js_file
if (invoker.html_type == "dom-module") {
output_js_file =
"$target_gen_dir/" + get_path_info(invoker.js_file, "file") + ".m.js"
}
outputs = [ outputs = [
"$target_gen_dir/" + invoker.js_file, "$target_gen_dir/" + output_js_file,
] ]
args = [ args = [
...@@ -27,5 +42,10 @@ template("js_html_template") { ...@@ -27,5 +42,10 @@ template("js_html_template") {
"--out_folder", "--out_folder",
rebase_path(target_gen_dir, root_build_dir), rebase_path(target_gen_dir, root_build_dir),
] ]
args += [ "--namespace_rewrites" ] + common_namespace_rewrites
if (defined(invoker.namespace_rewrites)) {
args += invoker.namespace_rewrites
}
} }
} }
This diff is collapsed.
...@@ -13,156 +13,65 @@ import unittest ...@@ -13,156 +13,65 @@ import unittest
_HERE_DIR = os.path.dirname(__file__) _HERE_DIR = os.path.dirname(__file__)
class HtmlToJsTest(unittest.TestCase): class PolymerModulizerTest(unittest.TestCase):
def setUp(self): def setUp(self):
self._out_folder = None self._out_folder = None
self._tmp_dirs = []
self._tmp_src_dir = None
def tearDown(self): def tearDown(self):
for tmp_dir in self._tmp_dirs: shutil.rmtree(self._out_folder)
shutil.rmtree(tmp_dir)
def _write_file_to_src_dir(self, file_path, file_contents):
if not self._tmp_src_dir:
self._tmp_src_dir = self._create_tmp_dir()
file_path_normalized = os.path.normpath(os.path.join(self._tmp_src_dir,
file_path))
file_dir = os.path.dirname(file_path_normalized)
if not os.path.exists(file_dir):
os.makedirs(file_dir)
with open(file_path_normalized, 'w') as tmp_file:
tmp_file.write(file_contents)
def _create_tmp_dir(self):
tmp_dir = tempfile.mkdtemp(dir=_HERE_DIR)
self._tmp_dirs.append(tmp_dir)
return tmp_dir
def _read_out_file(self, file_name): def _read_out_file(self, file_name):
assert self._out_folder assert self._out_folder
return open(os.path.join(self._out_folder, file_name), 'r').read() return open(os.path.join(self._out_folder, file_name), 'r').read()
def _run_html_to_js(self, js_file, html_file, html_type): def _run_test(self, html_type, html_file, js_file,
js_out_file, js_file_expected):
assert not self._out_folder assert not self._out_folder
self._out_folder = self._create_tmp_dir() self._out_folder = tempfile.mkdtemp(dir=_HERE_DIR)
polymer.main([ polymer.main([
'--in_folder', self._tmp_src_dir, '--in_folder', 'tests',
'--out_folder', self._out_folder, '--out_folder', self._out_folder,
'--js_file', js_file, '--js_file', js_file,
'--html_file', html_file, '--html_file', html_file,
'--html_type', html_type, '--html_type', html_type,
'--namespace_rewrites', 'Polymer.PaperRippleBehavior|PaperRippleBehavior',
]) ])
def _run_test_(self, html_type, src_html, src_js, expected_js): actual_js = self._read_out_file(js_out_file)
self._write_file_to_src_dir('foo.html', src_html) expected_js = open(os.path.join('tests', js_file_expected), 'r').read()
self._write_file_to_src_dir('foo.js', src_js)
self._run_html_to_js('foo.js', 'foo.html', html_type)
actual_js = self._read_out_file('foo.js')
self.assertEquals(expected_js, actual_js) self.assertEquals(expected_js, actual_js)
# Test case where HTML is extracted from a Polymer2 <dom-module>. # Test case where HTML is extracted from a Polymer2 <dom-module>.
def testSuccess_DomModule(self): def testSuccess_DomModule(self):
self._run_test_('dom-module', ''' self._run_test(
<link rel="import" href="../../foo/bar.html"> 'dom-module', 'dom_module.html', 'dom_module.js',
<link rel="import" href="chrome://resources/foo/bar.html"> 'dom_module.m.js', 'dom_module_expected.js')
<dom-module id="cr-checkbox"> # Test case where HTML is extracted from a Polymer2 style module.
<template> def testSuccess_StyleModule(self):
<style> self._run_test(
div { 'style-module', 'style_module.html', 'style_module.m.js',
font-size: 2rem; 'style_module.m.js', 'style_module_expected.js')
} return
</style>
<div>Hello world</div>
</template>
<script src="foo.js"></script>
</dom-module>
''', '''
Polymer({
is: 'cr-foo',
_template: html`
{__html_template__}
`,
});
''', '''
Polymer({
is: 'cr-foo',
_template: html`
<style>
div {
font-size: 2rem;
}
</style>
<div>Hello world</div>
`,
});
''')
# Test case where HTML is extracted from a Polymer2 <custom-style>. # Test case where HTML is extracted from a Polymer2 <custom-style>.
def testSuccess_CustomStyle(self): def testSuccess_CustomStyle(self):
self._run_test_('custom-style', ''' self._run_test(
<link rel="import" href="../../foo/bar.html"> 'custom-style', 'custom_style.html', 'custom_style.m.js',
<link rel="import" href="chrome://resources/foo/bar.html"> 'custom_style.m.js', 'custom_style_expected.js')
<custom-style> # Test case where HTML is extracted from a Polymer2 iron-iconset-svg file.
<style> def testSuccess_IronIconset(self):
html { self._run_test(
--foo-bar: 2rem; 'iron-iconset', 'iron_iconset.html', 'iron_iconset.m.js',
} 'iron_iconset.m.js', 'iron_iconset_expected.js')
</style>
</custom-style>
''', '''
$_documentContainer.innerHTML = `
{__html_template__}
`;
''', '''
$_documentContainer.innerHTML = `
<custom-style>
<style>
html {
--foo-bar: 2rem;
}
</style>
</custom-style>
`;
''')
# Test case where the provided HTML is already in the form needed by Polymer3. # Test case where the provided HTML is already in the form needed by Polymer3.
def testSuccess_V3Ready(self): def testSuccess_V3Ready(self):
self._run_test_('v3-ready', '''<style> self._run_test(
div { 'v3-ready', 'v3_ready.html', 'v3_ready.js',
font-size: 2rem; 'v3_ready.js', 'v3_ready_expected.js')
}
</style>
<div>Hello world</div>
''', '''
Polymer({
is: 'cr-foo',
_template: html`
{__html_template__}
`,
});
''', '''
Polymer({
is: 'cr-foo',
_template: html`
<style>
div {
font-size: 2rem;
}
</style>
<div>Hello world</div>
`,
});
''')
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
<link rel="import" href="../../html/polymer.html">
<custom-style>
<style>
html {
--foo-bar: 2rem;
}
</style>
</custom-style>
import {Polymer, html} from 'chrome://resources/polymer/v3_0/polymer/polymer_bundled.min.js';
const $_documentContainer = document.createElement('template');
$_documentContainer.innerHTML = `
<custom-style>
<style>
html {
--foo-bar: 2rem;
}
</style>
</custom-style>
`;
document.head.appendChild($_documentContainer.content);
\ No newline at end of file
<link rel="import" href="../../html/polymer.html">
<link rel="import" href="chrome://resources/polymer/v1_0/paper-behaviors/paper-ripple-behavior.html">
<link rel="import" href="../shared_vars_css.html">
<dom-module id="cr-test-foo">
<template>
<style>
div {
font-size: 2rem;
}
</style>
<div>Hello world</div>
</template>
<script src="dom_module.js"></script>
</dom-module>
Polymer({
is: 'cr-test-foo',
behaviors: [Polymer.PaperRippleBehavior],
});
import {Polymer, html} from 'chrome://resources/polymer/v3_0/polymer/polymer_bundled.min.js';
import {PaperRippleBehavior} from 'chrome://resources/polymer/v3_0/paper-behaviors/paper-ripple-behavior.js';
import '../shared_vars_css.m.js';
Polymer({
_template: html`
<style>
div {
font-size: 2rem;
}
</style>
<div>Hello world</div>
`,
is: 'cr-test-foo',
behaviors: [PaperRippleBehavior],
});
<link rel="import" href="../html/polymer.html">
<link rel="import" href="chrome://resources/polymer/v1_0/iron-iconset-svg/iron-iconset-svg.html">
<iron-iconset-svg name="cr_foo_20" size="20">
<svg>
<defs>
<g id="add"><path d="M19 13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z"/></g>
</defs>
</svg>
</iron-iconset-svg>
<iron-iconset-svg name="cr_foo_24" size="24">
<svg>
<defs>
<g id="add"><path d="M19 13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z"/></g>
</defs>
</svg>
</iron-iconset-svg>
import {Polymer, html} from 'chrome://resources/polymer/v3_0/polymer/polymer_bundled.min.js';
import 'chrome://resources/polymer/v3_0/iron-iconset-svg/iron-iconset-svg.js';
const template = html`
<iron-iconset-svg name="cr_foo_20" size="20">
<svg>
<defs>
<g id="add"><path d="M19 13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z"/></g>
</defs>
</svg>
</iron-iconset-svg>
<iron-iconset-svg name="cr_foo_24" size="24">
<svg>
<defs>
<g id="add"><path d="M19 13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z"/></g>
</defs>
</svg>
</iron-iconset-svg>
`;
document.head.appendChild(template.content);
<link rel="import" href="../html/polymer.html">
<link rel="import" href="some_other_style.html">
<dom-module id="cr-foo-style">
<template>
<style include="some-other-style">
:host {
margin: 0;
}
</style>
</template>
</dom-module>
import {Polymer, html} from 'chrome://resources/polymer/v3_0/polymer/polymer_bundled.min.js';
import 'some_other_style.m.js';
const styleElement = document.createElement('dom-module');
styleElement.innerHTML = `
<template>
<style include="some-other-style">
:host {
margin: 0;
}
</style>
</template>
`;
styleElement.register('cr-foo-style');
\ No newline at end of file
<style>
div {
font-size: 2rem;
}
</style>
<div>Hello world</div>
Polymer({
is: 'cr-foo',
_template: html`
{__html_template__}
`,
});
Polymer({
is: 'cr-foo',
_template: html`
<style>
div {
font-size: 2rem;
}
</style>
<div>Hello world</div>
`,
});
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