|
| 1 | +// |
| 2 | +// BuildSettingInfoSource.m |
| 3 | +// BuildSettingExtractor |
| 4 | +// |
| 5 | +// Created by James Dempsey on 2/3/15. |
| 6 | +// Copyright (c) 2015 Tapas Software. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#define MAX_LINE_LENGTH 87 // For 90 columns, minus three (comment slashes and a space) |
| 10 | + |
| 11 | +#import "BuildSettingInfoSource.h" |
| 12 | +#import "Constants+Categories.h" |
| 13 | + |
| 14 | +@interface BuildSettingInfoSource () |
| 15 | + |
| 16 | +@property (strong, nonatomic) NSDictionary *buildSettingInfoDictionary; |
| 17 | + |
| 18 | +@end |
| 19 | + |
| 20 | + |
| 21 | +@implementation BuildSettingInfoSource |
| 22 | + |
| 23 | + |
| 24 | +- (NSString *)commentForBuildSettingWithName:(NSString *)buildSettingName { |
| 25 | + NSMutableString *comment = [[NSMutableString alloc] init]; |
| 26 | + |
| 27 | + NSString *presentationName = [self presentationNameForKey:buildSettingName]; |
| 28 | + NSString *settingDescription = [self descriptionForKey:buildSettingName]; |
| 29 | + |
| 30 | + if (presentationName || settingDescription) { |
| 31 | + |
| 32 | + if (settingDescription) { |
| 33 | + settingDescription = [self processedDescriptionString:settingDescription forKey:buildSettingName]; |
| 34 | + } |
| 35 | + |
| 36 | + [comment appendString:@"\n\n\n"]; |
| 37 | + if (presentationName) { |
| 38 | + [comment appendFormat:@"// %@\n", presentationName]; |
| 39 | + } |
| 40 | + if (settingDescription) { |
| 41 | + [comment appendFormat:@"// %@\n", settingDescription]; |
| 42 | + } |
| 43 | + [comment appendString:@"\n"]; |
| 44 | + } else { |
| 45 | + // For now, leave some space above an entry without a name or description |
| 46 | + [comment appendString:@"\n\n\n"]; |
| 47 | + } |
| 48 | + |
| 49 | + return comment; |
| 50 | +} |
| 51 | + |
| 52 | +- (NSString *)presentationNameForKey:(NSString *)key { |
| 53 | + if (!self.buildSettingInfoDictionary) { |
| 54 | + [self loadBuildSettingInfo]; |
| 55 | + } |
| 56 | + |
| 57 | + NSString *processedKey = [key tps_baseBuildSettingName]; // strip any conditional part of build setting |
| 58 | + |
| 59 | + processedKey = [NSString stringWithFormat:@"[%@]-name", processedKey]; |
| 60 | + return self.buildSettingInfoDictionary[processedKey]; |
| 61 | +} |
| 62 | + |
| 63 | +- (NSString *)descriptionForKey:(NSString *)key { |
| 64 | + if (!self.buildSettingInfoDictionary) { |
| 65 | + [self loadBuildSettingInfo]; |
| 66 | + } |
| 67 | + |
| 68 | + NSString *processedKey = [key tps_baseBuildSettingName]; // strip any conditional part of build setting |
| 69 | + |
| 70 | + processedKey = [NSString stringWithFormat:@"[%@]-description", processedKey]; |
| 71 | + return self.buildSettingInfoDictionary[processedKey]; |
| 72 | +} |
| 73 | + |
| 74 | +- (NSString *)processedDescriptionString:(NSString *)string forKey:(NSString *)key { |
| 75 | + |
| 76 | + NSString *processedString = @""; |
| 77 | + |
| 78 | + // Take the repetition of the build setting name out of the description. |
| 79 | + NSString *baseBuildSettingName = [key tps_baseBuildSettingName]; |
| 80 | + NSString *buildNameString = [NSString stringWithFormat:@"[%@]", baseBuildSettingName]; |
| 81 | + string = [string stringByReplacingOccurrencesOfString:buildNameString withString:@""]; |
| 82 | + |
| 83 | + buildNameString = [NSString stringWithFormat:@"[%@, ", baseBuildSettingName]; |
| 84 | + string = [string stringByReplacingOccurrencesOfString:buildNameString withString:@"["]; |
| 85 | + string = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; |
| 86 | + |
| 87 | + for (NSString *paragraphString in [string componentsSeparatedByString:@"\n"]) { |
| 88 | + |
| 89 | + |
| 90 | + // Yes, this is not using grapheme clusters and assumes one char is one character. |
| 91 | + // It also assumes spaces separate words, which is not true in some languages. |
| 92 | + NSInteger characterCount = 0; |
| 93 | + NSString *currentLine = @""; |
| 94 | + |
| 95 | + for (NSString *wordish in [paragraphString componentsSeparatedByString:@" "]) { |
| 96 | + characterCount += wordish.length; |
| 97 | + if (characterCount < MAX_LINE_LENGTH) { |
| 98 | + currentLine = [currentLine stringByAppendingFormat:@"%@ ", wordish]; |
| 99 | + characterCount++; // Account for the space |
| 100 | + } else { |
| 101 | + // Trim whitespace |
| 102 | + currentLine = [currentLine stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; |
| 103 | + // Add newline and comment prefix |
| 104 | + currentLine = [NSString stringWithFormat:@"\n// %@", currentLine]; |
| 105 | + // Add to processed string |
| 106 | + processedString = [processedString stringByAppendingString:currentLine]; |
| 107 | + // reset line count and set current line to token that didn't fit. |
| 108 | + characterCount = wordish.length + 1; |
| 109 | + currentLine = [NSString stringWithFormat:@"%@ ", wordish]; |
| 110 | + } |
| 111 | + |
| 112 | + |
| 113 | + } |
| 114 | + |
| 115 | + // Append the last line if there is any |
| 116 | + if (![currentLine isEqualToString:@""]) { |
| 117 | + // Trim whitespace |
| 118 | + currentLine = [currentLine stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; |
| 119 | + // Add newline and comment prefix |
| 120 | + currentLine = [NSString stringWithFormat:@"\n// %@", currentLine]; |
| 121 | + // Add to processed string |
| 122 | + processedString = [processedString stringByAppendingString:currentLine]; |
| 123 | + } |
| 124 | + |
| 125 | + } |
| 126 | + |
| 127 | + return processedString; |
| 128 | +} |
| 129 | + |
| 130 | + |
| 131 | +- (void)loadBuildSettingInfo { |
| 132 | + NSString *defaultXcodePath = @"/Applications/Xcode.app"; |
| 133 | + NSURL *buildSettingInfoPlistURL = [[NSBundle mainBundle] URLForResource:@"BuildSettingInfoSubpaths" withExtension:@"plist"]; |
| 134 | + NSDictionary *buildSettingInfoDict = [NSDictionary dictionaryWithContentsOfURL:buildSettingInfoPlistURL]; |
| 135 | + NSArray *buildSettingInfoSubpaths = buildSettingInfoDict[@"subpaths"]; |
| 136 | + |
| 137 | + NSMutableDictionary *infoStringFile = [NSMutableDictionary new]; |
| 138 | + |
| 139 | + // A spot to put additional setting info. If a more official version is read in, it replaces the backstop info. |
| 140 | + NSDictionary *backstopSettingsInfo = buildSettingInfoDict[@"backstopSettingInfo"]; |
| 141 | + [infoStringFile addEntriesFromDictionary:backstopSettingsInfo]; |
| 142 | + |
| 143 | + for (NSString *subpath in buildSettingInfoSubpaths) { |
| 144 | + NSString *fullpath = [defaultXcodePath stringByAppendingPathComponent:subpath]; |
| 145 | + NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:fullpath]; |
| 146 | + if (!dictionary) NSLog(@"Could not read dictionary at path: %@", fullpath); |
| 147 | + [infoStringFile addEntriesFromDictionary:dictionary]; |
| 148 | + } |
| 149 | + |
| 150 | + _buildSettingInfoDictionary = infoStringFile; |
| 151 | +} |
| 152 | + |
| 153 | + |
| 154 | +@end |
0 commit comments