-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions.go
99 lines (80 loc) · 2.06 KB
/
functions.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
package graph
import (
"fmt"
"github.com/hephbuild/heph/exprs"
"github.com/hephbuild/heph/specs"
"strconv"
"strings"
)
func (e *State) collectMatcher(t specs.Target, expr exprs.Expr) (specs.Matcher, bool, error) {
s, err := expr.MustPosArg(0)
if err != nil {
return nil, false, err
}
pkgMatcher, err := specs.ParseMatcherInPkg(t.Package.Path, s)
if err != nil {
return nil, false, err
}
var include, exclude []string
must := false
for _, arg := range expr.NamedArgs {
switch arg.Name {
case "must":
must = true
case "include", "exclude":
if arg.Name == "exclude" {
exclude = append(exclude, arg.Value)
} else {
include = append(include, arg.Value)
}
default:
return nil, false, fmt.Errorf("unhandled %v arg `%v`", expr.Function, arg.Name)
}
}
matcher, err := specs.MatcherFromIncludeExclude(t.Package.Path, include, exclude)
if err != nil {
return nil, false, err
}
if matcher != nil {
matcher = specs.AndNodeFactory(pkgMatcher, matcher)
} else {
matcher = pkgMatcher
}
return matcher, must, nil
}
func (e *State) collect(t *Target, expr exprs.Expr) ([]*Target, error) {
matcher, must, err := e.collectMatcher(t.Spec(), expr)
if err != nil {
return nil, err
}
targets, err := e.Targets().Filter(matcher)
if err != nil {
return nil, err
}
if must && targets.Len() == 0 {
return nil, fmt.Errorf("must match a target, found none")
}
return targets.Slice(), nil
}
func (e *State) findParent(t *Target, expr exprs.Expr) (*Target, error) {
selector, err := expr.MustPosArg(0)
if err != nil {
return nil, err
}
must, _ := strconv.ParseBool(expr.NamedArg("must"))
if !strings.HasPrefix(selector, ":") {
return nil, fmt.Errorf("must be a target selector, got `%v`", selector)
}
parts := strings.Split(t.Package.Path, "/")
for len(parts) > 0 {
t := e.targets.Find("//" + strings.Join(parts, "/") + selector)
if t != nil {
return t, nil
}
parts = parts[:len(parts)-1]
}
if must {
return nil, fmt.Errorf("not target matching %v found in parent of %v", selector, t.Addr)
}
return nil, nil
}