/
util.go
73 lines (62 loc) · 1.34 KB
/
util.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
package internal
import (
"go/ast"
"go/token"
"io"
"os"
"path/filepath"
"strings"
)
func Must(err error) {
if err != nil {
panic(err)
}
}
func MustParse(file *ast.File, err error) *ast.File {
Must(err)
return file
}
func Assert(condition bool, msg string) {
if !condition {
panic("[assert] " + msg)
}
}
func CopyFile(path string, out io.Writer) error {
in, err := os.Open(path)
if err != nil {
return err
}
defer in.Close()
_, err = io.Copy(out, in)
return err
}
func IsGoFile(f os.FileInfo) bool {
name := f.Name()
return !f.IsDir() && IsGoFileName(name)
}
func IsTestGoFile(f os.FileInfo) bool {
name := f.Name()
return !f.IsDir() && IsTestGoFileName(name)
}
func IsGoFileName(name string) bool {
name = filepath.Base(name)
return strings.HasSuffix(name, ".go") && !strings.HasPrefix(name, ".") && !strings.HasPrefix(name, "_")
}
func IsTestGoFileName(name string) bool {
name = filepath.Base(name)
return strings.HasSuffix(name, "_test.go") && !strings.HasPrefix(name, ".") && !strings.HasPrefix(name, "_")
}
func IsBuildableFileName(name string) bool {
return IsGoFileName(name) && !IsTestGoFileName(name)
}
func IsBuildableFileSet(s *token.FileSet) bool {
contains := false
s.Iterate(func(f *token.File) bool {
if IsBuildableFileName(f.Name()) {
contains = true
return false
}
return true
})
return contains
}