Skip to content

Instantly share code, notes, and snippets.

@warrenburton
Last active August 29, 2015 14:04
Show Gist options
  • Save warrenburton/fded98079ba071b3503e to your computer and use it in GitHub Desktop.
Save warrenburton/fded98079ba071b3503e to your computer and use it in GitHub Desktop.
A command line tool for repairing Xcode xcasset sets which have gained a bunch of "unassigned image" warnings. Strips any dictionaries which have no "filename" reference from the images dictionary. Point it at the .imageset folder you wish to repair.
//
// main.m
// imageset-repair
//
// Created by Warren Burton on 25/07/2014.
// Create a new Xcode commmand line project and paste into main.m
// Use with a source control system
// May have unintended consequnces so you will want the ability to revert.
//
#import <Foundation/Foundation.h>
void recursiveRepairAtPath(NSString *path);
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSString *string = nil;
if (argc < 2) {
char buffer[1024];
getcwd((char *)&buffer, 1024);
string = [NSString stringWithUTF8String:(char *)&buffer];
NSLog(@"Yo, World! - %@",string);
}
else {
string = [NSString stringWithUTF8String:argv[1]];
}
recursiveRepairAtPath(string);
}
return 0;
}
void recursiveRepairAtPath(NSString *path) {
NSArray *items = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:NULL];
for (NSString *xpath in items) {
if ([[xpath pathExtension] isEqualToString:@"imageset"]) {
NSString *target = [[path stringByAppendingPathComponent:xpath] stringByAppendingPathComponent:@"Contents.json"];
NSData *json = [NSData dataWithContentsOfFile:target];
if (json.length > 0) {
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:json options:0 error:NULL];
NSMutableDictionary *mdict = [dict mutableCopy];
NSMutableArray *images = [dict[@"images"] mutableCopy];
NSEnumerator *revEnum = [images reverseObjectEnumerator];
NSDictionary *idict = nil;
while (idict = [revEnum nextObject]) {
if (!idict[@"filename"]) {
[images removeObject:idict];
NSLog(@"fixing - %@",target);
}
}
mdict[@"images"] = images;
BOOL res = [[NSJSONSerialization dataWithJSONObject:mdict options:0 error:NULL] writeToFile:target atomically:YES];
NSLog(@"%d - %@",res,target);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment