github.com/kngu9/glide@v0.0.0-20160505135211-e73500c73591/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  	buf := []*cfg.Dependency{}
    30  	file, err := os.Open(path)
    31  	if err != nil {
    32  		return buf, err
    33  	}
    34  	defer file.Close()
    35  
    36  	man := Manifest{}
    37  
    38  	dec := json.NewDecoder(file)
    39  	if err := dec.Decode(&man); err != nil {
    40  		return buf, err
    41  	}
    42  
    43  	seen := map[string]bool{}
    44  
    45  	for _, d := range man.Dependencies {
    46  		pkg, sub := util.NormalizeName(d.Importpath)
    47  		if _, ok := seen[pkg]; ok {
    48  			if len(sub) == 0 {
    49  				continue
    50  			}
    51  			for _, dep := range buf {
    52  				if dep.Name == pkg {
    53  					dep.Subpackages = append(dep.Subpackages, sub)
    54  				}
    55  			}
    56  		} else {
    57  			seen[pkg] = true
    58  			dep := &cfg.Dependency{
    59  				Name:       pkg,
    60  				Reference:  d.Revision,
    61  				Repository: d.Repository,
    62  			}
    63  			if len(sub) > 0 {
    64  				dep.Subpackages = []string{sub}
    65  			}
    66  			buf = append(buf, dep)
    67  		}
    68  	}
    69  	return buf, nil
    70  }