github.com/tiagovtristao/plz@v13.4.0+incompatible/tools/please_maven/maven/graph.go (about)

     1  package maven
     2  
     3  import (
     4  	"strings"
     5  )
     6  
     7  // A Graph is a minimal representation of the parts of `plz query graph`'s output that we care about.
     8  type Graph struct {
     9  	Packages       map[string]pkg `json:"packages"`
    10  	mavenToPackage map[string]string
    11  }
    12  
    13  type pkg struct {
    14  	Targets map[string]target `json:"targets"`
    15  }
    16  
    17  type target struct {
    18  	Labels []string `json:"labels,omitempty"`
    19  }
    20  
    21  // BuildMapping sets up the internal reverse mapping of maven id -> target.
    22  // It must be called once before anything else is.
    23  func (g *Graph) BuildMapping() {
    24  	g.mavenToPackage = map[string]string{}
    25  	for pkgName, pkg := range g.Packages {
    26  		for targetName, target := range pkg.Targets {
    27  			for _, label := range target.Labels {
    28  				if parts := strings.Split(label, ":"); len(parts) > 3 && parts[0] == "mvn" {
    29  					g.mavenToPackage[parts[1]+":"+parts[2]] = "//" + pkgName + ":" + targetName
    30  				}
    31  			}
    32  		}
    33  	}
    34  }
    35  
    36  // Needed returns true if we need a build rule for the given group ID / artifact ID.
    37  // It's false if one already exists in the current build files.
    38  func (g *Graph) Needed(groupID, artifactID string) bool {
    39  	if _, present := g.mavenToPackage[groupID+":"+artifactID]; present {
    40  		log.Debug("Dependency %s:%s not needed, already exists in graph", groupID, artifactID)
    41  		return false
    42  	}
    43  	return true
    44  }
    45  
    46  // Dep returns the dependency for a given groupID / artifact ID.
    47  // If it's not in the graph already it returns the label for a newly added target.
    48  func (g *Graph) Dep(groupID, artifactID string) string {
    49  	if dep, present := g.mavenToPackage[groupID+":"+artifactID]; present {
    50  		return dep
    51  	}
    52  	return ":" + artifactID
    53  }