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
/
Copy pathinit.go
445 lines (386 loc) · 10.8 KB
/
init.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
// 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 (
"encoding/json"
"flag"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"github.com/pkg/errors"
"github.com/sdboyer/gps"
)
const initShortHelp = `Initialize a new project with manifest and lock files`
const initLongHelp = `
Initialize the project at filepath root by parsing its dependencies and writing
manifest and lock files. If root isn't specified, use the current directory.
The version of each dependency will reflect the current state of the GOPATH. If
a dependency doesn't exist in the GOPATH, it won't be written to the manifest,
but it will be solved-for, and will appear in the lock.
Note: init may use the network to solve the dependency graph.
Note: init does NOT vendor dependencies at the moment. See dep ensure.
`
func (cmd *initCommand) Name() string { return "init" }
func (cmd *initCommand) Args() string { return "[root]" }
func (cmd *initCommand) ShortHelp() string { return initShortHelp }
func (cmd *initCommand) LongHelp() string { return initLongHelp }
func (cmd *initCommand) Hidden() bool { return false }
func (cmd *initCommand) Register(fs *flag.FlagSet) {}
type initCommand struct{}
func (cmd *initCommand) Run(args []string) error {
if len(args) > 1 {
return errors.Errorf("too many args (%d)", len(args))
}
var root string
if len(args) <= 0 {
wd, err := os.Getwd()
if err != nil {
return err
}
root = wd
} else {
root = args[0]
}
mf := filepath.Join(root, manifestName)
lf := filepath.Join(root, lockName)
mok, err := isRegular(mf)
if err != nil {
return err
}
if mok {
return fmt.Errorf("manifest file %q already exists", mf)
}
// Manifest file does not exist.
lok, err := isRegular(lf)
if err != nil {
return err
}
if lok {
return fmt.Errorf("invalid state: manifest %q does not exist, but lock %q does", mf, lf)
}
cpr, err := depContext.splitAbsoluteProjectRoot(root)
if err != nil {
return errors.Wrap(err, "determineProjectRoot")
}
vlogf("Finding dependencies for %q...", cpr)
pkgT, err := gps.ListPackages(root, cpr)
if err != nil {
return errors.Wrap(err, "gps.ListPackages")
}
vlogf("Found %d dependencies.", len(pkgT.Packages))
sm, err := depContext.sourceManager()
if err != nil {
return errors.Wrap(err, "getSourceManager")
}
sm.UseDefaultSignalHandling()
defer sm.Release()
pd, err := getProjectData(pkgT, cpr, sm)
if err != nil {
return err
}
m := manifest{
Dependencies: pd.constraints,
}
// Make an initial lock from what knowledge we've collected about the
// versions on disk
l := lock{
P: make([]gps.LockedProject, 0, len(pd.ondisk)),
}
for pr, v := range pd.ondisk {
// That we have to chop off these path prefixes is a symptom of
// a problem in gps itself
pkgs := make([]string, 0, len(pd.dependencies[pr]))
prslash := string(pr) + "/"
for _, pkg := range pd.dependencies[pr] {
if pkg == string(pr) {
pkgs = append(pkgs, ".")
} else {
pkgs = append(pkgs, strings.TrimPrefix(pkg, prslash))
}
}
l.P = append(l.P, gps.NewLockedProject(
gps.ProjectIdentifier{ProjectRoot: pr}, v, pkgs),
)
}
sw := safeWriter{
root: root,
sm: sm,
m: &m,
}
if len(pd.notondisk) > 0 {
vlogf("Solving...")
params := gps.SolveParameters{
RootDir: root,
RootPackageTree: pkgT,
Manifest: &m,
Lock: &l,
}
if *verbose {
params.Trace = true
params.TraceLogger = log.New(os.Stderr, "", 0)
}
s, err := gps.Prepare(params, sm)
if err != nil {
return errors.Wrap(err, "prepare solver")
}
soln, err := s.Solve()
if err != nil {
handleAllTheFailuresOfTheWorld(err)
return err
}
sw.l = lockFromInterface(soln)
} else {
sw.l = &l
}
vlogf("Writing manifest and lock files.")
if err := sw.writeAllSafe(false); err != nil {
return errors.Wrap(err, "safe write of manifest and lock")
}
return nil
}
// contains checks if a array of strings contains a value
func contains(a []string, b string) bool {
for _, v := range a {
if b == v {
return true
}
}
return false
}
// isStdLib reports whether $GOROOT/src/path should be considered
// part of the standard distribution. For historical reasons we allow people to add
// their own code to $GOROOT instead of using $GOPATH, but we assume that
// code will start with a domain name (dot in the first element).
// This was loving taken from src/cmd/go/pkg.go in Go's code (isStandardImportPath).
func isStdLib(path string) bool {
i := strings.Index(path, "/")
if i < 0 {
i = len(path)
}
elem := path[:i]
return !strings.Contains(elem, ".")
}
// TODO solve failures can be really creative - we need to be similarly creative
// in handling them and informing the user appropriately
func handleAllTheFailuresOfTheWorld(err error) {
fmt.Printf("ouchie, solve error: %s", err)
}
func writeFile(path string, in json.Marshaler) error {
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()
b, err := in.MarshalJSON()
if err != nil {
return err
}
_, err = f.Write(b)
return err
}
func isRegular(name string) (bool, error) {
// TODO: lstat?
fi, err := os.Stat(name)
if os.IsNotExist(err) {
return false, nil
}
if err != nil {
return false, err
}
if fi.IsDir() {
return false, fmt.Errorf("%q is a directory, should be a file", name)
}
return true, nil
}
func isDir(name string) (bool, error) {
// TODO: lstat?
fi, err := os.Stat(name)
if os.IsNotExist(err) {
return false, nil
}
if err != nil {
return false, err
}
if !fi.IsDir() {
return false, fmt.Errorf("%q is not a directory", name)
}
return true, nil
}
func hasImportPathPrefix(s, prefix string) bool {
if s == prefix {
return true
}
return strings.HasPrefix(s, prefix+"/")
}
type projectData struct {
constraints gps.ProjectConstraints // constraints that could be found
dependencies map[gps.ProjectRoot][]string // all dependencies (imports) found by project root
notondisk map[gps.ProjectRoot]bool // projects that were not found on disk
ondisk map[gps.ProjectRoot]gps.Version // projects that were found on disk
}
func getProjectData(pkgT gps.PackageTree, cpr string, sm *gps.SourceMgr) (projectData, error) {
vlogf("Building dependency graph...")
constraints := make(gps.ProjectConstraints)
dependencies := make(map[gps.ProjectRoot][]string)
packages := make(map[string]bool)
notondisk := make(map[gps.ProjectRoot]bool)
ondisk := make(map[gps.ProjectRoot]gps.Version)
for _, v := range pkgT.Packages {
// TODO: Some errors maybe should not be skipped ;-)
if v.Err != nil {
vlogf("%v", v.Err)
continue
}
vlogf("Package %q, analyzing...", v.P.ImportPath)
for _, ip := range v.P.Imports {
if isStdLib(ip) {
continue
}
if hasImportPathPrefix(ip, cpr) {
// Don't analyze imports from the current project.
continue
}
pr, err := sm.DeduceProjectRoot(ip)
if err != nil {
return projectData{}, errors.Wrap(err, "sm.DeduceProjectRoot") // TODO: Skip and report ?
}
packages[ip] = true
if _, ok := dependencies[pr]; ok {
if !contains(dependencies[pr], ip) {
dependencies[pr] = append(dependencies[pr], ip)
}
continue
}
vlogf("Package %q has import %q, analyzing...", v.P.ImportPath, ip)
dependencies[pr] = []string{ip}
v, err := depContext.versionInWorkspace(pr)
if err != nil {
notondisk[pr] = true
vlogf("Could not determine version for %q, omitting from generated manifest", pr)
continue
}
ondisk[pr] = v
pp := gps.ProjectProperties{}
switch v.Type() {
case gps.IsBranch, gps.IsVersion, gps.IsRevision:
pp.Constraint = v
case gps.IsSemver:
c, _ := gps.NewSemverConstraint("^" + v.String())
pp.Constraint = c
}
constraints[pr] = pp
}
}
vlogf("Analyzing transitive imports...")
// Explore the packages we've found for transitive deps, either
// completing the lock or identifying (more) missing projects that we'll
// need to ask gps to solve for us.
colors := make(map[string]uint8)
const (
white uint8 = iota
grey
black
)
// cache of PackageTrees, so we don't parse projects more than once
ptrees := make(map[gps.ProjectRoot]gps.PackageTree)
// depth-first traverser
var dft func(string) error
dft = func(pkg string) error {
switch colors[pkg] {
case white:
vlogf("Analyzing %q...", pkg)
colors[pkg] = grey
pr, err := sm.DeduceProjectRoot(pkg)
if err != nil {
return errors.Wrap(err, "could not deduce project root for "+pkg)
}
// We already visited this project root earlier via some other
// pkg within it, and made the decision that it's not on disk.
// Respect that decision, and pop the stack.
if notondisk[pr] {
colors[pkg] = black
return nil
}
ptree, has := ptrees[pr]
if !has {
// It's fine if the root does not exist - it indicates that this
// project is not present in the workspace, and so we need to
// solve to deal with this dep.
r := filepath.Join(depContext.GOPATH, "src", string(pr))
_, err := os.Lstat(r)
if os.IsNotExist(err) {
colors[pkg] = black
notondisk[pr] = true
return nil
}
ptree, err = gps.ListPackages(r, string(pr))
if err != nil {
// Any error here other than an a nonexistent dir (which
// can't happen because we covered that case above) is
// probably critical, so bail out.
return errors.Wrap(err, "gps.ListPackages")
}
ptrees[pr] = ptree
}
rm := ptree.ExternalReach(false, false, nil)
reached, ok := rm[pkg]
if !ok {
colors[pkg] = black
// not on disk...
notondisk[pr] = true
return nil
}
if _, ok := dependencies[pr]; ok {
if !contains(dependencies[pr], pkg) {
dependencies[pr] = append(dependencies[pr], pkg)
}
} else {
dependencies[pr] = []string{pkg}
}
// project must be on disk at this point; question is
// whether we're first seeing it here, in the transitive
// exploration, or if it arose in the direct dep parts
if _, in := ondisk[pr]; !in {
v, err := depContext.versionInWorkspace(pr)
if err != nil {
colors[pkg] = black
notondisk[pr] = true
return nil
}
ondisk[pr] = v
}
// recurse
for _, rpkg := range reached {
if isStdLib(rpkg) {
continue
}
err := dft(rpkg)
if err != nil {
return err
}
}
colors[pkg] = black
case grey:
return fmt.Errorf("Import cycle detected on %s", pkg)
}
return nil
}
// run the depth-first traversal from the set of immediate external
// package imports we found in the current project
for pkg := range packages {
err := dft(pkg)
if err != nil {
return projectData{}, err // already errors.Wrap()'d internally
}
}
pd := projectData{
constraints: constraints,
dependencies: dependencies,
notondisk: notondisk,
ondisk: ondisk,
}
return pd, nil
}