This repository has been archived by the owner on Jun 17, 2023. It is now read-only.
forked from stackb/rules_proto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
php_plugin.go
75 lines (61 loc) · 1.78 KB
/
php_plugin.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package builtin
import (
"path"
"strings"
"github.com/bazelbuild/bazel-gazelle/label"
"github.com/stackb/rules_proto/pkg/protoc"
)
func init() {
protoc.Plugins().MustRegisterPlugin(&PhpPlugin{})
}
// PhpPlugin implements Plugin for the built-in protoc php plugin.
type PhpPlugin struct{}
// Name implements part of the Plugin interface.
func (p *PhpPlugin) Name() string {
return "builtin:php"
}
// Configure implements part of the Plugin interface.
func (p *PhpPlugin) Configure(ctx *protoc.PluginContext) *protoc.PluginConfiguration {
return &protoc.PluginConfiguration{
Label: label.New("build_stack_rules_proto", "plugin/builtin", "php"),
Outputs: protoc.FlatMapFiles(
phpFileName(ctx.Rel),
protoc.Always,
ctx.ProtoLibrary.Files()...,
),
Out: ctx.Rel,
Options: ctx.PluginConfig.GetOptions(),
}
}
func phpFileName(rel string) func(f *protoc.File) []string {
relDir := strings.Title(rel)
return func(f *protoc.File) []string {
outs := make([]string, 0)
// Compute the base dir where files are generated
dir := ""
pkg := f.Package()
if pkg.Name != "" {
dir = path.Join(strings.Title(strings.ReplaceAll(pkg.Name, ".", "/")), dir)
}
// php_namespace overrides package
ns, _ := protoc.GetNamedOption(f.Options(), "php_namespace")
if ns != "" {
dir = ns
}
// Add the metadata file
mns, _ := protoc.GetNamedOption(f.Options(), "php_metadata_namespace")
if mns == "" {
mns = "GPBMetadata"
}
outs = append(outs, path.Join(rel, mns, relDir, strings.Title(f.Name))+".php")
// Add enums
for _, e := range f.Enums() {
outs = append(outs, path.Join(dir, rel, strings.Title(e.Name))+".php")
}
// Add messages
for _, m := range f.Messages() {
outs = append(outs, path.Join(dir, rel, strings.Title(m.Name))+".php")
}
return outs
}
}