-
Notifications
You must be signed in to change notification settings - Fork 8
/
config.go
211 lines (188 loc) · 5.24 KB
/
config.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package config
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/please-build/puku/kinds"
)
// KindConfig represents the configuration for a custom kind. See kinds.Kind for more information on how kinds work.
type KindConfig struct {
// NonGoSources indicates that this rule operates on non-go sources and we shouldn't attempt to parse them to
// generate the deps list. This is the case for rules like proto_library that still output a go package, but we
// shouldn't try to update their deps based on their sources.
NonGoSources bool `json:"nonGoSources"`
ProvidedDeps []string `json:"providedDeps"`
DefaultVisibility []string `json:"defaultVisibility"`
SrcsArg string `json:"srcsArg"`
}
func (kc *KindConfig) srcsArg() string {
if kc.SrcsArg == "" {
return "srcs"
}
return kc.SrcsArg
}
// Config represents a puku.json file discovered in the repo. These are loaded for each directory, and form a chain of
// configs all the way up to the root config. Configs at a deeper level in the file tree override values from configs at
// a shallower level. The shallower config file is stored in (*Config).base` and the methods on this struct will recurse
// into this base config where appropriate.
type Config struct {
base *Config
ThirdPartyDir string `json:"thirdPartyDir"`
PleasePath string `json:"pleasePath"`
KnownTargets map[string]string `json:"knownTargets"`
LibKinds map[string]*KindConfig `json:"libKinds"`
TestKinds map[string]*KindConfig `json:"testKinds"`
BinKinds map[string]*KindConfig `json:"binKinds"`
Stop *bool `json:"stop"`
EnsureSubincludes *bool `json:"ensureSubincludes"`
ExcludeBuiltinKinds []string `json:"excludeBuiltinKinds"`
}
// TODO we should reload this during plz watch so this probably needs to become a member of Update
// configs contains a cache of configs for a given directory
var configs = map[string]*Config{}
// ReadConfig builds up the config for a given path
func ReadConfig(dir string) (*Config, error) {
dir = filepath.Clean(dir)
var parts []string
if dir != "." {
parts = strings.Split(dir, "/")
}
c, err := readConfigs(nil, ".", parts)
if err != nil {
return nil, err
}
if c == nil {
return new(Config), nil
}
return c, nil
}
// readConfigs descends through the parts reading any config files it finds, building up the config chain.
func readConfigs(base *Config, path string, rest []string) (*Config, error) {
c, err := readOneConfig(path)
if err != nil {
return nil, err
}
if c != nil {
c.base = base
base = c
}
if len(rest) == 0 {
return base, nil
}
return readConfigs(base, filepath.Join(path, rest[0]), rest[1:])
}
// readOneConfig reads a config in a directory
func readOneConfig(path string) (*Config, error) {
if config, ok := configs[path]; ok {
return config, nil
}
f, err := os.ReadFile(filepath.Join(path, "puku.json"))
if err != nil {
if os.IsNotExist(err) {
c := new(Config)
configs[path] = c
return c, nil
}
return nil, err
}
c := new(Config)
if err := json.Unmarshal(f, c); err != nil {
return nil, fmt.Errorf("in %s: %w", path, err)
}
configs[path] = c
return c, nil
}
func (c *Config) GetThirdPartyDir() string {
if c.ThirdPartyDir != "" {
return c.ThirdPartyDir
}
if c.base != nil {
return c.base.GetThirdPartyDir()
}
return "third_party/go"
}
func (c *Config) GetStop() bool {
if c.Stop != nil {
return *c.Stop
}
return c.base != nil && c.base.GetStop()
}
func (c *Config) GetKnownTarget(importPath string) string {
if t, ok := c.KnownTargets[importPath]; ok {
return t
}
if c.base != nil {
return c.base.GetKnownTarget(importPath)
}
return ""
}
func (c *Config) GetPlzPath() string {
if c.PleasePath != "" {
return c.PleasePath
}
if c.base != nil {
return c.base.GetPlzPath()
}
return "plz"
}
func (c *Config) ShouldEnsureSubincludes() bool {
if c.EnsureSubincludes != nil {
return *c.EnsureSubincludes
}
if c.base != nil {
return c.base.ShouldEnsureSubincludes()
}
return true
}
func (c *Config) isExcludedDefaultKind(kind string) bool {
for _, c := range c.ExcludeBuiltinKinds {
if c == kind {
return true
}
}
if c.base == nil {
return false
}
return c.base.isExcludedDefaultKind(kind)
}
func (c *Config) GetKind(kind string) *kinds.Kind {
if k, ok := c.LibKinds[kind]; ok {
return &kinds.Kind{
Name: kind,
Type: kinds.Lib,
ProvidedDeps: k.ProvidedDeps,
SrcsAttr: k.srcsArg(),
DefaultVisibility: k.DefaultVisibility,
NonGoSources: k.NonGoSources,
}
}
if k, ok := c.TestKinds[kind]; ok {
return &kinds.Kind{
Name: kind,
Type: kinds.Test,
ProvidedDeps: k.ProvidedDeps,
SrcsAttr: k.srcsArg(),
NonGoSources: k.NonGoSources,
}
}
if k, ok := c.BinKinds[kind]; ok {
return &kinds.Kind{
Name: kind,
Type: kinds.Bin,
ProvidedDeps: k.ProvidedDeps,
SrcsAttr: k.srcsArg(),
NonGoSources: k.NonGoSources,
}
}
if c.base != nil {
return c.base.GetKind(kind)
}
if k, ok := kinds.DefaultKinds[kind]; ok {
if !c.isExcludedDefaultKind(kind) {
return k
}
}
return nil
}