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 3c1139ee68)
This commit is contained in:
viktard 2018-10-22 14:39:46 -07:00 committed by Paladox none
parent adbef6be8e
commit 063bdc632d

View File

@ -428,7 +428,7 @@ def bundle_assets(*args, **kwargs):
"""Combine html, js, css files and optionally split into js and html bundles.""" """Combine html, js, css files and optionally split into js and html bundles."""
_bundle_rule(*args, pkg = native.package_name(), **kwargs) _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. """Bundles plugin dependencies for deployment.
This rule bundles all Polymer elements and JS dependencies into .html and .js files. This rule bundles all Polymer elements and JS dependencies into .html and .js files.
@ -436,20 +436,40 @@ 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". Output of this rule is a FileSet with "${name}_fs", with deploy artifacts in "plugins/${name}/static".
Args: Args:
name: String, plugin name. name: String, rule name.
app: String, the main or root source file. app: String, the main or root source file.
assets: Fileset, additional files to be used by plugin in runtime, exported to "plugins/${name}/static". 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
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 # Combines all .js and .html files into foo_combined.js and foo_combined.html
_bundle_rule( _bundle_rule(
name = name + "_combined", name = name + "_combined",
app = app, app = app,
srcs = srcs if app in srcs else srcs + [app], srcs = srcs,
pkg = native.package_name(), pkg = native.package_name(),
**kwargs **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( closure_js_binary(
name = name + "_bin", name = name + "_bin",
@ -464,37 +484,27 @@ def polygerrit_plugin(name, app, srcs = [], assets = None, **kwargs):
], ],
) )
closure_js_library( if html_plugin:
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( native.genrule(
name = name + "_rename_html", name = name + "_rename_html",
srcs = [name + "_combined.html"], srcs = [name + "_combined.html"],
outs = [name + ".html"], outs = [plugin_name + ".html"],
cmd = "sed 's/<script src=\"" + name + "_combined.js\"/<script src=\"" + name + ".js\"/g' $(SRCS) > $(OUTS)", cmd = "sed 's/<script src=\"" + name + "_combined.js\"/<script src=\"" + plugin_name + ".js\"/g' $(SRCS) > $(OUTS)",
output_to_bindir = True, output_to_bindir = True,
) )
native.genrule( native.genrule(
name = name + "_rename_js", name = name + "_rename_js",
srcs = [name + "_bin.js"], srcs = [name + "_bin.js"],
outs = [name + ".js"], outs = [plugin_name + ".js"],
cmd = "cp $< $@", cmd = "cp $< $@",
output_to_bindir = True, output_to_bindir = True,
) )
static_files = [ if html_plugin:
name + ".js", static_files = [plugin_name + ".js", plugin_name + ".html"]
name + ".html", else:
] static_files = [plugin_name + ".js"]
if assets: if assets:
nested, direct = [], [] nested, direct = [], []