...

Source file src/edge-infra.dev/hack/build/rules/kustomize/gazelle/language/resolve.go

Documentation: edge-infra.dev/hack/build/rules/kustomize/gazelle/language

     1  package kustomize
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/bazelbuild/bazel-gazelle/config"
     7  	"github.com/bazelbuild/bazel-gazelle/label"
     8  	"github.com/bazelbuild/bazel-gazelle/repo"
     9  	"github.com/bazelbuild/bazel-gazelle/resolve"
    10  	"github.com/bazelbuild/bazel-gazelle/rule"
    11  
    12  	container "edge-infra.dev/hack/build/rules/container/gazelle/language"
    13  )
    14  
    15  // Imports returns a list of ImportSpecs that can be used to import the rule
    16  // r. This is used to populate RuleIndex.
    17  //
    18  // If nil is returned, the rule will not be indexed. If any non-nil slice is
    19  // returned, including an empty slice, the rule will be indexed.
    20  //
    21  // Kustomizations refer to each other by relative path, and only allow one
    22  // Kustomization per directory. This means that each Kustomization has a unique
    23  // path relative to the repository root, which can be normalized from any other
    24  // package.
    25  func (k *Kustomize) Imports(_ *config.Config, _ *rule.Rule, f *rule.File) []resolve.ImportSpec {
    26  	return []resolve.ImportSpec{{
    27  		Lang: kustomizationLangName,
    28  		Imp:  f.Pkg,
    29  	}}
    30  }
    31  
    32  func (k *Kustomize) Resolve(c *config.Config, ix *resolve.RuleIndex, _ *repo.RemoteCache, r *rule.Rule, imports interface{}, _ label.Label) {
    33  	specs, ok := imports.([]resolve.ImportSpec)
    34  
    35  	if !ok {
    36  		fmt.Println("failed to assert type ImportSpec from imports")
    37  		return
    38  	} else if len(specs) == 0 {
    39  		return
    40  	}
    41  
    42  	// Instantiate map for images attr after matching
    43  	images := make(map[string]string)
    44  
    45  	// TODO(aj185259): When creating these import specs, it'll probably be good
    46  	// to check the spec.Lang. If it's a `kustomization`, it can follow the below rules
    47  	// like label.New() being called with a blank repo. Other rules might require
    48  	// whatever the repo is that's passed.
    49  	depStrings := []string{}
    50  	for _, spec := range specs {
    51  		matches := ix.FindRulesByImportWithConfig(c, spec, spec.Lang)
    52  		switch len(matches) {
    53  		case 0:
    54  			fmt.Printf("no Rules matching %s were found\n", spec.Imp)
    55  			fmt.Printf("consider running 'bazel run %s %s' to resolve\n", gazellePath, spec.Imp)
    56  			continue
    57  		case 1:
    58  			m := matches[0]
    59  			rel := label.New("", m.Label.Pkg, m.Label.Name)
    60  
    61  			// If match is not a container_push rule, it should be added to deps
    62  			// TODO(wc185097_ncrvoyix): this should technically be handled by cross resolve
    63  			switch spec.Lang {
    64  			case container.ContainerLangName:
    65  				images[rel.String()] = spec.Imp
    66  				continue
    67  			case kustomizationLangName:
    68  				depStrings = append(depStrings, rel.String())
    69  				continue
    70  			}
    71  		default:
    72  			fmt.Println("somehow more than 1")
    73  		}
    74  
    75  		// TODO(wc185097_ncrvoyix): this is probably where we will need to handle naming conflicts
    76  		if len(matches) > 1 {
    77  			fmt.Printf("multiple possible matches for %s, ignoring", spec.Imp)
    78  			continue
    79  		}
    80  	}
    81  
    82  	if len(images) > 0 {
    83  		r.SetAttr(imgsAttr, images)
    84  	}
    85  	r.DelAttr("deps")
    86  	r.SetAttr("deps", depStrings)
    87  }
    88  

View as plain text