-
Notifications
You must be signed in to change notification settings - Fork 520
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add npm_package_bin rule #1139
Add npm_package_bin rule #1139
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
"A generic rule to run a tool that appears in node_modules/.bin" | ||
|
||
load("@build_bazel_rules_nodejs//internal/linker:link_node_modules.bzl", "module_mappings_aspect", "register_node_modules_linker") | ||
|
||
_ATTRS = { | ||
"srcs": attr.label_list(allow_files = True), | ||
"outs": attr.output_list(), | ||
"args": attr.string_list(mandatory = True), | ||
"tool": attr.label( | ||
executable = True, | ||
cfg = "host", | ||
mandatory = True, | ||
), | ||
"deps": attr.label_list(aspects = [module_mappings_aspect]), | ||
} | ||
|
||
def _impl(ctx): | ||
args = ctx.actions.args() | ||
inputs = ctx.files.srcs + ctx.files.deps | ||
outputs = ctx.outputs.outs | ||
register_node_modules_linker(ctx, args, inputs) | ||
for a in ctx.attr.args: | ||
args.add(ctx.expand_location(a)) | ||
ctx.actions.run( | ||
executable = ctx.executable.tool, | ||
inputs = inputs, | ||
outputs = outputs, | ||
arguments = [args], | ||
) | ||
|
||
_npm_genrule = rule( | ||
_impl, | ||
attrs = _ATTRS, | ||
) | ||
|
||
def npm_package_bin(tool = None, package = None, package_bin = None, **kwargs): | ||
"""Run an arbitrary npm package binary (anything under node_modules/.bin/*) under Bazel. | ||
|
||
This is like a genrule() except that it runs our launcher script that first | ||
links the node_modules tree before running the program. | ||
|
||
It's probably easy to wrap this with macros, so something with no rule logic | ||
like stylus_binary could probably just be a macro wrapping this. | ||
|
||
Args: | ||
srcs: identical to [genrule.srcs](https://docs.bazel.build/versions/master/be/general.html#genrule.srcs) | ||
deps: Targets that produce or reference npm packages which are needed by the tool | ||
outs: identical to [genrule.outs](https://docs.bazel.build/versions/master/be/general.html#genrule.outs) | ||
args: Command-line arguments to the tool. | ||
|
||
Subject to 'Make variable' substitution. | ||
Can use $(location) expansion. See https://docs.bazel.build/versions/master/be/make-variables.html | ||
|
||
package: an npm package whose binary to run, like "terser". Assumes your node_modules are installed in a workspace called "npm" | ||
package_bin: the "bin" entry from `package` that should be run. By default package_bin is the same string as `package` | ||
tool: a label for a binary to run, like `@npm//terser/bin:terser`. This is the longer form of package/package_bin | ||
""" | ||
if not tool: | ||
if not package: | ||
fail("You must supply either the tool or package attribute") | ||
if not package_bin: | ||
package_bin = package | ||
tool = "@npm//%s/bin:%s" % (package, package_bin) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems like this could allow a bit more flexibility by allow user to override the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm, we could easily have another attribute for the package workspace. but that does make the API surface of this rule bigger just to preserve some syntax sugar, which seems probably wrong to me. |
||
|
||
_npm_genrule( | ||
tool = tool, | ||
**kwargs | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
const content = fs.readFileSync(path.join(require.resolve(__dirname + '/minified.js')), 'utf-8'); | ||
if (!content.includes('{console.error("thing")}')) { | ||
console.error(content); | ||
process.exitCode = 1; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class A { | ||
doThing() { | ||
console.error('thing'); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
load("@build_bazel_rules_nodejs//:defs.bzl", "npm_package_bin") | ||
def test(**kwargs): | ||
npm_package_bin(tool = "@fine_grained_goldens//@gregmagolan/test-a/bin:test", **kwargs) |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
load("@build_bazel_rules_nodejs//:defs.bzl", "npm_package_bin") | ||
def jasmine(**kwargs): | ||
npm_package_bin(tool = "@fine_grained_goldens//jasmine/bin:jasmine", **kwargs) |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,8 @@ npm_package( | |
srcs = [ | ||
"@npm_bazel_less//:package_contents", | ||
], | ||
# less package is deprecated | ||
tags = ["do-not-publish"], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update release script to key off of this tag? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it already does, that's how we avoided publishing |
||
vendor_external = [ | ||
"npm_bazel_less", | ||
], | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As a user, can I use a relative path here such as
../test.css
? Not that it makes sense in this example but in general is that possible?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You linked the docs which answers the question :) https://docs.bazel.build/versions/master/be/general.html#genrule.srcs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, this will be tough for users I think, because if you just write "test.css" it will write to the input folder. if you're lucky you get an error about that folder being readonly in the sandbox, more likely it writes it and you get an error that bazel says the declared output wasn't produced.
But this is standard genrule behavior so not strictly our job to improve that?