From 063bdc632d3bc523036fb62029ac37d4cffcc458 Mon Sep 17 00:00:00 2001 From: viktard Date: Mon, 22 Oct 2018 14:39:46 -0700 Subject: [PATCH] Update polygerrit_plugin() build rule Add support for .js based plugins, custom plugin_name to prevent conflicts with server-side plugins with the same name. Here's an example: https://chromium-review.googlesource.com/c/infra/gerrit-plugins/landingwidget/+/1295049 So what we want to achieve is to have: gerrit_plugin( name = "landingwidget", [...] and polygerrit_plugin( name = "landingwidget_ui", plugin_name = "landingwidget", [...] and produce the following final artifacts: landingwidget.jar landingwidget.js Change-Id: Ic9c68809cbaaf4650301d98e56d7c1c76e008251 (cherry picked from commit 3c1139ee689ae0df4f99a6b6fd7575c994490c93) --- tools/bzl/js.bzl | 76 +++++++++++++++++++++++++++--------------------- 1 file changed, 43 insertions(+), 33 deletions(-) diff --git a/tools/bzl/js.bzl b/tools/bzl/js.bzl index 0997bcb8df..23d1ccd516 100644 --- a/tools/bzl/js.bzl +++ b/tools/bzl/js.bzl @@ -428,7 +428,7 @@ def bundle_assets(*args, **kwargs): """Combine html, js, css files and optionally split into js and html bundles.""" _bundle_rule(*args, pkg = native.package_name(), **kwargs) -def polygerrit_plugin(name, app, srcs = [], assets = None, **kwargs): +def polygerrit_plugin(name, app, srcs = [], assets = None, plugin_name = None, **kwargs): """Bundles plugin dependencies for deployment. This rule bundles all Polymer elements and JS dependencies into .html and .js files. @@ -436,19 +436,39 @@ def polygerrit_plugin(name, app, srcs = [], assets = None, **kwargs): Output of this rule is a FileSet with "${name}_fs", with deploy artifacts in "plugins/${name}/static". Args: - name: String, plugin name. + name: String, rule name. app: String, the main or root source file. assets: Fileset, additional files to be used by plugin in runtime, exported to "plugins/${name}/static". - srcs: Source files required for combining. + plugin_name: String, plugin name. ${name} is used if not provided. """ + if not plugin_name: + plugin_name = name - # Combines all .js and .html files into foo_combined.js and foo_combined.html - _bundle_rule( - name = name + "_combined", - app = app, - srcs = srcs if app in srcs else srcs + [app], - pkg = native.package_name(), - **kwargs + html_plugin = app.endswith(".html") + srcs = srcs if app in srcs else srcs + [app] + + if html_plugin: + # Combines all .js and .html files into foo_combined.js and foo_combined.html + _bundle_rule( + name = name + "_combined", + app = app, + srcs = srcs, + pkg = native.package_name(), + **kwargs + ) + js_srcs = [name + "_combined.js"] + else: + js_srcs = srcs + + closure_js_library( + name = name + "_closure_lib", + srcs = js_srcs, + convention = "GOOGLE", + no_closure_library = True, + deps = [ + "//lib/polymer_externs:polymer_closure", + "//polygerrit-ui/app/externs:plugin", + ], ) closure_js_binary( @@ -464,37 +484,27 @@ def polygerrit_plugin(name, app, srcs = [], assets = None, **kwargs): ], ) - closure_js_library( - name = name + "_closure_lib", - srcs = [name + "_combined.js"], - convention = "GOOGLE", - no_closure_library = True, - deps = [ - "//lib/polymer_externs:polymer_closure", - "//polygerrit-ui/app/externs:plugin", - ], - ) - - native.genrule( - name = name + "_rename_html", - srcs = [name + "_combined.html"], - outs = [name + ".html"], - cmd = "sed 's/