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

     1  package query
     2  
     3  import (
     4  	"github.com/thought-machine/please/src/core"
     5  	"fmt"
     6  	"sort"
     7  )
     8  
     9  // ReverseDeps For each input label, finds all targets which depend upon it.
    10  func ReverseDeps(state *core.BuildState, labels []core.BuildLabel) {
    11  
    12  	targets := GetRevDepsLabels(state, labels)
    13  
    14  	for _, target := range targets {
    15  		fmt.Printf("%s\n", target)
    16  	}
    17  }
    18  
    19  // GetRevDepsLabels returns a slice of build labels that are the reverse dependencies of the build labels being passed in
    20  func GetRevDepsLabels(state *core.BuildState, labels []core.BuildLabel) core.BuildLabels {
    21  	uniqueTargets := make(map[*core.BuildTarget]struct{})
    22  
    23  	graph := state.Graph
    24  	for _, label := range labels {
    25  		for _, child := range graph.PackageOrDie(label).AllChildren(graph.TargetOrDie(label)) {
    26  			for _, target := range graph.ReverseDependencies(child) {
    27  				if parent := target.Parent(graph); parent != nil {
    28  					uniqueTargets[parent] = struct{}{}
    29  				} else {
    30  					uniqueTargets[target] = struct{}{}
    31  				}
    32  			}
    33  		}
    34  	}
    35  	// Check for anything subincluding this guy too
    36  	for _, pkg := range graph.PackageMap() {
    37  		for _, label := range labels {
    38  			if pkg.HasSubinclude(label) {
    39  				for _, target := range pkg.AllTargets() {
    40  					uniqueTargets[target] = struct{}{}
    41  				}
    42  			}
    43  		}
    44  	}
    45  
    46  	targets := make(core.BuildLabels, 0, len(uniqueTargets))
    47  	for _, label := range labels {
    48  		delete(uniqueTargets, graph.TargetOrDie(label))
    49  	}
    50  	for target := range uniqueTargets {
    51  		if state.ShouldInclude(target) {
    52  			targets = append(targets, target.Label)
    53  		}
    54  	}
    55  	sort.Sort(targets)
    56  
    57  	return targets
    58  }