|
| 1 | +// Copyright 2017 The Go Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package gvt |
| 6 | + |
| 7 | +import ( |
| 8 | + "encoding/json" |
| 9 | + "io/ioutil" |
| 10 | + "log" |
| 11 | + "os" |
| 12 | + "path/filepath" |
| 13 | + |
| 14 | + "github.com/golang/dep" |
| 15 | + "github.com/golang/dep/internal/gps" |
| 16 | + "github.com/golang/dep/internal/importers/base" |
| 17 | + "github.com/pkg/errors" |
| 18 | +) |
| 19 | + |
| 20 | +const gvtPath = "vendor" + string(os.PathSeparator) + "manifest" |
| 21 | + |
| 22 | +// Importer imports gvt configuration into the dep configuration format. |
| 23 | +type Importer struct { |
| 24 | + *base.Importer |
| 25 | + gvtConfig gvtManifest |
| 26 | +} |
| 27 | + |
| 28 | +// NewImporter for gvt. It handles gb (gb-vendor) too as they share a common manifest file & format |
| 29 | +func NewImporter(logger *log.Logger, verbose bool, sm gps.SourceManager) *Importer { |
| 30 | + return &Importer{Importer: base.NewImporter(logger, verbose, sm)} |
| 31 | +} |
| 32 | + |
| 33 | +type gvtManifest struct { |
| 34 | + Deps []gvtPkg `json:"dependencies"` |
| 35 | +} |
| 36 | + |
| 37 | +type gvtPkg struct { |
| 38 | + ImportPath string |
| 39 | + Repository string |
| 40 | + Revision string |
| 41 | + Branch string |
| 42 | +} |
| 43 | + |
| 44 | +// Name of the importer. |
| 45 | +func (g *Importer) Name() string { |
| 46 | + return "gvt" |
| 47 | +} |
| 48 | + |
| 49 | +// HasDepMetadata checks if a directory contains config that the importer can handle. |
| 50 | +func (g *Importer) HasDepMetadata(dir string) bool { |
| 51 | + y := filepath.Join(dir, gvtPath) |
| 52 | + if _, err := os.Stat(y); err != nil { |
| 53 | + return false |
| 54 | + } |
| 55 | + |
| 56 | + return true |
| 57 | +} |
| 58 | + |
| 59 | +// Import the config found in the directory. |
| 60 | +func (g *Importer) Import(dir string, pr gps.ProjectRoot) (*dep.Manifest, *dep.Lock, error) { |
| 61 | + err := g.load(dir) |
| 62 | + if err != nil { |
| 63 | + return nil, nil, err |
| 64 | + } |
| 65 | + |
| 66 | + return g.convert(pr) |
| 67 | +} |
| 68 | + |
| 69 | +func (g *Importer) load(projectDir string) error { |
| 70 | + g.Logger.Println("Detected gb/gvt configuration files...") |
| 71 | + j := filepath.Join(projectDir, gvtPath) |
| 72 | + if g.Verbose { |
| 73 | + g.Logger.Printf(" Loading %s", j) |
| 74 | + } |
| 75 | + jb, err := ioutil.ReadFile(j) |
| 76 | + if err != nil { |
| 77 | + return errors.Wrapf(err, "unable to read %s", j) |
| 78 | + } |
| 79 | + err = json.Unmarshal(jb, &g.gvtConfig) |
| 80 | + if err != nil { |
| 81 | + return errors.Wrapf(err, "unable to parse %s", j) |
| 82 | + } |
| 83 | + |
| 84 | + return nil |
| 85 | +} |
| 86 | + |
| 87 | +func (g *Importer) convert(pr gps.ProjectRoot) (*dep.Manifest, *dep.Lock, error) { |
| 88 | + g.Logger.Println("Converting from vendor/manifest ...") |
| 89 | + |
| 90 | + packages := make([]base.ImportedPackage, 0, len(g.gvtConfig.Deps)) |
| 91 | + for _, pkg := range g.gvtConfig.Deps { |
| 92 | + // Validate |
| 93 | + if pkg.ImportPath == "" { |
| 94 | + err := errors.New("invalid gvt configuration, ImportPath is required") |
| 95 | + return nil, nil, err |
| 96 | + } |
| 97 | + |
| 98 | + if pkg.Revision == "" { |
| 99 | + err := errors.New("invalid gvt configuration, Revision is required") |
| 100 | + return nil, nil, err |
| 101 | + } |
| 102 | + |
| 103 | + var contstraintHint = "" |
| 104 | + if pkg.Branch == "HEAD" { |
| 105 | + // gb-vendor sets "branch" to "HEAD", if the package was feteched via -tag or -revision, |
| 106 | + // we pass the revision as the constraint hint |
| 107 | + contstraintHint = pkg.Revision |
| 108 | + } else if pkg.Branch != "master" { |
| 109 | + // both gvt & gb-vendor set "branch" to "master" unless a different branch was requested. |
| 110 | + // so it's not realy a constraint unless it's a different branch |
| 111 | + contstraintHint = pkg.Branch |
| 112 | + } |
| 113 | + |
| 114 | + ip := base.ImportedPackage{ |
| 115 | + Name: pkg.ImportPath, |
| 116 | + Source: pkg.Repository, |
| 117 | + LockHint: pkg.Revision, |
| 118 | + ConstraintHint: contstraintHint, |
| 119 | + } |
| 120 | + packages = append(packages, ip) |
| 121 | + } |
| 122 | + |
| 123 | + err := g.ImportPackages(packages, true) |
| 124 | + if err != nil { |
| 125 | + return nil, nil, err |
| 126 | + } |
| 127 | + |
| 128 | + return g.Manifest, g.Lock, nil |
| 129 | +} |
0 commit comments