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
/
proto_ts_library.go
189 lines (156 loc) · 5.22 KB
/
proto_ts_library.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package rules_nodejs
import (
"flag"
"log"
"strings"
"github.com/bazelbuild/bazel-gazelle/config"
"github.com/bazelbuild/bazel-gazelle/label"
"github.com/bazelbuild/bazel-gazelle/resolve"
"github.com/bazelbuild/bazel-gazelle/rule"
"github.com/stackb/rules_proto/pkg/protoc"
)
const (
ProtoTsLibraryRuleName = "proto_ts_library"
ProtoTsLibraryRuleSuffix = "_ts_proto"
)
func init() {
protoc.Rules().MustRegisterRule("stackb:rules_proto:proto_ts_library", &protoTsLibrary{})
}
// protoTsLibrary implements LanguageRule for the 'proto_ts_library' rule from
// @rules_proto.
type protoTsLibrary struct{}
// Name implements part of the LanguageRule interface.
func (s *protoTsLibrary) Name() string {
return ProtoTsLibraryRuleName
}
// KindInfo implements part of the LanguageRule interface.
func (s *protoTsLibrary) KindInfo() rule.KindInfo {
return rule.KindInfo{
MergeableAttrs: map[string]bool{
"srcs": true,
"tsc": true,
"args": true,
"data": true,
},
ResolveAttrs: map[string]bool{
"deps": true,
},
}
}
// LoadInfo implements part of the LanguageRule interface.
func (s *protoTsLibrary) LoadInfo() rule.LoadInfo {
return rule.LoadInfo{
Name: "@build_stack_rules_proto//rules/ts:proto_ts_library.bzl",
Symbols: []string{ProtoTsLibraryRuleName},
}
}
// ProvideRule implements part of the LanguageRule interface.
func (s *protoTsLibrary) ProvideRule(cfg *protoc.LanguageRuleConfig, pc *protoc.ProtocConfiguration) protoc.RuleProvider {
flags := parseProtoTsLibraryFlags(ProtoTsLibraryRuleName, cfg.GetOptions())
outputs := make([]string, 0)
for _, out := range pc.Outputs {
if strings.HasSuffix(out, ".ts") {
outputs = append(outputs, out)
}
}
if len(outputs) == 0 {
return nil
}
return &tsLibrary{
flags: flags,
KindName: ProtoTsLibraryRuleName,
RuleNameSuffix: ProtoTsLibraryRuleSuffix,
Outputs: outputs,
RuleConfig: cfg,
Config: pc,
}
}
// tsLibrary implements RuleProvider for 'ts_library'-like rules.
type tsLibrary struct {
flags *protoTsLibraryFlags
KindName string
RuleNameSuffix string
Outputs []string
Config *protoc.ProtocConfiguration
RuleConfig *protoc.LanguageRuleConfig
}
// Kind implements part of the ruleProvider interface.
func (s *tsLibrary) Kind() string {
return s.KindName
}
// Name implements part of the ruleProvider interface.
func (s *tsLibrary) Name() string {
return s.Config.Library.BaseName() + s.RuleNameSuffix
}
// Srcs computes the srcs list for the rule.
func (s *tsLibrary) Srcs() []string {
return s.Outputs
}
// Deps returns all known "configured" dependencies:
// 1. Those given by 'deps' directive on the rule config.
// 2. Those given by resolving "rewrite" specs against the proto file imports.
func (s *tsLibrary) Deps() []string {
deps := s.RuleConfig.GetDeps()
resolvedDeps := protoc.ResolveLibraryRewrites(s.RuleConfig.GetRewrites(), s.Config.Library)
deps = append(deps, resolvedDeps...)
return deps
}
// Visibility provides visibility labels.
func (s *tsLibrary) Visibility() []string {
return s.RuleConfig.GetVisibility()
}
// Rule implements part of the ruleProvider interface.
func (s *tsLibrary) Rule(otherGen ...*rule.Rule) *rule.Rule {
newRule := rule.NewRule(s.Kind(), s.Name())
newRule.SetAttr("srcs", s.Srcs())
deps := s.Deps()
if len(deps) > 0 {
newRule.SetAttr("deps", deps)
}
tsc := s.RuleConfig.GetAttr("tsc")
if len(tsc) > 0 {
if len(tsc) > 1 {
log.Printf("warning (%s): found multiple entries for 'tsc', choosing last one: %v", s.Kind(), tsc)
}
newRule.SetAttr("tsc", tsc[len(tsc)-1])
}
args := s.RuleConfig.GetAttr("args")
if len(args) > 0 {
newRule.SetAttr("args", args)
}
if s.flags.includeProtoLibraryData {
newRule.SetAttr("data", []string{s.Config.Library.Name()})
}
visibility := s.Visibility()
if len(visibility) > 0 {
newRule.SetAttr("visibility", visibility)
}
return newRule
}
// Imports implements part of the RuleProvider interface.
func (s *tsLibrary) Imports(c *config.Config, r *rule.Rule, file *rule.File) []resolve.ImportSpec {
if lib, ok := r.PrivateAttr(protoc.ProtoLibraryKey).(protoc.ProtoLibrary); ok {
return protoc.ProtoLibraryImportSpecsForKind(r.Kind(), lib)
}
return nil
}
// Resolve implements part of the RuleProvider interface.
func (s *tsLibrary) Resolve(c *config.Config, ix *resolve.RuleIndex, r *rule.Rule, imports []string, from label.Label) {
protoc.ResolveDepsAttr("deps", false /* resolve wkts */)(c, ix, r, imports, from)
}
// protoTsLibraryFlags represents the parsed flag configuration for the
// proto_ts_library rule.
type protoTsLibraryFlags struct {
// includeProtoLibraryData is true if the rule should populate the data
// attribute with the proto_library rule.
includeProtoLibraryData bool
}
func parseProtoTsLibraryFlags(kindName string, args []string) *protoTsLibraryFlags {
flags := flag.NewFlagSet(kindName, flag.ExitOnError)
var includeProtoLibraryData bool
flags.BoolVar(&includeProtoLibraryData, "include_proto_library_data", false, "--include_proto_library_data=true populates the data attribute with the proto_library rule")
if err := flags.Parse(args); err != nil {
log.Fatalf("failed to parse flags for %q: %v", kindName, err)
}
return &protoTsLibraryFlags{includeProtoLibraryData}
}