blob: 5a07c622e136a55f76d203f7b4ab7fd834fc5251 [file] [log] [blame] [edit]
package gb
import (
"encoding/json"
"os"
"path/filepath"
"github.com/Masterminds/glide/cfg"
"github.com/Masterminds/glide/msg"
"github.com/Masterminds/glide/util"
)
// Returns true if this dir has a GB-flavored manifest file.
func Has(dir string) bool {
path := filepath.Join(dir, "vendor/manifest")
_, err := os.Stat(path)
return err == nil
}
// Parse parses a GB-flavored manifest file.
func Parse(dir string) ([]*cfg.Dependency, error) {
path := filepath.Join(dir, "vendor/manifest")
if fi, err := os.Stat(path); err != nil || fi.IsDir() {
return []*cfg.Dependency{}, nil
}
msg.Info("Found GB manifest file.\n")
buf := []*cfg.Dependency{}
file, err := os.Open(path)
if err != nil {
return buf, err
}
defer file.Close()
man := Manifest{}
dec := json.NewDecoder(file)
if err := dec.Decode(&man); err != nil {
return buf, err
}
seen := map[string]bool{}
for _, d := range man.Dependencies {
pkg, sub := util.NormalizeName(d.Importpath)
if _, ok := seen[pkg]; ok {
if len(sub) == 0 {
continue
}
for _, dep := range buf {
if dep.Name == pkg {
dep.Subpackages = append(dep.Subpackages, sub)
}
}
} else {
seen[pkg] = true
dep := &cfg.Dependency{
Name: pkg,
Reference: d.Revision,
Repository: d.Repository,
}
if len(sub) > 0 {
dep.Subpackages = []string{sub}
}
buf = append(buf, dep)
}
}
return buf, nil
}