iOS 自动打包脚本

将配置文件与python脚本放在与.xcodeproj文件同级目录下, 使用方法:首先在终端上切到脚本所在目录下,执行 ./autobuild.py -p yourproject.xcodeproj -s schemename 或 ./autobuild.py -w yourproject.xcworkspace -s schemenae ,将autobuild.py换成你自己的python文件名称,yourproject, schemename换成自己的项目名称即可。

配置文件

exportAdhocOptions.plistiOS 自动打包脚本_第1张图片

exportAppstoreOptions.plist

iOS 自动打包脚本_第2张图片

python脚本

#!/usr/bin/env python
# -*- coding:utf-8 -*-

#./autobuild.py -p youproject.xcodeproj -s schemename
#./autobuild.py -w youproject.xcworkspace -s schemename

import argparse
import subprocess
import requests
import os
import time

#记录打包开始的时间
START_TIME = 0
#configuration for iOS build setting
CONFIGURATION = "Release"
EXPORT_OPTIONS_PLIST_ADHOC = "exportAdhocOptions.plist"
EXPORT_OPTIONS_PLIST_APPSTORE = "exportAppstoreOptions.plist"
EXPORT_OPTIONS_PLIST = EXPORT_OPTIONS_PLIST_ADHOC
#会在桌面创建输出ipa文件的目录
EXPORT_MAIN_DIRECTORY = "~/Desktop/"

# configuration for pgyer
PGYER_UPLOAD_URL = "http://www.pgyer.com/apiv1/app/upload"
DOWNLOAD_BASE_URL = "http://www.pgyer.com"
USER_KEY = "15d6xxxxxxxxxxxxxxxxxx"
API_KEY = "efxxxxxxxxxxxxxxxxxxxx"
#设置从蒲公英下载应用时的密码
PYGER_PASSWORD = "" 

def cleanArchiveFile(archiveFile):
	cleanCmd = "rm -r %s" %(archiveFile)
	process = subprocess.Popen(cleanCmd, shell = True)
	process.wait()
	print "cleaned archiveFile: %s" %(archiveFile)


def parserUploadResult(jsonResult):
	resultCode = jsonResult['code']
	if resultCode == 0:
		downUrl = DOWNLOAD_BASE_URL +"/"+jsonResult['data']['appShortcutUrl']
		print "Upload Success"
		print "DownUrl is:" + downUrl
	else:
		print "Upload Fail!"
		print "Reason:"+jsonResult['message']

def uploadIpaToPgyer(ipaPath):
    print "ipaPath:"+ipaPath
    ipaPath = os.path.expanduser(ipaPath)
    ipaPath = unicode(ipaPath, "utf-8")
    files = {'file': open(ipaPath, 'rb')}
    headers = {'enctype':'multipart/form-data'}
    payload = {'uKey':USER_KEY,'_api_key':API_KEY,'publishRange':'2','isPublishToPublic':'2', 'password':PYGER_PASSWORD}
    print "uploading...."
    r = requests.post(PGYER_UPLOAD_URL, data = payload ,files=files,headers=headers)
    if r.status_code == requests.codes.ok:
         result = r.json()
         parserUploadResult(result)
    else:
        print 'HTTPError,Code:'+r.status_code

#创建输出ipa文件路径: ~/Desktop/{scheme}{2016-12-28_08-08-10}
def buildExportDirectory(scheme):
	dateCmd = 'date "+%Y-%m-%d_%H-%M-%S"'
	process = subprocess.Popen(dateCmd, stdout=subprocess.PIPE, shell=True)
	(stdoutdata, stderrdata) = process.communicate()
	exportDirectory = "%s%s%s" %(EXPORT_MAIN_DIRECTORY, scheme, stdoutdata.strip())
	return exportDirectory

def buildArchivePath(tempName):
	process = subprocess.Popen("pwd", stdout=subprocess.PIPE)
	(stdoutdata, stderrdata) = process.communicate()
	archiveName = "%s.xcarchive" %(tempName)
	archivePath = stdoutdata.strip() + '/' + archiveName
	return archivePath

def getIpaPath(exportPath):
	cmd = "ls %s" %(exportPath)
	process = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
	(stdoutdata, stderrdata) = process.communicate()
	ipaName = stdoutdata.strip()
	ipaPath = exportPath + "/" + ipaName
	return ipaPath

