-
Notifications
You must be signed in to change notification settings - Fork 0
/
importer.go
211 lines (180 loc) · 5.77 KB
/
importer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// 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 glide
import (
"bytes"
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
"github.com/gwaylib/godep"
"github.com/gwaylib/godep/gps"
"github.com/gwaylib/godep/internal/fs"
"github.com/gwaylib/godep/internal/importers/base"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
)
const glideYamlName = "glide.yaml"
const glideLockName = "glide.lock"
// Importer imports glide configuration into the dep configuration format.
type Importer struct {
*base.Importer
glideConfig glideYaml
glideLock glideLock
lockFound bool
}
// NewImporter for glide.
func NewImporter(logger *log.Logger, verbose bool, sm gps.SourceManager) *Importer {
return &Importer{Importer: base.NewImporter(logger, verbose, sm)}
}
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"` // could contain a semver, tag or branch
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"`
Revision string `yaml:"version"`
Repository string `yaml:"repo"`
}
// Name of the importer.
func (g *Importer) Name() string {
return "glide"
}
// HasDepMetadata checks if a directory contains config that the importer can handle.
func (g *Importer) HasDepMetadata(dir string) bool {
// Only require glide.yaml, the lock is optional
y := filepath.Join(dir, glideYamlName)
if _, err := os.Stat(y); err != nil {
return false
}
return true
}
// Import the config found in the directory.
func (g *Importer) Import(dir string, pr gps.ProjectRoot) (*dep.Manifest, *dep.Lock, error) {
err := g.load(dir)
if err != nil {
return nil, nil, err
}
m, l := g.convert(pr)
return m, l, nil
}
// load the glide configuration files. Failure to load `glide.yaml` is considered
// unrecoverable and an error is returned for it. But if there is any error while trying
// to load the lock file, only a warning is logged.
func (g *Importer) load(projectDir string) error {
g.Logger.Println("Detected glide configuration files...")
y := filepath.Join(projectDir, glideYamlName)
if g.Verbose {
g.Logger.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.glideConfig)
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.Verbose {
g.Logger.Printf(" Loading %s", l)
}
lb, err := ioutil.ReadFile(l)
if err != nil {
g.Logger.Printf(" Warning: Ignoring lock file. Unable to read %s: %s\n", l, err)
return nil
}
lock := glideLock{}
err = yaml.Unmarshal(lb, &lock)
if err != nil {
g.Logger.Printf(" Warning: Ignoring lock file. Unable to parse %s: %s\n", l, err)
return nil
}
g.lockFound = true
g.glideLock = lock
}
return nil
}
// convert the glide configuration files into dep configuration files.
func (g *Importer) convert(pr gps.ProjectRoot) (*dep.Manifest, *dep.Lock) {
projectName := string(pr)
task := bytes.NewBufferString("Converting from glide.yaml")
if g.lockFound {
task.WriteString(" and glide.lock")
}
task.WriteString("...")
g.Logger.Println(task)
numPkgs := len(g.glideConfig.Imports) + len(g.glideConfig.TestImports) + len(g.glideLock.Imports) + len(g.glideLock.TestImports)
packages := make([]base.ImportedPackage, 0, numPkgs)
// Constraints
for _, pkg := range append(g.glideConfig.Imports, g.glideConfig.TestImports...) {
// Validate
if pkg.Name == "" {
g.Logger.Println(
" Warning: Skipping project. Invalid glide configuration, Name is required",
)
continue
}
// Warn
if g.Verbose {
if pkg.OS != "" {
g.Logger.Printf(" The %s package specified an os, but that isn't supported by dep yet, and will be ignored. See https://github.com/gwaylib/godep/issues/291.\n", pkg.Name)
}
if pkg.Arch != "" {
g.Logger.Printf(" The %s package specified an arch, but that isn't supported by dep yet, and will be ignored. See https://github.com/gwaylib/godep/issues/291.\n", pkg.Name)
}
}
ip := base.ImportedPackage{
Name: pkg.Name,
Source: pkg.Repository,
ConstraintHint: pkg.Reference,
}
packages = append(packages, ip)
}
// Locks
for _, pkg := range append(g.glideLock.Imports, g.glideLock.TestImports...) {
// Validate
if pkg.Name == "" {
g.Logger.Println(" Warning: Skipping project. Invalid glide lock, Name is required")
continue
}
ip := base.ImportedPackage{
Name: pkg.Name,
Source: pkg.Repository,
LockHint: pkg.Revision,
}
packages = append(packages, ip)
}
g.ImportPackages(packages, false)
// Ignores
g.Manifest.Ignored = append(g.Manifest.Ignored, g.glideConfig.Ignores...)
if len(g.glideConfig.ExcludeDirs) > 0 {
if g.glideConfig.Name != "" && g.glideConfig.Name != projectName {
g.Logger.Printf(" Glide thinks the package is '%s' but dep thinks it is '%s', using dep's value.\n", g.glideConfig.Name, projectName)
}
for _, dir := range g.glideConfig.ExcludeDirs {
pkg := path.Join(projectName, dir)
g.Manifest.Ignored = append(g.Manifest.Ignored, pkg)
}
}
return g.Manifest, g.Lock
}