Skip to content

Commit

Permalink
Supports exclude folders
Browse files Browse the repository at this point in the history
  • Loading branch information
tinytoy committed Apr 26, 2016
1 parent 1f17dde commit 34a0562
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 57 deletions.
5 changes: 4 additions & 1 deletion LSUnusedResources/Model/LSFileUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@

@interface LSFileUtils : NSObject
/**
* get file size, ignore directory
* get file size, contain directory
* @param path path
* @param isDir
*
* @return
*/
+ (uint64_t)fileSizeAtPath:(NSString *)path isDir:(BOOL *)isDir;

+ (uint64_t)folderSizeAtPath:(NSString *)path;

@end
16 changes: 15 additions & 1 deletion LSUnusedResources/Model/LSFileUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,31 @@
#import "LSFileUtils.h"

@implementation LSFileUtils

+ (uint64_t)fileSizeAtPath:(NSString *)path isDir:(BOOL *)isDir {
uint64_t size = 0L;
NSError *error = nil;
NSDictionary *attr = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:&error];
if (!error) {
*isDir = [attr[NSFileType]isEqualToString:NSFileTypeDirectory];
*isDir = [attr[NSFileType] isEqualToString:NSFileTypeDirectory];
if (!*isDir) {
size = [attr[NSFileSize] unsignedLongLongValue];
} else {
size = [self folderSizeAtPath:path];
}
}
return size;
}

+ (uint64_t)folderSizeAtPath:(NSString *)path {
uint64_t totalFileSize = 0;
NSDirectoryEnumerator *fileEnumerator = [[NSFileManager defaultManager] enumeratorAtPath:path];
for (NSString *fileName in fileEnumerator) {
NSString *filePath = [path stringByAppendingPathComponent:fileName];
NSDictionary *fileAttrs = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
totalFileSize += fileAttrs.fileSize;
}
return totalFileSize;
}

@end
3 changes: 2 additions & 1 deletion LSUnusedResources/Model/ResourceFileSearcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ extern NSString * const kNotificationResourceFileQueryDone;
@property (strong, nonatomic) NSString *path;
@property (assign, nonatomic) BOOL isDir;
@property (assign, nonatomic) uint64_t fileSize;

- (NSImage *)image;

@end
Expand All @@ -29,7 +30,7 @@ extern NSString * const kNotificationResourceFileQueryDone;

+ (instancetype)sharedObject;

- (void)startWithProjectPath:(NSString *)projectPath resourceSuffixs:(NSArray *)resourceSuffixs;
- (void)startWithProjectPath:(NSString *)projectPath excludeFolders:(NSArray *)excludeFolders resourceSuffixs:(NSArray *)resourceSuffixs;
- (void)reset;

