|
| 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 main |
| 6 | + |
| 7 | +import ( |
| 8 | + "encoding/json" |
| 9 | + "io/ioutil" |
| 10 | + "log" |
| 11 | + "os" |
| 12 | + "path/filepath" |
| 13 | + |
| 14 | + "github.com/golang/dep" |
| 15 | + fb "github.com/golang/dep/internal/feedback" |
| 16 | + "github.com/golang/dep/internal/gps" |
| 17 | + "github.com/pkg/errors" |
| 18 | +) |
| 19 | + |
| 20 | +// gbImporter imports gb configuration into the dep configuration format. |
| 21 | +type gbImporter struct { |
| 22 | + manifest gbManifest |
| 23 | + logger *log.Logger |
| 24 | + verbose bool |
| 25 | + sm gps.SourceManager |
| 26 | +} |
| 27 | + |
| 28 | +func newGbImporter(logger *log.Logger, verbose bool, sm gps.SourceManager) *gbImporter { |
| 29 | + return &gbImporter{ |
| 30 | + logger: logger, |
| 31 | + verbose: verbose, |
| 32 | + sm: sm, |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +// gbManifest represents the manifest file for GB projects |
| 37 | +type gbManifest struct { |
| 38 | + Version int `json:"version"` |
| 39 | + Dependencies []gbDependency `json:"dependencies"` |
| 40 | +} |
| 41 | + |
| 42 | +type gbDependency struct { |
| 43 | + Importpath string `json:"importpath"` |
| 44 | + Repository string `json:"repository"` |
| 45 | + |
| 46 | + // All gb vendored dependencies have a specific revision |
| 47 | + Revision string `json:"revision"` |
| 48 | + |
| 49 | + // Branch may be HEAD or an actual branch. In the case of HEAD, that means |
| 50 | + // the user vendored a dependency by specifying a tag or a specific revision |
| 51 | + // which results in a detached HEAD |
| 52 | + Branch string `json:"branch"` |
| 53 | + |
| 54 | + // Path is used when the actual used package exists in a subpath |
| 55 | + // For importing purposes, this isn't necessary as dep will figure |
| 56 | + // out which packages are actually being used during resolving |
| 57 | + Path string `json:"path,omitempty"` |
| 58 | +} |
| 59 | + |
| 60 | +func (i *gbImporter) Name() string { |
| 61 | + return "gb" |
| 62 | +} |
| 63 | + |
| 64 | +func (i *gbImporter) HasDepMetadata(dir string) bool { |
| 65 | + // gb stores the manifest in the vendor tree |
| 66 | + var m = filepath.Join(dir, "vendor", "manifest") |
| 67 | + if _, err := os.Stat(m); err != nil { |
| 68 | + return false |
| 69 | + } |
| 70 | + |
| 71 | + return true |
| 72 | +} |
| 73 | + |
| 74 | +func (i *gbImporter) Import(dir string, pr gps.ProjectRoot) (*dep.Manifest, *dep.Lock, error) { |
| 75 | + err := i.load(dir) |
| 76 | + if err != nil { |
| 77 | + return nil, nil, err |
| 78 | + } |
| 79 | + |
| 80 | + return i.convert(pr) |
| 81 | +} |
| 82 | + |
| 83 | +// load the gb manifest |
| 84 | +func (i *gbImporter) load(projectDir string) error { |
| 85 | + i.logger.Println("Detected gb manifest file...") |
| 86 | + var err error |
| 87 | + var mf = filepath.Join(projectDir, "vendor", "manifest") |
| 88 | + if i.verbose { |
| 89 | + i.logger.Printf(" Loading %s", mf) |
| 90 | + } |
| 91 | + |
| 92 | + var buf []byte |
| 93 | + if buf, err = ioutil.ReadFile(mf); err != nil { |
| 94 | + return errors.Wrapf(err, "Unable to read %s", mf) |
| 95 | + } |
| 96 | + if err = json.Unmarshal(buf, &i.manifest); err != nil { |
| 97 | + return errors.Wrapf(err, "Unable to parse %s", mf) |
| 98 | + } |
| 99 | + |
| 100 | + return nil |
| 101 | +} |
| 102 | + |
| 103 | +// convert the gb manifest into dep configuration files. |
| 104 | +func (i *gbImporter) convert(pr gps.ProjectRoot) (*dep.Manifest, *dep.Lock, error) { |
| 105 | + i.logger.Println("Converting from gb manifest...") |
| 106 | + |
| 107 | + manifest := &dep.Manifest{ |
| 108 | + Constraints: make(gps.ProjectConstraints), |
| 109 | + } |
| 110 | + |
| 111 | + lock := &dep.Lock{} |
| 112 | + |
| 113 | + for _, pkg := range i.manifest.Dependencies { |
| 114 | + pc, lp, err := i.convertOne(pkg) |
| 115 | + if err != nil { |
| 116 | + return nil, nil, err |
| 117 | + } |
| 118 | + manifest.Constraints[pc.Ident.ProjectRoot] = gps.ProjectProperties{Source: pc.Ident.Source, Constraint: pc.Constraint} |
| 119 | + lock.P = append(lock.P, lp) |
| 120 | + } |
| 121 | + |
| 122 | + return manifest, lock, nil |
| 123 | +} |
| 124 | + |
| 125 | +func (i *gbImporter) convertOne(pkg gbDependency) (pc gps.ProjectConstraint, lp gps.LockedProject, err error) { |
| 126 | + if pkg.Importpath == "" { |
| 127 | + err = errors.New("Invalid gb configuration, package import path is required") |
| 128 | + return |
| 129 | + } |
| 130 | + |
| 131 | + /* |
| 132 | + gb's vendor plugin (gb vendor), which manages the vendor tree and manifest |
| 133 | + file, supports fetching by a specific tag or revision, but if you specify |
| 134 | + either of those it's a detached checkout and the "branch" field is HEAD. |
| 135 | + The only time the "branch" field is not "HEAD" is if you do not specify a |
| 136 | + tag or revision, otherwise it's either "master" or the value of the -branch |
| 137 | + flag |
| 138 | +
|
| 139 | + This means that, generally, the only possible "constraint" we can really specify is |
| 140 | + the branch name if the branch name is not HEAD. Otherwise, it's a specific revision. |
| 141 | +
|
| 142 | + However, if we can infer a tag that points to the revision or the branch, we may be able |
| 143 | + to use that as the constraint |
| 144 | + */ |
| 145 | + |
| 146 | + pc.Ident = gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot(pkg.Importpath), Source: pkg.Repository} |
| 147 | + |
| 148 | + // The default constraint is just a revision |
| 149 | + var revision = gps.Revision(pkg.Revision) |
| 150 | + |
| 151 | + // See if we can get a version from that constraint |
| 152 | + version, err := lookupVersionForLockedProject(pc.Ident, revision, revision, i.sm) |
| 153 | + if err != nil { |
| 154 | + return |
| 155 | + } |
| 156 | + |
| 157 | + // And now try to infer a constraint from the returned version |
| 158 | + pc.Constraint, err = i.sm.InferConstraint(version.String(), pc.Ident) |
| 159 | + if err != nil { |
| 160 | + return |
| 161 | + } |
| 162 | + |
| 163 | + lp = gps.NewLockedProject(pc.Ident, version, nil) |
| 164 | + |
| 165 | + fb.NewLockedProjectFeedback(lp, fb.DepTypeImported).LogFeedback(i.logger) |
| 166 | + fb.NewConstraintFeedback(pc, fb.DepTypeImported).LogFeedback(i.logger) |
| 167 | + |
| 168 | + return |
| 169 | +} |
0 commit comments