5
5
import sys
6
6
import getopt
7
7
import csv
8
+ import plistlib
8
9
9
10
params = ["resources-folder=" , "configuration=" , "string-csv=" , "checkImageUsage" , "checkStringUsage" ,
10
- "stringsFileName=" , "stringsFilePath=" , "replaceRecursive" ]
11
+ "stringsFileName=" , "stringsFilePath=" , "replaceRecursive" , "verbose" , "infoPlistFile=" , "doNotWriteStringDefinitions" ]
11
12
configuration = None
12
13
criticalError = False
13
14
files = set ([])
14
15
imgConstants = []
15
16
stringConstants = []
16
- path = None
17
+ paths = None
17
18
stringCsv = None
18
19
checkImageUsage = False
19
20
checkStringUsage = False
20
21
stringsFilePath = ""
21
22
stringsFileName = "Localizable"
22
23
replaceRecursive = False
24
+ verbose = False
25
+ infoPlistFilePath = None
26
+ infoPlistFile = None
27
+ doNotWriteStringDefinitions = False
23
28
24
29
25
30
def usage ():
@@ -40,7 +45,7 @@ def usage():
40
45
41
46
for o , a in opts :
42
47
if o in ("-r" , "--resources-folder" ):
43
- path = a
48
+ paths = a . split ( "," )
44
49
elif o in ("-s" , "--string-csv" ):
45
50
stringCsv = a
46
51
elif o in ("-c" , '--configuration' ):
@@ -55,17 +60,23 @@ def usage():
55
60
stringsFilePath = a
56
61
elif o in "--replaceRecursive" :
57
62
replaceRecursive = True
63
+ elif o in ("--verbose" , "-v" ):
64
+ verbose = True
65
+ elif o in "--infoPlistFile" :
66
+ infoPlistFilePath = a
67
+ elif o in "--doNotWriteStringDefinitions" :
68
+ doNotWriteStringDefinitions = True
58
69
else :
59
70
assert False , "unhandled option" + o + a
60
71
61
- if path is None :
72
+ if paths is None :
62
73
print "the resource path was not defined"
63
74
usage ()
64
75
sys .exit (1 )
65
76
66
77
baseFolder = os .path .join (os .getcwd (), os .path .split (sys .argv [0 ])[0 ])
67
78
os .chdir (baseFolder )
68
- resourceConstantsHeaderFile = os .path .join (path , "ResourcesConstants.h" )
79
+ resourceConstantsHeaderFile = os .path .join (paths [ 0 ] , "ResourcesConstants.h" )
69
80
70
81
constantsString = "//this file contains the names of all resouces as constants\n \n "
71
82
@@ -80,8 +91,8 @@ def scanDirs(path):
80
91
print "fools added a Photoshop file: " + currentFile
81
92
criticalError = True
82
93
83
-
84
- scanDirs (path )
94
+ for path in paths :
95
+ scanDirs (path )
85
96
86
97
oldFileHash = ""
87
98
oldGitHash = ""
@@ -133,6 +144,7 @@ def replaceRecursiveAll(a, b):
133
144
os .popen (sed )
134
145
135
146
147
+
136
148
if stringCsv is not None :
137
149
# need to handle multiple files
138
150
localFile = open (os .path .join (stringsFilePath , "{0}.strings" .format (stringsFileName )), 'w' )
@@ -146,8 +158,9 @@ def replaceRecursiveAll(a, b):
146
158
constantName = cleanName
147
159
comment = row [len (row ) - 1 ]
148
160
german = row [1 ]
149
- constantsString += "#define {0} NSLocalizedStringFromTable(@\" {2}\" ,@\" {1}\" ,@\" {3}\" )\n " .format (
150
- constantName , stringsFileName , cleanName , comment )
161
+ if not doNotWriteStringDefinitions :
162
+ constantsString += "#define {0} NSLocalizedStringFromTable(@\" {2}\" ,@\" {1}\" ,@\" {3}\" )\n " .format (
163
+ constantName , stringsFileName , cleanName , comment )
151
164
stringConstants .append (constantName )
152
165
localFile .write ("\" {0}\" = \" {1}\" ;\n " .format (cleanName , german ))
153
166
@@ -157,20 +170,34 @@ def replaceRecursiveAll(a, b):
157
170
158
171
fileExceptions = [
"[email protected] " ]
159
172
173
+ if infoPlistFilePath is not None :
174
+ infoPlistFile = plistlib .readPlist (infoPlistFilePath )
175
+ infoPlistFile ["UIAppFonts" ] = []
176
+
177
+ def addFontToPlist (fileName ):
178
+ if infoPlistFile is not None :
179
+ infoPlistFile ["UIAppFonts" ].append (fileName )
180
+
181
+
160
182
for fileName in sorted (files ):
161
183
162
184
# fileName = os.path.basename(filepath)
163
185
164
- isImage = ".png" in fileName
165
- fileNameNoEnding = fileName .split ("." )[0 ].replace ("-" , "_" ).replace ("~" , "_" )
166
- constantName = fileNameNoEnding .upper ();
186
+ isImage = fileName .endswith (".png" ) or fileName .endswith ( ".jpg" )
187
+
188
+ fileNameNoEnding = fileName .split ("." )[0 ]
189
+ fileNameNoEnding_sanitized = fileNameNoEnding .replace ("-" , "_" ).replace ("~" , "_" )
190
+ constantName = fileNameNoEnding_sanitized .upper ()
167
191
168
192
for forbiddenChar in [":" ]:
169
193
if forbiddenChar .lower () in fileName .lower ():
170
194
criticalError = True
171
195
print fileName + " contains a fobidden character " + forbiddenChar
172
196
173
197
if isImage :
198
+ if "568h" in fileName :
199
+ print "ignoring {0} because ist a long phone file" .format (fileName )
200
+ continue
174
201
if not "@2x" in fileName :
175
202
name2x = fileName .replace (".png" , "@2x.png" );
176
203
if not name2x in files :
@@ -182,19 +209,26 @@ def replaceRecursiveAll(a, b):
182
209
imgConstants .append ([constantName , fileName ])
183
210
if replaceRecursive :
184
211
replaceRecursiveAll ("@\" {0}\" " .format (fileName ), constantName )
185
- replaceRecursiveAll ("@\" {0}\" " .format (os . path . splitext ( fileName )[ 0 ] ), constantName )
212
+ replaceRecursiveAll ("@\" {0}\" " .format (fileNameNoEnding ), constantName )
186
213
else :
187
214
normalName = fileName .replace ("@2x.png" , ".png" );
188
215
# ADDED exception on iPhone5 splashscreen
189
216
if not normalName in files and fileName not in fileExceptions :
190
- print "missing normal file for:" + fileName
217
+ print "missing normal file for: {0}" . format ( fileName )
191
218
criticalError = True
192
- elif ".otf" in fileName :
193
- constantsString += "#define FONT_" + constantName + " @\" " + fileName + " \" \n "
194
- elif ".ttf" in fileName :
195
- constantsString += "#define FONT_" + constantName + " @ \" " + fileName + " \" \n "
219
+ elif ".otf" in fileName or ".ttf" in fileName :
220
+ constantsString += "#define FONT_{0} @\" {1} \" \n " . format ( constantName , fileNameNoEnding )
221
+ if infoPlistFile is not None :
222
+ addFontToPlist ( fileName );
196
223
elif ".plist" in fileName :
197
- constantsString += "#define PLIST_" + constantName + " @\" " + fileName + "\" \n "
224
+ constantsString += "#define PLIST_{0} @\" {1}\" \n " .format (constantName , fileName )
225
+
226
+ if infoPlistFile is not None :
227
+ if len (infoPlistFile ["UIAppFonts" ]) > 0 :
228
+ print "writing the modified plist"
229
+ plistlib .writePlist (infoPlistFile , infoPlistFilePath )
230
+ else :
231
+ print "not writing the modified plist because no fonts were found"
198
232
199
233
if fileSetChanged or gitRevisionChanged or stringsProvided :
200
234
print "writing " + resourceConstantsHeaderFile
@@ -218,11 +252,21 @@ def replaceRecursiveAll(a, b):
218
252
219
253
unusedImages = []
220
254
255
+
256
+ def logVerbose (param ):
257
+ if verbose :
258
+ print (param )
259
+
260
+
221
261
if checkImageUsage :
222
262
os .chdir ("../../" )
223
263
for imgSet in imgConstants :
264
+
224
265
imgConstant = imgSet [0 ]
225
266
imgName = imgSet [1 ]
267
+
268
+ logVerbose ("checking useage of {0} and {1}" .format (imgConstant , imgName ))
269
+
226
270
if imgName in imageOccuranceExceptions :
227
271
continue
228
272
numOfOccurences = len (
0 commit comments