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

     1  package rules_java
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/bazelbuild/bazel-gazelle/config"
     7  	"github.com/bazelbuild/bazel-gazelle/label"
     8  	"github.com/bazelbuild/bazel-gazelle/resolve"
     9  	"github.com/bazelbuild/bazel-gazelle/rule"
    10  
    11  	"github.com/stackb/rules_proto/pkg/protoc"
    12  )
    13  
    14  var javaLibraryKindInfo = rule.KindInfo{
    15  	MergeableAttrs: map[string]bool{
    16  		"srcs":    true,
    17  		"exports": true,
    18  	},
    19  	NonEmptyAttrs: map[string]bool{
    20  		"srcs": true,
    21  	},
    22  	ResolveAttrs: map[string]bool{
    23  		"deps": true,
    24  	},
    25  }
    26  
    27  // JavaLibrary implements RuleProvider for 'java_library'-derived rules.
    28  type JavaLibrary struct {
    29  	KindName       string
    30  	RuleNameSuffix string
    31  	Outputs        []string
    32  	Config         *protoc.ProtocConfiguration
    33  	RuleConfig     *protoc.LanguageRuleConfig
    34  	Resolver       protoc.DepsResolver
    35  }
    36  
    37  // Kind implements part of the ruleProvider interface.
    38  func (s *JavaLibrary) Kind() string {
    39  	return s.KindName
    40  }
    41  
    42  // Name implements part of the ruleProvider interface.
    43  func (s *JavaLibrary) Name() string {
    44  	return s.Config.Library.BaseName() + s.RuleNameSuffix
    45  }
    46  
    47  // Srcs computes the srcs list for the rule.
    48  func (s *JavaLibrary) Srcs() []string {
    49  	srcs := make([]string, 0)
    50  	for _, output := range s.Outputs {
    51  		if strings.HasSuffix(output, ".srcjar") {
    52  			srcs = append(srcs, protoc.StripRel(s.Config.Rel, output))
    53  		}
    54  	}
    55  	return srcs
    56  }
    57  
    58  // Deps computes the deps list for the rule.
    59  func (s *JavaLibrary) Deps() []string {
    60  	return s.RuleConfig.GetDeps()
    61  }
    62  
    63  // Visibility provides visibility labels.
    64  func (s *JavaLibrary) Visibility() []string {
    65  	return s.RuleConfig.GetVisibility()
    66  }
    67  
    68  // Rule implements part of the ruleProvider interface.
    69  func (s *JavaLibrary) Rule(otherGen ...*rule.Rule) *rule.Rule {
    70  	newRule := rule.NewRule(s.Kind(), s.Name())
    71  
    72  	newRule.SetAttr("srcs", s.Srcs())
    73  
    74  	deps := s.Deps()
    75  	if len(deps) > 0 {
    76  		newRule.SetAttr("deps", deps)
    77  	}
    78  
    79  	visibility := s.Visibility()
    80  	if len(visibility) > 0 {
    81  		newRule.SetAttr("visibility", visibility)
    82  	}
    83  
    84  	return newRule
    85  }
    86  
    87  // Imports implements part of the RuleProvider interface.
    88  func (s *JavaLibrary) Imports(c *config.Config, r *rule.Rule, file *rule.File) []resolve.ImportSpec {
    89  	if lib, ok := r.PrivateAttr(protoc.ProtoLibraryKey).(protoc.ProtoLibrary); ok {
    90  		return protoc.ProtoLibraryImportSpecsForKind(r.Kind(), lib)
    91  	}
    92  	return nil
    93  }
    94  
    95  // Resolve implements part of the RuleProvider interface.
    96  func (s *JavaLibrary) Resolve(c *config.Config, ix *resolve.RuleIndex, r *rule.Rule, imports []string, from label.Label) {
    97  	if s.Resolver == nil {
    98  		return
    99  	}
   100  	s.Resolver(c, ix, r, imports, from)
   101  }