@end
98 changes: 79 additions & 19 deletions LSUnusedResources/Model/ResourceFileSearcher.m
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ - (NSImage *)image {

@interface ResourceFileSearcher ()

@property (strong, nonatomic) NSString *projectPath;
@property (strong, nonatomic) NSArray *resSuffixs;
@property (assign, nonatomic) BOOL isRunning;
@property (strong, nonatomic) NSMutableDictionary *resNameInfoDict;
@end
Expand All @@ -66,7 +64,7 @@ + (instancetype)sharedObject {
}


- (void)startWithProjectPath:(NSString *)projectPath resourceSuffixs:(NSArray *)resourceSuffixs {
- (void)startWithProjectPath:(NSString *)projectPath excludeFolders:(NSArray *)excludeFolders resourceSuffixs:(NSArray *)resourceSuffixs {
if (self.isRunning) {
return;
}
Expand All @@ -75,10 +73,8 @@ - (void)startWithProjectPath:(NSString *)projectPath resourceSuffixs:(NSArray *)
}

self.isRunning = YES;
self.projectPath = projectPath;
self.resSuffixs = resourceSuffixs;

[self scanResourceFile];
[self scanResourceFileWithProjectPath:projectPath excludeFolders:excludeFolders resourceSuffixs:resourceSuffixs];
}

- (void)reset {
Expand All @@ -88,9 +84,9 @@ - (void)reset {

#pragma mark - Private

- (void)scanResourceFile {
- (void)scanResourceFileWithProjectPath:(NSString *)projectPath excludeFolders:(NSArray *)excludeFolders resourceSuffixs:(NSArray *)resourceSuffixs {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSArray *resPaths = [self resourceFilesInDirectory:self.projectPath withSuffixs:self.resSuffixs];
NSArray *resPaths = [self resourceFilesInDirectory:projectPath excludeFolders:excludeFolders resourceSuffixs:resourceSuffixs];

NSMutableDictionary *tempResNameInfoDict = [NSMutableDictionary dictionary];
for (NSString *path in resPaths) {
Expand Down Expand Up @@ -120,13 +116,13 @@ - (void)scanResourceFile {
});
}

- (NSArray *)resourceFilesInDirectory:(NSString *)directoryPath withSuffixs:(NSArray *)suffixs {
- (NSArray *)resourceFilesInDirectory:(NSString *)directoryPath excludeFolders:(NSArray *)excludeFolders resourceSuffixs:(NSArray *)suffixs {

NSMutableArray *resources = [NSMutableArray array];

for (NSString *fileType in suffixs) {
// list of path<NSString>
NSArray *pathList = [self searchDirectory:directoryPath forFiletype:fileType];
NSArray *pathList = [self searchDirectory:directoryPath excludeFolders:excludeFolders forFiletype:fileType];
if (pathList.count) {
if (![fileType isEqualTo:kSuffixPng]) {
[resources addObjectsFromArray:pathList];
Expand All @@ -137,26 +133,49 @@ - (NSArray *)resourceFilesInDirectory:(NSString *)directoryPath withSuffixs:(NSA
&& [path rangeOfString:kSuffixBundle].location == NSNotFound
&& [path rangeOfString:kSuffixAppIcon].location == NSNotFound
&& [path rangeOfString:kSuffixLaunchImage].location == NSNotFound) {
[resources addObject:path];
[resources addObject:path];
}
}
}
}
}

// // list of path<NSString>
// NSArray *pathList = [self searchDirectory:directoryPath excludeFolders:excludeFolders forFiletypes:suffixs];
// if (pathList.count) {
// for (NSString *path in pathList) {
// // if the resource file is not in xxx/xxx.imageset/; xx/LaunchImage.launchimage; xx/AppIcon.appiconset
// if (![path hasPrefix:kSuffixPng]) {
// [resources addObjectsFromArray:pathList];
// } else if ([path rangeOfString:kSuffixImageSet].location == NSNotFound
// && [path rangeOfString:kSuffixBundle].location == NSNotFound
// && [path rangeOfString:kSuffixAppIcon].location == NSNotFound
// && [path rangeOfString:kSuffixLaunchImage].location == NSNotFound) {
// [resources addObject:path];
// }
// }
// }

return resources;
}

- (NSArray *)searchDirectory:(NSString *)directoryPath forFiletype:(NSString *)filetype {
- (NSArray *)searchDirectory:(NSString *)directoryPath excludeFolders:(NSArray *)excludeFolders forFiletype:(NSString *)filetype {
// Create a find task
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath: @"/usr/bin/find"];

// Search for all png files
NSArray *argvals = @[directoryPath,
@"-name",
[NSString stringWithFormat:@"*.%@", filetype]
];
// Search for all files
NSMutableArray *argvals = [NSMutableArray array];
[argvals addObject:directoryPath];
[argvals addObject:@"-name"];
[argvals addObject:[NSString stringWithFormat:@"*.%@", filetype]];

for (NSString *folder in excludeFolders) {
[argvals addObject:@"!"];
[argvals addObject:@"-path"];
[argvals addObject:[NSString stringWithFormat:@"*/%@/*", folder]];
}

[task setArguments: argvals];

NSPipe *pipe = [NSPipe pipe];
Expand All @@ -168,15 +187,56 @@ - (NSArray *)searchDirectory:(NSString *)directoryPath forFiletype:(NSString *)f

// Read the response
NSData *data = [file readDataToEndOfFile];
NSString *string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

// See if we can create a lines array
if (string.length > 1) {
if (string.length) {
NSArray *lines = [string componentsSeparatedByString:@"\n"];
return lines;
}
return nil;
}

// Toooooo Sloooooow
- (NSArray *)searchDirectory:(NSString *)directoryPath excludeFolders:(NSArray *)excludeFolders forFiletypes:(NSArray *)filetypes {
// find -E . -iregex ".*\.(html|plist)" ! -path "*/Movies/*" ! -path "*/Downloads/*" ! -path "*/Music/*"
// Create a find task
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath: @"/usr/bin/find"];

// Search for all files
NSMutableArray *argvals = [NSMutableArray array];
[argvals addObject:@"-E"];
[argvals addObject:directoryPath];
[argvals addObject:@"-iregex"];

[argvals addObject:[NSString stringWithFormat:@".*\\.(%@)", [filetypes componentsJoinedByString:@"|"]]];

for (NSString *folder in excludeFolders) {
[argvals addObject:@"!"];
[argvals addObject:@"-path"];
[argvals addObject:[NSString stringWithFormat:@"*/%@/*", folder]];
}

[task setArguments: argvals];

NSPipe *pipe = [NSPipe pipe];
[task setStandardOutput: pipe];
NSFileHandle *file = [pipe fileHandleForReading];

// Run task
[task launch];

// Read the response
NSData *data = [file readDataToEndOfFile];
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

// See if we can create a lines array
if (string.length) {
NSArray *lines = [string componentsSeparatedByString:@"\n"];
return lines;
}
return nil;
}

@end
2 changes: 1 addition & 1 deletion LSUnusedResources/Model/ResourceStringSearcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ extern NSString * const kNotificationResourceStringQueryDone;

+ (instancetype)sharedObject;

- (void)startWithProjectPath:(NSString *)projectPath resourceSuffixs:(NSArray *)resourceSuffixs fileSuffixs:(NSArray *)fileSuffixs;
- (void)startWithProjectPath:(NSString *)projectPath excludeFolders:(NSArray *)excludeFolders resourceSuffixs:(NSArray *)resourceSuffixs fileSuffixs:(NSArray *)fileSuffixs;
- (void)reset;

- (BOOL)containsResourceName:(NSString *)name;
Expand Down
7 changes: 6 additions & 1 deletion LSUnusedResources/Model/ResourceStringSearcher.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ @interface ResourceStringSearcher ()
@property (strong, nonatomic) NSString *projectPath;
@property (strong, nonatomic) NSArray *resSuffixs;
@property (strong, nonatomic) NSArray *fileSuffixs;
@property (strong, nonatomic) NSArray *excludeFolders;
@property (assign, nonatomic) BOOL isRunning;

@end
Expand All @@ -47,7 +48,7 @@ + (instancetype)sharedObject {
return _sharedInstance;
}

- (void)startWithProjectPath:(NSString *)projectPath resourceSuffixs:(NSArray *)resourceSuffixs fileSuffixs:(NSArray *)fileSuffixs {
- (void)startWithProjectPath:(NSString *)projectPath excludeFolders:(NSArray *)excludeFolders resourceSuffixs:(NSArray *)resourceSuffixs fileSuffixs:(NSArray *)fileSuffixs {
if (self.isRunning) {
return;
}
Expand All @@ -59,6 +60,7 @@ - (void)startWithProjectPath:(NSString *)projectPath resourceSuffixs:(NSArray *)
self.projectPath = projectPath;
self.resSuffixs = resourceSuffixs;
self.fileSuffixs = fileSuffixs;
self.excludeFolders = excludeFolders;

[self runSearchTask];
}
Expand Down Expand Up @@ -146,6 +148,9 @@ - (BOOL)handleFilesAtPath:(NSString *)dir
if ([file hasPrefix:@"."]) {
continue;
}
if ([self.excludeFolders containsObject:file]) {
continue;
}

NSString *tempPath = [dir stringByAppendingPathComponent:file];
if ([self isDirectory:tempPath]) {
Expand Down
Loading

0 comments on commit 34a0562

Please sign in to comment.