kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/go/indexer/flags.bzl (about) 1 """Starlark library to build FlagConstructors textproto files""" 2 3 def flag_constructor(func_name, pkg_path, name_arg_position, description_arg_position, var_arg_position = None): 4 """Produces a text proto encoding of a kythe.proto.FlagConstructor. 5 6 Meant to be used as a custom_flags value. 7 8 Args: 9 func_name: the name of the flag constructing function 10 pkg_path: the package path of the flag constructing function 11 name_arg_position: the 0-based position of the name string argument 12 description_arg_position: the 0-based position of the description string argument 13 var_arg_position: the 0-based position of the variable pointer argument 14 """ 15 flag = { 16 "func_name": func_name, 17 "pkg_path": pkg_path, 18 "name_arg_position": name_arg_position, 19 "description_arg_position": description_arg_position, 20 } 21 if var_arg_position != None: 22 flag["var_arg_position"] = var_arg_position 23 return proto.encode_text(struct(**flag)) 24 25 def _flag_constructors(ctx): 26 textpb = "# proto-file: kythe/proto/go.proto\n# proto-message: kythe.proto.FlagConstructors\n\n" 27 flags = [] 28 for pkg in ctx.attr.standard_flags: 29 for f in ctx.attr.standard_flags[pkg]: 30 flags.append(struct(**{ 31 "pkg_path": pkg, 32 "func_name": f, 33 "name_arg_position": 0, 34 "description_arg_position": 2, 35 })) 36 for pkg in ctx.attr.standard_var_flags: 37 for f in ctx.attr.standard_var_flags[pkg]: 38 flags.append(struct(**{ 39 "pkg_path": pkg, 40 "func_name": f, 41 "name_arg_position": 1, 42 "description_arg_position": 3, 43 "var_arg_position": 0, 44 })) 45 textpb += proto.encode_text(struct(**{"flag": flags})) 46 for f in ctx.attr.custom_flags: 47 textpb += "flag {\n " + f.replace("\n", "\n ").strip() + "\n}\n" 48 ctx.actions.write( 49 output = ctx.outputs.output, 50 content = textpb, 51 ) 52 53 flag_constructors = rule( 54 implementation = _flag_constructors, 55 attrs = { 56 "standard_flags": attr.string_list_dict(), 57 "standard_var_flags": attr.string_list_dict(), 58 "custom_flags": attr.string_list(), 59 }, 60 outputs = {"output": "%{name}.textproto"}, 61 )