github.com/stackb/rules_proto@v0.0.0-20240221195024-5428336c51f1/rules/protogenrule.bzl (about)

     1  """protogenrule wraps native.genrule with supplemental .update and _test targets
     2  
     3  protogenrule is a drop-in replacement for native.genrule that provides two additional targets
     4  used to copying source back into the monorepo and asserting that the
     5  generated file(s) remain consistent with the source control version of the file.
     6  
     7  For any protogenrule target `//:a` that outputs `a.txt`, `//:a.update`
     8  copies `a.txt` back into the source tree as `a.txt'`; `//:a_test` asserts that
     9  `a.txt` and `a.txt'` are identical.
    10  """
    11  
    12  load(
    13      "@build_stack_rules_proto//rules:proto_compile_gencopy.bzl",
    14      "proto_compile_gencopy_run",
    15      "proto_compile_gencopy_test",
    16  )
    17  load(
    18      "@build_stack_rules_proto//rules:providers.bzl",
    19      "ProtoCompileInfo",
    20  )
    21  
    22  def _proto_compiled_sources_impl(ctx):
    23      dep = ctx.attr.dep[DefaultInfo]
    24      return ProtoCompileInfo(
    25          label = ctx.attr.dep.label,
    26          outputs = dep.files.to_list(),
    27      )
    28  
    29  _proto_compiled_sources = rule(
    30      doc = """Provider Adapter from DefaultInfo to ProtoCompileInfo.
    31          """,
    32      implementation = _proto_compiled_sources_impl,
    33      attrs = {"dep": attr.label(providers = [DefaultInfo])},
    34  )
    35  
    36  def protogenrule(
    37          name,
    38          run_target_suffix = ".update",
    39          sources_target_suffix = "d_sources",
    40          test_target_suffix = "_test",
    41          **kwargs):
    42      """protogenrule is used identically to native.gencopy
    43  
    44      Args:
    45          name: the name of the rule
    46          run_target_suffix: the suffix for the update/copy target
    47          sources_target_suffix: the suffix for the _proto_compiled_sources target
    48          test_target_suffix: the suffix for the test target
    49          **kwargs: remainder of non-positional args
    50      """
    51      name_run = name + run_target_suffix
    52      name_sources = name + sources_target_suffix
    53      name_test = name + test_target_suffix
    54  
    55      outs = kwargs.pop("outs", [])
    56      visibility = kwargs.pop("visibility", [])
    57  
    58      native.genrule(
    59          name = name,
    60          outs = outs,
    61          visibility = visibility,
    62          **kwargs
    63      )
    64  
    65      _proto_compiled_sources(
    66          name = name_sources,
    67          dep = name,
    68          visibility = visibility,
    69      )
    70  
    71      proto_compile_gencopy_test(
    72          name = name_test,
    73          srcs = outs,
    74          deps = [name_sources],
    75          mode = "check",
    76          update_target_label_name = name_run,
    77      )
    78  
    79      proto_compile_gencopy_run(
    80          name = name_run,
    81          deps = [name_sources],
    82          mode = "update",
    83          update_target_label_name = name_run,
    84      )