github.com/stackb/rules_proto@v0.0.0-20240221195024-5428336c51f1/pkg/protoc/package_test.go (about) 1 package protoc 2 3 import ( 4 "fmt" 5 6 "github.com/bazelbuild/bazel-gazelle/rule" 7 "github.com/bazelbuild/bazel-gazelle/config" 8 "github.com/emicklei/proto" 9 ) 10 11 const exampleDir = "proto/test" 12 13 func exampleFile() *File { 14 file := NewFile(exampleDir, "test.proto") 15 file.pkg = proto.Package{ 16 Name: "proto.test", 17 } 18 file.messages = append(file.messages, proto.Message{ 19 Name: "Foo", 20 }) 21 file.imports = append(file.imports, 22 proto.Import{Filename: "google/protobuf/any.proto"}, 23 proto.Import{Filename: "foo/foo.proto"}, 24 ) 25 return file 26 } 27 28 func exampleProtoLibraryRule() *rule.Rule { 29 rule := rule.NewRule("proto_library", "test_proto") 30 rule.SetAttr("deps", []string{"//foo:foo_proto"}) 31 return rule 32 } 33 34 func exampleProtoLibrary() ProtoLibrary { 35 return NewOtherProtoLibrary(nil, exampleProtoLibraryRule(), exampleFile()) 36 } 37 38 func examplePackageConfig() *PackageConfig { 39 emptyConfig := &config.Config{} 40 c := NewPackageConfig(emptyConfig) 41 if err := c.ParseDirectives(exampleDir, withDirectives( 42 "proto_rule", "proto_compile implementation stackb:rules_proto:proto_compile", 43 "proto_plugin", "fake_proto implementation protoc:fake", 44 "proto_plugin", "fake_proto enabled true", 45 "proto_language", "fake plugin fake_proto", 46 "proto_language", "fake enabled true", 47 "proto_language", "fake rule proto_compile", 48 )); err != nil { 49 panic("bad config: " + err.Error()) 50 } 51 return c 52 } 53 54 func examplePackage() *Package { 55 return NewPackage( 56 exampleDir, 57 examplePackageConfig(), 58 exampleProtoLibrary(), 59 ) 60 } 61 62 func ExamplePackage() { 63 printRules(examplePackage().Rules()) 64 // Output: 65 // proto_compile( 66 // name = "test_fake_compile", 67 // outputs = ["test_fake.pb.go"], 68 // plugins = ["@build_stack_rules_proto//plugin/builtin:fake"], 69 // proto = "test_proto", 70 // ) 71 } 72 73 func printRules(rules []*rule.Rule) { 74 file := rule.EmptyFile(exampleDir, "") 75 for _, r := range rules { 76 r.Insert(file) 77 } 78 fmt.Println(string(file.Format())) 79 }