github.com/stackb/rules_proto@v0.0.0-20240221195024-5428336c51f1/pkg/protoc/other_proto_library.go (about)

     1  package protoc
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/bazelbuild/bazel-gazelle/config"
     8  	"github.com/bazelbuild/bazel-gazelle/rule"
     9  )
    10  
    11  // OtherProtoLibrary implements the ProtoLibrary interface from an existing
    12  // ProtoLibrary rule generated by an other extension (typically the
    13  // bazel-gazelle/proto extension).
    14  type OtherProtoLibrary struct {
    15  	source *rule.File
    16  	rule   *rule.Rule
    17  	files  []*File
    18  }
    19  
    20  // NewOtherProtoLibrary constructs a new ProtoLibrary implementation.
    21  func NewOtherProtoLibrary(source *rule.File, rule *rule.Rule, files ...*File) *OtherProtoLibrary {
    22  	return &OtherProtoLibrary{source, rule, files}
    23  }
    24  
    25  // Name implements part of the ProtoLibrary interface
    26  func (s *OtherProtoLibrary) Name() string {
    27  	return s.rule.Name()
    28  }
    29  
    30  // BaseName implements part of the ProtoLibrary interface
    31  func (s *OtherProtoLibrary) BaseName() string {
    32  	name := s.rule.Name()
    33  	if !strings.HasSuffix(name, "_proto") {
    34  		panic(fmt.Sprintf("Unexpected proto_library name %q (it should always end in '_proto')", name))
    35  	}
    36  	return name[0 : len(name)-len("_proto")]
    37  }
    38  
    39  // Rule implements part of the ProtoLibrary interface
    40  func (s *OtherProtoLibrary) Rule() *rule.Rule {
    41  	return s.rule
    42  }
    43  
    44  // Files implements part of the ProtoLibrary interface
    45  func (s *OtherProtoLibrary) Files() []*File {
    46  	return s.files
    47  }
    48  
    49  // Deps implements part of the ProtoLibrary interface
    50  func (s *OtherProtoLibrary) Deps() []string {
    51  	return s.rule.AttrStrings("deps")
    52  }
    53  
    54  // Imports implements part of the ProtoLibrary interface
    55  func (s *OtherProtoLibrary) Imports() []string {
    56  	// Not supposed to be using this private attr, but...
    57  	importRaw := s.rule.PrivateAttr(config.GazelleImportsKey)
    58  	if v, ok := importRaw.([]string); ok {
    59  		return v
    60  	}
    61  	return nil
    62  }
    63  
    64  // Srcs returns the srcs attribute
    65  func (s *OtherProtoLibrary) Srcs() []string {
    66  	return s.rule.AttrStrings("srcs")
    67  }
    68  
    69  // StripImportPrefix implements part of the ProtoLibrary interface
    70  func (s *OtherProtoLibrary) StripImportPrefix() string {
    71  	prefix := s.rule.AttrString("strip_import_prefix")
    72  	if prefix != "" {
    73  		return prefix
    74  	}
    75  	return GetKeptFileRuleAttrString(s.source, s.rule, "strip_import_prefix")
    76  }