forked from bazel-contrib/rules_python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtarget.go
More file actions
146 lines (131 loc) · 4.11 KB
/
Copy pathtarget.go
File metadata and controls
146 lines (131 loc) · 4.11 KB
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
package python
import (
"path/filepath"
"github.com/bazelbuild/bazel-gazelle/config"
"github.com/bazelbuild/bazel-gazelle/rule"
"github.com/emirpasic/gods/sets/treeset"
godsutils "github.com/emirpasic/gods/utils"
)
// targetBuilder builds targets to be generated by Gazelle.
type targetBuilder struct {
kind string
name string
pythonProjectRoot string
bzlPackage string
uuid string
srcs *treeset.Set
deps *treeset.Set
resolvedDeps *treeset.Set
visibility *treeset.Set
main *string
imports []string
testonly bool
}
// newTargetBuilder constructs a new targetBuilder.
func newTargetBuilder(kind, name, pythonProjectRoot, bzlPackage string) *targetBuilder {
return &targetBuilder{
kind: kind,
name: name,
pythonProjectRoot: pythonProjectRoot,
bzlPackage: bzlPackage,
srcs: treeset.NewWith(godsutils.StringComparator),
deps: treeset.NewWith(moduleComparator),
resolvedDeps: treeset.NewWith(godsutils.StringComparator),
visibility: treeset.NewWith(godsutils.StringComparator),
}
}
// setUUID sets the given UUID for the target. It's used to index the generated
// target based on this value in addition to the other ways the targets can be
// imported. py_{binary,test} targets in the same Bazel package can add a
// virtual dependency to this UUID that gets resolved in the Resolver interface.
func (t *targetBuilder) setUUID(uuid string) *targetBuilder {
t.uuid = uuid
return t
}
// addSrc adds a single src to the target.
func (t *targetBuilder) addSrc(src string) *targetBuilder {
t.srcs.Add(src)
return t
}
// addSrcs copies all values from the provided srcs to the target.
func (t *targetBuilder) addSrcs(srcs *treeset.Set) *targetBuilder {
it := srcs.Iterator()
for it.Next() {
t.srcs.Add(it.Value().(string))
}
return t
}
// addModuleDependency adds a single module dep to the target.
func (t *targetBuilder) addModuleDependency(dep module) *targetBuilder {
t.deps.Add(dep)
return t
}
// addModuleDependencies copies all values from the provided deps to the target.
func (t *targetBuilder) addModuleDependencies(deps *treeset.Set) *targetBuilder {
it := deps.Iterator()
for it.Next() {
t.deps.Add(it.Value().(module))
}
return t
}
// addResolvedDependency adds a single dependency the target that has already
// been resolved or generated. The Resolver step doesn't process it further.
func (t *targetBuilder) addResolvedDependency(dep string) *targetBuilder {
t.resolvedDeps.Add(dep)
return t
}
// addVisibility adds a visibility to the target.
func (t *targetBuilder) addVisibility(visibility string) *targetBuilder {
t.visibility.Add(visibility)
return t
}
// setMain sets the main file to the target.
func (t *targetBuilder) setMain(main string) *targetBuilder {
t.main = &main
return t
}
// setTestonly sets the testonly attribute to true.
func (t *targetBuilder) setTestonly() *targetBuilder {
t.testonly = true
return t
}
// generateImportsAttribute generates the imports attribute.
// These are a list of import directories to be added to the PYTHONPATH. In our
// case, the value we add is on Bazel sub-packages to be able to perform imports
// relative to the root project package.
func (t *targetBuilder) generateImportsAttribute() *targetBuilder {
p, _ := filepath.Rel(t.bzlPackage, t.pythonProjectRoot)
p = filepath.Clean(p)
if p == "." {
return t
}
t.imports = []string{p}
return t
}
// build returns the assembled *rule.Rule for the target.
func (t *targetBuilder) build() *rule.Rule {
r := rule.NewRule(t.kind, t.name)
if t.uuid != "" {
r.SetPrivateAttr(uuidKey, t.uuid)
}
if !t.srcs.Empty() {
r.SetAttr("srcs", t.srcs.Values())
}
if !t.visibility.Empty() {
r.SetAttr("visibility", t.visibility.Values())
}
if t.main != nil {
r.SetAttr("main", *t.main)
}
if t.imports != nil {
r.SetAttr("imports", t.imports)
}
if !t.deps.Empty() {
r.SetPrivateAttr(config.GazelleImportsKey, t.deps)
}
if t.testonly {
r.SetAttr("testonly", true)
}
r.SetPrivateAttr(resolvedDepsKey, t.resolvedDeps)
return r
}