iOS批量编译输出渠道包

iOS批量编译输出渠道包

背景

最近朋友公司的app渠道变多了,每个渠道包基本没有太多差异,只要更换一下图片和渠道号就行。

如果手动打的话,实在太繁琐了,所以,我就帮他写了个Python脚本

(主要是我Python比较水,找机会练手)

工程目录结构

工程目录结构主要类似于下面:

.
├── ArcTest
│   ├── ArcTest
│   ├── ArcTest.xcodeproj
│   ├── ArcTestTests
│   └── build
├── IPA
│   └── PP
├── MultiChannels
│   ├── 91
│   ├── AppStore
│   ├── HAIMA
│   ├── HULU
│   ├── KuaiYong
│   ├── PP
│   ├── TongBuTui
│   ├── XY
│   └── iTools
└── build.py
  • ArcTest:工程目录
  • MultiChannels:多渠道文件,里面包含需要替换的图片和包含渠道名称和渠道号的plist文件
.
├── 91
│   ├── Channel.plist
│   ├── default_splash_1134@2x.png
│   ├── default_splash_1908@3x.png
│   ├── default_splash_760@2x.png
│   └── default_splash_936@2x.png
├── AppStore
│   ├── Channel.plist
│   ├── default_splash_1134@2x.png
│   ├── default_splash_1908@3x.png
│   ├── default_splash_760@2x.png
│   └── default_splash_936@2x.png
  • build.py:批量打包使用的python文件
  • IPA:最后打包生成的文件目录,里面包含各个渠道的文件夹,各渠道文件夹内包含生成的.app .dSYM .ipa文件

build.py

主要过程就是:

  1. 替换文件
  2. clean
  3. 编译
  4. copy文件到IPA目录
  5. 生成.ipa文件

附上代码:

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

import sys
import os
import shutil
import commands

channellist         = ['AppStore','91','PP', 'KuaiYong','TongBuTui','iTools', 'XY','HAIMA', 'HULU']
channelpath         = 'MultiChannels'
filenamelist        = ['Channel.plist','default_splash_760@2x.png','default_splash_936@2x.png','default_splash_1134@2x.png','default_splash_1908@3x.png']
filepathlist        = ['ArcTest/Channel.plist','ArcTest/default_splash_760@2x.png','ArcTest/default_splash_936@2x.png','ArcTest/default_splash_1134@2x.png','ArcTest/default_splash_1908@3x.png']
projectname         = 'ArcTest'
outputpath          = 'IPA/'

xcodebuildclean     = 'xcodebuild clean'
# release版本 需要调整对证书 否则打包时需要设置签名和证书
xcodebuildrelease   = 'xcodebuild -sdk iphoneos -configuration Release'
xcodebuilddebug     = 'xcodebuild -sdk iphonesimulator8.3 -configuration Release'

# 替换文件
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 move(buildmode, channel):
    buildpath = ''
    if buildmode == xcodebuilddebug:
        buildpath = projectname+'/build/Debug-iphonesimulator'
    else:
        buildpath = projectname+'/build/Release-iphoneos'

    outputfiletree = outputpath+channel
    if os.path.exists(outputfiletree):
        shutil.rmtree(outputfiletree)

    shutil.copytree(buildpath, outputfiletree)  

def clean():
    status ,output = commands.getstatusoutput('cd '+ projectname + '/;' + xcodebuildclean)
    print output

def build(buildmode):
    print '编译中,请稍等...'
    status ,output = commands.getstatusoutput('cd '+ projectname + '/;' + buildmode)
    print output

def transtoipa(buildmode, channel):
    print '生成ipa中...'
    apppath = outputpath+channel+'/'+'ArcTest.app'
    print apppath
    ipapath = os.getcwd()+'/'+outputpath+channel+'/'+'ArcTest.ipa'
    print ipapath
    status ,output = commands.getstatusoutput('xcrun -sdk iphoneos PackageApplication  -v '+apppath+' -o '+ipapath)
    print output

def main(argvs):
    if len(argvs) < 2:
        print "至少输入一个渠道名称"
        return
    else:
        for i in xrange(1,len(argvs)):
            channel = argvs[i]
            if not channel in channellist:
                print "没有此渠道名称"
            else:
                replaceflies(channel)
                # 先clean后编译
                clean()
                # 编译
                build(xcodebuildrelease)
                # 迁移出去
                move(xcodebuildrelease, channel)
                # 生成ipa包
                transtoipa(xcodebuildrelease, channel)



if __name__ == '__main__':
    # 外部输入 类似:
    # python build.py PP AppStore 91 KuaiYong TongBuTui
    main(sys.argv)

    # 内部设置
    # buildlist = ['AppStore','91']
    # main(buildlist)

Demo

附上demo地址:

https://github.com/Forkong/iOSPackage

Article Published in on iOS, Python

Article by 付军