gitee.com/zhongguo168a/gocodes@v0.0.0-20230609140523-e1828349603f/gox/pathx/gopath/path.go (about)

     1  package gopath
     2  
     3  import (
     4  	"errors"
     5  	"gitee.com/zhongguo168a/gocodes/gox/pathx"
     6  	"gitee.com/zhongguo168a/gocodes/gox/pathx/filepathx"
     7  	"os"
     8  	"path/filepath"
     9  	"regexp"
    10  	"sort"
    11  	"strings"
    12  )
    13  
    14  // GetGoPath 获取go包所在的go path
    15  func GetGoPath(pkg string) (goPath string, err error) {
    16  	envPath := os.Getenv("GOPATH")
    17  	if envPath == "" {
    18  		err = errors.New("get env $GOPATH: not found")
    19  		return
    20  	}
    21  
    22  	arr := strings.Split(envPath, ";")
    23  	for _, val := range arr {
    24  		val = filepath.ToSlash(val)
    25  		val = strings.ToLower(val)
    26  		pkgPath := val + "/src/" + string(pkg)
    27  		if pathx.PathExists(pkgPath) == false {
    28  			continue
    29  		}
    30  
    31  		goPath = val
    32  		return
    33  	}
    34  
    35  	err = errors.New("not found")
    36  	return
    37  }
    38  
    39  // Package go的包类型
    40  // 不包含gopath的包字符串标识
    41  type Package string
    42  
    43  func (pkg Package) MustGoPath() string {
    44  	p, err := pkg.GetGoPath()
    45  	if err != nil {
    46  		panic(err)
    47  	}
    48  	return p
    49  }
    50  
    51  // GetGoPath 获取go包所在的go path
    52  func (pkg Package) GetGoPath() (goPath string, err error) {
    53  	return GetGoPath(string(pkg))
    54  }
    55  
    56  func (pkg Package) Name() string {
    57  	return filepathx.Name(string(pkg))
    58  }
    59  
    60  func (pkg Package) String() string {
    61  	return string(pkg)
    62  }
    63  
    64  // GetFilePath 获取完整的pkg文件路径
    65  func (pkg Package) GetFilePath() string {
    66  	gopath, _ := pkg.GetGoPath()
    67  	return gopath + "/src/" + string(pkg)
    68  }
    69  
    70  // GetSubpackages 获取指定项目所在包的子包
    71  func (pkg Package) GetSubpackages() (subs []string, err error) {
    72  	gopath, err := pkg.GetGoPath()
    73  	if err != nil {
    74  		return
    75  	}
    76  
    77  	submap := map[string]bool{}
    78  	err = filepath.Walk(pkg.GetFilePath(), func(path string, info os.FileInfo, err error) error {
    79  		if err != nil {
    80  			return err
    81  		}
    82  		if info.IsDir() == false {
    83  			return nil
    84  		}
    85  
    86  		path = filepath.ToSlash(path)
    87  		if strings.Contains(path, "generate") {
    88  			return nil
    89  		}
    90  		if strings.Contains(path, "internal") {
    91  			return nil
    92  		}
    93  
    94  		match, err := regexp.MatchString(`\/\.`, path)
    95  		if match {
    96  			return nil
    97  		}
    98  		pkg := strings.Replace(path, gopath+"/src/", "", -1)
    99  		submap[pkg] = true
   100  		return nil
   101  	})
   102  	if err != nil {
   103  		return
   104  	}
   105  
   106  	subs = func() (x []string) {
   107  		for key, _ := range submap {
   108  			x = append(x, key)
   109  		}
   110  
   111  		sort.Strings(x)
   112  		return
   113  	}()
   114  
   115  	return
   116  }