|
| 1 | +// |
| 2 | +// CGCodeGenTool.m |
| 3 | +// codegenutils |
| 4 | +// |
| 5 | +// Created by Jim Puls on 9/6/13. |
| 6 | +// Copyright (c) 2013 Square, Inc. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import"CGUCodeGenTool.h" |
| 10 | + |
| 11 | +#import<libgen.h> |
| 12 | + |
| 13 | +@interfaceCGUCodeGenTool () |
| 14 | + |
| 15 | +@property (copy)NSString *toolName; |
| 16 | + |
| 17 | +@end |
| 18 | + |
| 19 | + |
| 20 | +@implementationCGUCodeGenTool |
| 21 | + |
| 22 | ++ (NSString *)inputFileExtension; |
| 23 | +{ |
| 24 | +NSAssert(NO,@"Unimplemented abstract method:%@",NSStringFromSelector(_cmd)); |
| 25 | +returnnil; |
| 26 | +} |
| 27 | + |
| 28 | ++ (int)startWithArgc:(int)argcargv:(constchar **)argv; |
| 29 | +{ |
| 30 | +char opt = -1; |
| 31 | +NSURL *searchURL =nil; |
| 32 | +NSString *classPrefix =@""; |
| 33 | +NSMutableArray *inputURLs = [NSMutableArrayarray]; |
| 34 | + |
| 35 | +while ((opt =getopt(argc, (char *const*)argv,"o:f:p:h")) != -1) { |
| 36 | +switch (opt) { |
| 37 | +case'h': { |
| 38 | +printf("Usage:%s [-o <path>] [-f <path>] [-p <prefix>] [<paths>]\n",basename((char *)argv[0])); |
| 39 | +printf("%s -h\n\n",basename((char *)argv[0])); |
| 40 | +printf("Options:\n"); |
| 41 | +printf(" -o <path> Output files at <path>\n"); |
| 42 | +printf(" -f <path> Search for *.%s folders starting from <path>\n", [[selfinputFileExtension]UTF8String]); |
| 43 | +printf(" -p <prefix> Use <prefix> as the class prefix in the generated code\n"); |
| 44 | +printf(" -h Print this help and exit\n"); |
| 45 | +printf(" <paths> Input files; this and/or -f are required.\n"); |
| 46 | +return0; |
| 47 | + } |
| 48 | + |
| 49 | +case'o': { |
| 50 | +NSString *outputPath = [[NSStringalloc]initWithUTF8String:optarg]; |
| 51 | + outputPath = [outputPathstringByExpandingTildeInPath]; |
| 52 | + [[NSFileManagerdefaultManager]changeCurrentDirectoryPath:outputPath]; |
| 53 | +break; |
| 54 | + } |
| 55 | + |
| 56 | +case'f': { |
| 57 | +NSString *searchPath = [[NSStringalloc]initWithUTF8String:optarg]; |
| 58 | + searchPath = [searchPathstringByExpandingTildeInPath]; |
| 59 | + searchURL = [NSURLfileURLWithPath:searchPath]; |
| 60 | +break; |
| 61 | + } |
| 62 | + |
| 63 | +case'p': { |
| 64 | + classPrefix = [[NSStringalloc]initWithUTF8String:optarg]; |
| 65 | +break; |
| 66 | + } |
| 67 | + |
| 68 | +default: |
| 69 | +break; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | +for (int index = optind; index < argc; index++) { |
| 74 | +NSString *inputPath = [[NSStringalloc]initWithUTF8String:argv[index]]; |
| 75 | + inputPath = [inputPathstringByExpandingTildeInPath]; |
| 76 | + [inputURLsaddObject:[NSURLfileURLWithPath:inputPath]]; |
| 77 | + } |
| 78 | + |
| 79 | +if (searchURL) { |
| 80 | +NSDirectoryEnumerator *enumerator = [[NSFileManagerdefaultManager]enumeratorAtURL:searchURLincludingPropertiesForKeys:@[NSURLNameKey]options:0errorHandler:NULL]; |
| 81 | +for (NSURL *url in enumerator) { |
| 82 | +if ([url.pathExtensionisEqualToString:@"xcassets"]) { |
| 83 | + [inputURLsaddObject:url]; |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | +dispatch_group_t group =dispatch_group_create(); |
| 89 | + |
| 90 | +for (NSURL *url in inputURLs) { |
| 91 | +dispatch_group_enter(group); |
| 92 | + |
| 93 | + CGUCodeGenTool *target = [selfnew]; |
| 94 | + target.inputURL = url; |
| 95 | + target.classPrefix = classPrefix; |
| 96 | + target.toolName = [[NSStringstringWithUTF8String:argv[0]]lastPathComponent]; |
| 97 | + [targetstartWithCompletionHandler:^{ |
| 98 | +dispatch_group_leave(group); |
| 99 | + }]; |
| 100 | + } |
| 101 | + |
| 102 | +dispatch_group_wait(group, DISPATCH_TIME_FOREVER); |
| 103 | +return0; |
| 104 | +} |
| 105 | + |
| 106 | +- (void)startWithCompletionHandler:(dispatch_block_t)completionBlock; |
| 107 | +{ |
| 108 | +NSAssert(NO,@"Unimplemented abstract method:%@",NSStringFromSelector(_cmd)); |
| 109 | +} |
| 110 | + |
| 111 | +- (void)writeOutputFiles; |
| 112 | +{ |
| 113 | +NSAssert(self.className,@"Class name isn't set"); |
| 114 | + |
| 115 | +NSString *classNameH = [self.classNamestringByAppendingPathExtension:@"h"]; |
| 116 | +NSString *classNameM = [self.classNamestringByAppendingPathExtension:@"m"]; |
| 117 | + |
| 118 | +NSURL *currentDirectory = [NSURLfileURLWithPath:[[NSFileManagernew]currentDirectoryPath]]; |
| 119 | +NSURL *interfaceURL = [currentDirectoryURLByAppendingPathComponent:classNameH]; |
| 120 | +NSURL *implementationURL = [currentDirectoryURLByAppendingPathComponent:classNameM]; |
| 121 | + |
| 122 | + [self.interfaceContentssortUsingComparator:^NSComparisonResult(id obj1,id obj2) { |
| 123 | +return [obj1compare:obj2]; |
| 124 | + }]; |
| 125 | + [self.implementationContentssortUsingComparator:^NSComparisonResult(id obj1,id obj2) { |
| 126 | +return [obj1compare:obj2]; |
| 127 | + }]; |
| 128 | + |
| 129 | +NSString *interface = [NSStringstringWithFormat:@"//\n// This file is generated from%@ by%@.\n// Please do not edit.\n//\n\n#import <UIKit/UIKit.h>\n\n\n@interface%@ : NSObject\n\n%@\n@end\n",self.inputURL.lastPathComponent,self.toolName,self.className, [self.interfaceContentscomponentsJoinedByString:@""]]; |
| 130 | +if (![interfaceisEqualToString:[NSStringstringWithContentsOfURL:interfaceURLencoding:NSUTF8StringEncodingerror:NULL]]) { |
| 131 | + [interfacewriteToURL:interfaceURLatomically:YESencoding:NSUTF8StringEncodingerror:NULL]; |
| 132 | + } |
| 133 | + |
| 134 | +NSString *implementation = [NSStringstringWithFormat:@"//\n// This file is generated from%@ by%@.\n// Please do not edit.\n//\n\n#import\"%@\"\n\n\n@implementation%@\n\n%@\n\n@end\n",self.inputURL.lastPathComponent,self.toolName, classNameH,self.className, [self.implementationContentscomponentsJoinedByString:@"\n"]]; |
| 135 | +if (![implementationisEqualToString:[NSStringstringWithContentsOfURL:implementationURLencoding:NSUTF8StringEncodingerror:NULL]]) { |
| 136 | + [implementationwriteToURL:implementationURLatomically:YESencoding:NSUTF8StringEncodingerror:NULL]; |
| 137 | + } |
| 138 | + |
| 139 | +NSLog(@"Wrote%@ to%@", self.className, currentDirectory); |
| 140 | +} |
| 141 | + |
| 142 | +- (NSString *)methodNameForKey:(NSString *)key; |
| 143 | +{ |
| 144 | +NSMutableString *mutableKey = [keymutableCopy]; |
| 145 | + [mutableKeyreplaceCharactersInRange:NSMakeRange(0,1)withString:[[keysubstringToIndex:1]lowercaseString]]; |
| 146 | + [mutableKeyreplaceOccurrencesOfString:@""withString:@""options:0range:NSMakeRange(0, mutableKey.length)]; |
| 147 | + [mutableKeyreplaceOccurrencesOfString:@"~"withString:@""options:0range:NSMakeRange(0, mutableKey.length)]; |
| 148 | +return [mutableKeycopy]; |
| 149 | +} |
| 150 | + |
| 151 | +@end |