gopkg.in/tools/godep.v73@v73.0.0-20160531225236-f4edf338389e/pkg.go (about)

     1  package main
     2  
     3  import (
     4  	"go/build"
     5  	"regexp"
     6  	"strings"
     7  )
     8  
     9  // Package represents a Go package.
    10  type Package struct {
    11  	Dir        string
    12  	Root       string
    13  	ImportPath string
    14  	Deps       []string
    15  	Standard   bool
    16  	Processed  bool
    17  
    18  	GoFiles        []string
    19  	CgoFiles       []string
    20  	IgnoredGoFiles []string
    21  
    22  	TestGoFiles  []string
    23  	TestImports  []string
    24  	XTestGoFiles []string
    25  	XTestImports []string
    26  
    27  	Error struct {
    28  		Err string
    29  	}
    30  
    31  	// --- New stuff for now
    32  	Imports      []string
    33  	Dependencies []build.Package
    34  }
    35  
    36  // LoadPackages loads the named packages using go list -json.
    37  // Unlike the go tool, an empty argument list is treated as an empty list; "."
    38  // must be given explicitly if desired.
    39  // IgnoredGoFiles will be processed and their dependencies resolved recursively
    40  // Files with a build tag of `ignore` are skipped. Files with other build tags
    41  // are however processed.
    42  func LoadPackages(names ...string) (a []*Package, err error) {
    43  	debugln("LoadPackages", names)
    44  	if len(names) == 0 {
    45  		return nil, nil
    46  	}
    47  	for _, i := range importPaths(names) {
    48  		p, err := listPackage(i)
    49  		if err != nil {
    50  			return nil, err
    51  		}
    52  		a = append(a, p)
    53  	}
    54  	return a, nil
    55  }
    56  
    57  func (p *Package) allGoFiles() []string {
    58  	var a []string
    59  	a = append(a, p.GoFiles...)
    60  	a = append(a, p.CgoFiles...)
    61  	a = append(a, p.TestGoFiles...)
    62  	a = append(a, p.XTestGoFiles...)
    63  	a = append(a, p.IgnoredGoFiles...)
    64  	return a
    65  }
    66  
    67  // matchPattern(pattern)(name) reports whether
    68  // name matches pattern.  Pattern is a limited glob
    69  // pattern in which '...' means 'any string' and there
    70  // is no other special syntax.
    71  // Taken from $GOROOT/src/cmd/go/main.go.
    72  func matchPattern(pattern string) func(name string) bool {
    73  	re := regexp.QuoteMeta(pattern)
    74  	re = strings.Replace(re, `\.\.\.`, `.*`, -1)
    75  	// Special case: foo/... matches foo too.
    76  	if strings.HasSuffix(re, `/.*`) {
    77  		re = re[:len(re)-len(`/.*`)] + `(/.*)?`
    78  	}
    79  	reg := regexp.MustCompile(`^` + re + `$`)
    80  	return func(name string) bool {
    81  		return reg.MatchString(name)
    82  	}
    83  }