summaryrefslogtreecommitdiffstats
path: root/tools/bzl/js.bzl
blob: 9baa30c1568cc30989d0b62172b2aa84429709fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
load("@io_bazel_rules_closure//closure:defs.bzl", "closure_js_binary", "closure_js_library")
load("//lib/js:npm.bzl", "NPM_SHA1S", "NPM_VERSIONS")

NPMJS = "NPMJS"

GERRIT = "GERRIT:"

def _npm_tarball(name):
    return "%s@%s.npm_binary.tgz" % (name, NPM_VERSIONS[name])

def _npm_binary_impl(ctx):
    """rule to download a NPM archive."""
    name = ctx.name
    version = NPM_VERSIONS[name]
    sha1 = NPM_SHA1S[name]

    dir = "%s-%s" % (name, version)
    filename = "%s.tgz" % dir
    base = "%s@%s.npm_binary.tgz" % (name, version)
    dest = ctx.path(base)
    repository = ctx.attr.repository
    if repository == GERRIT:
        url = "https://gerrit-maven.storage.googleapis.com/npm-packages/%s" % filename
    elif repository == NPMJS:
        url = "https://registry.npmjs.org/%s/-/%s" % (name, filename)
    else:
        fail("repository %s not in {%s,%s}" % (repository, GERRIT, NPMJS))

    python = ctx.which("python3")
    script = ctx.path(ctx.attr._download_script)

    args = [python, script, "-o", dest, "-u", url, "-v", sha1]
    out = ctx.execute(args)
    if out.return_code:
        fail("failed %s: %s" % (args, out.stderr))
    ctx.file("BUILD", "package(default_visibility=['//visibility:public'])\nfilegroup(name='tarball', srcs=['%s'])" % base, False)

npm_binary = repository_rule(
    attrs = {
        "repository": attr.string(default = NPMJS),
        # Label resolves within repo of the .bzl file.
        "_download_script": attr.label(default = Label("//tools:download_file.py")),
    },
    local = True,
    implementation = _npm_binary_impl,
)

ComponentInfo = provider()

# for use in repo rules.
def _run_npm_binary_str(ctx, tarball, args):
    python_bin = ctx.which("python3")
    return " ".join([
        str(python_bin),
        str(ctx.path(ctx.attr._run_npm)),
        str(ctx.path(tarball)),
    ] + args)

def _bower_archive(ctx):
    """Download a bower package."""
    download_name = "%s__download_bower.zip" % ctx.name
    renamed_name = "%s__renamed.zip" % ctx.name
    version_name = "%s__version.json" % ctx.name

    cmd = [
        ctx.which("python3"),
        ctx.path(ctx.attr._download_bower),
        "-b",
        "%s" % _run_npm_binary_str(ctx, ctx.attr._bower_archive, []),
        "-n",
        ctx.name,
        "-p",
        ctx.attr.package,
        "-v",
        ctx.attr.version,
        "-s",
        ctx.attr.sha1,
        "-o",
        download_name,
    ]

    out = ctx.execute(cmd)
    if out.return_code:
        fail("failed %s: %s" % (cmd, out.stderr))

    _bash(ctx, " && ".join([
        "TMP=$(mktemp -d || mktemp -d -t bazel-tmp)",
        "TZ=UTC",
        "export UTC",
        "cd $TMP",
        "mkdir bower_components",
        "cd bower_components",
        "unzip %s" % ctx.path(download_name),
        "cd ..",
        "find . -exec touch -t 198001010000 '{}' ';'",
        "zip -Xr %s bower_components" % renamed_name,
        "cd ..",
        "rm -rf ${TMP}",
    ]))

    dep_version = ctx.attr.semver if ctx.attr.semver else ctx.attr.version
    ctx.file(
        version_name,
        '"%s":"%s#%s"' % (ctx.name, ctx.attr.package, dep_version),
    )
    ctx.file(
        "BUILD",
        "\n".join([
            "package(default_visibility=['//visibility:public'])",
            "filegroup(name = 'zipfile', srcs = ['%s'], )" % download_name,
            "filegroup(name = 'version_json', srcs = ['%s'], visibility=['//visibility:public'])" % version_name,
        ]),
        False,
    )

def _bash(ctx, cmd):
    cmd_list = ["bash", "-c", cmd]
    out = ctx.execute(cmd_list)
    if out.return_code:
        fail("failed %s: %s" % (cmd_list, out.stderr))

