gopkg.in/tools/godep.v38@v38.0.0-20151216225452-4154dbb67855/pkg.go (about)

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