From 8bedb085c642f0ef8d4e3d732c198aa42775f196 Mon Sep 17 00:00:00 2001 From: mstrong Date: Sun, 1 Oct 2017 13:18:08 -0500 Subject: [PATCH] Prefetch and log output during InitializeRootManifestAndLock --- cmd/dep/init.go | 2 +- cmd/dep/root_analyzer.go | 71 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 71 insertions(+), 2 deletions(-) diff --git a/cmd/dep/init.go b/cmd/dep/init.go index 4cebf4ae34..2cc6cbc5be 100644 --- a/cmd/dep/init.go +++ b/cmd/dep/init.go @@ -146,7 +146,7 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error { // Initialize with imported data, then fill in the gaps using the GOPATH rootAnalyzer := newRootAnalyzer(cmd.skipTools, ctx, directDeps, sm) - p.Manifest, p.Lock, err = rootAnalyzer.InitializeRootManifestAndLock(root, p.ImportRoot) + p.Manifest, p.Lock, err = rootAnalyzer.InitializeRootManifestAndLock(root, p.ImportRoot, pkgT) if err != nil { return err } diff --git a/cmd/dep/root_analyzer.go b/cmd/dep/root_analyzer.go index c300eadad8..04f7ca8b20 100644 --- a/cmd/dep/root_analyzer.go +++ b/cmd/dep/root_analyzer.go @@ -5,12 +5,15 @@ package main import ( + "fmt" "io/ioutil" "log" + "strings" "github.com/golang/dep" fb "github.com/golang/dep/internal/feedback" "github.com/golang/dep/internal/gps" + "github.com/golang/dep/internal/gps/pkgtree" "github.com/golang/dep/internal/importers" ) @@ -35,7 +38,7 @@ func newRootAnalyzer(skipTools bool, ctx *dep.Ctx, directDeps map[string]bool, s } } -func (a *rootAnalyzer) InitializeRootManifestAndLock(dir string, pr gps.ProjectRoot) (rootM *dep.Manifest, rootL *dep.Lock, err error) { +func (a *rootAnalyzer) InitializeRootManifestAndLock(dir string, pr gps.ProjectRoot, pkgT pkgtree.PackageTree) (rootM *dep.Manifest, rootL *dep.Lock, err error) { if !a.skipTools { rootM, rootL, err = a.importManifestAndLock(dir, pr, false) if err != nil { @@ -45,6 +48,7 @@ func (a *rootAnalyzer) InitializeRootManifestAndLock(dir string, pr gps.ProjectR if rootM == nil { rootM = dep.NewManifest() + a.cacheDeps(pr, pkgT) } if rootL == nil { rootL = &dep.Lock{} @@ -53,6 +57,71 @@ func (a *rootAnalyzer) InitializeRootManifestAndLock(dir string, pr gps.ProjectR return } +func (a *rootAnalyzer) cacheDeps(pr gps.ProjectRoot, pkgT pkgtree.PackageTree) { + packages := make(map[string]bool) + dependencies := make(map[gps.ProjectRoot][]string) + logger := a.ctx.Err + + syncDep := func(pr gps.ProjectRoot, sm gps.SourceManager) { + message := fmt.Sprintf("Cached %s", pr) + if err := sm.SyncSourceFor(gps.ProjectIdentifier{ProjectRoot: pr}); err != nil { + message = fmt.Sprintf("Unable to cache %s - %s", pr, err) + } + logger.Printf(message) + } + + for _, v := range pkgT.Packages { + // TODO: Some errors maybe should not be skipped ;-) + if v.Err != nil { + logger.Printf("%v", v.Err) + continue + } + + logger.Printf("Package %q, analyzing...", v.P.ImportPath) + + for _, ip := range v.P.Imports { + if isStdLib(ip) { + continue + } + if hasImportPathPrefix(ip, string(pr)) { + continue + } + + pr, err := a.sm.DeduceProjectRoot(ip) + if err != nil { + // return nil, nil, nil + } + + packages[ip] = true + if _, ok := dependencies[pr]; ok { + if !contains(dependencies[pr], ip) { + dependencies[pr] = append(dependencies[pr], ip) + } + continue + } + + go syncDep(pr, a.sm) + dependencies[pr] = []string{ip} + } + } +} + +func hasImportPathPrefix(s, prefix string) bool { + if s == prefix { + return true + } + return strings.HasPrefix(s, prefix+"/") +} + +func isStdLib(path string) bool { + i := strings.Index(path, "/") + if i < 0 { + i = len(path) + } + elem := path[:i] + return !strings.Contains(elem, ".") +} + func (a *rootAnalyzer) importManifestAndLock(dir string, pr gps.ProjectRoot, suppressLogs bool) (*dep.Manifest, *dep.Lock, error) { logger := a.ctx.Err if suppressLogs {