|
| 1 | +"go_proto_library wrapper macro to check in the resulting stub files for the editor" |
| 2 | + |
| 3 | +load("@aspect_bazel_lib//lib:write_source_files.bzl", "write_source_files") |
| 4 | +load("@aspect_bazel_lib//lib:output_files.bzl", "make_output_files") |
| 5 | +load("@bazel_skylib//lib:paths.bzl", "paths") |
| 6 | +load("@rules_go//proto:def.bzl", _go_proto_library = "go_proto_library") |
| 7 | + |
| 8 | +def go_proto_library(name, importpath, proto_srcs = [], **kwargs): |
| 9 | + """Wrap go_proto_library with write_source_files. |
| 10 | +
|
| 11 | + This causes the resulting .pb.go files to be checked into the source tree. |
| 12 | + Args: |
| 13 | + name: name of the go_proto_library rule produced |
| 14 | + importpath: passed to go_proto_library#importpath |
| 15 | + proto_srcs: the srcs of the proto_library target passed to go_proto_library#proto |
| 16 | + If unset, a glob() of all ".proto" files in the package is used. |
| 17 | + **kwargs: remaining arguments to go_proto_library |
| 18 | + """ |
| 19 | + |
| 20 | + # Based on our knowledge of the rule implementation, |
| 21 | + # predict the output paths it writes. |
| 22 | + proto_out_path = "{0}/{1}_/{2}/%s.pb.go".format( |
| 23 | + native.package_name(), |
| 24 | + name, |
| 25 | + importpath, |
| 26 | + ) |
| 27 | + |
| 28 | + if len(proto_srcs) < 1: |
| 29 | + proto_srcs = native.glob(["*.proto"]) |
| 30 | + |
| 31 | + _go_proto_library( |
| 32 | + name = name, |
| 33 | + importpath = importpath, |
| 34 | + **kwargs |
| 35 | + ) |
| 36 | + |
| 37 | + write_source_files( |
| 38 | + name = name + ".update_go_pb", |
| 39 | + files = { |
| 40 | + base + ".pb.go": make_output_files(base + "_pb_go", name, [proto_out_path % base], output_group = "go_generated_srcs") |
| 41 | + for base in [paths.replace_extension(p, "") for p in proto_srcs] |
| 42 | + }, |
| 43 | + visibility = ["//write_source_files:__pkg__"], |
| 44 | + ) |
0 commit comments