-
Notifications
You must be signed in to change notification settings - Fork 21
/
configure.go
214 lines (192 loc) · 6.87 KB
/
configure.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
212
213
214
package gazelle
import (
"flag"
"fmt"
"log"
"os"
"path"
"strings"
. "aspect.build/cli/gazelle/common/log"
"github.com/bazelbuild/bazel-gazelle/config"
"github.com/bazelbuild/bazel-gazelle/label"
"github.com/bazelbuild/bazel-gazelle/rule"
)
// Configurer satisfies the config.Configurer interface. It's the
// language-specific configuration extension.
type Configurer struct {
lang *typeScriptLang
}
func NewConfigurer(lang *typeScriptLang) config.Configurer {
return &Configurer{
lang: lang,
}
}
// RegisterFlags registers command-line flags used by the extension. This
// method is called once with the root configuration when Gazelle
// starts. RegisterFlags may set an initial values in Config.Exts. When flags
// are set, they should modify these values.
func (ts *Configurer) RegisterFlags(fs *flag.FlagSet, cmd string, c *config.Config) {}
// CheckFlags validates the configuration after command line flags are parsed.
// This is called once with the root configuration when Gazelle starts.
// CheckFlags may set default values in flags or make implied changes.
func (ts *Configurer) CheckFlags(fs *flag.FlagSet, c *config.Config) error {
return nil
}
// KnownDirectives returns a list of directive keys that this Configurer can
// interpret. Gazelle prints errors for directives that are not recoginized by
// any Configurer.
func (ts *Configurer) KnownDirectives() []string {
return []string{
Directive_TypeScriptExtension,
Directive_TypeScriptProtoExtension,
Directive_GenerationMode,
Directive_Lockfile,
Directive_IgnoreImports,
Directive_Resolve,
Directive_ValidateImportStatements,
Directive_LibraryNamingConvention,
Directive_TestsNamingConvention,
Directive_NpmPackageNameConvention,
Directive_LibraryFiles,
Directive_TestFiles,
Directive_CustomTargetFiles,
Directive_CustomTargetTestFiles,
}
}
// Configure modifies the configuration using directives and other information
// extracted from a build file. Configure is called in each directory.
//
// c is the configuration for the current directory. It starts out as a copy
// of the configuration for the parent directory.
//
// rel is the slash-separated relative path from the repository root to
// the current directory. It is "" for the root directory itself.
//
// f is the build file for the current directory or nil if there is no
// existing build file.
func (ts *Configurer) Configure(c *config.Config, rel string, f *rule.File) {
BazelLog.Tracef("Configure %s", rel)
// Create the root config.
if cfg, exists := c.Exts[LanguageName]; !exists {
c.Exts[LanguageName] = newRootConfig()
} else {
c.Exts[LanguageName] = cfg.(*JsGazelleConfig).NewChild(rel)
}
if f != nil {
ts.readDirectives(c, rel, f)
// Read configurations relative to the current BUILD file.
ts.readConfigurations(c, rel)
}
ts.lang.gitignore.CollectIgnoreFiles(c, rel)
}
func (ts *Configurer) readConfigurations(c *config.Config, rel string) {
config := c.Exts[LanguageName].(*JsGazelleConfig)
// pnpm
lockfilePath := path.Join(c.RepoRoot, rel, config.PnpmLockfile())
if _, err := os.Stat(lockfilePath); err == nil {
ts.lang.addPnpmLockfile(config, c.RepoName, c.RepoRoot, path.Join(rel, config.PnpmLockfile()))
}
// tsconfig
configPath := path.Join(c.RepoRoot, rel, config.tsconfigName)
if _, err := os.Stat(configPath); err == nil {
ts.lang.tsconfig.AddTsConfigFile(c.RepoRoot, rel, config.tsconfigName)
}
}
func (ts *Configurer) readDirectives(c *config.Config, rel string, f *rule.File) {
config := c.Exts[LanguageName].(*JsGazelleConfig)
for _, d := range f.Directives {
value := strings.TrimSpace(d.Value)
switch d.Key {
case "exclude":
// We record the exclude directive since we do manual tree traversal of subdirs.
config.AddExcludedPattern(value)
case Directive_TypeScriptExtension:
switch d.Value {
case "enabled":
config.SetGenerationEnabled(true)
case "disabled":
config.SetGenerationEnabled(false)
default:
err := fmt.Errorf("invalid value for directive %q: %s: possible values are enabled/disabled",
Directive_TypeScriptExtension, d.Value)
log.Fatal(err)
}
case Directive_TypeScriptProtoExtension:
switch d.Value {
case "enabled":
config.SetProtoGenerationEnabled(true)
case "disabled":
config.SetProtoGenerationEnabled(false)
default:
err := fmt.Errorf("invalid value for directive %q: %s: possible values are enabled/disabled",
Directive_TypeScriptProtoExtension, d.Value)
log.Fatal(err)
}
case Directive_GenerationMode:
mode := GenerationModeType(strings.TrimSpace(d.Value))
switch mode {
case GenerationModeDirectory:
config.SetGenerationMode(mode)
case GenerationModeNone:
config.SetGenerationMode(mode)
default:
log.Fatalf("invalid value for directive %q: %s", Directive_GenerationMode, d.Value)
}
case Directive_Lockfile:
config.SetPnpmLockfile(value)
case Directive_IgnoreImports:
config.AddIgnoredImport(strings.TrimSpace(value))
case Directive_Resolve:
globTarget := strings.Split(value, " ")
if len(globTarget) != 2 {
err := fmt.Errorf("invalid value for directive %q: %s: value must be filename/glob + label",
Directive_Resolve, d.Value)
log.Fatal(err)
}
label, labelErr := label.Parse(strings.TrimSpace(globTarget[1]))
if labelErr != nil {
err := fmt.Errorf("invalid label for directive %q: %s",
Directive_Resolve, label)
log.Fatal(err)
}
config.AddResolve(strings.TrimSpace(globTarget[0]), &label)
case Directive_ValidateImportStatements:
switch value {
case "error":
config.SetValidateImportStatements(ValidationError)
case "warn":
config.SetValidateImportStatements(ValidationWarn)
case "off":
config.SetValidateImportStatements(ValidationOff)
default:
log.Fatalf("invalid value for directive %q: %s", Directive_ValidateImportStatements, d.Value)
}
case Directive_LibraryNamingConvention:
config.SetLibraryNamingConvention(value)
case Directive_TestsNamingConvention:
config.SetTestsNamingLibraryConvention(value)
case Directive_NpmPackageNameConvention:
config.SetNpmPackageNamingConvention(value)
case Directive_LibraryFiles:
config.AddTargetGlob(DefaultLibraryName, value, false)
case Directive_TestFiles:
config.AddTargetGlob(DefaultTestsName, value, true)
case Directive_CustomTargetFiles:
groupGlob := strings.Split(value, " ")
if len(groupGlob) != 2 {
err := fmt.Errorf("invalid value for directive %q: %s: value must be group + glob",
Directive_CustomTargetFiles, d.Value)
log.Fatal(err)
}
config.AddTargetGlob(groupGlob[0], groupGlob[1], false)
case Directive_CustomTargetTestFiles:
groupGlob := strings.Split(value, " ")
if len(groupGlob) != 2 {
err := fmt.Errorf("invalid value for directive %q: %s: value must be group + glob",
Directive_CustomTargetTestFiles, d.Value)
log.Fatal(err)
}
config.AddTargetGlob(groupGlob[0], groupGlob[1], true)
}
}
}