diff --git a/BUILD.bazel b/BUILD.bazel index ba53f5bb..9239ddf0 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -1,5 +1,8 @@ load("@bazel_gazelle//:def.bzl", "gazelle") +load("//:def.bzl", "prototool") # gazelle:prefix github.com/uber/prototool # gazelle:proto disable_global gazelle(name = "gazelle") + +prototool(name = "prototool") diff --git a/def.bzl b/def.bzl new file mode 100644 index 00000000..0c1a282a --- /dev/null +++ b/def.bzl @@ -0,0 +1,36 @@ +load("@bazel_skylib//lib:paths.bzl", "paths") + +def _prototool_impl(ctx): + commands = [] + + # Prototool works better from relative paths, so cd to the directroy where + # the action was invoked. + commands.append("cd \"$BUILD_WORKING_DIRECTORY\"") + + # Invoke prototool with the user arguments. + abs_prototool_path = paths.join("\"$BUILD_WORKSPACE_DIRECTORY\"", ctx.executable._prototool.path) + commands.append("{0} $@".format(abs_prototool_path)) + + ctx.actions.run_shell( + outputs = [ctx.outputs.executable], + command = "echo '{commands}' > {output}".format( + commands = " && ".join(commands), + output = ctx.outputs.executable.path, + ), + arguments = ["$@"], + tools = [ctx.executable._prototool], + ) + + return DefaultInfo(executable = ctx.outputs.executable) + +prototool = rule( + implementation = _prototool_impl, + executable = True, + attrs = { + "_prototool": attr.label( + cfg = "host", + default = Label("//cmd/prototool"), + executable = True, + ), + }, +) diff --git a/docs/README.md b/docs/README.md index c48b4456..f6896f8b 100644 --- a/docs/README.md +++ b/docs/README.md @@ -58,6 +58,7 @@ Protobuf file, or under a second for a larger number (500+) of Protobuf files. * [prototool descriptor-set](#prototool-descriptor-set) * [prototool grpc](#prototool-grpc) * [Tips and Tricks](#tips-and-tricks) + * [Bazel Integration](#bazel-integration) * [Vim Integration](#vim-integration) * [Stability](#stability) * [Development](#development) @@ -338,6 +339,46 @@ should follow some basic rules: - Do not use long-form `go_package` values, ie use `foopb`, not `github.com/bar/baz/foo;foopb`. This helps `prototool generate` do the best job. +## Bazel Integration + +Prototool can also be invoked via Bazel. + +##### Setup +In your `WORKSAPCE`: + +```python +PROTOTOOL_VERSION = "" + +http_archive( + name = "com_uber_prototool", + strip_prefix = "prototool-" + PROTOTOOL_VERSION, + url = "https://github.com/uber/prototool/archive/v" + PROTOTOOL_VERSION + ".tar.gz", +) + +load("@com_uber_prototool//bazel:deps.bzl", "prototool_deps") + +prototool_deps() +``` + +In your `BUILD.bazel`: + +```python +load("@com_uber_prototool//:def.bzl", "prototool") + +prototool(name = "prototool") +``` + +##### Usage + +Prototool can now be invoked similar to the CLI tool. Note that you must pass arguments preceding "--". + +```bash +> bazel run //:prototool -- version +> bazel run //:prototool -- lint protobuf/grpc/health/v1/health.proto +> bazel run //:prototool -- format protobuf/grpc/health/v1/health.proto +> bazel run //:prototool -- break check --git-branch master protobuf +``` + ## Vim Integration This repository is a self-contained plugin for use with the