bower_archive = repository_rule(
    _bower_archive,
    attrs = {
        "package": attr.string(mandatory = True),
        "semver": attr.string(),
        "sha1": attr.string(mandatory = True),
        "version": attr.string(mandatory = True),
        "_bower_archive": attr.label(default = Label("@bower//:%s" % _npm_tarball("bower"))),
        "_download_bower": attr.label(default = Label("//tools/js:download_bower.py")),
        "_run_npm": attr.label(default = Label("//tools/js:run_npm_binary.py")),
    },
)

def _bower_component_impl(ctx):
    transitive_zipfiles = depset(
        direct = [ctx.file.zipfile],
        transitive = [d[ComponentInfo].transitive_zipfiles for d in ctx.attr.deps],
    )

    transitive_licenses = depset(
        direct = [ctx.file.license],
        transitive = [d[ComponentInfo].transitive_licenses for d in ctx.attr.deps],
    )

    transitive_versions = depset(
        direct = ctx.files.version_json,
        transitive = [d[ComponentInfo].transitive_versions for d in ctx.attr.deps],
    )

    return [
        ComponentInfo(
            transitive_licenses = transitive_licenses,
            transitive_versions = transitive_versions,
            transitive_zipfiles = transitive_zipfiles,
        ),
    ]

_common_attrs = {
    "deps": attr.label_list(providers = [ComponentInfo]),
}

def _js_component(ctx):
    dir = ctx.outputs.zip.path + ".dir"
    name = ctx.outputs.zip.basename
    if name.endswith(".zip"):
        name = name[:-4]
    dest = "%s/%s" % (dir, name)
    cmd = " && ".join([
        "TZ=UTC",
        "export TZ",
        "mkdir -p %s" % dest,
        "cp %s %s/" % (" ".join([s.path for s in ctx.files.srcs]), dest),
        "cd %s" % dir,
        "find . -exec touch -t 198001010000 '{}' ';'",
        "zip -Xqr ../%s *" % ctx.outputs.zip.basename,
    ])

    ctx.actions.run_shell(
        inputs = ctx.files.srcs,
        outputs = [ctx.outputs.zip],
        command = cmd,
        mnemonic = "GenBowerZip",
    )

    licenses = []
    if ctx.file.license:
        licenses.append(ctx.file.license)

    return [
        ComponentInfo(
            transitive_licenses = depset(licenses),
            transitive_versions = depset(),
            transitive_zipfiles = list([ctx.outputs.zip]),
        ),
    ]

js_component = rule(
    _js_component,
    attrs = dict(_common_attrs.items() + {
        "srcs": attr.label_list(allow_files = [".js"]),
        "license": attr.label(allow_single_file = True),
    }.items()),
    outputs = {
        "zip": "%{name}.zip",
    },
)

_bower_component = rule(
    _bower_component_impl,
    attrs = dict(_common_attrs.items() + {
        "license": attr.label(allow_single_file = True),

        # If set, define by hand, and don't regenerate this entry in bower2bazel.
        "seed": attr.bool(default = False),
        "version_json": attr.label(allow_files = [".json"]),
        "zipfile": attr.label(allow_single_file = [".zip"]),
    }.items()),
)

# TODO(hanwen): make license mandatory.
def bower_component(name, license = None, **kwargs):
    prefix = "//lib:LICENSE-"
    if license and not license.startswith(prefix):
        license = prefix + license
    _bower_component(
        name = name,
        license = license,
        zipfile = "@%s//:zipfile" % name,
        version_json = "@%s//:version_json" % name,
        **kwargs
    )

