forked from rainycape/gondola
/
pkgutil.go
117 lines (109 loc) · 2.83 KB
/
pkgutil.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
110
111
112
113
114
115
116
117
// Package pkgutil contains some small utilities for working
// with go packages.
package pkgutil
import (
"bytes"
"go/build"
"io/ioutil"
"os"
"path/filepath"
"sort"
"strings"
)
// SplitQualifiedName splits a qualified name into the package
// and the local name inside the package.
func SplitQualifiedName(qname string) (pkg string, name string) {
pos := strings.LastIndex(qname, "/")
if pos == -1 {
pos = 0
}
dot := strings.Index(qname[pos:], ".")
if dot == -1 {
return "", ""
}
dot += pos
return qname[:dot], qname[dot+1:]
}
// IsPackage returns wheter the given directory is a Go package.
func IsPackage(dir string) bool {
f, err := os.Open(dir)
if err != nil {
return false
}
defer f.Close()
names, err := f.Readdirnames(-1)
if err != nil {
return false
}
for _, v := range names {
if strings.ToLower(filepath.Ext(v)) == ".go" {
return true
}
}
return false
}
// ListPackages returns a list of packages found under the given
// directory (including itself, if it's a package).
func ListPackages(dir string) ([]string, error) {
var pkgs []string
e := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info != nil && info.IsDir() && !shouldIgnorePackage(path) && IsPackage(path) {
pkg, err := build.ImportDir(path, 0)
if err == nil && !pkgIsEmpty(pkg) {
pkgs = append(pkgs, path)
}
}
return nil
})
return pkgs, e
}
// LineCount returns the number of lines of code in
// the given package.
func LineCount(p *build.Package) (int, error) {
lines := 0
// Count all non-header and non-test sources, even
// the ignored ones
nl := []byte{'\n'}
for _, files := range [][]string{p.GoFiles, p.CgoFiles, p.IgnoredGoFiles, p.CFiles, p.CXXFiles, p.SFiles, p.SwigFiles, p.SwigCXXFiles} {
for _, file := range files {
data, err := ioutil.ReadFile(filepath.Join(p.Dir, file))
if err != nil {
return 0, err
}
lines += bytes.Count(data, nl)
}
}
return lines, nil
}
// Filenames returns all filenames referenced by the package
// as relative paths to the package directory.
func Filenames(p *build.Package) []string {
var files []string
for _, v := range [][]string{p.GoFiles, p.CgoFiles, p.IgnoredGoFiles, p.CFiles, p.CXXFiles, p.HFiles, p.SFiles, p.SysoFiles, p.SwigFiles, p.SwigCXXFiles} {
files = append(files, v...)
}
sort.Strings(files)
return files
}
func shouldIgnorePackage(path string) bool {
for _, v := range strings.Split(path, string(filepath.Separator)) {
if v != "" && (v[0] == '.' || v[0] == '_') {
return true
}
if v == "example" || v == "examples" || v == "sample" || v == "samples" || v == "testdata" {
return true
}
}
return false
}
func pkgIsEmpty(p *build.Package) bool {
for _, v := range [][]string{p.GoFiles, p.CgoFiles, p.IgnoredGoFiles} {
if len(v) > 0 {
return false
}
}
return true
}