|
| 1 | +#import <Foundation/Foundation.h> |
| 2 | +#import <ClassDump/ClassDump.h> |
| 3 | + |
| 4 | +static CDClassModel *safelyGenerateModelForClass(Class const cls, IMP const blankIMP) { |
| 5 | + Method const initializeMthd = class_getClassMethod(cls, @selector(initialize)); |
| 6 | + method_setImplementation(initializeMthd, blankIMP); |
| 7 | + |
| 8 | + return [CDClassModel modelWithClass:cls]; |
| 9 | +} |
| 10 | + |
| 11 | +static void dumpHeader(NSString *requestImage, NSString *outputDir) { |
| 12 | + IMP const blankIMP = imp_implementationWithBlock(^{ }); // returns void, takes no parameters |
| 13 | + |
| 14 | + CDGenerationOptions *const generationOptions = [CDGenerationOptions new]; |
| 15 | + generationOptions.stripSynthesized = YES; |
| 16 | + |
| 17 | + unsigned int classCount = 0; |
| 18 | + const char **classNames = objc_copyClassNamesForImage(requestImage.fileSystemRepresentation, &classCount); |
| 19 | + for (unsigned int classIndex = 0; classIndex < classCount; classIndex++) { |
| 20 | + Class const cls = objc_getClass(classNames[classIndex]); |
| 21 | + CDClassModel *model = safelyGenerateModelForClass(cls, blankIMP); |
| 22 | + CDSemanticString *semanticString = [model semanticLinesWithOptions:generationOptions]; |
| 23 | + NSString *lines = [semanticString string]; |
| 24 | + NSString *headerName = [NSStringFromClass(cls) stringByAppendingPathExtension:@"h"]; |
| 25 | + |
| 26 | + NSString *headerPath = [outputDir stringByAppendingPathComponent:headerName]; |
| 27 | + |
| 28 | + NSError *writeError = nil; |
| 29 | + if (![lines writeToFile:headerPath atomically:NO encoding:NSUTF8StringEncoding error:&writeError]) { |
| 30 | + NSLog(@"writeToFileError: %@", writeError); |
| 31 | + } |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +%ctor { |
| 36 | + dumpHeader(@"TODO", @"TODO"); |
| 37 | +} |
0 commit comments