def _bower_component_bundle_impl(ctx):
    """A bunch of bower components zipped up."""
    zips = depset()
    for d in ctx.attr.deps:
        files = d[ComponentInfo].transitive_zipfiles

        # TODO(davido): Make sure the field always contains a depset
        if type(files) == "list":
            files = depset(files)
        zips = depset(transitive = [zips, files])

    versions = depset(transitive = [d[ComponentInfo].transitive_versions for d in ctx.attr.deps])

    licenses = depset(transitive = [d[ComponentInfo].transitive_versions for d in ctx.attr.deps])

    out_zip = ctx.outputs.zip
    out_versions = ctx.outputs.version_json

    ctx.actions.run_shell(
        inputs = zips.to_list(),
        outputs = [out_zip],
        command = " && ".join([
            "p=$PWD",
            "TZ=UTC",
            "export TZ",
            "rm -rf %s.dir" % out_zip.path,
            "mkdir -p %s.dir/bower_components" % out_zip.path,
            "cd %s.dir/bower_components" % out_zip.path,
            "for z in %s; do unzip -q $p/$z ; done" % " ".join(sorted([z.path for z in zips.to_list()])),
            "cd ..",
            "find . -exec touch -t 198001010000 '{}' ';'",
            "zip -Xqr $p/%s bower_components/*" % out_zip.path,
        ]),
        mnemonic = "BowerCombine",
    )

    ctx.actions.run_shell(
        inputs = versions.to_list(),
        outputs = [out_versions],
        mnemonic = "BowerVersions",
        command = "(echo '{' ; for j in  %s ; do cat $j; echo ',' ; done ; echo \\\"\\\":\\\"\\\"; echo '}') > %s" % (" ".join([v.path for v in versions.to_list()]), out_versions.path),
    )

    return [
        ComponentInfo(
            transitive_licenses = licenses,
            transitive_versions = versions,
            transitive_zipfiles = zips,
        ),
    ]

bower_component_bundle = rule(
    _bower_component_bundle_impl,
    attrs = _common_attrs,
    outputs = {
        "version_json": "%{name}-versions.json",
        "zip": "%{name}.zip",
    },
)

def _bundle_impl(ctx):
    """Groups a set of .html and .js together in a zip file.

    Outputs:
      NAME-versions.json:
        a JSON file containing a PKG-NAME => PKG-NAME#VERSION mapping for the
        transitive dependencies.
    NAME.zip:
      a zip file containing the transitive dependencies for this bundle.
    """

    # intermediate artifact if split is wanted.
    if ctx.attr.split:
        bundled = ctx.actions.declare_file(ctx.outputs.html.path + ".bundled.html")
    else:
        bundled = ctx.outputs.html
    destdir = ctx.outputs.html.path + ".dir"
    zips = [z for d in ctx.attr.deps for z in d[ComponentInfo].transitive_zipfiles.to_list()]

    # We are splitting off the package dir from the app.path such that
    # we can set the package dir as the root for the bundler, which means
    # that absolute imports are interpreted relative to that root.
    pkg_dir = ctx.attr.pkg.lstrip("/")
    app_path = ctx.file.app.path
    app_path = app_path[app_path.index(pkg_dir) + len(pkg_dir):]

    hermetic_npm_binary = " ".join([
        "python3",
        "$p/" + ctx.file._run_npm.path,
        "$p/" + ctx.file._bundler_archive.path,
        "--inline-scripts",
        "--inline-css",
        "--sourcemaps",
        "--strip-comments",
        "--out-file",
        "$p/" + bundled.path,
        "--root",
        pkg_dir,
        app_path,
    ])

    cmd = " && ".join([
        # unpack dependencies.
        "export PATH",
        "p=$PWD",
        "rm -rf %s" % destdir,
        "mkdir -p %s/%s/bower_components" % (destdir, pkg_dir),
        "for z in %s; do unzip -qd %s/%s/bower_components/ $z; done" % (
            " ".join([z.path for z in zips]),
            destdir,
            pkg_dir,
        ),
        "tar -cf - %s | tar -C %s -xf -" % (" ".join([s.path for s in ctx.files.srcs]), destdir),
        "cd %s" % destdir,
        hermetic_npm_binary,
    ])

    # Node/NPM is not (yet) hermeticized, so we have to get the binary
    # from the environment, and it may be under $HOME, so we can't run
    # in the sandbox.
    node_tweaks = dict(
        execution_requirements = {"local": "1"},
        use_default_shell_env = True,
    )
    ctx.actions.run_shell(
        mnemonic = "Bundle",
        inputs = [
            ctx.file._run_npm,
            ctx.file.app,
            ctx.file._bundler_archive,
        ] + list(zips) + ctx.files.srcs,
        outputs = [bundled],
        command = cmd,
        **node_tweaks
    )

    if ctx.attr.split:
        hermetic_npm_command = "export PATH && " + " ".join([
            "python3",
            ctx.file._run_npm.path,
            ctx.file._crisper_archive.path,
            "--script-in-head=false",
            "--always-write-script",
            "--source",
            bundled.path,
            "--html",
            ctx.outputs.html.path,
            "--js",
            ctx.outputs.js.path,
        ])

        ctx.actions.run_shell(
            mnemonic = "Crisper",
            inputs = [
                ctx.file._run_npm,
                ctx.file.app,
                ctx.file._crisper_archive,
                bundled,
            ],
            outputs = [ctx.outputs.js, ctx.outputs.html],
            command = hermetic_npm_command,
            **node_tweaks
        )

