/support/iphone/transport.py

https://github.com/chdorner/titanium_mobile
Python | 143 lines | 89 code | 34 blank | 20 comment | 20 complexity | 22288fdc92ccec1b88d7637447d51516 MD5 | raw file
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Make an iOS project transportable so that it can be zipped and
  5. # sent to another machine
  6. #
  7. import os, sys, shutil, codecs, glob
  8. template_dir = os.path.abspath(os.path.dirname(sys._getframe(0).f_code.co_filename))
  9. def find_sdk(version):
  10. dir = os.path.join(os.path.expanduser("~/Library/Application Support/Titanium"),"mobilesdk","osx",version)
  11. if os.path.exists(dir):
  12. return dir
  13. dir = os.path.join("/Library","Application Support","Titanium","mobilesdk","osx",version)
  14. if os.path.exists(dir):
  15. return dir
  16. print "Is Titanium installed? I can't find it"
  17. sys.exit(1)
  18. def info(msg):
  19. print msg
  20. sys.stdout.flush()
  21. def main(args):
  22. if len(args)!=2:
  23. print "Usage: %s <directory>" % os.path.basename(args[0])
  24. sys.exit(1)
  25. # these are the following things that need to be done to make an xcode project transportable
  26. #
  27. # 1. copy in libTiCore and fix symlink
  28. # 2. copy in iphone
  29. # 3. migrate TI_SDK_DIR to project.xcconfig
  30. # 4. migrate shellScript in xcodeproj
  31. project_dir = os.path.abspath(args[1])
  32. tiapp_xml = os.path.join(project_dir,'tiapp.xml')
  33. build_dir = os.path.join(project_dir,'build','iphone')
  34. lib_file = os.path.join(build_dir,'lib','libTiCore.a')
  35. support_dir = os.path.join(build_dir,'support')
  36. info("Migrating iOS project ... ")
  37. files = glob.glob('%s/*.xcodeproj' % build_dir)
  38. if len(files)!=1:
  39. print "Couldn't find the .xcodeproj file at %s" % build_dir
  40. sys.exit(1)
  41. xcodeproj_dir = files[0]
  42. # first check that we're in a valid project folder that has been built
  43. if not os.path.exists(tiapp_xml):
  44. print "%s doesn't look like a valid project folder" % project_dir
  45. sys.exit(1)
  46. if not os.path.exists(build_dir):
  47. print "%s hasn't build built by Titanium yet. Build it and re-try" % project_dir
  48. sys.exit(1)
  49. info(" + Preparing project")
  50. if os.path.islink(lib_file) or os.path.exists(lib_file):
  51. os.remove(lib_file)
  52. if os.path.exists(support_dir):
  53. shutil.rmtree(support_dir)
  54. os.makedirs(support_dir)
  55. # migrate TI_SDK_DIR
  56. project_xcconfig = os.path.join(build_dir,'project.xcconfig')
  57. contents = codecs.open(project_xcconfig,'r',encoding='utf-8').read()
  58. new_contents = u''
  59. version = None
  60. for line in contents.splitlines(True):
  61. if line.find('TI_VERSION=')!=-1:
  62. k,v = line.split("=")
  63. version = v.strip()
  64. if line.find('TI_SDK_DIR=')==-1:
  65. new_contents+=line
  66. else:
  67. new_contents+="TI_SDK_DIR=support/iphone\n"
  68. ti_sdk = find_sdk(version)
  69. iphone_dir = os.path.join(ti_sdk,'iphone')
  70. info(" + Detected version %s" % version)
  71. # write our migrated version
  72. f = codecs.open(project_xcconfig,'w',encoding='utf-8')
  73. f.write(new_contents)
  74. f.close()
  75. info(" + Migrated xcconfig")
  76. # copy in key folders
  77. for f in ('common','iphone'):
  78. info(" + Copying %s directory" % f)
  79. shutil.copytree(os.path.join(ti_sdk,f),os.path.join(support_dir,f))
  80. # remove some folders that aren't needed in transport
  81. for f in ('Classes','headers','include','resources'):
  82. shutil.rmtree(os.path.join(support_dir,'iphone',f))
  83. # copy in key files
  84. for f in ('tiapp.py','manifest.py'):
  85. shutil.copy(os.path.join(ti_sdk,f),support_dir)
  86. info(" + Copied scripts")
  87. # create our symlink
  88. cwd = os.getcwd()
  89. os.chdir(os.path.join(build_dir,'lib'))
  90. os.symlink('../support/iphone/libTiCore.a','libTiCore.a')
  91. os.chdir(cwd)
  92. xcodeproj = os.path.join(xcodeproj_dir,'project.pbxproj')
  93. contents = codecs.open(xcodeproj,'r',encoding='utf-8').read()
  94. new_contents = u''
  95. for line in contents.splitlines(True):
  96. if line.find('shellScript =')==-1:
  97. new_contents+=line
  98. else:
  99. new_contents+=' shellScript = "support/iphone/builder.py xcode\\nexit $?";\n'
  100. # write our migrated version
  101. f = codecs.open(xcodeproj,'w',encoding='utf-8')
  102. f.write(new_contents)
  103. f.close()
  104. info(" + Removing temporary build")
  105. shutil.rmtree(os.path.join(build_dir,'build'))
  106. info("Finished!")
  107. if __name__ == "__main__":
  108. main(sys.argv)
  109. sys.exit(0)