Skip to content

Commit

Permalink
Related tests cache.
Browse files Browse the repository at this point in the history
  • Loading branch information
johnno1962 committed Oct 27, 2018
1 parent 08bf12b commit 3173567
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 44 deletions.
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>1287</string>
<string>1302</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.developer-tools</string>
<key>LSMinimumSystemVersion</key>
Expand Down
79 changes: 41 additions & 38 deletions InjectionIII/InjectionServer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,20 @@ - (void)runInBackground {

// start up a file watcher to write generated tmpfile path to client app

NSMutableDictionary<NSString *, NSArray *> *testCache = [NSMutableDictionary new];

injector = ^(NSArray *changed) {
NSTimeInterval now = [NSDate timeIntervalSinceReferenceDate];
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 *injectedFile in changed) {
NSArray *matchedTests = testCache[injectedFile] ?:
(testCache[injectedFile] = [InjectionServer searchForTestWithFile:injectedFile
projectRoot:projectFile.stringByDeletingLastPathComponent
fileManager:[NSFileManager defaultManager]]);
[changedFiles addObjectsFromArray:matchedTests];
}
}

for (NSString *swiftSource in changedFiles)
Expand Down Expand Up @@ -207,43 +212,41 @@ - (void)setProject:(NSString *)project {
plugin:injector];
}

+ (NSArray *)searchForTestWithFiles:(NSArray *)injectedFiles projectRoot:(NSString *)projectRoot fileManager:(NSFileManager *)fileManager;
+ (NSArray *)searchForTestWithFile:(NSString *)injectedFile 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;
}
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];
}
if (![isDirectory boolValue] &&
![[filename lastPathComponent] isEqualToString:[injectedFile lastPathComponent]] &&
[[filename lowercaseString] containsString:[injectedFileName lowercaseString]]) {
[matchedTests addObject:fileURL.path];
}
}

Expand Down
19 changes: 14 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ Bundle(path: "/Applications/InjectionIII.app/Contents/Resources/tvOSInjection.bu
Bundle(path: "/Applications/InjectionIII.app/Contents/Resources/macOSInjection.bundle")?.load()
#endif
```
Or, for Xcode 10:

```Swift
#if DEBUG
Bundle(path: "/Applications/InjectionIII.app/Contents/Resources/iOSInjection10.bundle")?.load()
//for tvOS:
Bundle(path: "/Applications/InjectionIII.app/Contents/Resources/tvOSInjection10.bundle")?.load()
//Or for macOS:
Bundle(path: "/Applications/InjectionIII.app/Contents/Resources/macOSInjection10.bundle")?.load()
#endif
```

Once injection is connected, a file watcher is started in the InjectionIII app and whenever
you save a Swift or Objective-C source the target app is messaged to update the implementation.
Expand All @@ -47,11 +58,9 @@ from source you'll need to use:

### Available downloads

| Xcode 9 | Xcode 10 |
| ------------- | ------------- |
| [Mac app store](https://itunes.apple.com/app/injectioniii/id1380446739?mt=12) | [Direct download](http://johnholdsworth.com/InjectionX.app.zip) |

The reason for multiple versions is routed in Swift not being AB stable yet, this means that there will be one version per Xcode release, hence numerous versions being available for download.
| Xcode 9.3/4, Xcode 10 |
| ------------- |
| [Mac app store](https://itunes.apple.com/app/injectioniii/id1380446739?mt=12) |

### Limitations

Expand Down

0 comments on commit 3173567

Please sign in to comment.