Skip to content
This repository was archived by the owner on Sep 9, 2020. It is now read-only.

Commit 9bd639a

Browse files
committed
internal/gps: add pruneProject func
Signed-off-by: Ibrahim AshShohail <[email protected]>
1 parent e4c3218 commit 9bd639a

File tree

2 files changed

+91
-305
lines changed

2 files changed

+91
-305
lines changed

internal/gps/prune.go

Lines changed: 83 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"sort"
1313
"strings"
1414

15-
"github.com/golang/dep/internal/fs"
1615
"github.com/pkg/errors"
1716
)
1817

@@ -58,17 +57,29 @@ var (
5857
//
5958
// A Lock must be passed if PruneUnusedPackages is toggled on.
6059
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 {
6175
if (options & PruneNestedVendorDirs) != 0 {
6276
if err := pruneNestedVendorDirs(baseDir); err != nil {
6377
return err
6478
}
6579
}
6680

6781
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 {
7283
return errors.Wrap(err, "failed to prune unused packages")
7384
}
7485
}
@@ -85,39 +96,25 @@ func Prune(baseDir string, options PruneOptions, l Lock, logger *log.Logger) err
8596
}
8697
}
8798

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-
9399
return nil
94100
}
95101

96102
// pruneNestedVendorDirs deletes all nested vendor directories within baseDir.
97103
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)
115105
}
116106

117107
// pruneUnusedPackages deletes unimported packages found within baseDir.
118108
// 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))
121118
if err != nil {
122119
return err
123120
}
@@ -148,20 +145,45 @@ func pruneUnusedPackages(baseDir string, l Lock, logger *log.Logger) error {
148145
return nil
149146
}
150147

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
158153

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")
162157
}
163158

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 {
165187
if err != nil {
166188
return err
167189
}
@@ -176,8 +198,7 @@ func calculateUnusedPackages(baseDir string, l Lock, logger *log.Logger) ([]stri
176198
logger.Printf(" %s", pkg)
177199
}
178200

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.
181202
i := sort.Search(len(imported), func(i int) bool {
182203
return pkg <= imported[i]
183204
})
@@ -186,44 +207,14 @@ func calculateUnusedPackages(baseDir string, l Lock, logger *log.Logger) ([]stri
186207
}
187208

188209
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-
}
204210
}
205-
return imported
206211
}
207212

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 {
227218
if err != nil {
228219
return err
229220
}
@@ -243,13 +234,13 @@ func calculateNonGoFiles(baseDir string) ([]string, error) {
243234
}
244235

245236
return nil
246-
})
247-
248-
return files, err
237+
}
249238
}
250239

251240
// isPreservedNonGoFile checks if the file name idicates that the file should be
252241
// 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.
253244
func isPreservedNonGoFile(name string) bool {
254245
name = strings.ToLower(name)
255246

@@ -268,22 +259,10 @@ func isPreservedNonGoFile(name string) bool {
268259
return false
269260
}
270261

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 {
287266
if err != nil {
288267
return err
289268
}
@@ -294,73 +273,25 @@ func calculateGoTestFiles(baseDir string) ([]string, error) {
294273
}
295274

296275
// 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
299278
}
300279

301-
files = append(files, path)
302-
303280
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:")
318281
}
282+
}
319283

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 {
325287
return err
326288
}
327289
}
328-
329290
return nil
330291
}
331292

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 {
364295
if err := os.Remove(path); err != nil {
365296
return err
366297
}

0 commit comments

Comments
 (0)