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

Commit

Permalink
build import map and then check if constraint match
Browse files Browse the repository at this point in the history
  • Loading branch information
JackyChiu committed Dec 24, 2017
1 parent d9ce240 commit 5b317da
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions cmd/dep/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -793,12 +793,23 @@ func collectConstraints(ctx *dep.Ctx, p *dep.Project, sm gps.SourceManager) (con
// Get project constraints.
pc := manifest.DependencyConstraints()

imports, err := projectImports(sm, proj)
if err != nil {
errCh <- errors.Wrapf(err, "error listing packages used in %s", proj.Ident().ProjectRoot)
return
}

// Obtain a lock for constraintCollection.
mutex.Lock()
defer mutex.Unlock()
// Iterate through the project constraints to get individual dependency
// project and constraint values.
for pr, pp := range pc {
// Check if the project constraint is imported in the project
if _, ok := imports[string(pr)]; !ok {
continue
}

tempCC := append(
constraintCollection[string(pr)],
projectConstraint{proj.Ident().ProjectRoot, pp.Constraint},
Expand Down Expand Up @@ -826,6 +837,26 @@ func collectConstraints(ctx *dep.Ctx, p *dep.Project, sm gps.SourceManager) (con
return constraintCollection, errs
}

// projectImports creates a set of all imports paths used in a project.
// The set of imports be used to check if an package is imported in a project.
func projectImports(sm gps.SourceManager, proj dep.Project) (map[string]bool, error) {
pkgTree, err := sm.ListPackages(proj.Ident(), proj.Version())
if err != nil {
return nil, err
}

imports := make(map[string]bool)
for _, pkg := range pkgTree.Packages {
if pkg.Err != nil {
continue
}
for _, imp := range pkg.P.Imports {
imports[imp] = true
}
}
return imports, nil
}

type byProject []projectConstraint

func (p byProject) Len() int { return len(p) }
Expand Down

0 comments on commit 5b317da

Please sign in to comment.