-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStoryboardDefGenerator.py
More file actions
212 lines (142 loc) · 5.67 KB
/
StoryboardDefGenerator.py
File metadata and controls
212 lines (142 loc) · 5.67 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import os,sys,shutil,optparse,re,io
rootDir = ""
# ------------------------------------| Options Parser |----------------------------------------------
# Parse options
parser = optparse.OptionParser()
parser.add_option('-r', '--root', help='specify root dir to search from', dest="rootDir")
parser.add_option('-d', '--dest', help='specify dest path to output generated files', default="./generated",dest="dest_res")
(opts, args) = parser.parse_args()
if not opts.rootDir:
rootDir = os.path.abspath('../');
else:
rootDir = opts.rootDir;
opts.dest_res = os.path.abspath(opts.dest_res);
print "root dir: " + os.path.abspath(rootDir);
print "dest dir: " + opts.dest_res;
# ------------------------------------| Methods |----------------------------------------------
# Find all storyboards in path
def storyboardsInPath(path):
all_files = []
for root, dirs, files in os.walk(path, topdown=False):
for name in files:
all_files.append(os.path.join(root, name));
result = []
for fitem in all_files:
if "/.git/" not in fitem and os.path.splitext(fitem)[1] == ".storyboard":
result.append(fitem)
return result
# debug print each storyboard filename and each controllers storyboardIdentifier
def printStoryboardDictionary():
for key in storyboardDict:
files = storyboardDict[key];
# print "StoryBoard: " + key
print "-----" + key + " Storyboard----"
for name in files:
print "" + name
# Parse each storyboard in pathlist and extract dictionary with filename and controller names
# dict[filename] = [storyboardIdentifier]
def parseStoryBoards(path_list):
dictionary = {}
for path in path_list:
storyfile = io.open(path,"r").read()
filename = os.path.basename(path).split('.')[0]
dictionary[filename] = []
matches = re.findall(r'(storyboardIdentifier=")(.*?)"',storyfile)
if matches:
for group in matches:
name = group[1]
# print "name: " + name
dictionary[filename].append(name)
return dictionary;
# ------------------------------------| Script Body |----------------------------------------------
# Find all storyboard paths from root
storyboardPaths = storyboardsInPath(rootDir)
# Parse each storyboard and extract filename and storyboardIdentifiers
storyboardDict = parseStoryBoards(storyboardPaths);
# Debug print
# printStoryboardDictionary()
# Create Destination Path
folderPath = opts.dest_res
if not os.path.exists(folderPath):
os.makedirs(folderPath)
storyPathHeader = os.path.join(opts.dest_res,'StoryBoardManager.h');
storyPathMain = os.path.join(opts.dest_res,'StoryBoardManager.m');
if not (os.path.exists( storyPathHeader ) or os.path.exists(storyPathMain)) :
head = io.open(storyPathHeader,'w',encoding='utf8');
head.write( u"""
//
// StoryBoardManager.h
//
// Created by Simon Coulter
//
#import <Foundation/Foundation.h>
#import "_StoryBoardManager.h"
@interface StoryBoardManager : _StoryBoardManager
@end
""");
head.close();
mainT = io.open(storyPathMain,'w',encoding='utf8');
mainT.write(u"""
//
// StoryBoardManager.m
//
// Created by Simon Coulter
//
#import "StoryBoardManager.h"
@implementation StoryBoardManager
@end """);
mainT.close();
# --------------------------------------------------------------------------------------------------
# ------------------------------- Template Header Here ---------------------------------------------
# --------------------------------------------------------------------------------------------------
# TODO: cleanup and generalize this code (its crap i know)
headerPath = os.path.join(opts.dest_res,'_StoryBoardManager.h');
header = io.open(headerPath,'w',encoding='utf8')
header.write( u"""
// Created by Simon Coulter
// ******* Careful this file is auto-generated *********
#import <Foundation/Foundation.h>
@interface _StoryBoardManager : NSObject
""");
for filename in storyboardDict:
header.write("+ (UIStoryboard*)" + unicode(filename.lower()) + "StoryBoard;\n")
header.write(u"""+ (UIViewController*)viewController:(NSString*)viewControlelrID inStoryBoard:(NSString*)storyboardID;
@end
// StoryBoardIDs
""")
for filename in storyboardDict:
header.write("static NSString* StoryBoardID_" + unicode(filename.capitalize()) + "= @\"" + unicode(filename) + "\";\n")
header.write(u"\n\n// Controller ID's \n")
for filename in storyboardDict:
for controllerName in storyboardDict[filename]:
header.write("static NSString* SB" + filename.capitalize() + "_" + controllerName.capitalize() + " = @\"" + controllerName + "\";\n")
header.write(u'\n\n')
header.close()
# --------------------------------------------------------------------------------------------------
# ------------------------------- Template Main Here ---------------------------------------------
# --------------------------------------------------------------------------------------------------
mainPath = os.path.join(opts.dest_res,'_StoryBoardManager.m');
main = io.open(mainPath,'w',encoding='utf8')
main.write( u"""
//
// _StoryBoardManager.m
//
// Created by Simon Coulter
//
// ******* Careful this file is auto-generated *********
#import "_StoryBoardManager.h"
@implementation _StoryBoardManager
""");
for filename in storyboardDict:
main.write(u"+ (UIStoryboard*)" + filename.lower() + "StoryBoard\n{\n");
main.write(u" return [UIStoryboard storyboardWithName:StoryBoardID_" + filename.capitalize() + " bundle:nil];\n}\n");
main.write(u"""
+ (UIViewController*)viewController:(NSString*)controllerName inStoryBoard:(NSString*)storyboardID
{
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:storyboardID bundle:nil];
UIViewController* controller = (UIViewController*)[storyboard instantiateViewControllerWithIdentifier:controllerName];
return controller;
}
@end
""");
main.close();