From e8eaaf146dd9525bf8d9764790d58a2ed8fd0182 Mon Sep 17 00:00:00 2001 From: Guillaume Algis Date: Mon, 25 Apr 2016 21:53:54 +0200 Subject: [PATCH] Make -findXcodeprojPathForPlugin: a bit more flexible Instead of searching only for a file named "PluginName.xcodeproj", take any xcodeproj if there is only one in the searched directory. If multiple xcodeprojs are found, try to find one named like the plugin, return nil for all other cases. --- Alcatraz/Installers/ATZPluginInstaller.m | 56 +++++++++++++++++------- 1 file changed, 39 insertions(+), 17 deletions(-) diff --git a/Alcatraz/Installers/ATZPluginInstaller.m b/Alcatraz/Installers/ATZPluginInstaller.m index 20268b8..ac4f645 100644 --- a/Alcatraz/Installers/ATZPluginInstaller.m +++ b/Alcatraz/Installers/ATZPluginInstaller.m @@ -113,12 +113,12 @@ - (void)reloadXcodeForPackage:(ATZPackage *)plugin completion:(void(^)(NSError * #pragma mark - Private - (void)buildPlugin:(ATZPlugin *)plugin completion:(void (^)(NSError *))completion { - - NSString *xcodeProjPath; - - @try { xcodeProjPath = [self findXcodeprojPathForPlugin:plugin]; } - @catch (NSException *exception) { - completion([NSError errorWithDomain:exception.reason code:666 userInfo:nil]); + NSError *error; + NSString *xcodeProjPath = [self findXcodeprojPathForPackage:plugin error:&error]; + + if (!xcodeProjPath) { + NSLog(@"Error building plugin: %@", error); + completion(error); return; } @@ -130,25 +130,47 @@ - (void)buildPlugin:(ATZPlugin *)plugin completion:(void (^)(NSError *))completi }]; } -- (NSString *)findXcodeprojPathForPlugin:(ATZPlugin *)plugin { - NSString *clonedDirectory = [self pathForDownloadedPackage:plugin]; - NSString *xcodeProjFilename = [plugin.name stringByAppendingString:XCODEPROJ]; +- (NSString *)findXcodeprojPathForPackage:(ATZPackage *)package error:(NSError **)error { + NSString *clonedDirectory = [self pathForDownloadedPackage:package]; + NSMutableArray *xcodeProjCandidates = [NSMutableArray array]; NSDirectoryEnumerator *enumerator = [[NSFileManager sharedManager] enumeratorAtPath:clonedDirectory]; NSString *directoryEntry; - while (directoryEntry = [enumerator nextObject]) - if ([directoryEntry.pathComponents.lastObject isEqualToString:xcodeProjFilename]) - return [clonedDirectory stringByAppendingPathComponent:directoryEntry]; + while (directoryEntry = [enumerator nextObject]) { + NSString *filename = directoryEntry.pathComponents.lastObject; + if ([filename hasSuffix:XCODEPROJ]) { + [xcodeProjCandidates addObject:filename]; + } + } + + if (xcodeProjCandidates.count == 1) { + return [clonedDirectory stringByAppendingPathComponent:[xcodeProjCandidates firstObject]]; + } + + for (NSString *xcodeProjCandidate in xcodeProjCandidates) { + // Trying to find an xcodeproj matching the plugin name + if ([[package.name stringByAppendingString:XCODEPROJ] isEqualToString:xcodeProjCandidate]) { + return [clonedDirectory stringByAppendingPathComponent:xcodeProjCandidate]; + } + } - NSLog(@"Wasn't able to find: %@ in %@", xcodeProjFilename, clonedDirectory); - @throw [NSException exceptionWithName:@"Not found" reason:@".xcodeproj was not found" userInfo:nil]; + if (error) { + NSString *description = [NSString stringWithFormat:@"Wasn't able to find any suitable .xcodeproj file in %@. Candidates were: %@.", clonedDirectory, xcodeProjCandidates]; + NSDictionary *userInfo = @{NSLocalizedDescriptionKey: description}; + *error = [NSError errorWithDomain:@"Xcodeproj not found" code:666 userInfo:userInfo]; + } + return nil; } - (NSString *)installNameFromPbxproj:(ATZPackage *)package { - NSString *pbxprojPath = [[[[self pathForDownloadedPackage:package] - stringByAppendingPathComponent:package.name] stringByAppendingString:XCODEPROJ] - stringByAppendingPathComponent:PROJECT_PBXPROJ]; + NSString *xcodeprojPath = [self findXcodeprojPathForPackage:package error:nil]; + + if (!xcodeprojPath) { + return nil; + } + + NSString *pbxprojPath = [xcodeprojPath stringByAppendingPathComponent:PROJECT_PBXPROJ]; return [ATZPbxprojParser xcpluginNameFromPbxproj:pbxprojPath]; }