-
Notifications
You must be signed in to change notification settings - Fork 4
/
build.go
47 lines (40 loc) · 1.26 KB
/
build.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
package please
import (
"encoding/json"
"strings"
)
// Build builds a target and returns the outputted files
func Build(plz, target string) ([]string, error) {
out, err := execPlease(plz, "build", "-p", target)
if err != nil {
return nil, err
}
return strings.Split(strings.TrimSpace(string(out)), "\n"), nil
}
// RecursivelyProvide queries the target, checking if it provides a different target for the given requirement, and
// returns that, repeating the operation if that target also provides a different target.
func RecursivelyProvide(plz, target, requires string) (string, error) {
out, err := execPlease(plz, "query", "print", "--json", "--field=provides", target)
if err != nil {
return "", err
}
// Returns a map of labels to fields. One of the fields in provides, which is a map of requests to targets, so we
// index res[target]["provides"][requires] to get the provided target
res := map[string]map[string]map[string]string{}
if err := json.Unmarshal(out, &res); err != nil {
return "", err
}
t, ok := res[target]
if !ok {
return target, nil
}
provides, ok := t["provides"]
if !ok {
return target, nil
}
ret := provides[requires]
if ret == "" || ret == target {
return target, nil
}
return RecursivelyProvide(plz, ret, requires)
}