Skip to content

Commit

Permalink
feat(ts_proto_library): expose protoc_gen_options on ts_proto_library (
Browse files Browse the repository at this point in the history
  • Loading branch information
mattem authored Jul 23, 2024
1 parent 1e74685 commit 56f0338
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
3 changes: 2 additions & 1 deletion docs/proto.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Future work
## ts_proto_library

<pre>
ts_proto_library(<a href="#ts_proto_library-name">name</a>, <a href="#ts_proto_library-node_modules">node_modules</a>, <a href="#ts_proto_library-proto">proto</a>, <a href="#ts_proto_library-gen_connect_es">gen_connect_es</a>, <a href="#ts_proto_library-gen_connect_query">gen_connect_query</a>,
ts_proto_library(<a href="#ts_proto_library-name">name</a>, <a href="#ts_proto_library-node_modules">node_modules</a>, <a href="#ts_proto_library-proto">proto</a>, <a href="#ts_proto_library-protoc_gen_options">protoc_gen_options</a>, <a href="#ts_proto_library-gen_connect_es">gen_connect_es</a>, <a href="#ts_proto_library-gen_connect_query">gen_connect_query</a>,
<a href="#ts_proto_library-gen_connect_query_service_mapping">gen_connect_query_service_mapping</a>, <a href="#ts_proto_library-copy_files">copy_files</a>, <a href="#ts_proto_library-proto_srcs">proto_srcs</a>, <a href="#ts_proto_library-files_to_copy">files_to_copy</a>, <a href="#ts_proto_library-kwargs">kwargs</a>)
</pre>

Expand All @@ -61,6 +61,7 @@ A macro to generate JavaScript code and TypeScript typings from .proto files.
| <a id="ts_proto_library-name"></a>name | name of resulting ts_proto_library target | none |
| <a id="ts_proto_library-node_modules"></a>node_modules | Label pointing to the linked node_modules target where @bufbuild/protoc-gen-es is linked, e.g. //:node_modules. Since the generated code depends on @bufbuild/protobuf, this package must also be linked. If `gen_connect_es = True` then @bufbuild/proto-gen-connect-es should be linked as well. If `gen_connect_query = True` then @bufbuild/proto-gen-connect-query should be linked as well. | none |
| <a id="ts_proto_library-proto"></a>proto | the `proto_library` target that contains the .proto files to generate code for. | none |
| <a id="ts_proto_library-protoc_gen_options"></a>protoc_gen_options | dict of protoc_gen_es options. See https://github.com/bufbuild/protobuf-es/tree/main/packages/protoc-gen-es#plugin-options | `{}` |
| <a id="ts_proto_library-gen_connect_es"></a>gen_connect_es | whether protoc_gen_connect_es should generate grpc services, and therefore `*_connect.{js,d.ts}` should be written. | `True` |
| <a id="ts_proto_library-gen_connect_query"></a>gen_connect_query | whether protoc_gen_connect_query should generate [TanStack Query](https://tanstack.com/query) clients, and therefore `*_connectquery.{js,d.ts}` should be written. | `False` |
| <a id="ts_proto_library-gen_connect_query_service_mapping"></a>gen_connect_query_service_mapping | mapping from source proto file to the named RPC services that file contains. Needed to predict which files will be generated by gen_connect_query. See https://github.com/connectrpc/connect-query-es/tree/main/examples/react/basic/src/gen<br><br>For example, given `a.proto` which contains a service `Foo` and `b.proto` that contains a service `Bar`, the mapping would be `{"a.proto": ["Foo"], "b.proto": ["Bar"]}` | `{}` |
Expand Down
19 changes: 15 additions & 4 deletions ts/private/ts_proto_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ load("@rules_proto//proto:proto_common.bzl", proto_toolchains = "toolchains")
_PROTO_TOOLCHAIN_TYPE = "@rules_proto//proto:toolchain_type"

# buildifier: disable=function-docstring-header
def _protoc_action(ctx, proto_info, outputs, options = {
"keep_empty_files": True,
"target": "js+dts",
}):
def _protoc_action(ctx, proto_info, outputs):
"""Create an action like
bazel-out/k8-opt-exec-2B5CBBC6/bin/external/com_google_protobuf/protoc $@' '' \
'--plugin=protoc-gen-es=bazel-out/k8-opt-exec-2B5CBBC6/bin/plugin/bufbuild/protoc-gen-es.sh' \
Expand All @@ -21,6 +18,16 @@ def _protoc_action(ctx, proto_info, outputs, options = {
"""
inputs = depset(proto_info.direct_sources, transitive = [proto_info.transitive_descriptor_sets])

options = dict({
"keep_empty_files": True,
"target": "js+dts",
}, **ctx.attr.protoc_gen_options)

if not options["keep_empty_files"]:
fail("protoc_gen_options.keep_empty_files must be True")
if options["target"] != "js+dts":
fail("protoc_gen_options.target must be 'js+dts'")

args = ctx.actions.args()
args.add_joined(["--plugin", "protoc-gen-es", ctx.executable.protoc_gen_es.path], join_with = "=")
for (key, value) in options.items():
Expand Down Expand Up @@ -105,6 +112,10 @@ ts_proto_library = rule(
providers = [ProtoInfo],
mandatory = True,
),
"protoc_gen_options": attr.string_dict(
doc = "dict of protoc_gen_es options",
default = {},
),
"protoc_gen_es": attr.label(
doc = "protoc plugin to generate messages",
mandatory = True,
Expand Down
5 changes: 4 additions & 1 deletion ts/proto.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ load("@aspect_bazel_lib//lib:write_source_files.bzl", "write_source_files")
load("@aspect_rules_js//js:defs.bzl", "js_binary", "js_library")
load("//ts/private:ts_proto_library.bzl", ts_proto_library_rule = "ts_proto_library")

def ts_proto_library(name, node_modules, proto, gen_connect_es = True, gen_connect_query = False, gen_connect_query_service_mapping = {}, copy_files = True, proto_srcs = None, files_to_copy = None, **kwargs):
def ts_proto_library(name, node_modules, proto, protoc_gen_options = {}, gen_connect_es = True, gen_connect_query = False, gen_connect_query_service_mapping = {}, copy_files = True, proto_srcs = None, files_to_copy = None, **kwargs):
"""
A macro to generate JavaScript code and TypeScript typings from .proto files.
Expand All @@ -58,6 +58,8 @@ def ts_proto_library(name, node_modules, proto, gen_connect_es = True, gen_conne
If `gen_connect_es = True` then @bufbuild/proto-gen-connect-es should be linked as well.
If `gen_connect_query = True` then @bufbuild/proto-gen-connect-query should be linked as well.
proto: the `proto_library` target that contains the .proto files to generate code for.
protoc_gen_options: dict of protoc_gen_es options.
See https://github.com/bufbuild/protobuf-es/tree/main/packages/protoc-gen-es#plugin-options
gen_connect_es: whether protoc_gen_connect_es should generate grpc services, and therefore `*_connect.{js,d.ts}` should be written.
gen_connect_query: whether protoc_gen_connect_query should generate [TanStack Query](https://tanstack.com/query) clients, and therefore `*_connectquery.{js,d.ts}` should be written.
gen_connect_query_service_mapping: mapping from source proto file to the named RPC services that file contains.
Expand Down Expand Up @@ -132,6 +134,7 @@ def ts_proto_library(name, node_modules, proto, gen_connect_es = True, gen_conne
gen_connect_query = gen_connect_query,
gen_connect_query_service_mapping = gen_connect_query_service_mapping,
proto = proto,
protoc_gen_options = protoc_gen_options,
)

js_library(
Expand Down

0 comments on commit 56f0338

Please sign in to comment.