-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathIDStoryboardDumper.m
More file actions
79 lines (58 loc) · 3.17 KB
/
IDStoryboardDumper.m
File metadata and controls
79 lines (58 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//
// IDStoryboardDumper.m
// codegenutils
//
// Created by Jim Puls on 2/3/14.
// Licensed to Square, Inc. under one or more contributor license agreements.
// See the LICENSE file distributed with this work for the terms under
// which Square, Inc. licenses this file to you.
#import "IDStoryboardDumper.h"
@interface NSString (IDStoryboardAddition)
- (NSString *)IDS_titlecaseString;
@end
@implementation IDStoryboardDumper
+ (NSString *)inputFileExtension;
{
return @"storyboard";
}
- (void)startWithCompletionHandler:(dispatch_block_t)completionBlock;
{
self.skipClassDeclaration = YES;
NSString *storyboardFilename = [[self.inputURL lastPathComponent] stringByDeletingPathExtension];
NSString *storyboardName = [[storyboardFilename stringByReplacingOccurrencesOfString:@" " withString:@""] stringByReplacingOccurrencesOfString:@"~" withString:@"_"];
self.className = [NSString stringWithFormat:@"%@%@StoryboardIdentifiers", self.classPrefix, storyboardName];
NSError *error = nil;
NSXMLDocument *document = [[NSXMLDocument alloc] initWithContentsOfURL:self.inputURL options:0 error:&error];
NSArray *storyboardIdentifiers = [[document nodesForXPath:@"//@storyboardIdentifier" error:&error] valueForKey:NSStringFromSelector(@selector(stringValue))];
NSArray *reuseIdentifiers = [[document nodesForXPath:@"//@reuseIdentifier" error:&error] valueForKey:NSStringFromSelector(@selector(stringValue))];
NSArray *segueIdentifiers = [[document nodesForXPath:@"//segue/@identifier" error:&error] valueForKey:NSStringFromSelector(@selector(stringValue))];
NSMutableArray *identifiers = [NSMutableArray arrayWithArray:storyboardIdentifiers];
[identifiers addObjectsFromArray:reuseIdentifiers];
[identifiers addObjectsFromArray:segueIdentifiers];
self.interfaceContents = [NSMutableArray array];
self.implementationContents = [NSMutableArray array];
NSMutableDictionary *uniqueKeys = [NSMutableDictionary dictionary];
uniqueKeys[[NSString stringWithFormat:@"%@%@StoryboardName", self.classPrefix, storyboardName]] = storyboardFilename;
for (NSString *identifier in identifiers) {
NSString *key = [NSString stringWithFormat:@"%@%@Storyboard%@Identifier", self.classPrefix, storyboardName, [identifier IDS_titlecaseString]];
uniqueKeys[key] = identifier;
}
for (NSString *key in [uniqueKeys keysSortedByValueUsingSelector:@selector(caseInsensitiveCompare:)]) {
[self.interfaceContents addObject:[NSString stringWithFormat:@"extern NSString *const %@;\n", key]];
[self.implementationContents addObject:[NSString stringWithFormat:@"NSString *const %@ = @\"%@\";\n", key, uniqueKeys[key]]];
}
[self writeOutputFiles];
completionBlock();
}
@end
@implementation NSString (IDStoryboardAddition)
- (NSString *)IDS_titlecaseString;
{
NSArray *words = [self componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSMutableString *output = [NSMutableString string];
for (NSString *word in words) {
[output appendFormat:@"%@%@", [[word substringToIndex:1] uppercaseString], [word substringFromIndex:1]];
}
return output;
}
@end