/
config.go
115 lines (96 loc) · 3.08 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
/* Copyright 2018 The Bazel Authors. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package resolve
import (
"flag"
"log"
"strings"
"github.com/bazelbuild/bazel-gazelle/config"
"github.com/bazelbuild/bazel-gazelle/label"
"github.com/bazelbuild/bazel-gazelle/rule"
)
// FindRuleWithOverride searches the current configuration for user-specified
// dependency resolution overrides. Overrides specified later (in configuration
// files in deeper directories, or closer to the end of the file) are
// returned first. If no override is found, label.NoLabel is returned.
func FindRuleWithOverride(c *config.Config, imp ImportSpec, lang string) (label.Label, bool) {
rc := getResolveConfig(c)
for i := len(rc.overrides) - 1; i >= 0; i-- {
o := rc.overrides[i]
if o.matches(imp, lang) {
return o.dep, true
}
}
return label.NoLabel, false
}
type overrideSpec struct {
imp ImportSpec
lang string
dep label.Label
}
func (o overrideSpec) matches(imp ImportSpec, lang string) bool {
return imp.Lang == o.imp.Lang &&
imp.Imp == o.imp.Imp &&
(o.lang == "" || o.lang == lang)
}
type resolveConfig struct {
overrides []overrideSpec
}
const resolveName = "_resolve"
func getResolveConfig(c *config.Config) *resolveConfig {
return c.Exts[resolveName].(*resolveConfig)
}
type Configurer struct{}
func (_ *Configurer) RegisterFlags(fs *flag.FlagSet, cmd string, c *config.Config) {
c.Exts[resolveName] = &resolveConfig{}
}
func (_ *Configurer) CheckFlags(fs *flag.FlagSet, c *config.Config) error { return nil }
func (_ *Configurer) KnownDirectives() []string {
return []string{"resolve"}
}
func (_ *Configurer) Configure(c *config.Config, rel string, f *rule.File) {
rc := getResolveConfig(c)
rcCopy := &resolveConfig{
overrides: rc.overrides[:len(rc.overrides):len(rc.overrides)],
}
if f != nil {
for _, d := range f.Directives {
if d.Key == "resolve" {
parts := strings.Fields(d.Value)
o := overrideSpec{}
var lbl string
if len(parts) == 3 {
o.imp.Lang = parts[0]
o.imp.Imp = parts[1]
lbl = parts[2]
} else if len(parts) == 4 {
o.imp.Lang = parts[0]
o.lang = parts[1]
o.imp.Imp = parts[2]
lbl = parts[3]
} else {
log.Printf("could not parse directive: %s\n\texpected gazelle:resolve source-language [import-language] import-string label", d.Value)
continue
}
var err error
o.dep, err = label.Parse(lbl)
if err != nil {
log.Printf("gazelle:resolve %s: %v", d.Value, err)
continue
}
o.dep = o.dep.Abs("", rel)
rcCopy.overrides = append(rcCopy.overrides, o)
}
}
}
c.Exts[resolveName] = rcCopy
}