# -*- coding: utf-8 -*-
"""
Xcode打包工具类

Author: liyan
File: xcode_builder.py

Features:
1. 获取项目版本号
2. 获取当前svn版本号
3. 重命名ipa为%(export_path)s/%(scheme_path)s_V%(version)s(%(build)s).ipa
"""

import argparse
from util import Command

def read_version():
	"""read get version command from a file,
	excute command to get version
	"""
	try:
		# grep MyEditionId ../MyServiceDefine.h |cut -d "\\"" -f 2
		version_cmd = open('version_cmd').readline().strip()
		(cost, out, err) = Command.excute(version_cmd)
		if len(err) > 0:
			print version_cmd, '\nerr: ', err
		return out.strip('\n')
	except: 
		return 'unknown'


def main():
	parser = argparse.ArgumentParser(description='Xcode Builder.')
	parser.add_argument('-p', dest='project', type=str, default='Demo.xcodeproj',
											help='Xcode Project Folder Name')
	parser.add_argument('-s', dest='scheme', type=str, default='DemoRelease',
											help='Xcode Project Scheme')
	parser.add_argument('-e', dest='export', type=str, default='./build',
											help='Build IPA to this directory')
	parser.add_argument('--dry-run', dest='dry_run', action='store_true',
											help='Dry Run Mode: do not excute time-consuming operation')
	parser.set_defaults(dry_run=False)

	args = parser.parse_args()

	if args.dry_run: print 'in dry-run mode'
	build = Command.svn_ver('..')
	version = read_version()
	print 'version =',version
	# xcodebuild -list
	Command.xcodebuild_ipa(project=args.project+'.xcodeproj', scheme=args.scheme, export=args.export)
	rename_cmd = 'mv %(export_path)s/%(scheme_path)s.ipa %(export_path)s/%(scheme_path)s_V%(version)s\\(%(build)s\\).ipa' % {
			'scheme_path': args.scheme,
			'export_path': args.export,
			'version': version,
			'build': build
		}
	(cost, out, err) = Command.excute(rename_cmd)
	print err

if __name__ == '__main__':
	main()
@@ -133,36 +133,36 @@
 path)s/%25(scheme_
-path
+name
 )s_V%25(version)s(
@@ -680,16 +680,114 @@
 lder.')%0A
+%09parser.add_argument('-a', dest='app', type=str, default='',%0A%09%09%09%09%09%09%09%09%09%09%09help='the IPA file name')%0A
 %09parser.
@@ -1443,22 +1443,94 @@
 rsion()%0A
-%09print
+%0A%09if len(args.app) ==0: args.app = args.scheme%0A%0A%09print 'ipa file =', args.app,
  'versio
@@ -1534,16 +1534,17 @@
 sion =',
+ 
 version%0A
@@ -1705,20 +1705,20 @@
 (scheme_
-path
+name
 )s.ipa %25
@@ -1734,27 +1734,24 @@
 ath)s/%25(
-scheme_path
+ipa_file
 )s_V%25(ve
@@ -1793,20 +1793,20 @@
 'scheme_
-path
+name
 ': args.
@@ -1844,16 +1844,41 @@
 export,%0A
+%09%09%09'ipa_file': args.app,%0A
 %09%09%09'vers

add parameter -a, present ipa prefix file name