iOS開發之 使用python自動化打包

一般使用企業證書打包的朋友可能會想到使用腳本進行打包,因為打包的次數相對普通開發者比較多,So本人自己網搜一堆Python(注:本人python一竅不通)并改成所需腳本,基本可以滿足打包所需要求。

打包命令

直接上代碼:

#-*-coding:utf-8-*-

#!/usr/bin/python

import sys

reload(sys)

sys.setdefaultencoding('utf-8')

from optparse import OptionParser

import subprocess

#import requests

import os

import shutil

import commands

#渠道文件

channellist? ? ? ? = ['渠道1','渠道2','....']

#渠道文件存放路徑

channelpath? ? ? ? = 'MultiChannels'

#渠道文件下的資源文件

filenamelist? ? ? ? = ['Info.plist','58 - Settings.png','80 - Spotlight.png','120 - iPhone.png']

#項目中被替換的資源文件

filepathlist? ? ? ? = ['IosClient/Info.plist','IosClient/Assets.xcassets/AppIcon.appiconset/58 - Settings.png','IosClient/Assets.xcassets/AppIcon.appiconset/80 - Spotlight.png','IosClient/Assets.xcassets/AppIcon.appiconset/120 - iPhone.png']

#項目名稱

projectname? ? ? ? = 'IosClient'

#導出ipa文件存放路徑

OUTPUT = "/Users/xxxxx/Desktop/xxxxxx(文件夾名)"

#注:本人項目是cocoapods管理,因此此處路徑是xcwordsapce,否則是xcodepro路徑

WORKSAPCE = "/Users/xxxxxx/Desktop/xxxx/xxxx.xcworkspace"

#此處與項目保持一致

TARGET = "IosClient"

#同上

SCHEME = "IosClient"

#項目的配置信息

#configuration for iOS build setting

#CODE_SIGN_IDENTITY = "iPhone Distribution: xxxxxxxx Co. Ltd (xxxxxxx9A)"

#PROVISIONING_PROFILE = "xxxxxxxxxx-xxxxx-xxxxx-xxxx-xxxxxxxxxxxx"

#CONFIGURATION = "Release"

#SDK = "iphoneos"

#上傳蒲公英

# configuration for pgyer

#PGYER_UPLOAD_URL = "http://www.pgyer.com/apiv1/app/upload"

#DOWNLOAD_BASE_URL = "http://www.pgyer.com"

#USER_KEY = "5994aef875878ee001ab35900803da74"

#API_KEY = "390700ad2e15a298225e0210161438a3"

def cleanBuildDir(buildDir):

cleanCmd = "rm -r %s" %(buildDir)

process = subprocess.Popen(cleanCmd, shell = True)

process.wait()

print "cleaned buildDir: %s" %(buildDir)

# 替換文件

def replaceflies(channel):

workspacepath = os.getcwd()

srcpath = os.path.join(workspacepath, channelpath)

srcpath = os.path.join(srcpath, channel)

despath = os.path.join(workspacepath, projectname)

# 替換資源

for filename in filenamelist:

index = filenamelist.index(filename)

filepath = filepathlist[index]

srcfullpath = os.path.join(srcpath, filename)

desfullpath = os.path.join(despath, filepath)

if os.path.exists(srcfullpath):

shutil.copy(srcfullpath, desfullpath)

print "Copy '" + srcfullpath + "' to '" + desfullpath + "'."

else:

print "'" + srcfullpath + "'" + "不存在"

#def buildProject(project, target, output):

#? ? if len(CODE_SIGN_IDENTITY) and len(PROVISIONING_PROFILE):

#? ? ? ? buildCmd = 'xcodebuild -project %s -target %s -sdk %s -configuration %s build CODE_SIGN_IDENTITY="%s" PROVISIONING_PROFILE="%s"' %(project, target, SDK, CONFIGURATION, CODE_SIGN_IDENTITY, PROVISIONING_PROFILE)

#? ? ? ? print buildCmd

#? ? ? ? pass

#? ? else:

#? ? ? ? buildCmd = 'xcodebuild -project %s -target %s -sdk %s -configuration %s' %(project, target, SDK, CONFIGURATION)

#? ? ? ? print buildCmd

#

#? ? ? ? process = subprocess.Popen(buildCmd, shell = True)

#? ? ? ? process.wait()

#

#? ? ? ? signApp = "./build/%s-iphoneos/%s.app" %(CONFIGURATION, target)

#? ? ? ? signCmd = "xcrun -sdk %s -v PackageApplication %s -o %s" %(SDK, signApp, output)

#? ? ? ? process = subprocess.Popen(signCmd, shell=True)

#? ? ? ? (stdoutdata, stderrdata) = process.communicate()

#

#? ? ? ? #? ? ? ? uploadIpaToPgyer(output)

#? ? ? ? cleanBuildDir("./build")

def buildWorkspace(workspace, scheme, output, i):

process = subprocess.Popen("pwd", stdout=subprocess.PIPE)

(stdoutdata, stderrdata) = process.communicate()

buildDir = stdoutdata.strip() + '/build'

print "buildDir: " + buildDir

if len(CODE_SIGN_IDENTITY) and len(PROVISIONING_PROFILE):

buildCmd = 'xcodebuild -workspace %s -scheme %s -sdk %s -configuration %s build CODE_SIGN_IDENTITY="%s" PROVISIONING_PROFILE="%s" SYMROOT=%s' %(workspace, scheme, SDK, CONFIGURATION,CODE_SIGN_IDENTITY, PROVISIONING_PROFILE, buildDir)

print buildCmd

pass

else:

buildCmd = 'xcodebuild -workspace %s -scheme %s -sdk %s -configuration %s SYMROOT=%s' %(workspace, scheme, SDK, CONFIGURATION, buildDir)

print buildCmd

process = subprocess.Popen(buildCmd, shell = True)

process.wait()

signApp = "./build/%s-iphoneos/%s.app" %(CONFIGURATION, scheme)

signCmd = "xcrun -sdk %s -v PackageApplication %s -o %s" %(SDK, signApp, output+"/"+str(i)+".ipa")

process = subprocess.Popen(signCmd, shell=True)

(stdoutdata, stderrdata) = process.communicate()

cleanBuildDir(buildDir)

def main(argvs):

if len(argvs) < 2:

print "至少輸入一個渠道名稱"

return

else:

for i in xrange(1,len(argvs)):

channel = argvs[i]

print len(argvs)

print argvs

if not channel in channellist:

print "沒有此渠道名稱"

else:

#? ? ? ? ? ? ? ? pass

replaceflies(channel)

buildWorkspace(WORKSAPCE, SCHEME, OUTPUT,i)

if __name__ == '__main__':

main(sys.argv)

------------------------------腳本結束-------------------------------------

調用方法:

終端命令輸入:

1.把build.py 放在與項目文件同一級文件目錄下

2. cd 項目存在的文件路徑?

3.python build.py 渠道1 渠道2?

?大功告成就會發現 output路徑下會出現1.ipa文件可自行選擇上傳。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容