Skip to content

Commit bfc39ef

Browse files
committed
Merge pull request #3 from deadfalkon/master
multiple improvements
2 parents fb65fd9 + b6928a7 commit bfc39ef

File tree

4 files changed

+73
-19
lines changed

4 files changed

+73
-19
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,14 @@ In our project, we cloned the iOS-resource-helper to **/submodules/iOS-resource-
99

1010
![image](https://raw.github.com/sinnerschrader-mobile/iOS-resource-helper/master/documentation/sample_XCode_integration.png)
1111

12+
Scan one folder:
13+
1214
./submodules/iOS-resource-helper/resources.py --configuration=${CONFIGURATION} --resources-folder=../../resources
15+
16+
You can also scan multiple folders:
17+
18+
./submodules/iOS-resource-helper/resources.py --configuration=${CONFIGURATION} --resources-folder=../../resources,../../otherResources
19+
20+
In Xcode 5 it became harder to add a custom script build script:
21+
22+
![image](https://raw.github.com/deadfalkon/iOS-resource-helper/master/documentation/xCode5addScriptBuildPhase.png)
-20.9 KB
Loading
179 KB
Loading

resources.py

Lines changed: 63 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,26 @@
55
import sys
66
import getopt
77
import csv
8+
import plistlib
89

910
params = ["resources-folder=", "configuration=", "string-csv=", "checkImageUsage", "checkStringUsage",
10-
"stringsFileName=", "stringsFilePath=", "replaceRecursive"]
11+
"stringsFileName=", "stringsFilePath=", "replaceRecursive", "verbose", "infoPlistFile=", "doNotWriteStringDefinitions"]
1112
configuration = None
1213
criticalError = False
1314
files = set([])
1415
imgConstants = []
1516
stringConstants = []
16-
path = None
17+
paths = None
1718
stringCsv = None
1819
checkImageUsage = False
1920
checkStringUsage = False
2021
stringsFilePath = ""
2122
stringsFileName = "Localizable"
2223
replaceRecursive = False
24+
verbose = False
25+
infoPlistFilePath = None
26+
infoPlistFile = None
27+
doNotWriteStringDefinitions = False
2328

2429

2530
def usage():
@@ -40,7 +45,7 @@ def usage():
4045

4146
for o, a in opts:
4247
if o in ("-r", "--resources-folder"):
43-
path = a
48+
paths = a.split(",")
4449
elif o in ("-s", "--string-csv"):
4550
stringCsv = a
4651
elif o in ("-c", '--configuration'):
@@ -55,17 +60,23 @@ def usage():
5560
stringsFilePath = a
5661
elif o in "--replaceRecursive":
5762
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
5869
else:
5970
assert False, "unhandled option" + o + a
6071

61-
if path is None:
72+
if paths is None:
6273
print "the resource path was not defined"
6374
usage()
6475
sys.exit(1)
6576

6677
baseFolder = os.path.join(os.getcwd(), os.path.split(sys.argv[0])[0])
6778
os.chdir(baseFolder)
68-
resourceConstantsHeaderFile = os.path.join(path, "ResourcesConstants.h")
79+
resourceConstantsHeaderFile = os.path.join(paths[0], "ResourcesConstants.h")
6980

7081
constantsString = "//this file contains the names of all resouces as constants\n\n"
7182

@@ -80,8 +91,8 @@ def scanDirs(path):
8091
print "fools added a Photoshop file: " + currentFile
8192
criticalError = True
8293

83-
84-
scanDirs(path)
94+
for path in paths:
95+
scanDirs(path)
8596

8697
oldFileHash = ""
8798
oldGitHash = ""
@@ -133,6 +144,7 @@ def replaceRecursiveAll(a, b):
133144
os.popen(sed)
134145

135146

147+
136148
if stringCsv is not None:
137149
# need to handle multiple files
138150
localFile = open(os.path.join(stringsFilePath, "{0}.strings".format(stringsFileName)), 'w')
@@ -146,8 +158,9 @@ def replaceRecursiveAll(a, b):
146158
constantName = cleanName
147159
comment = row[len(row) - 1]
148160
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)
151164
stringConstants.append(constantName)
152165
localFile.write("\"{0}\" = \"{1}\";\n".format(cleanName, german))
153166

@@ -157,20 +170,34 @@ def replaceRecursiveAll(a, b):
157170

158171
fileExceptions = ["[email protected]"]
159172

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+
160182
for fileName in sorted(files):
161183

162184
# fileName = os.path.basename(filepath)
163185

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()
167191

168192
for forbiddenChar in [":"]:
169193
if forbiddenChar.lower() in fileName.lower():
170194
criticalError = True
171195
print fileName + " contains a fobidden character " + forbiddenChar
172196

173197
if isImage:
198+
if "568h" in fileName:
199+
print "ignoring {0} because ist a long phone file".format(fileName)
200+
continue
174201
if not "@2x" in fileName:
175202
name2x = fileName.replace(".png", "@2x.png");
176203
if not name2x in files:
@@ -182,19 +209,26 @@ def replaceRecursiveAll(a, b):
182209
imgConstants.append([constantName, fileName])
183210
if replaceRecursive:
184211
replaceRecursiveAll("@\"{0}\"".format(fileName), constantName)
185-
replaceRecursiveAll("@\"{0}\"".format(os.path.splitext(fileName)[0]), constantName)
212+
replaceRecursiveAll("@\"{0}\"".format(fileNameNoEnding), constantName)
186213
else:
187214
normalName = fileName.replace("@2x.png", ".png");
188215
# ADDED exception on iPhone5 splashscreen
189216
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)
191218
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);
196223
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"
198232

199233
if fileSetChanged or gitRevisionChanged or stringsProvided:
200234
print "writing " + resourceConstantsHeaderFile
@@ -218,11 +252,21 @@ def replaceRecursiveAll(a, b):
218252

219253
unusedImages = []
220254

255+
256+
def logVerbose(param):
257+
if verbose:
258+
print(param)
259+
260+
221261
if checkImageUsage:
222262
os.chdir("../../")
223263
for imgSet in imgConstants:
264+
224265
imgConstant = imgSet[0]
225266
imgName = imgSet[1]
267+
268+
logVerbose("checking useage of {0} and {1}".format(imgConstant, imgName))
269+
226270
if imgName in imageOccuranceExceptions:
227271
continue
228272
numOfOccurences = len(

0 commit comments

Comments
 (0)