def exportArchive(scheme, archivePath):
	exportDirectory = buildExportDirectory(scheme)
	exportCmd = "xcodebuild -exportArchive -archivePath %s -exportPath %s -exportOptionsPlist %s" %(archivePath, exportDirectory, EXPORT_OPTIONS_PLIST)
	process = subprocess.Popen(exportCmd, shell=True)
	(stdoutdata, stderrdata) = process.communicate()

	signReturnCode = process.returncode
	if signReturnCode != 0:
		print "export %s failed" %(scheme)
		return ""
	else:
          print(START_TIME)
          print "花费了: %d 秒" %(time.time()-START_TIME)
          return exportDirectory

def buildProject(project, scheme):
	archivePath = buildArchivePath(scheme)
	print "archivePath: " + archivePath
	archiveCmd = 'xcodebuild -project %s -scheme %s -configuration %s archive -archivePath %s -destination generic/platform=iOS' %(project, scheme, CONFIGURATION, archivePath)
	process = subprocess.Popen(archiveCmd, shell=True)
	process.wait()

	archiveReturnCode = process.returncode
	if archiveReturnCode != 0:
		print "archive workspace %s failed" %(workspace)
		cleanArchiveFile(archivePath)
	else:
		exportDirectory = exportArchive(scheme, archivePath)
		cleanArchiveFile(archivePath)
        print "~~~~~~~~~build project success~~~~~~~~"
            #if exportDirectory != "":
            #ipaPath = getIpaPath(exportDirectory)
#uploadIpaToPgyer(ipaPath)

def buildWorkspace(workspace, scheme):
	archivePath = buildArchivePath(scheme)
	print "archivePath: " + archivePath
	archiveCmd = 'xcodebuild -workspace %s -scheme %s -configuration %s archive -archivePath %s -destination generic/platform=iOS' %(workspace, scheme, CONFIGURATION, archivePath)
	process = subprocess.Popen(archiveCmd, shell=True)
	process.wait()

	archiveReturnCode = process.returncode
	if archiveReturnCode != 0:
		print "archive workspace %s failed" %(workspace)
		cleanArchiveFile(archivePath)
	else:
		exportDirectory = exportArchive(scheme, archivePath)
		cleanArchiveFile(archivePath)
        print "~~~~~~~~~build workspace success~~~~~~~~"
            #if exportDirectory != "":
            #ipaPath = getIpaPath(exportDirectory)
#uploadIpaToPgyer(ipaPath)

def xcbuild(options):
	project = options.project
	workspace = options.workspace
	scheme = options.scheme

	if project is None and workspace is None:
		pass
	elif project is not None:
		buildProject(project, scheme)
	elif workspace is not None:
		buildWorkspace(workspace, scheme)

def createParser():
    parser = argparse.ArgumentParser()
    parser.add_argument("-w", "--workspace", help="Build the workspace name.xcworkspace.", metavar="name.xcworkspace")
    parser.add_argument("-p", "--project", help="Build the project name.xcodeproj.", metavar="name.xcodeproj")
    parser.add_argument("-s", "--scheme", help="Build the scheme specified by schemename. Required if building a workspace.", metavar="schemename")
    
    options = parser.parse_args()
    print "options: %s" % (options)
    xcbuild(options)

def main():
    print "~~~~~~~~~选择打包的方式~~~~~~~~~~~"
    print "~~~~~~~~~~1.Ad-hoc~~~~~~~~~~~~~"
    print "~~~~~~~~~~2.AppStrore~~~~~~~~~~"
    selectNum = raw_input("请选择打包的方式:")
    global EXPORT_OPTIONS_PLIST
    if selectNum == '2':
        EXPORT_OPTIONS_PLIST = EXPORT_OPTIONS_PLIST_APPSTORE
        print "~~~~~~APP-STORE~~~~~~~~~~"
    else:
        EXPORT_OPTIONS_PLIST = EXPORT_OPTIONS_PLIST_ADHOC
        print "~~~~~~AD-HOC~~~~~~~"

    print "~~~~~~~~~~开始打包~~~~~~~~~~~~"
    global START_TIME
    START_TIME = time.time()
    createParser()

if __name__ == '__main__':
	main()

你可能感兴趣的:(iOS)