/
whatoutputs.go
38 lines (35 loc) · 1.11 KB
/
whatoutputs.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
package query
import (
"core"
"fmt"
"path"
)
// WhatOutputs prints the target responsible for producing each of the provided files
// The targets are printed in the same order as the provided files, separated by a newline
// Use printFiles to additionally echo the files themselves (i.e. print <file> <target>)
func WhatOutputs(graph *core.BuildGraph, files []string, printFiles bool) {
packageMap := filesToLabelMap(graph)
for _, f := range files {
if printFiles {
fmt.Printf("%s ", f)
}
if buildLabel, present := packageMap[f]; present {
fmt.Printf("%s\n", buildLabel)
} else {
// # TODO(dimitar): is this a good way to handle unknown files?
fmt.Println("Error: the file is not a product of any current target")
}
}
}
func filesToLabelMap(graph *core.BuildGraph) map[string]*core.BuildLabel {
packageMap := make(map[string]*core.BuildLabel)
for _, pkg := range graph.PackageMap() {
for _, target := range pkg.Outputs {
for _, output := range target.Outputs() {
artifactPath := path.Join(target.OutDir(), output)
packageMap[artifactPath] = &target.Label
}
}
}
return packageMap
}