|
| 1 | +package scanner |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + |
| 7 | + "github.com/thanhdevapp/dev-cleaner/pkg/types" |
| 8 | +) |
| 9 | + |
| 10 | +// FlutterGlobalPaths contains global Flutter/Dart cache paths |
| 11 | +var FlutterGlobalPaths = []struct { |
| 12 | + Path string |
| 13 | + Name string |
| 14 | +}{ |
| 15 | + {"~/.pub-cache", "Pub Cache"}, |
| 16 | + {"~/.dart_tool", "Dart Tool Cache"}, |
| 17 | + {"~/Library/Caches/Flutter", "Flutter Cache"}, |
| 18 | + {"~/Library/Caches/dart", "Dart Cache"}, |
| 19 | +} |
| 20 | + |
| 21 | +// ScanFlutter scans for Flutter/Dart development artifacts |
| 22 | +func (s *Scanner) ScanFlutter(maxDepth int) []types.ScanResult { |
| 23 | + var results []types.ScanResult |
| 24 | + |
| 25 | + // Scan global caches |
| 26 | + for _, target := range FlutterGlobalPaths { |
| 27 | + path := s.ExpandPath(target.Path) |
| 28 | + if !s.PathExists(path) { |
| 29 | + continue |
| 30 | + } |
| 31 | + |
| 32 | + size, count, err := s.calculateSize(path) |
| 33 | + if err != nil || size == 0 { |
| 34 | + continue |
| 35 | + } |
| 36 | + |
| 37 | + results = append(results, types.ScanResult{ |
| 38 | + Path: path, |
| 39 | + Type: types.TypeFlutter, |
| 40 | + Size: size, |
| 41 | + FileCount: count, |
| 42 | + Name: target.Name, |
| 43 | + }) |
| 44 | + } |
| 45 | + |
| 46 | + // Scan for Flutter projects in common development directories |
| 47 | + projectDirs := []string{ |
| 48 | + "~/Documents", |
| 49 | + "~/Projects", |
| 50 | + "~/Development", |
| 51 | + "~/Developer", |
| 52 | + "~/Code", |
| 53 | + "~/repos", |
| 54 | + "~/workspace", |
| 55 | + } |
| 56 | + |
| 57 | + for _, dir := range projectDirs { |
| 58 | + expandedDir := s.ExpandPath(dir) |
| 59 | + if !s.PathExists(expandedDir) { |
| 60 | + continue |
| 61 | + } |
| 62 | + |
| 63 | + flutterProjects := s.findFlutterProjects(expandedDir, maxDepth) |
| 64 | + results = append(results, flutterProjects...) |
| 65 | + } |
| 66 | + |
| 67 | + return results |
| 68 | +} |
| 69 | + |
| 70 | +// findFlutterProjects recursively finds Flutter projects via pubspec.yaml |
| 71 | +func (s *Scanner) findFlutterProjects(root string, maxDepth int) []types.ScanResult { |
| 72 | + var results []types.ScanResult |
| 73 | + |
| 74 | + if maxDepth <= 0 { |
| 75 | + return results |
| 76 | + } |
| 77 | + |
| 78 | + entries, err := os.ReadDir(root) |
| 79 | + if err != nil { |
| 80 | + return results |
| 81 | + } |
| 82 | + |
| 83 | + // Check if current directory is a Flutter project |
| 84 | + hasPubspec := false |
| 85 | + for _, entry := range entries { |
| 86 | + if !entry.IsDir() && entry.Name() == "pubspec.yaml" { |
| 87 | + hasPubspec = true |
| 88 | + break |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + // If Flutter project, scan build artifacts |
| 93 | + if hasPubspec { |
| 94 | + projectName := filepath.Base(root) |
| 95 | + buildTargets := []struct { |
| 96 | + subPath string |
| 97 | + name string |
| 98 | + }{ |
| 99 | + {"build", "build/"}, |
| 100 | + {".dart_tool", ".dart_tool/"}, |
| 101 | + {"ios/build", "ios/build/"}, |
| 102 | + {"android/build", "android/build/"}, |
| 103 | + {"macos/build", "macos/build/"}, |
| 104 | + {"linux/build", "linux/build/"}, |
| 105 | + {"windows/build", "windows/build/"}, |
| 106 | + {"web/build", "web/build/"}, |
| 107 | + } |
| 108 | + |
| 109 | + for _, target := range buildTargets { |
| 110 | + buildPath := filepath.Join(root, target.subPath) |
| 111 | + if !s.PathExists(buildPath) { |
| 112 | + continue |
| 113 | + } |
| 114 | + |
| 115 | + size, count, _ := s.calculateSize(buildPath) |
| 116 | + if size > 0 { |
| 117 | + results = append(results, types.ScanResult{ |
| 118 | + Path: buildPath, |
| 119 | + Type: types.TypeFlutter, |
| 120 | + Size: size, |
| 121 | + FileCount: count, |
| 122 | + Name: projectName + "/" + target.name, |
| 123 | + }) |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + // Don't recurse into Flutter project subdirectories |
| 128 | + return results |
| 129 | + } |
| 130 | + |
| 131 | + // Recurse into subdirectories |
| 132 | + for _, entry := range entries { |
| 133 | + if !entry.IsDir() { |
| 134 | + continue |
| 135 | + } |
| 136 | + |
| 137 | + name := entry.Name() |
| 138 | + |
| 139 | + // Skip hidden and known non-project directories |
| 140 | + if shouldSkipDir(name) { |
| 141 | + continue |
| 142 | + } |
| 143 | + |
| 144 | + fullPath := filepath.Join(root, name) |
| 145 | + subResults := s.findFlutterProjects(fullPath, maxDepth-1) |
| 146 | + results = append(results, subResults...) |
| 147 | + } |
| 148 | + |
| 149 | + return results |
| 150 | +} |
0 commit comments