Wednesday 22 April 2015

Py: Xcode: Add ARC disable flag to multiple files in Xcode project

I'm assume that Cocos2d files need to be ARC disables and each file is start with CC. Then If you have hundreds of files and you want to add ARC disable flag to each file, then my method could help you in quick way. Thanks for reading.
Beware Please take backup of your project before running this script.

##########################################################################
###
### terminal$ python CCArc.py
###
### GauravDS Apr 22, 2015.
###
### Add ARC disable flag '-fno-objc-arc' to cocos2d files starting with CC
###
##########################################################################

import os

xcodeFile = [f for f in os.listdir('.') if f.endswith('.xcodeproj')]

if len(xcodeFile) == 0:
    print "Could not find .xcodeproj File, place this python file where your .xcodeproj file"
    exit()

f = open(xcodeFile[0]+'/project.pbxproj')

Alllines = ''

fileAdded = 0

for line in f.readlines():
    if line.find('/* CC') != -1 and line.find('in Sources */') != -1:
        if line.find('settings = {COMPILER_FLAGS = "-fno-objc-arc"; };') == -1 and line.endswith('};\n'):
            print line
            line = line[:-3] + ' settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; ' + line[-3:]
            print "compiler flag added"
            print line
            print '------------'
            fileAdded = fileAdded + 1
#        else:
#            print line
#            print '@@@@@@@@@@@@@@'

    Alllines = Alllines + line

if fileAdded != 0:
    print '\n\nFlag added to ' + str(fileAdded) + ' file(s).\n\n'
    fw = open('project.pbxproj','w');
    fw.write( str(Alllines)  )
    fw.close()
    print "Step 1: Close the xcode\nStep 2: copy 'project.pbxproj'\nStep 3: Right click on .xcodeproj file\nStep 4: Tap on 'Show package contents'\nStep 5: Paste 'project.pbxproj', asking for replace then tap replace. \nStep 6: Reopen the XCode project and Voila!\n\n"
else:
    print "No need to updated any thing"

f.close()

No comments:

Post a Comment