Skip to content

Commit

Permalink
Add Godep support to analyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
sdboyer committed Apr 18, 2016
1 parent 0363313 commit c708bf7
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
22 changes: 20 additions & 2 deletions dependency/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"path/filepath"

"github.com/Masterminds/glide/cfg"
"github.com/Masterminds/glide/godep"
gpath "github.com/Masterminds/glide/path"
"github.com/sdboyer/vsolver"
)
Expand Down Expand Up @@ -42,8 +43,12 @@ func (a Analyzer) GetInfo(ctx build.Context, pn vsolver.ProjectName) (vsolver.Ma
}

// The happy path of finding both a glide manifest and lock file failed.
// Now, let us begin our descent into the filth, wherein we attempt to suss
// out just which circle of hell we're in.
// Now, we begin our descent, in which we attempt to divine exactly *which*
// circle of hell we're in.

// Try godep first
m, l, err = a.lookForGodep(root)

return nil, nil, fmt.Errorf("No usable project data found")
}

Expand Down Expand Up @@ -85,3 +90,16 @@ func (a Analyzer) lookForGlide(root string) (vsolver.Manifest, vsolver.Lock, err

return m, l, nil
}

func (a Analyzer) lookForGodep(root string) (vsolver.Manifest, vsolver.Lock, error) {
if !godep.Has(root) {
return nil, nil, notApplicable{}
}

d, l, err := godep.AsMetadataPair(root)
if err != nil {
return nil, nil, err
}

return &cfg.Config{ProjectName: root, Imports: d}, l, nil
}
46 changes: 46 additions & 0 deletions godep/godep.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,52 @@ func Parse(dir string) ([]*cfg.Dependency, error) {
return buf, nil
}

func AsMetadataPair(dir string) ([]*cfg.Dependency, *cfg.Lockfile, error) {
path := filepath.Join(dir, "Godeps/Godeps.json")
if _, err := os.Stat(path); err != nil {
return nil, nil, err
}

m := []*cfg.Dependency{}
l := &cfg.Lockfile{}
godeps := &Godeps{}

// Get a handle to the file.
file, err := os.Open(path)
if err != nil {
return nil, nil, err
}
defer file.Close()

dec := json.NewDecoder(file)
if err := dec.Decode(godeps); err != nil {
return nil, nil, err
}

seen := map[string]bool{}
for _, d := range godeps.Deps {
pkg, _ := util.NormalizeName(d.ImportPath)
if _, ok := seen[pkg]; !ok {
seen[pkg] = true

// Place no real *actual* constraint on the project; instead, we
// rely on vsolver using the 'preferred' version mechanism by
// working from the lock file. Without this, users would end up with
// the same mind-numbing diamond dep problems as currently exist.
// This approach does make for an uncomfortably wide possibility
// space where deps aren't getting what they expect, but that's
// better than just having the solver give up completely.
m = append(m, &cfg.Dependency{Name: pkg, Reference: "*"})
l.Imports = append(l.Imports, &cfg.Lock{Name: pkg, Version: d.Rev})

// TODO this fails to differentiate between dev and non-dev imports;
// need static analysis for that
}
}

return m, l, nil
}

// RemoveGodepSubpackages strips subpackages from a cfg.Config dependencies that
// contain "Godeps/_workspace/src" as part of the path.
func RemoveGodepSubpackages(c *cfg.Config) *cfg.Config {
Expand Down

0 comments on commit c708bf7

Please sign in to comment.