-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathclean.go
193 lines (179 loc) · 4.3 KB
/
clean.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package main
import (
"io/ioutil"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"github.com/LK4D4/vndr/build"
"github.com/LK4D4/vndr/godl"
)
func isCDir(fis []os.FileInfo) bool {
var hFound bool
for _, fi := range fis {
ext := filepath.Ext(fi.Name())
if ext == ".cc" || ext == ".cpp" || ext == ".py" {
return false
}
if ext == ".h" || ext == ".c" {
hFound = true
}
}
return hFound
}
func isPBDir(fis []os.FileInfo) bool {
var pbFound bool
for _, fi := range fis {
if fi.IsDir() {
continue
}
ext := filepath.Ext(fi.Name())
if ext != ".proto" {
return false
}
pbFound = true
}
return pbFound
}
func isInterestingDir(path string) bool {
fis, err := ioutil.ReadDir(path)
if err != nil {
return false
}
return isCDir(fis) || isPBDir(fis)
}
func isGoFile(path string) bool {
ext := filepath.Ext(path)
return ext == ".go" || ext == ".c" || ext == ".h" || ext == ".s" || ext == ".proto" || ext == ".syso"
}
// licenseFilesRegexp is a regexp of file names that are likely to contain licensing information
var licenseFilesRegexp = regexp.MustCompile(`(?i).*(LICENSE|COPYING|PATENT|NOTICE|README).*`)
func isLicenseFile(path string) bool {
return licenseFilesRegexp.MatchString(path) && !isGoFile(path)
}
func isVendorConfFile(path string) bool {
switch filepath.Base(path) {
case "go.mod", "vendor.conf":
return true
default:
return false
}
}
// cleanVendor removes files from unused packages and non-go files
func cleanVendor(vendorDir string, realDeps []*build.Package) error {
realPaths := make(map[string]bool)
ignoredGoFiles := []string{}
for _, pkg := range realDeps {
realPaths[pkg.Dir] = true
for _, f := range pkg.IgnoredGoFiles {
ignoredGoFiles = append(ignoredGoFiles, filepath.Join(pkg.Dir, f))
}
}
var paths []string
err := filepath.Walk(vendorDir, func(path string, i os.FileInfo, err error) error {
if path == vendorDir {
return nil
}
if err != nil {
return nil
}
// Make sure we don't delete anything that matches the whitelist. The
// whitelist is relative to the vendor directory.
relPath, err := filepath.Rel(vendorDir, path)
if err != nil {
return err
}
if cleanWhitelist.matchString(relPath) {
return nil
}
if strings.HasPrefix(i.Name(), ".") || strings.HasPrefix(i.Name(), "_") {
return os.RemoveAll(path)
}
if i.IsDir() {
if i.Name() == "testdata" {
return os.RemoveAll(path)
}
if isInterestingDir(path) {
realPaths[path] = true
return nil
}
if !realPaths[path] {
paths = append(paths, path)
}
return nil
}
// keep files for licenses and recursive vendoring
if isLicenseFile(i.Name()) || isVendorConfFile(i.Name()) {
return nil
}
// remove files from non-deps, non-go files and test files
if !realPaths[filepath.Dir(path)] || !isGoFile(path) || strings.HasSuffix(path, "_test.go") {
return os.Remove(path)
}
// remove ignored go files
for _, f := range ignoredGoFiles {
if f == path {
return os.Remove(path)
}
}
return nil
})
if err != nil {
return err
}
sort.Sort(sort.Reverse(sort.StringSlice(paths)))
// iterate over paths (longest first)
for _, p := range paths {
// at this point we cleaned all files from unused deps dirs
lst, err := ioutil.ReadDir(p)
if err != nil {
return err
}
// remove whole dir if there is only non-go files
// keep whitelisted, though
var onlyNonGoFile = true
for _, fi := range lst {
relPath, err := filepath.Rel(vendorDir, filepath.Join(p, fi.Name()))
if err != nil {
return err
}
if cleanWhitelist.matchString(relPath) || fi.IsDir() || isGoFile(fi.Name()) {
onlyNonGoFile = false
break
}
}
if !onlyNonGoFile {
continue
}
// remove all directories if they're not in dependency paths
if err := os.RemoveAll(p); err != nil {
return err
}
}
return nil
}
func cleanVCS(v *godl.VCS) error {
if err := os.RemoveAll(filepath.Join(v.Root, "."+v.Type)); err != nil {
return err
}
return filepath.Walk(v.Root, func(path string, i os.FileInfo, err error) error {
if err != nil {
return nil
}
if path == vendorDir {
return nil
}
if !i.IsDir() {
return nil
}
name := i.Name()
if name == "vendor" || name == "Godeps" || name == "_vendor" {
if err := os.RemoveAll(path); err != nil {
return err
}
return filepath.SkipDir
}
return nil
})
}