123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- package depth
- import (
- "errors"
- "go/build"
- "os"
- )
- var ErrRootPkgNotResolved = errors.New("unable to resolve root package")
- type Importer interface {
- Import(name, srcDir string, im build.ImportMode) (*build.Package, error)
- }
- type Tree struct {
- Root *Pkg
- ResolveInternal bool
- ResolveTest bool
- MaxDepth int
- Importer Importer
- importCache map[string]struct{}
- }
- func (t *Tree) Resolve(name string) error {
- pwd, err := os.Getwd()
- if err != nil {
- return err
- }
- t.Root = &Pkg{
- Name: name,
- Tree: t,
- SrcDir: pwd,
- Test: false,
- }
-
-
- t.importCache = nil
-
- if t.Importer == nil {
- t.Importer = &build.Default
- }
- t.Root.Resolve(t.Importer)
- if !t.Root.Resolved {
- return ErrRootPkgNotResolved
- }
- return nil
- }
- func (t *Tree) shouldResolveInternal(parent *Pkg) bool {
- if t.ResolveInternal {
- return true
- }
- return parent == t.Root
- }
- func (t *Tree) isAtMaxDepth(p *Pkg) bool {
- if t.MaxDepth == 0 {
- return false
- }
- return p.depth() >= t.MaxDepth
- }
- func (t *Tree) hasSeenImport(name string) bool {
- if t.importCache == nil {
- t.importCache = make(map[string]struct{})
- }
- if _, ok := t.importCache[name]; ok {
- return true
- }
- t.importCache[name] = struct{}{}
- return false
- }
|