github.com/tiagovtristao/plz@v13.4.0+incompatible/src/query/whatoutputs.go (about)

     1  package query
     2  
     3  import (
     4  	"github.com/thought-machine/please/src/core"
     5  	"fmt"
     6  	"path"
     7  )
     8  
     9  // WhatOutputs prints the target responsible for producing each of the provided files
    10  // The targets are printed in the same order as the provided files, separated by a newline
    11  // Use printFiles to additionally echo the files themselves (i.e. print <file> <target>)
    12  func WhatOutputs(graph *core.BuildGraph, files []string, printFiles bool) {
    13  	packageMap := filesToLabelMap(graph)
    14  	for _, f := range files {
    15  		if printFiles {
    16  			fmt.Printf("%s ", f)
    17  		}
    18  		if buildLabel, present := packageMap[f]; present {
    19  			fmt.Printf("%s\n", buildLabel)
    20  		} else {
    21  			// # TODO(dimitar): is this a good way to handle unknown files?
    22  			fmt.Println("Error: the file is not a product of any current target")
    23  		}
    24  	}
    25  }
    26  
    27  func filesToLabelMap(graph *core.BuildGraph) map[string]*core.BuildLabel {
    28  	packageMap := make(map[string]*core.BuildLabel)
    29  	for _, pkg := range graph.PackageMap() {
    30  		for _, target := range pkg.Outputs {
    31  			for _, output := range target.Outputs() {
    32  				artifactPath := path.Join(target.OutDir(), output)
    33  				packageMap[artifactPath] = &target.Label
    34  			}
    35  		}
    36  	}
    37  	return packageMap
    38  }