forked from xkeyideal/glide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gb.go
71 lines (62 loc) · 1.49 KB
/
gb.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package gb
import (
"encoding/json"
"os"
"path/filepath"
"github.com/xkeyideal/glide/cfg"
"github.com/xkeyideal/glide/msg"
gpath "github.com/xkeyideal/glide/path"
"github.com/xkeyideal/glide/util"
)
// Has 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 in %s", gpath.StripBasepath(dir))
msg.Info("--> Parsing GB metadata...")
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
}