github.com/MealCraft/glide@v0.13.4/gb/gb.go (about)

     1  package gb
     2  
     3  import (
     4  	"encoding/json"
     5  	"os"
     6  	"path/filepath"
     7  
     8  	"github.com/Masterminds/glide/cfg"
     9  	"github.com/Masterminds/glide/msg"
    10  	gpath "github.com/Masterminds/glide/path"
    11  	"github.com/Masterminds/glide/util"
    12  )
    13  
    14  // Has returns true if this dir has a GB-flavored manifest file.
    15  func Has(dir string) bool {
    16  	path := filepath.Join(dir, "vendor/manifest")
    17  	_, err := os.Stat(path)
    18  	return err == nil
    19  }
    20  
    21  // Parse parses a GB-flavored manifest file.
    22  func Parse(dir string) ([]*cfg.Dependency, error) {
    23  	path := filepath.Join(dir, "vendor/manifest")
    24  	if fi, err := os.Stat(path); err != nil || fi.IsDir() {
    25  		return []*cfg.Dependency{}, nil
    26  	}
    27  
    28  	msg.Info("Found GB manifest file in %s", gpath.StripBasepath(dir))
    29  	msg.Info("--> Parsing GB metadata...")
    30  	buf := []*cfg.Dependency{}
    31  	file, err := os.Open(path)
    32  	if err != nil {
    33  		return buf, err
    34  	}
    35  	defer file.Close()
    36  
    37  	man := Manifest{}
    38  
    39  	dec := json.NewDecoder(file)
    40  	if err := dec.Decode(&man); err != nil {
    41  		return buf, err
    42  	}
    43  
    44  	seen := map[string]bool{}
    45  
    46  	for _, d := range man.Dependencies {
    47  		pkg, sub := util.NormalizeName(d.Importpath)
    48  		if _, ok := seen[pkg]; ok {
    49  			if len(sub) == 0 {
    50  				continue
    51  			}
    52  			for _, dep := range buf {
    53  				if dep.Name == pkg {
    54  					dep.Subpackages = append(dep.Subpackages, sub)
    55  				}
    56  			}
    57  		} else {
    58  			seen[pkg] = true
    59  			dep := &cfg.Dependency{
    60  				Name:       pkg,
    61  				Reference:  d.Revision,
    62  				Repository: d.Repository,
    63  			}
    64  			if len(sub) > 0 {
    65  				dep.Subpackages = []string{sub}
    66  			}
    67  			buf = append(buf, dep)
    68  		}
    69  	}
    70  	return buf, nil
    71  }