Skip to content

Commit

Permalink
Update icons parsing and extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
ealeksandrov committed Oct 28, 2018
1 parent f225142 commit 3fa5309
Showing 1 changed file with 39 additions and 31 deletions.
70 changes: 39 additions & 31 deletions ProvisionQL/Shared.m
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,32 @@ int expirationStatus(NSDate *date, NSCalendar *calendar) {
if ([dataType isEqualToString:kDataType_xcode_archive]) {
// get the embedded icon for the iOS app
NSURL *appsDir = [URL URLByAppendingPathComponent:@"Products/Applications/"];
if (appsDir != nil) {
NSArray *dirFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:appsDir.path error:nil];
if (dirFiles.count > 0) {
appIcon = [[NSImage alloc] initWithContentsOfURL:[appsDir URLByAppendingPathComponent:[NSString stringWithFormat:@"%@/%@", dirFiles[0], fileName]]];
}
if (!appsDir) {
return nil;
}

NSArray *dirFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:appsDir.path error:nil];
NSString *appName = dirFiles.firstObject;
if (!appName) {
return nil;
}

NSURL *appURL = [appsDir URLByAppendingPathComponent:appName];
NSArray *appContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:appURL.path error:nil];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains %@", fileName];
NSString *appIconFullName = [appContents filteredArrayUsingPredicate:predicate].lastObject;
if (!appIconFullName) {
return nil;
}

NSURL *appIconFullURL = [appURL URLByAppendingPathComponent:appIconFullName];
appIcon = [[NSImage alloc] initWithContentsOfURL:appIconFullURL];
} else if([dataType isEqualToString:kDataType_ipa]) {
// get the embedded icon from an app arcive using: unzip -p <URL> 'Payload/*.app/<fileName>' (piped to standard output)
NSTask *unzipTask = [NSTask new];
[unzipTask setLaunchPath:@"/usr/bin/unzip"];
[unzipTask setStandardOutput:[NSPipe pipe]];
[unzipTask setArguments:@[@"-p", [URL path], [NSString stringWithFormat:@"Payload/*.app/%@", fileName], @"-x", @"*/*/*/*"]];
[unzipTask setArguments:@[@"-p", [URL path], [NSString stringWithFormat:@"Payload/*.app/%@*", fileName], @"-x", @"*/*/*/*"]];
[unzipTask launch];

NSData *pipeData = [[[unzipTask standardOutput] fileHandleForReading] readDataToEndOfFile];
Expand All @@ -69,22 +83,30 @@ int expirationStatus(NSDate *date, NSCalendar *calendar) {
return appIcon;
}

NSString *mainIconNameForApp(NSDictionary *appPropertyList) {
id icons;
NSString *iconName;

//Check for CFBundleIcons (since 5.0)
id iconsDict = [appPropertyList objectForKey:@"CFBundleIcons"];
NSArray *iconsListForDictionary(NSDictionary *iconsDict) {
if ([iconsDict isKindOfClass:[NSDictionary class]]) {
id primaryIconDict = [iconsDict objectForKey:@"CFBundlePrimaryIcon"];
if ([primaryIconDict isKindOfClass:[NSDictionary class]]) {
id tempIcons = [primaryIconDict objectForKey:@"CFBundleIconFiles"];
if ([tempIcons isKindOfClass:[NSArray class]]) {
icons = tempIcons;
return tempIcons;
}
}
}

return nil;
}

NSString *mainIconNameForApp(NSDictionary *appPropertyList) {
NSArray *icons;
NSString *iconName;

//Check for CFBundleIcons (since 5.0)
icons = iconsListForDictionary([appPropertyList objectForKey:@"CFBundleIcons"]);
if (!icons) {
icons = iconsListForDictionary([appPropertyList objectForKey:@"CFBundleIcons~ipad"]);
}

if (!icons) {
//Check for CFBundleIconFiles (since 3.2)
id tempIcons = [appPropertyList objectForKey:@"CFBundleIconFiles"];
Expand All @@ -95,26 +117,20 @@ int expirationStatus(NSDate *date, NSCalendar *calendar) {

if (icons) {
//Search some patterns for primary app icon (120x120)
NSArray *matches = @[@"120",@"60",@"@2x"];
NSArray *matches = @[@"120",@"60"];

for (NSString *match in matches) {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@",match];
NSArray *results = [icons filteredArrayUsingPredicate:predicate];
if ([results count]) {
iconName = [results firstObject];
//Check for @2x existence
if ([match isEqualToString:@"60"] && ![[iconName pathExtension] length]) {
if (![iconName hasSuffix:@"@2x"]) {
iconName = [iconName stringByAppendingString:@"@2x"];
}
}
break;
}
}

//If no one matches any pattern, just take first item
//If no one matches any pattern, just take last item
if (!iconName) {
iconName = [icons firstObject];
iconName = [icons lastObject];
}
} else {
//Check for CFBundleIconFile (legacy, before 3.2)
Expand All @@ -124,13 +140,5 @@ int expirationStatus(NSDate *date, NSCalendar *calendar) {
}
}

//Load NSImage
if ([iconName length]) {
if (![[iconName pathExtension] length]) {
iconName = [iconName stringByAppendingPathExtension:@"png"];
}
return iconName;
}

return nil;
return iconName;
}

0 comments on commit 3fa5309

Please sign in to comment.