|
| 1 | +// Copyright 2016 Sysdata Digital |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#import "SeguesGenerator.h" |
| 16 | +#import <XMLDictionary/XMLDictionary.h> |
| 17 | + |
| 18 | +@interface SegueResource : NSObject |
| 19 | +@property (nonatomic, strong) NSMutableArray* segues; |
| 20 | +@property (nonatomic, strong) NSString* sourceClassName; |
| 21 | +@property (nonatomic, readonly) NSString* sourceClassType; |
| 22 | +@property (nonatomic, readonly) NSString* methodName; |
| 23 | +@end |
| 24 | + |
| 25 | +@implementation SegueResource |
| 26 | +- (instancetype)init |
| 27 | +{ |
| 28 | + self = [super init]; |
| 29 | + if (self) |
| 30 | + { |
| 31 | + self.segues = [NSMutableArray new]; |
| 32 | + } |
| 33 | + return self; |
| 34 | +} |
| 35 | + |
| 36 | +- (void)setSourceClassName:(NSString *)sourceClassName |
| 37 | +{ |
| 38 | + if (![sourceClassName hasSuffix:@"Segues"]) |
| 39 | + { |
| 40 | + sourceClassName = [sourceClassName stringByAppendingString:@"Segues"]; |
| 41 | + } |
| 42 | + _sourceClassName = sourceClassName; |
| 43 | +} |
| 44 | + |
| 45 | +- (NSString *)classType |
| 46 | +{ |
| 47 | + return [self.sourceClassName stringByAppendingString:@"*"]; |
| 48 | +} |
| 49 | + |
| 50 | +- (NSString *)methodName |
| 51 | +{ |
| 52 | + return [CommonUtils methodNameFromFilename:self.sourceClassName removingExtension:@"Segues"]; |
| 53 | +} |
| 54 | + |
| 55 | +@end |
| 56 | + |
| 57 | +@interface SeguesGenerator () |
| 58 | +@property (nonatomic, strong) NSMutableArray<SegueResource*>* resources; |
| 59 | +@end |
| 60 | + |
| 61 | +@implementation SeguesGenerator |
| 62 | + |
| 63 | +- (instancetype)initWithResourceFinder:(ResourceFinder *)finder |
| 64 | +{ |
| 65 | + self = [super initWithResourceFinder:finder]; |
| 66 | + if (self) |
| 67 | + { |
| 68 | + self.resources = [NSMutableArray new]; |
| 69 | + } |
| 70 | + return self; |
| 71 | +} |
| 72 | + |
| 73 | +- (NSString *)className |
| 74 | +{ |
| 75 | + return @"Segues"; |
| 76 | +} |
| 77 | + |
| 78 | +- (NSString *)propertyName |
| 79 | +{ |
| 80 | + return @"segue"; |
| 81 | +} |
| 82 | + |
| 83 | +- (SegueResource*)resourceForViewController:(NSString*)className |
| 84 | +{ |
| 85 | + NSUInteger index = [self.resources indexOfObjectPassingTest:^BOOL(SegueResource * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { |
| 86 | + if ([obj.className isEqualToString:className]) |
| 87 | + { |
| 88 | + *stop = YES; |
| 89 | + return YES; |
| 90 | + } |
| 91 | + return NO; |
| 92 | + }]; |
| 93 | + |
| 94 | + if (index != NSNotFound) |
| 95 | + { |
| 96 | + return self.resources[index]; |
| 97 | + } |
| 98 | + |
| 99 | + SegueResource* res = [SegueResource new]; |
| 100 | + res.sourceClassName = className; |
| 101 | + [self.resources addObject:res]; |
| 102 | + return res; |
| 103 | +} |
| 104 | + |
| 105 | +- (BOOL)generateResourceFileWithError:(NSError *__autoreleasing *)error |
| 106 | +{ |
| 107 | + [self mapStoryboards]; |
| 108 | + |
| 109 | + if (![self writeInResourceFileWithError:error]) |
| 110 | + { |
| 111 | + return NO; |
| 112 | + } |
| 113 | + |
| 114 | + return YES; |
| 115 | +} |
| 116 | + |
| 117 | +- (void)mapStoryboards |
| 118 | +{ |
| 119 | + NSArray<NSURL*>* urls = [self.finder filesWithExtension:@"storyboard"]; |
| 120 | + |
| 121 | + for (NSURL* url in urls) |
| 122 | + { |
| 123 | + NSDictionary* storyboard = [NSDictionary dictionaryWithXMLFile:url.path]; |
| 124 | + |
| 125 | + if (storyboard) |
| 126 | + { |
| 127 | + id scenes = [storyboard.childNodes[@"scenes"] arrayValueForKeyPath:@"scene"]; |
| 128 | + |
| 129 | + for (NSDictionary* scene in scenes) |
| 130 | + { |
| 131 | + NSDictionary* objects = scene[@"objects"]; |
| 132 | + NSDictionary* viewController = objects[@"viewController"]; |
| 133 | + if ([viewController isKindOfClass:[NSDictionary class]]) |
| 134 | + { |
| 135 | + NSString* sourceClass = viewController[@"_customClass"] ?: @"UIViewController"; |
| 136 | + if ([sourceClass isKindOfClass:[NSString class]]) |
| 137 | + { |
| 138 | + SegueResource* res = [self resourceForViewController:sourceClass]; |
| 139 | + |
| 140 | + NSArray* segues = [viewController.childNodes[@"connections"] arrayValueForKeyPath:@"segue"]; |
| 141 | + |
| 142 | + for (NSDictionary* segue in segues) |
| 143 | + { |
| 144 | + NSString* identifier = segue[@"_identifier"]; |
| 145 | + |
| 146 | + if ([identifier isKindOfClass:[NSString class]]) |
| 147 | + { |
| 148 | + [res.segues addObject:identifier]; |
| 149 | + } |
| 150 | + else if (!identifier) |
| 151 | + { |
| 152 | + [CommonUtils log:@"warning in %@ storyboard: segue without identifier in view controller %@", url.lastPathComponent, viewController[@"_customClass"]]; |
| 153 | + } |
| 154 | + else |
| 155 | + { |
| 156 | + [CommonUtils log:@"invalid %@ storyboard structure: segue identifier is not a string", url.lastPathComponent]; |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + else |
| 161 | + { |
| 162 | + [CommonUtils log:@"invalid %@ storyboard structure: 'storyboardIdentifier' is not a string", url.lastPathComponent]; |
| 163 | + } |
| 164 | + } |
| 165 | + else if (viewController != nil) |
| 166 | + { |
| 167 | + [CommonUtils log:@"invalid %@ storyboard structure: 'viewController' is not a dictionary", url.lastPathComponent]; |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +- (BOOL)writeInResourceFileWithError:(NSError *__autoreleasing *)error |
| 175 | +{ |
| 176 | + // generate RSegue class |
| 177 | + RClass *clazz = [[RClass alloc] initWithName:@"RSegue"]; |
| 178 | + [self.otherClasses addObject:clazz]; |
| 179 | + |
| 180 | + // generate methods declaration for RSegue |
| 181 | + RProperty* identifierProp = [[RProperty alloc] initWithClass:@"NSString*" name:@"identifier"]; |
| 182 | + [clazz.interface.properties addObject:identifierProp]; |
| 183 | + RMethodSignature* performMethod = [[RMethodSignature alloc] initWithReturnType:@"void" signature:@"performWithSource:sender:"]; |
| 184 | + RMethodArgument* sourceArg = [[RMethodArgument alloc] initWithType:@"UIViewController*" name:@"sourceViewController"]; |
| 185 | + RMethodArgument* senderArg = [[RMethodArgument alloc] initWithType:@"id" name:@"sender"]; |
| 186 | + [performMethod.arguments addObjectsFromArray:@[sourceArg, senderArg]]; |
| 187 | + [clazz.interface.methods addObject:performMethod]; |
| 188 | + |
| 189 | + // generate methods implementation for RSegue |
| 190 | + NSString* impl = @"[sourceViewController performSegueWithIdentifier:self.identifier sender:sender];"; |
| 191 | + RMethodImplementation* performImpl = [[RMethodImplementation alloc] initWithReturnType:performMethod.returnType signature:performMethod.signature implementation:impl]; |
| 192 | + [performImpl.arguments addObjectsFromArray:performMethod.arguments]; |
| 193 | + [clazz.implementation.methods addObject:performImpl]; |
| 194 | + |
| 195 | + for (SegueResource* res in self.resources) |
| 196 | + { |
| 197 | + if (res.segues.count > 0) |
| 198 | + { |
| 199 | + // generates Segues interface methods, one for every view controller |
| 200 | + RMethodSignature* method = [[RMethodSignature alloc] initWithReturnType:res.classType signature:res.methodName]; |
| 201 | + [self.clazz.interface.methods addObject:method]; |
| 202 | + |
| 203 | + // segue resource class |
| 204 | + RClass* clazz = [[RClass alloc] initWithName:res.sourceClassName]; |
| 205 | + [self.otherClasses addObject:clazz]; |
| 206 | + |
| 207 | + // property declaration in extension and lazy getter implementation for every clazz |
| 208 | + NSString* codableKey = [CommonUtils codableNameFromString:res.methodName]; |
| 209 | + |
| 210 | + RProperty* property = [[RProperty alloc] initWithClass:res.classType name:codableKey]; |
| 211 | + [self.clazz.extension.properties addObject:property]; |
| 212 | + |
| 213 | + RLazyGetterImplementation *lazy = [[RLazyGetterImplementation alloc] initReturnType:res.sourceClassName name:codableKey]; |
| 214 | + [self.clazz.implementation.lazyGetters addObject:lazy]; |
| 215 | + |
| 216 | + // sort segues in alphabetic order |
| 217 | + NSArray* segues = [res.segues sortedArrayUsingSelector:@selector(compare:)]; |
| 218 | + for (NSString* segue in segues) |
| 219 | + { |
| 220 | + codableKey = [CommonUtils codableNameFromString:segue]; |
| 221 | + |
| 222 | + // method declaration for segue |
| 223 | + method = [[RMethodSignature alloc] initWithReturnType:@"RSegue*" signature:codableKey]; |
| 224 | + [clazz.interface.methods addObject:method]; |
| 225 | + |
| 226 | + // private property for segue |
| 227 | + RProperty* prop = [[RProperty alloc] initWithClass:@"RSegue*" name:codableKey]; |
| 228 | + [clazz.extension.properties addObject:prop]; |
| 229 | + |
| 230 | + // lazy property getter |
| 231 | + NSMutableString* implString = [NSMutableString new]; |
| 232 | + [implString appendFormat:@"\n"]; |
| 233 | + [implString appendFormat:@"\tif (!_%@)\n", codableKey]; |
| 234 | + [implString appendString:@"\t{\n"]; |
| 235 | + [implString appendFormat:@"\t\t_%@ = [RSegue new];\n", codableKey]; |
| 236 | + [implString appendFormat:@"\t\t_%@.identifier = @\"%@\";\n", codableKey, segue]; |
| 237 | + [implString appendString:@"\t}\n"]; |
| 238 | + [implString appendFormat:@"\treturn _%@;", codableKey]; |
| 239 | + RMethodImplementation* impl = [[RMethodImplementation alloc] initWithReturnType:@"RSegue*" signature:codableKey implementation:implString]; |
| 240 | + impl.indent = YES; |
| 241 | + [clazz.implementation.methods addObject:impl]; |
| 242 | + } |
| 243 | + } |
| 244 | + } |
| 245 | + |
| 246 | + return [self writeStringInRFilesWithError:error]; |
| 247 | +} |
| 248 | + |
| 249 | +@end |
0 commit comments