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 9, 2017
1 parent ac1a162 commit 4f991d4
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions cmd/dep/root_analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,22 @@
package main

import (
"context"
"io/ioutil"
"log"
"strings"

"golang.org/x/sync/errgroup"

"github.com/golang/dep"
fb "github.com/golang/dep/internal/feedback"
"github.com/golang/dep/internal/gps"
"github.com/golang/dep/internal/gps/paths"
"github.com/golang/dep/internal/importers"
)

const concurrency = 4

// rootAnalyzer supplies manifest/lock data from both dep and external tool's
// configuration files.
// * When used on the root project, it imports only from external tools.
Expand Down Expand Up @@ -45,6 +52,9 @@ func (a *rootAnalyzer) InitializeRootManifestAndLock(dir string, pr gps.ProjectR

if rootM == nil {
rootM = dep.NewManifest()
if err := a.cacheDeps(pr); err != nil {
return nil, nil, err
}
}
if rootL == nil {
rootL = &dep.Lock{}
Expand All @@ -53,6 +63,65 @@ func (a *rootAnalyzer) InitializeRootManifestAndLock(dir string, pr gps.ProjectR
return
}

func (a *rootAnalyzer) cacheDeps(pr gps.ProjectRoot) error {
deps := make(map[gps.ProjectRoot]bool)
logger := a.ctx.Err
g, ctx := errgroup.WithContext(context.TODO())
sem := make(chan struct{}, concurrency)

syncDep := func(pr gps.ProjectRoot, sm gps.SourceManager) error {
if err := sm.SyncSourceFor(gps.ProjectIdentifier{ProjectRoot: pr}); err != nil {
logger.Printf("Unable to cache %s - %s", pr, err)
return err
}
logger.Printf("Cached %s", pr)
return nil
}

for ip := range a.directDeps {
logger.Printf("Package %q, analyzing...", ip)
if paths.IsStandardImportPath(ip) {
continue
}
if hasImportPathPrefix(ip, string(pr)) {
continue
}

pr, err := a.sm.DeduceProjectRoot(ip)
if err != nil {
return err
}

if _, ok := deps[pr]; ok {
continue
}

g.Go(func() error {
select {
case sem <- struct{}{}:
defer func() { <-sem }()
case <-ctx.Done():
return ctx.Err()
}
err := syncDep(pr, a.sm)
return err
})

deps[pr] = true
}
if err := g.Wait(); err == nil {
logger.Printf("Successfully cached all deps.")
}
return nil
}

func hasImportPathPrefix(s, prefix string) bool {
if s == prefix {
return true
}
return strings.HasPrefix(s, prefix+"/")
}

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 4f991d4

Please sign in to comment.