Skip to content
This repository has been archived by the owner on Mar 4, 2022. It is now read-only.

Expose prototool as a Bazel rule #556

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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")
36 changes: 36 additions & 0 deletions def.bzl
Original file line number Diff line number Diff line change
@@ -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,
),
},
)
41 changes: 41 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 = "<desired version from github.com/uber/prototool/releases>"

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
Expand Down