github.com/stackb/rules_proto@v0.0.0-20240221195024-5428336c51f1/pkg/protoc/fake_proto_library_test.go (about) 1 package protoc 2 3 import ( 4 "path" 5 "strings" 6 7 "github.com/bazelbuild/bazel-gazelle/label" 8 "github.com/bazelbuild/bazel-gazelle/rule" 9 ) 10 11 func init() { 12 Plugins().MustRegisterPlugin(&fakePlugin{}) 13 Rules().MustRegisterRule("fake_proto_library", &fakeProtoLibrary{}) 14 } 15 16 // fakePlugin implements a mock Plugin 17 type fakePlugin struct{} 18 19 // Name implements part of the Plugin interface. 20 func (p *fakePlugin) Name() string { 21 return "protoc:fake" 22 } 23 24 // Configure implements part of the Plugin interface 25 func (p *fakePlugin) Configure(ctx *PluginContext) *PluginConfiguration { 26 return &PluginConfiguration{ 27 Label: label.New("build_stack_rules_proto", "plugin/builtin", "fake"), 28 Outputs: p.outputs(ctx.ProtoLibrary), 29 Options: p.options(ctx.ProtoLibrary), 30 } 31 } 32 33 func (p *fakePlugin) outputs(lib ProtoLibrary) []string { 34 srcs := make([]string, 0) 35 for _, f := range lib.Files() { 36 base := f.Name 37 pkg := f.Package() 38 if pkg.Name != "" { 39 base = path.Join(strings.ReplaceAll(pkg.Name, ".", "/"), base) 40 } 41 if f.HasMessages() || f.HasEnums() { 42 srcs = append(srcs, base+"_fake.pb.go") 43 } 44 } 45 return srcs 46 } 47 48 // Options computes additional options for the plugin. If the library contains 49 // services, apply the grpc plugin. 50 func (p *fakePlugin) options(lib ProtoLibrary) []string { 51 for _, f := range lib.Files() { 52 if f.HasServices() { 53 return []string{"plugins=grpc"} 54 } 55 } 56 return nil 57 } 58 59 // fakeProtoLibrary implements a mock LanguageRule 60 type fakeProtoLibrary struct{} 61 62 // Name implements part of the LanguageRule interface. 63 func (s *fakeProtoLibrary) Name() string { 64 return "fake_proto_library" 65 } 66 67 // KindInfo implements part of the LanguageRule interface. 68 func (s *fakeProtoLibrary) KindInfo() rule.KindInfo { 69 return rule.KindInfo{} 70 } 71 72 // LoadInfo implements part of the LanguageRule interface. 73 func (s *fakeProtoLibrary) LoadInfo() rule.LoadInfo { 74 return rule.LoadInfo{} 75 } 76 77 // ProvideRule implements part of the LanguageRule interface. 78 func (s *fakeProtoLibrary) ProvideRule(rc *LanguageRuleConfig, pc *ProtocConfiguration) RuleProvider { 79 return nil 80 }