@@ -12,7 +12,6 @@ import (
12
12
"sort"
13
13
"strings"
14
14
15
- "github.com/golang/dep/internal/fs"
16
15
"github.com/pkg/errors"
17
16
)
18
17
@@ -58,17 +57,29 @@ var (
58
57
//
59
58
// A Lock must be passed if PruneUnusedPackages is toggled on.
60
59
func Prune (baseDir string , options PruneOptions , l Lock , logger * log.Logger ) error {
60
+ // TODO(ibrasho) allow passing specific options per project
61
+ for _ , lp := range l .Projects () {
62
+ projectDir := filepath .Join (baseDir , string (lp .Ident ().ProjectRoot ))
63
+ err := pruneProject (projectDir , lp , options , logger )
64
+ if err != nil {
65
+ return err
66
+ }
67
+ }
68
+
69
+ return nil
70
+ }
71
+
72
+ // pruneProject remove excess files according to the options passed, from
73
+ // the lp directory in baseDir.
74
+ func pruneProject (baseDir string , lp LockedProject , options PruneOptions , logger * log.Logger ) error {
61
75
if (options & PruneNestedVendorDirs ) != 0 {
62
76
if err := pruneNestedVendorDirs (baseDir ); err != nil {
63
77
return err
64
78
}
65
79
}
66
80
67
81
if (options & PruneUnusedPackages ) != 0 {
68
- if l == nil {
69
- return errors .New ("pruning unused packages requires passing a non-nil Lock" )
70
- }
71
- if err := pruneUnusedPackages (baseDir , l , logger ); err != nil {
82
+ if err := pruneUnusedPackages (baseDir , lp , logger ); err != nil {
72
83
return errors .Wrap (err , "failed to prune unused packages" )
73
84
}
74
85
}
@@ -85,39 +96,25 @@ func Prune(baseDir string, options PruneOptions, l Lock, logger *log.Logger) err
85
96
}
86
97
}
87
98
88
- // Delete all empty directories.
89
- if err := pruneEmptyDirs (baseDir , logger ); err != nil {
90
- return errors .Wrap (err , "failed to prune empty dirs" )
91
- }
92
-
93
99
return nil
94
100
}
95
101
96
102
// pruneNestedVendorDirs deletes all nested vendor directories within baseDir.
97
103
func pruneNestedVendorDirs (baseDir string ) error {
98
- return filepath .Walk (baseDir , func (path string , info os.FileInfo , err error ) error {
99
- if ! info .IsDir () {
100
- return nil
101
- }
102
-
103
- // Ignore the base vendor directory.
104
- if path == baseDir {
105
- return nil
106
- }
107
-
108
- if err := filepath .Walk (path , stripVendor ); err != nil {
109
- return err
110
- }
111
-
112
- // Don't walk into directories again.
113
- return filepath .SkipDir
114
- })
104
+ return filepath .Walk (baseDir , stripVendor )
115
105
}
116
106
117
107
// pruneUnusedPackages deletes unimported packages found within baseDir.
118
108
// Determining whether packages are imported or not is based on the passed Lock.
119
- func pruneUnusedPackages (baseDir string , l Lock , logger * log.Logger ) error {
120
- unused , err := calculateUnusedPackages (baseDir , l , logger )
109
+ func pruneUnusedPackages (baseDir string , lp LockedProject , logger * log.Logger ) error {
110
+ if logger != nil {
111
+ logger .Printf ("Calculating unused packages in %s to prune.\n " , lp .Ident ().ProjectRoot )
112
+ logger .Println ("Checking the following packages:" )
113
+ }
114
+
115
+ var unused []string
116
+
117
+ err := filepath .Walk (baseDir , findUnusedPackages (lp , unused , baseDir , logger ))
121
118
if err != nil {
122
119
return err
123
120
}
@@ -148,20 +145,45 @@ func pruneUnusedPackages(baseDir string, l Lock, logger *log.Logger) error {
148
145
return nil
149
146
}
150
147
151
- // calculateUnusedPackages generates a list of unused packages existing within
152
- // baseDir depending on the imported packages found in the passed Lock.
153
- func calculateUnusedPackages (baseDir string , l Lock , logger * log.Logger ) ([]string , error ) {
154
- imported := calculateImportedPackages (l )
155
- sort .Strings (imported )
156
-
157
- var unused []string
148
+ // pruneNonGoFiles delete all non-Go files existing within baseDir.
149
+ // Files with names that are prefixed by any entry in preservedNonGoFiles
150
+ // are not deleted.
151
+ func pruneNonGoFiles (baseDir string , logger * log.Logger ) error {
152
+ var files []string
158
153
159
- if logger != nil {
160
- logger . Println ( "Calculating unused packages to prune." )
161
- logger . Println ( "Checking the following packages: " )
154
+ err := filepath . Walk ( baseDir , findNonGoFiles ( files ))
155
+ if err != nil {
156
+ return errors . Wrap ( err , "could not prune non-Go files " )
162
157
}
163
158
164
- err := filepath .Walk (baseDir , func (path string , info os.FileInfo , err error ) error {
159
+ return deleteFiles (files )
160
+ }
161
+
162
+ // pruneGoTestFiles deletes all Go test files (*_test.go) within baseDir.
163
+ func pruneGoTestFiles (baseDir string , logger * log.Logger ) error {
164
+ filesCh := make (chan string )
165
+
166
+ var err error
167
+ go func () {
168
+ if walkErr := filepath .Walk (baseDir , findGoTestsFile (filesCh )); err != nil {
169
+ err = errors .Wrap (walkErr , "could not prune Go test files" )
170
+ close (filesCh )
171
+ }
172
+ }()
173
+
174
+ go func () {
175
+ err = deleteFilesCh (filesCh )
176
+ }()
177
+
178
+ return err
179
+ }
180
+
181
+ // findUnusedPackages generates a list of unused packages in lp.
182
+ func findUnusedPackages (lp LockedProject , unused []string , baseDir string , logger * log.Logger ) filepath.WalkFunc {
183
+ imported := lp .Packages ()
184
+ sort .Strings (imported )
185
+
186
+ return func (path string , info os.FileInfo , err error ) error {
165
187
if err != nil {
166
188
return err
167
189
}
@@ -176,8 +198,7 @@ func calculateUnusedPackages(baseDir string, l Lock, logger *log.Logger) ([]stri
176
198
logger .Printf (" %s" , pkg )
177
199
}
178
200
179
- // If pkg is not a parent of an imported package, add it to the
180
- // unused list.
201
+ // If pkg is not a parent of an imported package, add it to the unused list.
181
202
i := sort .Search (len (imported ), func (i int ) bool {
182
203
return pkg <= imported [i ]
183
204
})
@@ -186,44 +207,14 @@ func calculateUnusedPackages(baseDir string, l Lock, logger *log.Logger) ([]stri
186
207
}
187
208
188
209
return nil
189
- })
190
-
191
- return unused , err
192
- }
193
-
194
- // calculateImportedPackages generates a list of imported packages from
195
- // the passed Lock.
196
- func calculateImportedPackages (l Lock ) []string {
197
- var imported []string
198
-
199
- for _ , project := range l .Projects () {
200
- projectRoot := string (project .Ident ().ProjectRoot )
201
- for _ , pkg := range project .Packages () {
202
- imported = append (imported , filepath .Join (projectRoot , pkg ))
203
- }
204
210
}
205
- return imported
206
211
}
207
212
208
- // pruneNonGoFiles delete all non-Go files existing within baseDir.
209
- // Files with names that are prefixed by any entry in preservedNonGoFiles
210
- // are not deleted.
211
- func pruneNonGoFiles (baseDir string , logger * log.Logger ) error {
212
- files , err := calculateNonGoFiles (baseDir )
213
- if err != nil {
214
- return errors .Wrap (err , "could not prune non-Go files" )
215
- }
216
-
217
- return deleteFiles (files )
218
- }
219
-
220
- // calculateNonGoFiles returns a list of all non-Go files within baseDir.
221
- // Files with names that are prefixed by any entry in preservedNonGoFiles
222
- // are not deleted.
223
- func calculateNonGoFiles (baseDir string ) ([]string , error ) {
224
- var files []string
225
-
226
- err := filepath .Walk (baseDir , func (path string , info os.FileInfo , err error ) error {
213
+ // findNonGoFiles returns a WalkFunc that appends all non-Go files to the
214
+ // passed slice.
215
+ // Files passing the checks in isPreservedNonGoFile are not appended.
216
+ func findNonGoFiles (files []string ) filepath.WalkFunc {
217
+ return func (path string , info os.FileInfo , err error ) error {
227
218
if err != nil {
228
219
return err
229
220
}
@@ -243,13 +234,13 @@ func calculateNonGoFiles(baseDir string) ([]string, error) {
243
234
}
244
235
245
236
return nil
246
- })
247
-
248
- return files , err
237
+ }
249
238
}
250
239
251
240
// isPreservedNonGoFile checks if the file name idicates that the file should be
252
241
// preserved. It assumes the file is not a Go file (doesn't have a .go suffix).
242
+ // It checks if the file name contains one of the prefixes in licenseFilePrefixes
243
+ // or contains one of the legalFileSubstrings entries.
253
244
func isPreservedNonGoFile (name string ) bool {
254
245
name = strings .ToLower (name )
255
246
@@ -268,22 +259,10 @@ func isPreservedNonGoFile(name string) bool {
268
259
return false
269
260
}
270
261
271
- // pruneGoTestFiles deletes all Go test files (*_test.go) within baseDir.
272
- func pruneGoTestFiles (baseDir string , logger * log.Logger ) error {
273
- files , err := calculateGoTestFiles (baseDir )
274
- if err != nil {
275
- return errors .Wrap (err , "could not prune Go test files" )
276
- }
277
-
278
- return deleteFiles (files )
279
- }
280
-
281
- // calculateGoTestFiles walks over baseDir and returns a list of all
282
- // Go test files (any file that has the name *_test.go).
283
- func calculateGoTestFiles (baseDir string ) ([]string , error ) {
284
- var files []string
285
-
286
- err := filepath .Walk (baseDir , func (path string , info os.FileInfo , err error ) error {
262
+ // findGoTestsFile returns a WalkFunc that appends Go test files (any files
263
+ // prefixed with _test.go) to the passed slice.
264
+ func findGoTestsFile (filesCh chan string ) filepath.WalkFunc {
265
+ return func (path string , info os.FileInfo , err error ) error {
287
266
if err != nil {
288
267
return err
289
268
}
@@ -294,73 +273,25 @@ func calculateGoTestFiles(baseDir string) ([]string, error) {
294
273
}
295
274
296
275
// Ignore any files that is not a Go test file.
297
- if ! strings .HasSuffix (info .Name (), "_test.go" ) {
298
- return nil
276
+ if strings .HasSuffix (info .Name (), "_test.go" ) {
277
+ filesCh <- path
299
278
}
300
279
301
- files = append (files , path )
302
-
303
280
return nil
304
- })
305
-
306
- return files , err
307
- }
308
-
309
- // pruneEmptyDirs delete all empty directories within baseDir.
310
- func pruneEmptyDirs (baseDir string , logger * log.Logger ) error {
311
- empty , err := calculateEmptyDirs (baseDir )
312
- if err != nil {
313
- return err
314
- }
315
-
316
- if logger != nil {
317
- logger .Println ("Deleting empty directories:" )
318
281
}
282
+ }
319
283
320
- for _ , dir := range empty {
321
- if logger != nil {
322
- logger .Printf (" %s\n " , strings .TrimPrefix (dir , baseDir + string (os .PathSeparator )))
323
- }
324
- if err := os .Remove (dir ); err != nil {
284
+ func deleteFiles (paths []string ) error {
285
+ for _ , path := range paths {
286
+ if err := os .Remove (path ); err != nil {
325
287
return err
326
288
}
327
289
}
328
-
329
290
return nil
330
291
}
331
292
332
- // calculateEmptyDirs walks over baseDir and returns a slice of empty directory paths.
333
- func calculateEmptyDirs (baseDir string ) ([]string , error ) {
334
- var empty []string
335
-
336
- err := filepath .Walk (baseDir , func (path string , info os.FileInfo , err error ) error {
337
- if err != nil {
338
- return nil
339
- }
340
-
341
- if baseDir == path {
342
- return nil
343
- }
344
-
345
- if ! info .IsDir () {
346
- return nil
347
- }
348
-
349
- nonEmpty , err := fs .IsNonEmptyDir (path )
350
- if err != nil {
351
- return err
352
- } else if ! nonEmpty {
353
- empty = append (empty , path )
354
- }
355
-
356
- return nil
357
- })
358
-
359
- return empty , err
360
- }
361
-
362
- func deleteFiles (paths []string ) error {
363
- for _ , path := range paths {
293
+ func deleteFilesCh (paths chan string ) error {
294
+ for path := range paths {
364
295
if err := os .Remove (path ); err != nil {
365
296
return err
366
297
}
0 commit comments