-
Notifications
You must be signed in to change notification settings - Fork 4
/
template_generator.py
executable file
·375 lines (305 loc) · 14.6 KB
/
template_generator.py
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#!/usr/bin/python
# -*- coding: utf-8 -*-
# ----------------------------------------------------------------------------
# Xcode 4 template generator
#
# Based on the original code by Ricardo Quesada.
# Modifications & Ugly Hacks by Nicolas Goles D.
#
# LICENSE: MIT
#
# Generates an Xcode4 template given several input parameters.
#
# Format taken from: http://blog.boreal-kiss.net/2011/03/11/a-minimal-project-template-for-xcode-4/
#
# NOTE: Not everything is automated, and some understanding about the Xcode4 template system
# is still needed to use this script properly (read the link above).
# ----------------------------------------------------------------------------
'''
Xcode 4 template generator
'''
__docformat__ = 'restructuredtext'
#Add here whatever you need before your Node
_template_open_body = """<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>"""
_template_description = None
_template_identifier = None
_template_concrete = "yes"
_template_ancestors = None
_template_kind = "Xcode.Xcode3.ProjectTemplateUnitKind"
_template_close_body = "\n</dict>\n</plist>"
_template_plist_name = "TemplateInfo.plist"
_template_shared_settings = None
_template_script = None
_template_script_shell_path = "/bin/bash"
# python imports
import sys
import os
import getopt
import glob
import shutil
class Xcode4Template(object):
def __init__( self, directories, group = None, in_output_path = None, ignored_files = None):
self.currentRootDirectory = None
self.directories = directories #directory list
self.ignored_files = ignored_files
self.files_to_include = []
self.wildcard = '*'
self.allowed_extensions = ['h', 'hpp', 'c', 'cpp', 'cc', 'm', 'mm', 'lua', 'png', 'fnt', 'pvr'] # Extensions of files to add to project.
self.ignore_dir_extensions = ['xcodeproj']
self.group = group # fixed group name
self.group_index = 1 # automatic group name taken from path
self.output = []
self.output_path = in_output_path
def scandirs(self, path):
for currentFile in glob.glob(os.path.join(path, self.wildcard)):
if os.path.isdir(currentFile):
name_extension = currentFile.split('.')
extension = name_extension[-1]
if extension not in self.ignore_dir_extensions and currentFile not in self.ignored_files:
self.scandirs(currentFile)
else:
self.include_file_to_append(currentFile)
#
# append file
#
def include_file_to_append(self, currentFile):
currentExtension = currentFile.split('.')
if currentExtension[-1] in self.allowed_extensions:
self.files_to_include.append( currentFile )
#
# Helper method to filter files by absolute path when using shutils.copytree
#
def ignore_files(self, directory_entered, directory_contents):
should_ignore = []
for file in self.ignored_files:
for content in directory_contents:
if os.path.join(os.path.abspath(directory_entered), content) == file:
should_ignore.append(content)
return should_ignore
#
# Change the Absolute Path to a relative path ( starting from directory that scandirs is using )
#
def change_path_to_relative( self, absolute_path ):
return os.path.relpath( absolute_path, os.path.split( os.path.relpath(self.currentRootDirectory) )[0])
#
# append the definitions
#
def append_definition( self, output_body, path, group ):
output_body.append("\n\t\t<key>%s</key>" % self.change_path_to_relative(path) )
output_body.append("\t\t<dict>")
#Fix the absolute path so that the Xcode Groups created for the .xctemplate directory are relative.
path = self.change_path_to_relative(path)
groups = path.split('/')
output_body.append("\t\t\t<key>Group</key>\t\t\t")
output_body.append("\t\t\t<array>")
for group in groups[:(len(groups)-1)]:
output_body.append("\t\t\t\t<string>%s</string>" % group)
output_body.append("\t\t\t</array>")
output_body.append("\t\t\t<key>Path</key>\n\t\t\t<string>%s</string>" % path )
output_body.append("\t\t</dict>")
#
# Generate the "Definitions" section
#
def generate_definitions( self ):
output_banner = "\n\n\t<!-- Definitions section -->"
output_header = "\n\t<key>Definitions</key>"
output_dict_open = "\n\t<dict>"
output_dict_close = "\n\t</dict>"
output_body = []
for path in self.files_to_include:
# group name
group = None
if self.group is not None:
group = self.group
else:
# obtain group name from directory
dirs = os.path.dirname(path)
subdirs = dirs.split('/')
if self.group_index < len(subdirs):
group = subdirs[self.group_index]
else:
# error
group = None
# get the extension
filename = os.path.basename(path)
name_extension= filename.split('.')
extension = None
if len(name_extension) == 2:
extension = name_extension[1]
self.append_definition( output_body, path, group )
self.output.append( output_banner )
self.output.append( output_header )
self.output.append( output_dict_open )
self.output.append( "\n".join( output_body ) )
self.output.append( output_dict_close )
#
# Generates the "Nodes" section
#
def generate_nodes( self ):
output_banner = "\n\n\t<!-- Nodes section -->"
output_header = "\n\t<key>Nodes</key>"
output_open = "\n\t<array>\n"
output_close = "\n\t</array>"
output_body = []
for path in self.files_to_include:
output_body.append("\t\t<string>%s</string>" % self.change_path_to_relative(path) )
self.output.append( output_banner )
self.output.append( output_header )
self.output.append( output_open )
self.output.append( "\n".join( output_body ) )
self.output.append( output_close )
#
# Format the output .plist string
#
def format_xml( self ):
self.output.append( _template_open_body )
if _template_description or _template_identifier or _template_kind:
self.output.append ("\n\t<!--Header Section-->")
if _template_description != None:
self.output.append( "\n\t<key>Description</key>\n\t<string>%s</string>" % _template_description )
if _template_identifier:
self.output.append( "\n\t<key>Identifier</key>\n\t<string>%s</string>" % _template_identifier )
self.output.append( "\n\t<key>Concrete</key>")
if _template_concrete.lower() == "yes":
self.output.append( "\n\t<string>True</string>" )
elif _template_concrete.lower() == "no":
self.output.append( "\n\t<string>False</string>" )
self.output.append( ("\n\t<key>Kind</key>\n\t<string>%s</string>" % _template_kind) )
if _template_ancestors:
self.output.append("\n\t<key>Ancestors</key>\n\t<array>")
ancestors = _template_ancestors.split(" ")
for ancestor in ancestors:
self.output.append("\n\t\t<string>%s</string>" % str(ancestor))
self.output.append("\n\t</array>")
if _template_shared_settings:
self.output.append("\n\t<key>Project</key>")
self.output.append("\n\t<array>\n\t\t<dict>")
self.output.append("\n\t\t\t<key>SharedSettings</key>")
self.output.append("\n\t\t\t<dict>")
shared_settings = _template_shared_settings.split(" ")
if len(shared_settings) % 2 != 0:
print "Shared Settings parameters should be an even number (use '*' if only key is needed)"
sys.exit(-1)
for i in range( len(shared_settings) - 1 ) :
if( str(shared_settings[i]) != "*"):
self.output.append("\n\t\t\t\t<key>%s</key>" % str(shared_settings[i]))
if (shared_settings[i+1] == "*"):
self.output.append("\n\t\t\t\t<string></string>")
else:
self.output.append("\n\t\t\t\t<string>%s</string>" % str(shared_settings[i+1]))
self.output.append("\n\t\t\t</dict>\n\t\t</dict>\n\t</array>")
if _template_script:
script_file = open(_template_script, 'r')
if not script_file:
sys.exit("Error reading " + _template_script)
self.output.append("\n\t<key>Targets</key>")
self.output.append("\n\t<array>\n\t\t<dict>")
self.output.append("\n\t\t\t<key>BuildPhases</key>")
self.output.append("\n\t\t\t<array>\n\t\t\t\t<dict>")
self.output.append("\n\t\t\t\t\t<key>Class</key>")
self.output.append("\n\t\t\t\t\t<string>ShellScript</string>")
self.output.append("\n\t\t\t\t\t<key>ShellPath</key>")
self.output.append("\n\t\t\t\t\t<string>"+ _template_script_shell_path +"</string>")
self.output.append("\n\t\t\t\t\t<key>ShellScript</key>")
self.output.append("\n\t\t\t\t\t<string>\n\t\t\t\t\t\t" + script_file.read() + "\n\t\t\t\t\t</string>")
self.output.append("\n\t\t\t</dict>\n\t\t\t\t</array>")
self.output.append("\n\t</dict>\n\t\t</array>")
self.generate_definitions()
self.generate_nodes()
self.output.append( _template_close_body )
#
# Create "TemplateInfo.plist" file.
#
def write_xml( self ):
FILE = open( _template_plist_name, "w" )
FILE.writelines( self.output )
FILE.close()
#
# Generates the template directory.
#
def pack_template_dir ( self, full_output_path ):
if full_output_path is None:
full_output_path = os.path.abspath("./UntitledTemplate")
(template_path, template_name) = os.path.split( os.path.normpath(full_output_path) )
for directory in self.directories:
(_, base_dir) = os.path.split(directory)
if(os.path.splitext(template_name)[1] != ".xctemplate"):
full_output_path = os.path.join(template_path, template_name + ".xctemplate")
target_dir = os.path.normpath(full_output_path) + "/" + base_dir
shutil.copytree(directory,
target_dir,
ignore = self.ignore_files)
shutil.move("TemplateInfo.plist", os.path.normpath(full_output_path))
#
# Scan Dirs, format & write.
#
def generate( self ):
for aDirectory in self.directories:
self.currentRootDirectory = aDirectory
self.scandirs(aDirectory)
self.format_xml()
self.write_xml()
def help():
print "%s v1.1 - Xcode 4 Template Generator v1.1" % sys.argv[0]
print "Usage:"
print "\t-c concrete (concrete or \"abstract\" Xcode template)"
print "\t-d one or more space separated directories (e.g -d \"box2d/ someLib/ someOtherLib/\")"
print "\t-g group (group name for Xcode template)"
print "\t-o output (output path)"
print "\t--script Specify Script to be run in the compile phases"
print "\t--shell_path Specify script shell path (defaults to /bin/bash)"
print "\t--description \"Xcode Template description\""
print "\t--identifier (string to identify this template)"
print "\t--ancestors (string separated by spaces containing all ancestor ids)"
print "\t--settings Specify build settings for the project (experimental)"
print "\t--ignore_files Specify a space separated list of files to ignore (e.g \"ignore/a/dir ignore/some/file.txt\")"
print "\nExample:"
print "\t%s -d cocos2d --description \"This is my template\" -i com.yoursite.template --ancestors com.yoursite.ancestor1 -c no --settings \"GCC_THUMB_SUPPORT[arch=armv6] *\" " % sys.argv[0]
sys.exit(-1)
if __name__ == "__main__":
if len( sys.argv ) == 1:
help()
directories = []
ignored_files = []
group = None
output = None
argv = sys.argv[1:]
try:
opts, args = getopt.getopt(argv, "d:g:i:a:c:o:", ["directories=","group=", "identifier=", "ancestors=", "concrete=", "output=", "settings=", "description=", "ignore_files=", "script=", "shell_path="])
for opt, arg in opts:
if opt in ("-d","--directory"):
for directory in arg.split(" "):
directory = os.path.abspath(directory)
directories.append(directory)
elif opt in ("-g","--group"):
group = arg
elif opt in ("-o", "--output"):
output = arg
elif opt in ("--description"):
_template_description = arg
elif opt in ("--identifier"):
_template_identifier = arg
elif opt in ("--ancestors"):
_template_ancestors = arg
elif opt in ("-c", "--concrete"):
_template_concrete = arg
elif opt in ("-s", "--settings"):
_template_shared_settings = arg
elif opt in ("--ignore_files"):
for directory in arg.split(" "):
directory = os.path.abspath(directory.strip('/'))
ignored_files.append(directory)
elif opt in ("--script"):
_template_script = arg
elif opt in ("--shell_path"):
_template_script_shell_path = arg
except getopt.GetoptError,e:
print e
if directory == None:
help()
gen = Xcode4Template(directories, group, output, ignored_files)
gen.generate()
gen.pack_template_dir(output)