This repository has been archived by the owner on Sep 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support importing glide configuration
* init imports by default unless -skip-tools is specified * glide config in dependencies is taken into account during solve
- Loading branch information
Showing
58 changed files
with
12,639 additions
and
435 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.