forked from izumin5210/grapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
path.go
109 lines (100 loc) · 2.94 KB
/
path.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package fs
import (
"go/build"
"go/parser"
"go/token"
"os"
"os/user"
"path/filepath"
"strings"
"github.com/pkg/errors"
"github.com/spf13/afero"
"go.uber.org/zap"
)
type getOSUserFunc func() (*user.User, error)
const (
// PackageSeparator is a package separator string on protobuf.
PackageSeparator = "."
)
// Make visible for testing
var (
BuildContext build.Context
GetOSUser getOSUserFunc
)
func init() {
BuildContext = build.Default
GetOSUser = user.Current
}
// GetImportPath creates the golang package path from the given path.
func GetImportPath(rootPath string) (importPath string, err error) {
for _, gopath := range filepath.SplitList(BuildContext.GOPATH) {
prefix := filepath.Join(gopath, "src") + string(filepath.Separator)
// FIXME: should not use strings.HasPrefix
if strings.HasPrefix(rootPath, prefix) {
importPath = filepath.ToSlash(strings.Replace(rootPath, prefix, "", 1))
break
}
}
if importPath == "" {
importPath = filepath.Base(rootPath)
}
return
}
// GetPackageName generates the package name of this application from the given path and envs.
func GetPackageName(rootPath string) (string, error) {
importPath, err := GetImportPath(rootPath)
if err != nil {
return "", errors.WithStack(err)
}
entries := strings.Split(importPath, string(filepath.Separator))
if len(entries) < 2 {
u, err := GetOSUser()
if err != nil {
return "", errors.WithStack(err)
}
entries = []string{u.Username, entries[0]}
}
entries = entries[len(entries)-2:]
if strings.Contains(entries[0], PackageSeparator) {
s := strings.Split(entries[0], PackageSeparator)
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
entries[0] = strings.Join(s, PackageSeparator)
}
pkgName := strings.Join(entries[len(entries)-2:], PackageSeparator)
pkgName = strings.Replace(pkgName, "-", "_", -1)
return pkgName, nil
}
// FindMainPackagesAndSources returns go source file names by main package directories.
func FindMainPackagesAndSources(fs afero.Fs, dir string) (map[string][]string, error) {
out := make(map[string][]string)
fset := token.NewFileSet()
err := afero.Walk(fs, dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return errors.WithStack(err)
}
if info.IsDir() || filepath.Ext(info.Name()) != ".go" || strings.HasSuffix(info.Name(), "_test.go") {
return nil
}
data, err := afero.ReadFile(fs, path)
if err != nil {
zap.L().Warn("failed to read a file", zap.Error(err), zap.String("path", path))
return nil
}
f, err := parser.ParseFile(fset, "", data, parser.PackageClauseOnly)
if err != nil {
zap.L().Warn("failed to parse a file", zap.Error(err), zap.String("path", path), zap.String("body", string(data)))
return nil
}
if f.Package.IsValid() && f.Name.Name == "main" {
dir := filepath.Dir(path)
out[dir] = append(out[dir], info.Name())
}
return nil
})
if err != nil {
return nil, errors.WithStack(err)
}
return out, nil
}