Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 99 lines (84 sloc) 3.39 kB
c97ac67 @davedelong Initial commit of the BuildCleaner project
authored
1 //
2 // Cleaner.m
3 // BuildCleaner
4 //
5 // Created by Dave DeLong on 2/26/09.
6 // Copyright 2009 Home. All rights reserved.
7 //
8
9 #import "Cleaner.h"
10 #import "NSFileManager+FileSize.h"
11
12
13 @implementation Cleaner
14
15 - (id) init {
16 if (self = [super init]) {
17 query = [[NSMetadataQuery alloc] init];
18 [query setPredicate:[NSPredicate predicateWithFormat:@"kMDItemContentType = 'com.apple.xcode.project' && kMDItemDisplayName != '___PROJECTNAME___.xcodeproj'"]];
19 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(queryNotification:) name:nil object:query];
20 }
21 return self;
22 }
23
24 - (void) processXcodeprojFile:(NSString *)xcodeproj {
25 NSArray * ignoreFiles = [[NSUserDefaults standardUserDefaults] objectForKey:@"ignoreFiles"];
26 if ([ignoreFiles containsObject:xcodeproj]) {
27 NSLog(@"Ignoring %@", xcodeproj);
28 return;
29 }
30
31 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
32
33 NSString * projectDir = [xcodeproj stringByDeletingLastPathComponent];
34 NSString * buildDir = [projectDir stringByAppendingPathComponent:@"build"];
35
36 BOOL isDir = NO;
37 if ([[NSFileManager defaultManager] fileExistsAtPath:buildDir isDirectory:&isDir] && isDir == YES) {
38 NSDate * now = [NSDate date];
39 NSTimeInterval interval = [[NSUserDefaults standardUserDefaults] integerForKey:@"cutoffInterval"];
40 if (interval == 0) { interval = 3600; }
41
42 NSDate * modDate = [self _lastModificationDateForDirectory:buildDir];
43 modDate = [modDate addTimeInterval:interval];
44 NSDate * laterDate = [modDate laterDate:now];
45 if ([laterDate isEqualToDate:now]) {
46 NSUInteger dirSize = [[NSFileManager defaultManager] sizeOfItemAtPath:buildDir];
47 //modDate + 1 hour is less than now (ie, it's more than an hour old)
48 NSError * error = nil;
49 if ([[NSFileManager defaultManager] removeItemAtPath:buildDir error:&error]) {
50 //yay, we deleted the build folder!
51 totalSavedSpace += dirSize;
52 NSLog(@"Deleted: %@", buildDir);
53 } else {
54 NSLog(@"Failed to delete build directory at \"%@\" with error %@", buildDir, error);
55 }
56 }
57 }
58 [pool release];
59 }
60
61 - (void) queryNotification:(NSNotification *)note {
62 if ([[note name] isEqualToString:NSMetadataQueryDidFinishGatheringNotification]) {
63 NSArray * results = [query results];
64 [query stopQuery];
65 totalSavedSpace = [[NSUserDefaults standardUserDefaults] integerForKey:@"totalSaved"];
66 for (NSMetadataItem * i in results) {
67 [self processXcodeprojFile:[i valueForAttribute:(NSString *)kMDItemPath]];
68 }
69 [[NSUserDefaults standardUserDefaults] setInteger:totalSavedSpace forKey:@"totalSaved"];
70 [[NSNotificationCenter defaultCenter] postNotificationName:BCCleanFinished object:self];
71 }
72 }
73
74 - (void) clean {
75 [query startQuery];
76 }
77
78 - (NSDate *) _lastModificationDateForDirectory:(NSString *)dir {
79 NSDictionary * attrs = [[NSFileManager defaultManager] fileAttributesAtPath:dir traverseLink:NO];
80 NSDate * d = [attrs objectForKey:NSFileModificationDate];
81 BOOL isDir = NO;
82 if ([[NSFileManager defaultManager] fileExistsAtPath:dir isDirectory:&isDir] == YES && isDir == NO) {
83 return d;
84 }
85 NSDirectoryEnumerator * dirEnum = [[NSFileManager defaultManager] enumeratorAtPath:dir];
86 for(NSString * child in dirEnum) {
87 NSDate * childDate = [[dirEnum fileAttributes] objectForKey:NSFileModificationDate];
88 d = [d laterDate:childDate];
89 }
90 return d;
91 }
92
93 - (void) dealloc {
94 [query release];
95 [super dealloc];
96 }
97
98 @end
Something went wrong with that request. Please try again.