github.com/fibonacci1729/glide@v0.0.0-20160513190140-d9640dc62d0f/godep/godep.go (about) 1 // Package godep provides basic importing of Godep dependencies. 2 // 3 // This is not a complete implementation of Godep. 4 package godep 5 6 import ( 7 "encoding/json" 8 "os" 9 "path/filepath" 10 "strings" 11 12 "github.com/Masterminds/glide/cfg" 13 "github.com/Masterminds/glide/msg" 14 gpath "github.com/Masterminds/glide/path" 15 "github.com/Masterminds/glide/util" 16 ) 17 18 // This file contains commands for working with Godep. 19 20 // The Godeps struct from Godep. 21 // 22 // https://raw.githubusercontent.com/tools/godep/master/dep.go 23 // 24 // We had to copy this because it's in the package main for Godep. 25 type Godeps struct { 26 ImportPath string 27 GoVersion string 28 Packages []string `json:",omitempty"` // Arguments to save, if any. 29 Deps []Dependency 30 31 outerRoot string 32 } 33 34 // Dependency is a modified version of Godep's Dependency struct. 35 // It drops all of the unexported fields. 36 type Dependency struct { 37 ImportPath string 38 Comment string `json:",omitempty"` // Description of commit, if present. 39 Rev string // VCS-specific commit ID. 40 } 41 42 // Has is a command to detect if a package contains a Godeps.json file. 43 func Has(dir string) bool { 44 path := filepath.Join(dir, "Godeps/Godeps.json") 45 _, err := os.Stat(path) 46 return err == nil 47 } 48 49 // Parse parses a Godep's Godeps file. 50 // 51 // It returns the contents as a dependency array. 52 func Parse(dir string) ([]*cfg.Dependency, error) { 53 path := filepath.Join(dir, "Godeps/Godeps.json") 54 if _, err := os.Stat(path); err != nil { 55 return []*cfg.Dependency{}, nil 56 } 57 msg.Info("Found Godeps.json file in %s", gpath.StripBasepath(dir)) 58 59 buf := []*cfg.Dependency{} 60 61 godeps := &Godeps{} 62 63 // Get a handle to the file. 64 file, err := os.Open(path) 65 if err != nil { 66 return buf, err 67 } 68 defer file.Close() 69 70 dec := json.NewDecoder(file) 71 if err := dec.Decode(godeps); err != nil { 72 return buf, err 73 } 74 75 seen := map[string]bool{} 76 for _, d := range godeps.Deps { 77 pkg, sub := util.NormalizeName(d.ImportPath) 78 if _, ok := seen[pkg]; ok { 79 if len(sub) == 0 { 80 continue 81 } 82 // Modify existing dep with additional subpackages. 83 for _, dep := range buf { 84 if dep.Name == pkg { 85 dep.Subpackages = append(dep.Subpackages, sub) 86 } 87 } 88 } else { 89 seen[pkg] = true 90 dep := &cfg.Dependency{Name: pkg, Reference: d.Rev} 91 if sub != "" { 92 dep.Subpackages = []string{sub} 93 } 94 buf = append(buf, dep) 95 } 96 } 97 98 return buf, nil 99 } 100 101 // RemoveGodepSubpackages strips subpackages from a cfg.Config dependencies that 102 // contain "Godeps/_workspace/src" as part of the path. 103 func RemoveGodepSubpackages(c *cfg.Config) *cfg.Config { 104 for _, d := range c.Imports { 105 n := []string{} 106 for _, v := range d.Subpackages { 107 if !strings.HasPrefix(v, "Godeps/_workspace/src") { 108 n = append(n, v) 109 } 110 } 111 d.Subpackages = n 112 } 113 114 for _, d := range c.DevImports { 115 n := []string{} 116 for _, v := range d.Subpackages { 117 if !strings.HasPrefix(v, "Godeps/_workspace/src") { 118 n = append(n, v) 119 } 120 } 121 d.Subpackages = n 122 } 123 124 return c 125 }