github.com/stackb/rules_proto@v0.0.0-20240221195024-5428336c51f1/pkg/rule/rules_python/grpc_py_library.go (about)

     1  package rules_python
     2  
     3  import (
     4  	"github.com/bazelbuild/bazel-gazelle/config"
     5  	"github.com/bazelbuild/bazel-gazelle/label"
     6  	"github.com/bazelbuild/bazel-gazelle/resolve"
     7  	"github.com/bazelbuild/bazel-gazelle/rule"
     8  
     9  	"github.com/stackb/rules_proto/pkg/protoc"
    10  )
    11  
    12  const (
    13  	grpcPyLibraryRuleName   = "grpc_py_library"
    14  	grpcPyLibraryRuleSuffix = "_grpc_py_library"
    15  )
    16  
    17  func init() {
    18  	protoc.Rules().MustRegisterRule("stackb:rules_proto:grpc_py_library", &grpcPyLibrary{})
    19  }
    20  
    21  // grpcPyLibrary implements LanguageRule for the 'grpc_py_library' rule from
    22  // @rules_proto.
    23  type grpcPyLibrary struct{}
    24  
    25  // Name implements part of the LanguageRule interface.
    26  func (s *grpcPyLibrary) Name() string {
    27  	return grpcPyLibraryRuleName
    28  }
    29  
    30  // KindInfo implements part of the LanguageRule interface.
    31  func (s *grpcPyLibrary) KindInfo() rule.KindInfo {
    32  	return pyLibraryKindInfo
    33  }
    34  
    35  // LoadInfo implements part of the LanguageRule interface.
    36  func (s *grpcPyLibrary) LoadInfo() rule.LoadInfo {
    37  	return rule.LoadInfo{
    38  		Name:    "@build_stack_rules_proto//rules/py:grpc_py_library.bzl",
    39  		Symbols: []string{grpcPyLibraryRuleName},
    40  	}
    41  }
    42  
    43  // ProvideRule implements part of the LanguageRule interface.
    44  func (s *grpcPyLibrary) ProvideRule(cfg *protoc.LanguageRuleConfig, pc *protoc.ProtocConfiguration) protoc.RuleProvider {
    45  	outputs := pc.GetPluginOutputs("grpc:grpc:protoc-gen-grpc-python")
    46  	if len(outputs) == 0 {
    47  		return nil
    48  	}
    49  
    50  	return &PyLibrary{
    51  		KindName:       grpcPyLibraryRuleName,
    52  		RuleNameSuffix: grpcPyLibraryRuleSuffix,
    53  		Outputs:        outputs,
    54  		RuleConfig:     cfg,
    55  		Config:         pc,
    56  		Resolver: func(c *config.Config, ix *resolve.RuleIndex, r *rule.Rule, imports []string, from label.Label) {
    57  			deps := append(r.AttrStrings("deps"), ":"+pc.Library.BaseName()+ProtoPyLibraryRuleSuffix)
    58  
    59  			if len(deps) > 0 {
    60  				r.SetAttr("deps", deps)
    61  			}
    62  		},
    63  	}
    64  }