Skip to content

Commit

Permalink
WIP: Replace python-based JS extraction script with run_shell
Browse files Browse the repository at this point in the history
This removes the python dependency in favor of a heavier reliance the
shell.

While not ideal right now, a resolution of
bazelbuild/bazel#5511 would allow us to do
the file grepping in starlark

We could use entirely native rules this way, only calling out to the
`jar` binary directly, which should be included with any jdk, and
therefore result in a rule that is much leaner on dependencies while
also more portable.

Note: This hasn't been tested yet. I've thought a few times I did
using the JS example, but each time it has turned out bazel ended up
using different rules. This is a start, and an approach, but not a
ready patch.

This also still crucially lacks an implementation for .js.map files,
which is technically trivial, but I'd like to ideally do it without
invoking more shells.
  • Loading branch information
TLATER committed Sep 21, 2021
1 parent d174c98 commit 1bc4b6f
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions kotlin/internal/js/impl.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -128,24 +128,28 @@ def kt_js_import_impl(ctx):

srcjar = ctx.files.srcjar[0] if len(ctx.files.srcjar) == 1 else None

args = ctx.actions.args()
args.add("--jar", jar_file)
args.add("--import_pattern", "^[^/.]+\\.js$")
args.add("--import_out", ctx.outputs.js)
args.add("--aux_pattern", "^[^/]+\\.js\\.map$")
args.add("--aux_out", ctx.outputs.js_map)

tools, _, input_manifest = ctx.resolve_command(tools = [ctx.attr._importer])
ctx.actions.run(
# The files to extract are the first .js and the first .js.map
# files in the root module of the jar - easiest way to get them is
# to list the contents and extract the first one.
#
# For a more satisfactory solution, we'd need
# https://github.com/bazelbuild/bazel/issues/5511
#
# TODO: Deal with .js.map
# TODO: Implement for Windows
to_extract = ctx.actions.declare_file(ctx.label.name + '_to_extract')
ctx.actions.run_shell(
inputs = [jar_file],
tools = tools,
executable = ctx.executable._importer,
outputs = [
ctx.outputs.js,
ctx.outputs.js_map,
],
arguments = [args],
input_manifests = input_manifest,
outputs = [to_extract],
arguments = [jar_file.path, to_extract.path],
command = "jar -f $1 -t | grep -m 1 '.js' > $2",
)

ctx.actions.run_shell(
inputs = [jar_file, to_extract],
outputs = [ctx.outputs.js],
arguments = [jar_file.path, to_extract.path, ctx.output.js.path],
command = "jar -f $1 -x $(< $2) && mv $(< $2) $3",
)

return [
Expand Down

0 comments on commit 1bc4b6f

Please sign in to comment.