From 157bf67a5b2782242d79bf92c4a3c143de4049fc Mon Sep 17 00:00:00 2001 From: Jathu Satkunarajah Date: Sat, 30 May 2020 16:00:42 -0400 Subject: [PATCH 1/3] create prototool rule --- def.bzl | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 def.bzl diff --git a/def.bzl b/def.bzl new file mode 100644 index 00000000..1b21731b --- /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_executable.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_executable], + ) + + return DefaultInfo(executable = ctx.outputs.executable) + +prototool = rule( + implementation = _prototool_impl, + executable = True, + attrs = { + "_prototool_executable": attr.label( + cfg = "host", + default = Label("//cmd/prototool"), + executable = True, + ), + }, +) From 0052e24412fcc0100647355c1576e6e4bc9a944d Mon Sep 17 00:00:00 2001 From: Jathu Satkunarajah Date: Sat, 30 May 2020 16:11:06 -0400 Subject: [PATCH 2/3] include instructions in README --- docs/README.md | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) 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 From aaff4f8e089cb863e827e41e54760c3590cbedfb Mon Sep 17 00:00:00 2001 From: Jathu Satkunarajah Date: Wed, 3 Jun 2020 09:43:21 -0400 Subject: [PATCH 3/3] update prototool executable name and include a build target --- BUILD.bazel | 3 +++ def.bzl | 6 +++--- 2 files changed, 6 insertions(+), 3 deletions(-) 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 index 1b21731b..0c1a282a 100644 --- a/def.bzl +++ b/def.bzl @@ -8,7 +8,7 @@ def _prototool_impl(ctx): commands.append("cd \"$BUILD_WORKING_DIRECTORY\"") # Invoke prototool with the user arguments. - abs_prototool_path = paths.join("\"$BUILD_WORKSPACE_DIRECTORY\"", ctx.executable._prototool_executable.path) + abs_prototool_path = paths.join("\"$BUILD_WORKSPACE_DIRECTORY\"", ctx.executable._prototool.path) commands.append("{0} $@".format(abs_prototool_path)) ctx.actions.run_shell( @@ -18,7 +18,7 @@ def _prototool_impl(ctx): output = ctx.outputs.executable.path, ), arguments = ["$@"], - tools = [ctx.executable._prototool_executable], + tools = [ctx.executable._prototool], ) return DefaultInfo(executable = ctx.outputs.executable) @@ -27,7 +27,7 @@ prototool = rule( implementation = _prototool_impl, executable = True, attrs = { - "_prototool_executable": attr.label( + "_prototool": attr.label( cfg = "host", default = Label("//cmd/prototool"), executable = True,