-
Notifications
You must be signed in to change notification settings - Fork 150
/
protoc-gen-grpc-node.go
51 lines (44 loc) · 1.42 KB
/
protoc-gen-grpc-node.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package grpcnode
import (
"path"
"strings"
"github.com/bazelbuild/bazel-gazelle/label"
"github.com/stackb/rules_proto/pkg/protoc"
)
func init() {
protoc.Plugins().MustRegisterPlugin(&ProtocGenGrpcNode{})
}
// ProtocGenGrpcNode implements Plugin for grpc_node_plugin in the
// grpc/grpc-node repo.
type ProtocGenGrpcNode struct{}
// Name implements part of the Plugin interface.
func (p *ProtocGenGrpcNode) Name() string {
return "grpc:grpc-node:protoc-gen-grpc-node"
}
// Configure implements part of the Plugin interface.
func (p *ProtocGenGrpcNode) Configure(ctx *protoc.PluginContext) *protoc.PluginConfiguration {
if !protoc.HasServices(ctx.ProtoLibrary.Files()...) {
return nil
}
return &protoc.PluginConfiguration{
Label: label.New("build_stack_rules_proto", "plugin/grpc/grpc-node", "protoc-gen-grpc-node"),
Outputs: protoc.FlatMapFiles(
grpcGeneratedFileName(ctx.Rel),
protoc.HasService,
ctx.ProtoLibrary.Files()...,
),
Options: ctx.PluginConfig.GetOptions(),
}
}
// grpcGeneratedFileName is a utility function that returns a function that
// computes the name of a predicted generated file having the given extension(s)
// relative to the given dir.
func grpcGeneratedFileName(reldir string) func(f *protoc.File) []string {
return func(f *protoc.File) []string {
name := strings.ReplaceAll(f.Name, "-", "_")
if reldir != "" {
name = path.Join(reldir, name)
}
return []string{name + "_grpc_pb.js"}
}
}