Skip to content

Commit

Permalink
Move implementation to InjectionServer
Browse files Browse the repository at this point in the history
Based of feedback from @johnno1962, the implementation for TDD has been moved from FileWatcher to InjectionServer
  • Loading branch information
zenangst committed Oct 25, 2018
1 parent e81f03d commit c41a236
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 50 deletions.
1 change: 0 additions & 1 deletion InjectionIII/FileWatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ typedef void (^InjectionCallback)(NSArray *filesChanged);

@interface FileWatcher : NSObject

@property(retain, nonatomic) NSString *projectRoot;
@property(copy) InjectionCallback callback;

- (instancetype)initWithRoot:(NSString *)projectRoot plugin:(InjectionCallback)callback;
Expand Down
47 changes: 0 additions & 47 deletions InjectionIII/FileWatcher.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
// Copyright (c) 2015 John Holdsworth. All rights reserved.
//

#import "UserDefaults.h"
#import "FileWatcher.h"

@implementation FileWatcher {
Expand Down Expand Up @@ -40,7 +39,6 @@ static void fileCallback(ConstFSEventStreamRef streamRef,
- (instancetype)initWithRoot:(NSString *)projectRoot plugin:(InjectionCallback)callback;
{
if ((self = [super init])) {
self.projectRoot = projectRoot;
self.callback = callback;
static struct FSEventStreamContext context;
context.info = (__bridge void *)self;
Expand Down Expand Up @@ -70,11 +68,6 @@ - (void)filesChanged:(NSArray *)changes;
[fileManager fileExistsAtPath:path]) {

[changed addObject:path];

if ([[NSUserDefaults standardUserDefaults] boolForKey:UserDefaultsTDDEnabled]) {
NSArray *matchedTests = [self searchForTestWithFile:path projectRoot:self.projectRoot fileManager:fileManager];
[changed addObjectsFromArray:matchedTests];
}
}
}

Expand All @@ -83,46 +76,6 @@ - (void)filesChanged:(NSArray *)changes;
self.callback([[changed objectEnumerator] allObjects]);
}

- (NSArray *)searchForTestWithFile:(NSString *)injectedFile projectRoot:(NSString *)projectRoot fileManager:(NSFileManager *)fileManager;
{
NSString *injectedFileName = [[injectedFile lastPathComponent] stringByDeletingPathExtension];
NSURL *projectUrl = [NSURL URLWithString:projectRoot];
NSDirectoryEnumerator *enumerator = [fileManager enumeratorAtURL:projectUrl
includingPropertiesForKeys:@[NSURLNameKey, NSURLIsDirectoryKey]
options:NSDirectoryEnumerationSkipsHiddenFiles
errorHandler:^BOOL(NSURL *url, NSError *error)
{
if (error) {
NSLog(@"[Error] %@ (%@)", error, url);
return NO;
}

return YES;
}];

NSMutableArray *matchedTests = [NSMutableArray array];
for (NSURL *fileURL in enumerator) {
NSString *filename;
NSNumber *isDirectory;

[fileURL getResourceValue:&filename forKey:NSURLNameKey error:nil];
[fileURL getResourceValue:&isDirectory forKey:NSURLIsDirectoryKey error:nil];

if ([filename hasPrefix:@"_"] && [isDirectory boolValue]) {
[enumerator skipDescendants];
continue;
}

if (![isDirectory boolValue] &&
![[filename lastPathComponent] isEqualToString:[injectedFile lastPathComponent]] &&
[[filename lowercaseString] containsString:[injectedFileName lowercaseString]]) {
[matchedTests addObject:fileURL.path];
}
}

return matchedTests;
}

- (void)dealloc;
{
FSEventStreamStop(fileEvents);
Expand Down
2 changes: 1 addition & 1 deletion InjectionIII/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<key>CFBundleShortVersionString</key>
<string>1.2</string>
<key>CFBundleVersion</key>
<string>1268</string>
<string>1287</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.developer-tools</string>
<key>LSMinimumSystemVersion</key>
Expand Down
55 changes: 54 additions & 1 deletion InjectionIII/InjectionServer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#import "Xcode.h"
#import "XcodeHash.h"
#import "UserDefaults.h"

#import "InjectionIII-Swift.h"

Expand Down Expand Up @@ -155,7 +156,16 @@ - (void)runInBackground {

injector = ^(NSArray *changed) {
NSTimeInterval now = [NSDate timeIntervalSinceReferenceDate];
for (NSString *swiftSource in changed)
NSMutableArray *changedFiles = [NSMutableArray arrayWithArray:changed];

if ([[NSUserDefaults standardUserDefaults] boolForKey:UserDefaultsTDDEnabled]) {
NSArray *matchedTests = [InjectionServer searchForTestWithFiles:changed
projectRoot:projectFile.stringByDeletingLastPathComponent
fileManager:[NSFileManager defaultManager]];
[changedFiles addObjectsFromArray:matchedTests];
}

for (NSString *swiftSource in changedFiles)
if (now > lastInjected[swiftSource].doubleValue + MIN_INJECTION_INTERVAL && now > pause) {
lastInjected[swiftSource] = [NSNumber numberWithDouble:now];
inject(swiftSource);
Expand Down Expand Up @@ -197,6 +207,49 @@ - (void)setProject:(NSString *)project {
plugin:injector];
}

+ (NSArray *)searchForTestWithFiles:(NSArray *)injectedFiles projectRoot:(NSString *)projectRoot fileManager:(NSFileManager *)fileManager;
{
NSMutableArray *matchedTests = [NSMutableArray array];
for (NSString *injectedFile in injectedFiles) {
NSString *injectedFileName = [[injectedFile lastPathComponent] stringByDeletingPathExtension];
NSURL *projectUrl = [NSURL URLWithString:projectRoot];
NSDirectoryEnumerator *enumerator = [fileManager enumeratorAtURL:projectUrl
includingPropertiesForKeys:@[NSURLNameKey, NSURLIsDirectoryKey]
options:NSDirectoryEnumerationSkipsHiddenFiles
errorHandler:^BOOL(NSURL *url, NSError *error)
{
if (error) {
NSLog(@"[Error] %@ (%@)", error, url);
return NO;
}

return YES;
}];


for (NSURL *fileURL in enumerator) {
NSString *filename;
NSNumber *isDirectory;

[fileURL getResourceValue:&filename forKey:NSURLNameKey error:nil];
[fileURL getResourceValue:&isDirectory forKey:NSURLIsDirectoryKey error:nil];

if ([filename hasPrefix:@"_"] && [isDirectory boolValue]) {
[enumerator skipDescendants];
continue;
}

if (![isDirectory boolValue] &&
![[filename lastPathComponent] isEqualToString:[injectedFile lastPathComponent]] &&
[[filename lowercaseString] containsString:[injectedFileName lowercaseString]]) {
[matchedTests addObject:fileURL.path];
}
}
}

return matchedTests;
}

- (void)dealloc {
NSLog(@"- [%@ dealloc]", self);
}
Expand Down

0 comments on commit c41a236

Please sign in to comment.