Skip to content
This repository has been archived by the owner on Sep 9, 2020. It is now read-only.

Commit

Permalink
Prefetch and log output during InitializeRootManifestAndLock
Browse files Browse the repository at this point in the history
  • Loading branch information
xmattstrongx committed Oct 1, 2017
1 parent ac1a162 commit 8bedb08
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
2 changes: 1 addition & 1 deletion cmd/dep/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
71 changes: 70 additions & 1 deletion cmd/dep/root_analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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 {
Expand All @@ -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{}
Expand All @@ -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 {
Expand Down

0 comments on commit 8bedb08

Please sign in to comment.