Skip to content
This repository has been archived by the owner on Sep 9, 2020. It is now read-only.

Commit

Permalink
Support importing glide configuration
Browse files Browse the repository at this point in the history
* init imports by default unless -skip-tools is specified
* glide config in dependencies is taken into account during solve
  • Loading branch information
carolynvs committed May 28, 2017
1 parent 3aff824 commit 22a534d
Show file tree
Hide file tree
Showing 58 changed files with 12,639 additions and 435 deletions.
9 changes: 8 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
name = "github.com/Masterminds/vcs"
version = "1.11.0"

[[constraint]]
branch = "v2"
name = "github.com/go-yaml/yaml"

[[constraint]]
branch = "master"
name = "github.com/pelletier/go-toml"
Expand Down
15 changes: 9 additions & 6 deletions analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@ import (

type Analyzer struct{}

func (a Analyzer) DeriveManifestAndLock(path string, n gps.ProjectRoot) (gps.Manifest, gps.Lock, error) {
// TODO: If we decide to support other tools manifest, this is where we would need
// to add that support.
func (a Analyzer) HasConfig(path string) bool {
mf := filepath.Join(path, ManifestName)
if fileOK, err := fs.IsRegular(mf); err != nil || !fileOK {
// Do not return an error, when does not exist.
fileOK, err := fs.IsRegular(mf)
return err == nil && fileOK
}

func (a Analyzer) DeriveManifestAndLock(path string, n gps.ProjectRoot) (gps.Manifest, gps.Lock, error) {
if !a.HasConfig(path) {
return nil, nil, nil
}
f, err := os.Open(mf)

f, err := os.Open(filepath.Join(path, ManifestName))
if err != nil {
return nil, nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/dep/ensure.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ func getProjectConstraint(arg string, sm gps.SourceManager) (gps.ProjectConstrai
arg = parts[0]
versionStr = parts[1]
}

// TODO: if we decide to keep equals.....

// split on colon if there is a network location
Expand Down
199 changes: 199 additions & 0 deletions cmd/dep/glide_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
"bytes"
"io/ioutil"
"path"
"path/filepath"

"github.com/Masterminds/semver"
"github.com/go-yaml/yaml"
"github.com/golang/dep"
fb "github.com/golang/dep/internal/feedback"
"github.com/golang/dep/internal/fs"
"github.com/golang/dep/internal/gps"
"github.com/pkg/errors"
)

type glideFiles struct {
yaml glideYaml
lock *glideLock
ctx *dep.Ctx
lockHints map[string]string
}

func newGlideFiles(ctx *dep.Ctx) *glideFiles {
return &glideFiles{
ctx: ctx,
lockHints: make(map[string]string),
}
}

type glideYaml struct {
Name string `yaml:"package"`
Ignores []string `yaml:"ignore"`
ExcludeDirs []string `yaml:"excludeDirs"`
Imports []glidePackage `yaml:"import"`
TestImports []glidePackage `yaml:"testImport"`
}

type glideLock struct {
Imports []glideLockedPackage `yaml:"imports"`
TestImports []glideLockedPackage `yaml:"testImports"`
}

type glidePackage struct {
Name string `yaml:"package"`
Reference string `yaml:"version"`
Repository string `yaml:"repo"`

// Unsupported fields that we will warn if used
Subpackages []string `yaml:"subpackages"`
OS string `yaml:"os"`
Arch string `yaml:"arch"`
}

type glideLockedPackage struct {
Name string `yaml:"name"`
Reference string `yaml:"version"`
Repository string `yaml:"repo"`
}

func (g *glideFiles) load(projectDir string) error {
g.ctx.Err.Println("Detected glide configuration files...")
y := filepath.Join(projectDir, glideYamlName)
if g.ctx.Loggers.Verbose {
g.ctx.Loggers.Err.Printf(" Loading %s", y)
}
yb, err := ioutil.ReadFile(y)
if err != nil {
return errors.Wrapf(err, "Unable to read %s", y)
}
err = yaml.Unmarshal(yb, &g.yaml)
if err != nil {
return errors.Wrapf(err, "Unable to parse %s", y)
}

l := filepath.Join(projectDir, glideLockName)
if exists, _ := fs.IsRegular(l); exists {
if g.ctx.Loggers.Verbose {
g.ctx.Loggers.Err.Printf(" Loading %s", l)
}
lb, err := ioutil.ReadFile(l)
if err != nil {
return errors.Wrapf(err, "Unable to read %s", l)
}
lock := &glideLock{}
err = yaml.Unmarshal(lb, lock)
if err != nil {
return errors.Wrapf(err, "Unable to parse %s", l)
}
g.lock = lock
}

return nil
}

func (g *glideFiles) convert(projectName string, sm gps.SourceManager) (*dep.Manifest, *dep.Lock, error) {
task := bytes.NewBufferString("Converting from glide.yaml")
if g.lock != nil {
task.WriteString(" and glide.lock")
}
task.WriteString("...")
g.ctx.Loggers.Err.Println(task)

manifest := &dep.Manifest{
Constraints: make(gps.ProjectConstraints),
}

for _, pkg := range g.yaml.Imports {
pc, err := g.buildProjectConstraint(pkg, sm)
if err != nil {
return nil, nil, err
}
manifest.Constraints[pc.Ident.ProjectRoot] = gps.ProjectProperties{Source: pc.Ident.Source, Constraint: pc.Constraint}
}
for _, pkg := range g.yaml.TestImports {
pc, err := g.buildProjectConstraint(pkg, sm)
if err != nil {
return nil, nil, err
}
manifest.Constraints[pc.Ident.ProjectRoot] = gps.ProjectProperties{Source: pc.Ident.Source, Constraint: pc.Constraint}
}

manifest.Ignored = append(manifest.Ignored, g.yaml.Ignores...)

if len(g.yaml.ExcludeDirs) > 0 {
if g.yaml.Name != "" && g.yaml.Name != projectName {
g.ctx.Loggers.Err.Printf(" Glide thinks the package is '%s' but dep thinks it is '%s', using dep's value.\n", g.yaml.Name, projectName)
}

for _, dir := range g.yaml.ExcludeDirs {
pkg := path.Join(projectName, dir)
manifest.Ignored = append(manifest.Ignored, pkg)
}
}

var lock *dep.Lock
if g.lock != nil {
lock = &dep.Lock{}

for _, pkg := range g.lock.Imports {
lp := g.buildLockedProject(pkg)
lock.P = append(lock.P, lp)
}
for _, pkg := range g.lock.TestImports {
lp := g.buildLockedProject(pkg)
lock.P = append(lock.P, lp)
}
}

return manifest, lock, nil
}

func (g *glideFiles) buildProjectConstraint(pkg glidePackage, sm gps.SourceManager) (pc gps.ProjectConstraint, err error) {
if pkg.Name == "" {
err = errors.New("Invalid glide configuration, package name is required")
return
}

if g.ctx.Loggers.Verbose {
if pkg.OS != "" {
g.ctx.Loggers.Err.Printf(" The %s package specified an os, but that isn't supported by dep yet, and will be ignored. See https://github.com/golang/dep/issues/291.\n", pkg.Name)
}
if pkg.Arch != "" {
g.ctx.Loggers.Err.Printf(" The %s package specified an arch, but that isn't supported by dep yet, and will be ignored. See https://github.com/golang/dep/issues/291.\n", pkg.Name)
}
}

pc.Ident = gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot(pkg.Name), Source: pkg.Repository}
pc.Constraint, err = deduceConstraint(pkg.Reference, pc.Ident, sm)
if err == nil {
g.lockHints[pkg.Name] = pkg.Reference
}

return
}

func (g *glideFiles) buildLockedProject(pkg glideLockedPackage) gps.LockedProject {
id := gps.ProjectIdentifier{
ProjectRoot: gps.ProjectRoot(pkg.Name),
Source: pkg.Repository,
}
revision := gps.Revision(pkg.Reference)

var version gps.Version
lh := g.lockHints[pkg.Name]
if _, err := semver.NewVersion(lh); err == nil {
version = gps.NewVersion(lh).Is(revision)
} else {
version = revision
}

feedback(version, id.ProjectRoot, fb.DepTypeImported, g.ctx)
return gps.NewLockedProject(id, version, nil)
}
Loading

0 comments on commit 22a534d

Please sign in to comment.