<commit_before>def _collect_includes(srcs):
    includes = ["."]
    for src in srcs:
        include = src.dirname
        if include and not include in includes:
            includes += [include]

    return includes

def _run_proto_gen_swagger(direct_proto_srcs, transitive_proto_srcs, actions, protoc, protoc_gen_swagger):
    swagger_files = []
    for proto in direct_proto_srcs:
      swagger_file = actions.declare_file(
          "%s.swagger.json" % proto.basename[:-len(".proto")],
          sibling = proto,
      )

      args = actions.args()
      args.add("--plugin=%s" % protoc_gen_swagger.path)
      args.add("--swagger_out=logtostderr=true:%s" % swagger_file.dirname)
      args.add("-Iexternal/com_google_protobuf/src")
      args.add("-Iexternal/com_github_googleapis_googleapis")
      args.add(["-I%s" % include for include in _collect_includes(direct_proto_srcs + transitive_proto_srcs)])
      args.add(proto.basename)

      actions.run(
          executable = protoc,
          inputs = direct_proto_srcs + transitive_proto_srcs + [protoc_gen_swagger],
          outputs = [swagger_file],
          arguments = [args],
      )

      swagger_files.append(swagger_file)

    return swagger_files

def _proto_gen_swagger_impl(ctx):
    proto = ctx.attr.proto.proto

    return struct(
        files=depset(
            _run_proto_gen_swagger(
                direct_proto_srcs = proto.direct_sources,
                transitive_proto_srcs = ctx.files._well_known_protos + proto.transitive_sources.to_list(),
                actions = ctx.actions,
                protoc = ctx.executable._protoc,
                protoc_gen_swagger = ctx.executable._protoc_gen_swagger,
            )
        )
    )

protoc_gen_swagger = rule(
    attrs = {
        "proto": attr.label(
            allow_rules = ["proto_library"],
            mandatory = True,
            providers = ['proto'],
        ),
        "_protoc": attr.label(
            default = "@com_google_protobuf//:protoc",
            executable = True,
            cfg = "host",
        ),
        "_well_known_protos": attr.label(
            default = "@com_google_protobuf//:well_known_protos",
            allow_files = True,
        ),
        "_protoc_gen_swagger": attr.label(
            default = Label("//protoc-gen-swagger:protoc-gen-swagger"),
            executable = True,<commit_msg>Add support for the grpc_api_configuration option in the bazel rule. (#632)<commit_after>"""Generated an open-api spec for a grpc api spec.

Reads the the api spec in protobuf format and generate an open-api spec. 
Optionally applies settings from the grpc-service configuration.
"""
def _collect_includes(srcs):
    includes = ["."]
    for src in srcs:
        include = src.dirname
        if include and not include in includes:
            includes += [include]

    return includes

def _run_proto_gen_swagger(direct_proto_srcs, transitive_proto_srcs, actions, protoc, protoc_gen_swagger, grpc_api_configuration):
    swagger_files = []
    for proto in direct_proto_srcs:
      swagger_file = actions.declare_file(
          "%s.swagger.json" % proto.basename[:-len(".proto")],
          sibling = proto,
      )
      
      inputs = direct_proto_srcs + transitive_proto_srcs + [protoc_gen_swagger]
      
      options=["logtostderr=true"]
      if grpc_api_configuration:
          options.append("grpc_api_configuration=%s" % grpc_api_configuration.path)
          inputs.append(grpc_api_configuration)

      args = actions.args()
      args.add("--plugin=%s" % protoc_gen_swagger.path)
      args.add("--swagger_out=%s:%s" % (",".join(options), swagger_file.dirname))
      args.add("-Iexternal/com_google_protobuf/src")
      args.add("-Iexternal/com_github_googleapis_googleapis")
      args.add(["-I%s" % include for include in _collect_includes(direct_proto_srcs + transitive_proto_srcs)])
      args.add(proto.basename)

      actions.run(
          executable = protoc,
          inputs = inputs,
          outputs = [swagger_file],
          arguments = [args],
      )

      swagger_files.append(swagger_file)

    return swagger_files

def _proto_gen_swagger_impl(ctx):
    proto = ctx.attr.proto.proto
    grpc_api_configuration = ctx.file.grpc_api_configuration

    return struct(
        files=depset(
            _run_proto_gen_swagger(
                direct_proto_srcs = proto.direct_sources,
                transitive_proto_srcs = ctx.files._well_known_protos + proto.transitive_sources.to_list(),
                actions = ctx.actions,
                protoc = ctx.executable._protoc,
                protoc_gen_swagger = ctx.executable._protoc_gen_swagger,
                grpc_api_configuration = grpc_api_configuration
            )
        )
    )

protoc_gen_swagger = rule(
    attrs = {
        "proto": attr.label(
            allow_rules = ["proto_library"],
            mandatory = True,
            providers = ['proto'],
        ),
        "grpc_api_configuration": attr.label(
            allow_single_file=True,
            mandatory=False
        ),
        "_protoc": attr.label(
            default = "@com_google_protobuf//:protoc",
            executable = True,
            cfg = "host",
        ),
        "_well_known_protos": attr.label(
            default = "@com_google_protobuf//:well_known_protos",
            allow_files = True,
        ),
        "_protoc_gen_swagger": attr.label(
            default = Label("//protoc-gen-swagger:protoc-gen-swagger"),
            executable = True,
{'fraction_non_alphanumeric': 0.10351067680057909, 'fraction_numerical': 0.0005428881650380022, 'mean_word_length': 2.3619221411192215, 'pattern_counts': {'":': 9, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4597023', 'n_tokens_mistral': 1695, 'n_tokens_neox': 1676, 'n_words': 290}