github.com/mayur-tolexo/godep@v0.0.0-20170205210856-a9cd0561f946/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
    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  func LoadPackages(names ...string) (a []*Package, err error) {
    41  	debugln("LoadPackages", names)
    42  	if len(names) == 0 {
    43  		return nil, nil
    44  	}
    45  	for _, i := range importPaths(names) {
    46  		p, err := listPackage(i)
    47  		if err != nil {
    48  			return nil, err
    49  		}
    50  		a = append(a, p)
    51  	}
    52  	return a, nil
    53  }
    54  
    55  func (p *Package) allGoFiles() []string {
    56  	var a []string
    57  	a = append(a, p.GoFiles...)
    58  	a = append(a, p.CgoFiles...)
    59  	a = append(a, p.TestGoFiles...)
    60  	a = append(a, p.XTestGoFiles...)
    61  	a = append(a, p.IgnoredGoFiles...)
    62  	return a
    63  }
    64  
    65  // matchPattern(pattern)(name) reports whether
    66  // name matches pattern.  Pattern is a limited glob
    67  // pattern in which '...' means 'any string' and there
    68  // is no other special syntax.
    69  // Taken from $GOROOT/src/cmd/go/main.go.
    70  func matchPattern(pattern string) func(name string) bool {
    71  	re := regexp.QuoteMeta(pattern)
    72  	re = strings.Replace(re, `\.\.\.`, `.*`, -1)
    73  	// Special case: foo/... matches foo too.
    74  	if strings.HasSuffix(re, `/.*`) {
    75  		re = re[:len(re)-len(`/.*`)] + `(/.*)?`
    76  	}
    77  	reg := regexp.MustCompile(`^` + re + `$`)
    78  	return func(name string) bool {
    79  		return reg.MatchString(name)
    80  	}
    81  }