def _bundle_output_func(name, split):
    _ignore = [name]  # unused.
    out = {"html": "%{name}.html"}
    if split:
        out["js"] = "%{name}.js"
    return out

_bundle_rule = rule(
    _bundle_impl,
    attrs = {
        "srcs": attr.label_list(allow_files = [
            ".js",
            ".html",
            ".txt",
            ".css",
            ".ico",
        ]),
        "app": attr.label(
            mandatory = True,
            allow_single_file = True,
        ),
        "pkg": attr.string(mandatory = True),
        "split": attr.bool(default = True),
        "deps": attr.label_list(providers = [ComponentInfo]),
        "_bundler_archive": attr.label(
            default = Label("@polymer-bundler//:%s" % _npm_tarball("polymer-bundler")),
            allow_single_file = True,
        ),
        "_crisper_archive": attr.label(
            default = Label("@crisper//:%s" % _npm_tarball("crisper")),
            allow_single_file = True,
        ),
        "_run_npm": attr.label(
            default = Label("//tools/js:run_npm_binary.py"),
            allow_single_file = True,
        ),
    },
    outputs = _bundle_output_func,
)

def bundle_assets(*args, **kwargs):
    """Combine html, js, css files and optionally split into js and html bundles."""
    _bundle_rule(pkg = native.package_name(), *args, **kwargs)

def polygerrit_plugin(name, app, srcs = [], deps = [], externs = [], 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.
    Run-time dependencies (e.g. JS libraries loaded after plugin starts) should be provided using "assets" property.
    Output of this rule is a FileSet with "${name}_fs", with deploy artifacts in "plugins/${name}/static".

    Args:
      name: String, rule name.
      app: String, the main or root source file.
      externs: Fileset, external definitions that should not be bundled.
      assets: Fileset, additional files to be used by plugin in runtime, exported to "plugins/${name}/static".
      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
        _bundle_rule(
            name = name + "_combined",
            app = app,
            srcs = srcs,
            deps = deps,
            pkg = native.package_name(),
            **kwargs
        )
        js_srcs = [name + "_combined.js"]
    else:
        js_srcs = srcs

    closure_js_library(
        name = name + "_closure_lib",
        srcs = js_srcs + externs,
        convention = "GOOGLE",
        no_closure_library = True,
        deps = [
            "//lib/polymer_externs:polymer_closure",
            "//polygerrit-ui/app/externs:plugin",
        ],
    )

    closure_js_binary(
        name = name + "_bin",
        compilation_level = "WHITESPACE_ONLY",
        defs = [
            "--polymer_version=2",
            "--language_out=ECMASCRIPT_2017",
            "--rewrite_polyfills=false",
        ],
        deps = [
            name + "_closure_lib",
        ],
        dependency_mode = "PRUNE_LEGACY",
    )

    if html_plugin:
        native.genrule(
            name = name + "_rename_html",
            srcs = [name + "_combined.html"],
            outs = [plugin_name + ".html"],
            cmd = "sed 's/<script src=\"" + name + "_combined.js\"/<script src=\"" + plugin_name + ".js\"/g' $(SRCS) > $(OUTS)",
            output_to_bindir = True,
        )

    native.genrule(
        name = name + "_rename_js",
        srcs = [name + "_bin.js"],
        outs = [plugin_name + ".js"],
        cmd = "cp $< $@",
        output_to_bindir = True,
    )

    if html_plugin:
        static_files = [plugin_name + ".js", plugin_name + ".html"]
    else:
        static_files = [plugin_name + ".js"]

    if assets:
        nested, direct = [], []
        for x in assets:
            target = nested if "/" in x else direct
            target.append(x)

        static_files += direct

        if nested:
            native.genrule(
                name = name + "_copy_assets",
                srcs = assets,
                outs = [f.split("/")[-1] for f in nested],
                cmd = "cp $(SRCS) $(@D)",
                output_to_bindir = True,
            )
            static_files.append(":" + name + "_copy_assets")

    native.filegroup(
        name = name,
        srcs = static_files,
    )