forked from Masterminds/glide
/
installer.go
756 lines (638 loc) · 20.5 KB
/
installer.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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
package repo
import (
"fmt"
"os"
"path/filepath"
"strings"
"sync"
"time"
"github.com/Masterminds/glide/cache"
"github.com/Masterminds/glide/cfg"
"github.com/Masterminds/glide/dependency"
"github.com/Masterminds/glide/importer"
"github.com/Masterminds/glide/msg"
gpath "github.com/Masterminds/glide/path"
"github.com/Masterminds/glide/util"
"github.com/Masterminds/semver"
"github.com/Masterminds/vcs"
"github.com/codegangsta/cli"
)
// Installer provides facilities for installing the repos in a config file.
type Installer struct {
// Force the install when certain normally stopping conditions occur.
Force bool
// Home is the location of cache
Home string
// Vendor contains the path to put the vendor packages
Vendor string
// Use a cache
UseCache bool
// Use Gopath to cache
UseCacheGopath bool
// Use Gopath as a source to read from
UseGopath bool
// UpdateVendored instructs the environment to update in a way that is friendly
// to packages that have been "vendored in" (e.g. are copies of source, not repos)
UpdateVendored bool
// DeleteUnused deletes packages that are unused, but found in the vendor dir.
DeleteUnused bool
// ResolveAllFiles enables a resolver that will examine the dependencies
// of every file of every package, rather than only following imported
// packages.
ResolveAllFiles bool
// Updated tracks the packages that have been remotely fetched.
Updated *UpdateTracker
}
func NewInstaller() *Installer {
i := &Installer{}
i.Updated = NewUpdateTracker()
return i
}
// VendorPath returns the path to the location to put vendor packages
func (i *Installer) VendorPath() string {
if i.Vendor != "" {
return i.Vendor
}
vp, err := gpath.Vendor()
if err != nil {
return filepath.FromSlash("./vendor")
}
return vp
}
// Install installs the dependencies from a Lockfile.
func (i *Installer) Install(lock *cfg.Lockfile, conf *cfg.Config) (*cfg.Config, error) {
cwd, err := gpath.Vendor()
if err != nil {
return conf, err
}
// Create a config setup based on the Lockfile data to process with
// existing commands.
newConf := &cfg.Config{}
newConf.Name = conf.Name
newConf.Imports = make(cfg.Dependencies, len(lock.Imports))
for k, v := range lock.Imports {
newConf.Imports[k] = &cfg.Dependency{
Name: v.Name,
Reference: v.Version,
Repository: v.Repository,
VcsType: v.VcsType,
Subpackages: v.Subpackages,
Arch: v.Arch,
Os: v.Os,
}
}
newConf.DevImports = make(cfg.Dependencies, len(lock.DevImports))
for k, v := range lock.DevImports {
newConf.DevImports[k] = &cfg.Dependency{
Name: v.Name,
Reference: v.Version,
Repository: v.Repository,
VcsType: v.VcsType,
Subpackages: v.Subpackages,
Arch: v.Arch,
Os: v.Os,
}
}
newConf.DeDupe()
if len(newConf.Imports) == 0 {
msg.Info("No dependencies found. Nothing installed.\n")
return newConf, nil
}
ConcurrentUpdate(newConf.Imports, cwd, i, newConf)
ConcurrentUpdate(newConf.DevImports, cwd, i, newConf)
return newConf, nil
}
// Checkout reads the config file and checks out all dependencies mentioned there.
//
// This is used when initializing an empty vendor directory, or when updating a
// vendor directory based on changed config.
func (i *Installer) Checkout(conf *cfg.Config, useDev bool) error {
dest := i.VendorPath()
if err := ConcurrentUpdate(conf.Imports, dest, i, conf); err != nil {
return err
}
if useDev {
return ConcurrentUpdate(conf.DevImports, dest, i, conf)
}
return nil
}
// Update updates all dependencies.
//
// It begins with the dependencies in the config file, but also resolves
// transitive dependencies. The returned lockfile has all of the dependencies
// listed, but the version reconciliation has not been done.
//
// In other words, all versions in the Lockfile will be empty.
func (i *Installer) Update(conf *cfg.Config) error {
base := "."
vpath := i.VendorPath()
ic := newImportCache()
m := &MissingPackageHandler{
destination: vpath,
cache: i.UseCache,
cacheGopath: i.UseCacheGopath,
useGopath: i.UseGopath,
home: i.Home,
force: i.Force,
updateVendored: i.UpdateVendored,
Config: conf,
Use: ic,
updated: i.Updated,
}
v := &VersionHandler{
Destination: vpath,
Use: ic,
Imported: make(map[string]bool),
Conflicts: make(map[string]bool),
Config: conf,
}
// Update imports
res, err := dependency.NewResolver(base)
if err != nil {
msg.Die("Failed to create a resolver: %s", err)
}
res.Config = conf
res.Handler = m
res.VersionHandler = v
res.ResolveAllFiles = i.ResolveAllFiles
msg.Info("Resolving imports")
_, err = allPackages(conf.Imports, res)
if err != nil {
msg.Die("Failed to retrieve a list of dependencies: %s", err)
}
if len(conf.DevImports) > 0 {
msg.Warn("dev imports not resolved.")
}
err = ConcurrentUpdate(conf.Imports, vpath, i, conf)
return err
}
// List resolves the complete dependency tree and returns a list of dependencies.
func (i *Installer) List(conf *cfg.Config) []*cfg.Dependency {
base := "."
vpath := i.VendorPath()
ic := newImportCache()
v := &VersionHandler{
Destination: vpath,
Use: ic,
Imported: make(map[string]bool),
Conflicts: make(map[string]bool),
Config: conf,
}
// Update imports
res, err := dependency.NewResolver(base)
if err != nil {
msg.Die("Failed to create a resolver: %s", err)
}
res.Config = conf
res.VersionHandler = v
res.ResolveAllFiles = i.ResolveAllFiles
msg.Info("Resolving imports")
_, err = allPackages(conf.Imports, res)
if err != nil {
msg.Die("Failed to retrieve a list of dependencies: %s", err)
}
if len(conf.DevImports) > 0 {
msg.Warn("dev imports not resolved.")
}
return conf.Imports
}
// ConcurrentUpdate takes a list of dependencies and updates in parallel.
func ConcurrentUpdate(deps []*cfg.Dependency, cwd string, i *Installer, c *cfg.Config) error {
done := make(chan struct{}, concurrentWorkers)
in := make(chan *cfg.Dependency, concurrentWorkers)
var wg sync.WaitGroup
var lock sync.Mutex
var returnErr error
msg.Info("Downloading dependencies. Please wait...")
for ii := 0; ii < concurrentWorkers; ii++ {
go func(ch <-chan *cfg.Dependency) {
for {
select {
case dep := <-ch:
var loc string
if dep.Repository != "" {
loc = dep.Repository
} else {
loc = "https://" + dep.Name
}
key, err := cache.Key(loc)
if err != nil {
msg.Die(err.Error())
}
cache.Lock(key)
dest := filepath.Join(i.VendorPath(), dep.Name)
if err := VcsUpdate(dep, dest, i.Home, i.UseCache, i.UseCacheGopath, i.UseGopath, i.Force, i.UpdateVendored, i.Updated); err != nil {
msg.Err("Update failed for %s: %s\n", dep.Name, err)
// Capture the error while making sure the concurrent
// operations don't step on each other.
lock.Lock()
if returnErr == nil {
returnErr = err
} else {
returnErr = cli.NewMultiError(returnErr, err)
}
lock.Unlock()
}
cache.Unlock(key)
wg.Done()
case <-done:
return
}
}
}(in)
}
for _, dep := range deps {
if !c.HasIgnore(dep.Name) {
wg.Add(1)
in <- dep
}
}
wg.Wait()
// Close goroutines setting the version
for ii := 0; ii < concurrentWorkers; ii++ {
done <- struct{}{}
}
return returnErr
}
// allPackages gets a list of all packages required to satisfy the given deps.
func allPackages(deps []*cfg.Dependency, res *dependency.Resolver) ([]string, error) {
if len(deps) == 0 {
return []string{}, nil
}
vdir, err := gpath.Vendor()
if err != nil {
return []string{}, err
}
vdir += string(os.PathSeparator)
ll, err := res.ResolveAll(deps)
if err != nil {
return []string{}, err
}
for i := 0; i < len(ll); i++ {
ll[i] = strings.TrimPrefix(ll[i], vdir)
}
return ll, nil
}
// MissingPackageHandler is a dependency.MissingPackageHandler.
//
// When a package is not found, this attempts to resolve and fetch.
//
// When a package is found on the GOPATH, this notifies the user.
type MissingPackageHandler struct {
destination string
home string
cache, cacheGopath, useGopath, force, updateVendored bool
Config *cfg.Config
Use *importCache
updated *UpdateTracker
}
// NotFound attempts to retrieve a package when not found in the local vendor/
// folder. It will attempt to get it from the remote location info.
func (m *MissingPackageHandler) NotFound(pkg string) (bool, error) {
root := util.GetRootFromPackage(pkg)
// Skip any references to the root package.
if root == m.Config.Name {
return false, nil
}
dest := filepath.Join(m.destination, root)
// This package may have been placed on the list to look for when it wasn't
// downloaded but it has since been downloaded before coming to this entry.
if _, err := os.Stat(dest); err == nil {
// Make sure the location contains files. It may be an empty directory.
empty, err := gpath.IsDirectoryEmpty(dest)
if err != nil {
return false, err
}
if empty {
msg.Warn("%s is an existing location with no files. Fetching a new copy of the dependency.", dest)
msg.Debug("Removing empty directory %s", dest)
err := os.RemoveAll(dest)
if err != nil {
msg.Debug("Installer error removing directory %s: %s", dest, err)
return false, err
}
} else {
msg.Debug("Found %s", dest)
return true, nil
}
}
msg.Info("Fetching %s into %s", pkg, m.destination)
d := m.Config.Imports.Get(root)
// If the dependency is nil it means the Config doesn't yet know about it.
if d == nil {
d, _ = m.Use.Get(root)
// We don't know about this dependency so we create a basic instance.
if d == nil {
d = &cfg.Dependency{Name: root}
}
m.Config.Imports = append(m.Config.Imports, d)
}
if err := VcsGet(d, dest, m.home, m.cache, m.cacheGopath, m.useGopath); err != nil {
return false, err
}
return true, nil
}
// OnGopath will either copy a package, already found in the GOPATH, to the
// vendor/ directory or download it from the internet. This is dependent if
// useGopath on the installer is set to true to copy from the GOPATH.
func (m *MissingPackageHandler) OnGopath(pkg string) (bool, error) {
// If useGopath is false, we fall back to the strategy of fetching from
// remote.
if !m.useGopath {
return m.NotFound(pkg)
}
root := util.GetRootFromPackage(pkg)
// Skip any references to the root package.
if root == m.Config.Name {
return false, nil
}
msg.Info("Copying package %s from the GOPATH.", pkg)
dest := filepath.Join(m.destination, pkg)
// Find package on Gopath
for _, gp := range gpath.Gopaths() {
src := filepath.Join(gp, pkg)
// FIXME: Should probably check if src is a dir or symlink.
if _, err := os.Stat(src); err == nil {
if err := os.MkdirAll(dest, os.ModeDir|0755); err != nil {
return false, err
}
if err := gpath.CopyDir(src, dest); err != nil {
return false, err
}
return true, nil
}
}
msg.Err("Could not locate %s on the GOPATH, though it was found before.", pkg)
return false, nil
}
// InVendor updates a package in the vendor/ directory to make sure the latest
// is available.
func (m *MissingPackageHandler) InVendor(pkg string) error {
root := util.GetRootFromPackage(pkg)
// Skip any references to the root package.
if root == m.Config.Name {
return nil
}
dest := filepath.Join(m.destination, root)
d := m.Config.Imports.Get(root)
// If the dependency is nil it means the Config doesn't yet know about it.
if d == nil {
d, _ = m.Use.Get(root)
// We don't know about this dependency so we create a basic instance.
if d == nil {
d = &cfg.Dependency{Name: root}
}
m.Config.Imports = append(m.Config.Imports, d)
}
if err := VcsUpdate(d, dest, m.home, m.cache, m.cacheGopath, m.useGopath, m.force, m.updateVendored, m.updated); err != nil {
return err
}
return nil
}
// VersionHandler handles setting the proper version in the VCS.
type VersionHandler struct {
// If Try to use the version here if we have one. This is a cache and will
// change over the course of setting versions.
Use *importCache
// Cache if importing scan has already occurred here.
Imported map[string]bool
// Where the packages exist to set the version on.
Destination string
Config *cfg.Config
// There's a problem where many sub-packages have been asked to set a version
// and you can end up with numerous conflict messages that are exactly the
// same. We are keeping track to only display them once.
// the parent pac
Conflicts map[string]bool
}
// Process imports dependencies for a package
func (d *VersionHandler) Process(pkg string) (e error) {
root := util.GetRootFromPackage(pkg)
// Skip any references to the root package.
if root == d.Config.Name {
return nil
}
// We have not tried to import, yet.
// Should we look in places other than the root of the project?
if d.Imported[root] == false {
d.Imported[root] = true
p := filepath.Join(d.Destination, root)
f, deps, err := importer.Import(p)
if f && err == nil {
for _, dep := range deps {
// The fist one wins. Would something smater than this be better?
exists, _ := d.Use.Get(dep.Name)
if exists == nil && (dep.Reference != "" || dep.Repository != "") {
d.Use.Add(dep.Name, dep, root)
}
}
} else if err != nil {
msg.Err("Unable to import from %s. Err: %s", root, err)
e = err
}
}
return
}
// SetVersion sets the version for a package. If that package version is already
// set it handles the case by:
// - keeping the already set version
// - proviting messaging about the version conflict
// TODO(mattfarina): The way version setting happens can be improved. Currently not optimal.
func (d *VersionHandler) SetVersion(pkg string) (e error) {
root := util.GetRootFromPackage(pkg)
// Skip any references to the root package.
if root == d.Config.Name {
return nil
}
v := d.Config.Imports.Get(root)
dep, req := d.Use.Get(root)
if dep != nil && v != nil {
if v.Reference == "" && dep.Reference != "" {
v.Reference = dep.Reference
// Clear the pin, if set, so the new version can be used.
v.Pin = ""
dep = v
} else if v.Reference != "" && dep.Reference != "" && v.Reference != dep.Reference {
dest := filepath.Join(d.Destination, filepath.FromSlash(v.Name))
dep = determineDependency(v, dep, dest, req)
} else {
dep = v
}
} else if v != nil {
dep = v
} else if dep != nil {
// We've got an imported dependency to use and don't already have a
// record of it. Append it to the Imports.
d.Config.Imports = append(d.Config.Imports, dep)
} else {
// If we've gotten here we don't have any depenency objects.
r, sp := util.NormalizeName(pkg)
dep = &cfg.Dependency{
Name: r,
}
if sp != "" {
dep.Subpackages = []string{sp}
}
d.Config.Imports = append(d.Config.Imports, dep)
}
err := VcsVersion(dep, d.Destination)
if err != nil {
msg.Warn("Unable to set version on %s to %s. Err: %s", root, dep.Reference, err)
e = err
}
return
}
func determineDependency(v, dep *cfg.Dependency, dest, req string) *cfg.Dependency {
repo, err := v.GetRepo(dest)
if err != nil {
singleWarn("Unable to access repo for %s\n", v.Name)
singleInfo("Keeping %s %s", v.Name, v.Reference)
return v
}
vIsRef := repo.IsReference(v.Reference)
depIsRef := repo.IsReference(dep.Reference)
// Both are references and they are different ones.
if vIsRef && depIsRef {
singleWarn("Conflict: %s rev is currently %s, but %s wants %s\n", v.Name, v.Reference, req, dep.Reference)
displayCommitInfo(repo, v)
displayCommitInfo(repo, dep)
singleInfo("Keeping %s %s", v.Name, v.Reference)
return v
} else if vIsRef {
// The current one is a reference and the suggestion is a SemVer constraint.
con, err := semver.NewConstraint(dep.Reference)
if err != nil {
singleWarn("Version issue for %s: '%s' is neither a reference or semantic version constraint\n", dep.Name, dep.Reference)
singleInfo("Keeping %s %s", v.Name, v.Reference)
return v
}
ver, err := semver.NewVersion(v.Reference)
if err != nil {
// The existing version is not a semantic version.
singleWarn("Conflict: %s version is %s, but also asked for %s\n", v.Name, v.Reference, dep.Reference)
displayCommitInfo(repo, v)
singleInfo("Keeping %s %s", v.Name, v.Reference)
return v
}
if con.Check(ver) {
singleInfo("Keeping %s %s because it fits constraint '%s'", v.Name, v.Reference, dep.Reference)
return v
}
singleWarn("Conflict: %s version is %s but does not meet constraint '%s'\n", v.Name, v.Reference, dep.Reference)
singleInfo("Keeping %s %s", v.Name, v.Reference)
return v
} else if depIsRef {
con, err := semver.NewConstraint(v.Reference)
if err != nil {
singleWarn("Version issue for %s: '%s' is neither a reference or semantic version constraint\n", v.Name, v.Reference)
singleInfo("Keeping %s %s", v.Name, v.Reference)
return v
}
ver, err := semver.NewVersion(dep.Reference)
if err != nil {
singleWarn("Conflict: %s version is %s, but also asked for %s\n", v.Name, v.Reference, dep.Reference)
displayCommitInfo(repo, dep)
singleInfo("Keeping %s %s", v.Name, v.Reference)
return v
}
if con.Check(ver) {
v.Reference = dep.Reference
singleInfo("Using %s %s because it fits constraint '%s'", v.Name, v.Reference, v.Reference)
return v
}
singleWarn("Conflict: %s semantic version constraint is %s but '%s' does not meet the constraint\n", v.Name, v.Reference, v.Reference)
singleInfo("Keeping %s %s", v.Name, v.Reference)
return v
}
// Neither is a vcs reference and both could be semantic version
// constraints that are different.
_, err = semver.NewConstraint(dep.Reference)
if err != nil {
// dd.Reference is not a reference or a valid constraint.
singleWarn("Version %s %s is not a reference or valid semantic version constraint\n", dep.Name, dep.Reference)
singleInfo("Keeping %s %s", v.Name, v.Reference)
return v
}
_, err = semver.NewConstraint(v.Reference)
if err != nil {
// existing.Reference is not a reference or a valid constraint.
// We really should never end up here.
singleWarn("Version %s %s is not a reference or valid semantic version constraint\n", v.Name, v.Reference)
v.Reference = dep.Reference
v.Pin = ""
singleInfo("Using %s %s because it is a valid version", v.Name, v.Reference)
return v
}
// Both versions are constraints. Try to merge them.
// If either comparison has an || skip merging. That's complicated.
ddor := strings.Index(dep.Reference, "||")
eor := strings.Index(v.Reference, "||")
if ddor == -1 && eor == -1 {
// Add the comparisons together.
newRef := v.Reference + ", " + dep.Reference
v.Reference = newRef
v.Pin = ""
singleInfo("Combining %s semantic version constraints %s and %s", v.Name, v.Reference, dep.Reference)
return v
}
singleWarn("Conflict: %s version is %s, but also asked for %s\n", v.Name, v.Reference, dep.Reference)
singleInfo("Keeping %s %s", v.Name, v.Reference)
return v
}
var warningMessage = make(map[string]bool)
var infoMessage = make(map[string]bool)
func singleWarn(ft string, v ...interface{}) {
m := fmt.Sprintf(ft, v...)
_, f := warningMessage[m]
if !f {
msg.Warn(m)
warningMessage[m] = true
}
}
func singleInfo(ft string, v ...interface{}) {
m := fmt.Sprintf(ft, v...)
_, f := infoMessage[m]
if !f {
msg.Info(m)
infoMessage[m] = true
}
}
type importCache struct {
cache map[string]*cfg.Dependency
from map[string]string
}
func newImportCache() *importCache {
return &importCache{
cache: make(map[string]*cfg.Dependency),
from: make(map[string]string),
}
}
func (i *importCache) Get(name string) (*cfg.Dependency, string) {
d, f := i.cache[name]
if f {
return d, i.from[name]
}
return nil, ""
}
func (i *importCache) Add(name string, dep *cfg.Dependency, root string) {
i.cache[name] = dep
i.from[name] = root
}
var displayCommitInfoPrefix = msg.Default.Color(msg.Green, "[INFO] ")
var displayCommitInfoTemplate = "%s reference %s:\n" +
displayCommitInfoPrefix + "- author: %s\n" +
displayCommitInfoPrefix + "- commit date: %s\n" +
displayCommitInfoPrefix + "- subject (first line): %s\n"
func displayCommitInfo(repo vcs.Repo, dep *cfg.Dependency) {
c, err := repo.CommitInfo(dep.Reference)
if err == nil {
singleInfo(displayCommitInfoTemplate, dep.Name, dep.Reference, c.Author, c.Date.Format(time.RFC1123Z), commitSubjectFirstLine(c.Message))
}
}
func commitSubjectFirstLine(sub string) string {
lines := strings.Split(sub, "\n")
if len(lines) <= 1 {
return sub
}
return lines[0]
}