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