iOS自动打包生成ipa

前几天看到iOS群里有人分享一个自动打包生成ipa的python文件,虽然挺感兴趣的,但是当时工作挺忙就没在意,正好今天有时间,就研究了一下这东西怎么玩,中间有一些需要注意的地方,在这里分享一下,python文章最下面贴出来。

确保python文件位置正确

  • 把python文件放在项目的工程文件的目录下。
  • 打开终端,进入到工程文件的目录下。

执行python文件

命令如下

python autobuild.py -p Gomefxs.xcodeproj -s Gomefxs -t Gomefxs -o  ~/Desktop/Gomefxs.ipa

这里要把工程名写对,还有一个注意的地方,如果工程文件是.xcworkspace,那么命令中的-p应该换成-w,python代码中有这么一段代码:

parser.add_option("-w", "--workspace", help="Build the workspace name.xcworkspace.", metavar="name.xcworkspace")
parser.add_option("-p", "--project", help="Build the project name.xcodeproj.", metavar="name.xcodeproj")

确保电脑中的python中有requests

当我以为我的.ipa文件马上就要出现在桌面的时候,终端里出现了一个我很愿意看到的东西,报错了:

NO module named requests

报错的代码是第三行,python代码里面的第三行是这样的:

import requests

作为python完全0知识储备的我,也早就听闻江湖中流传这么一句话:import大法好。但是很不幸,上面的终端中的信息已经说了,我没import到这个叫requests的东西(问了一下懂python的朋友,这个东西叫模块)。查了一下,终端里直接就能安装模块,代码如下:

easyinstall requests

然而,终端又一次打击了我:

command not found

又仔细看了一下,WTF!命令没敲对,应该是easy_install,作为一个有着尽量不要CV信仰的码农,真是掉了无数次坑啊,还是复制大法好。好了,那么换成正确的命令再来一次:

can't creat or remove files in install directory

WTF? 这又是什么?又回到网上查看了一下,原来这个命令需要root权限,终端好像有直接切换到root用户的命令,但是由于工作不怎么用终端,所以就暂时用sudo解决吧:

sudo easy_install requests

这次终端终于出现我想要看到的东西了:

Searching for requests
Reading http://https://pypi.python.org/simple/requests/

然后等了没几秒,直接提示超时了,这个命令我就不贴了(超时应该是墙的原因,由于stackoverflow几乎能解决我所有遇到的问题,所以我电脑一直没翻墙,不过这是题外话)。easy_install这个命令,不仅可以直接在终端里下载那幢模块,也可以安装本地模块文件,把http://https://pypi.python.org/simple/requests/ 打开,里面有两种格式的文件,上网查阅了一下,应该下载.tar.gz后缀的,下载好了之后,继续尝试easy_install,不要忘了sudo,这次要easy_install下载好之后的模块文件路径:

sudo easy_install /Users/xinzhi/Desktop/requests-2.9.1.tar.gz

这次总算是安装成功了!

Installed /Library/Python/2.7/site-packages/requests-2.9.1-py2.7.egg
Processing dependencies for request==2.9.1
Finished processing dependencies for requests==2.9.1

这样,缺少的模块,总算是安装成功了。再回头执行python文件:

python autobuild.py -p Gomefxs.xcodeproj -s Gomefxs -t Gomefxs -o  ~/Desktop/Gomefxs.ipa

这个时候,终端开始疯狂的log信息,log信息结束后,桌面上的.ipa文件也生成好了,大功告成。

python代码

下面是python的代码,把里面的CODE_SIGN_IDENTITY换成自己的证书,把PROVISIONING_PROFILE换成自己的描述文件,USER_KEY和API_KEY是蒲公英的东西,由于不用蒲公英,所以直接把这块的信息删掉了。需要改的地方就这四个。

from optparse import OptionParser
import subprocess
import requests

#configuration for iOS build setting
CODE_SIGN_IDENTITY = "iPhone Distribution: GOME Financial Holdings Investment CO., LTD"
PROVISIONING_PROFILE = "4bbd193f-7a2a-414f-9c00-4310817c14af"
CONFIGURATION = "Release"
SDK = "iphoneos"


# configuration for pgyer
#pgyer setting
PGYER_UPLOAD_URL    = "http://www.pgyer.com/apiv1/app/upload"
DOWNLOAD_BASE_URL   = "http://www.pgyer.com"
USER_KEY            = ""
API_KEY             = ""


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


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
    files = {'file': open(ipaPath, 'rb')}
    headers = {'enctype':'multipart/form-data'}
    payload = {'uKey':USER_KEY,'_api_key':API_KEY,'publishRange':'2','isPublishToPublic':'2', '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

def buildProject(project, target, output):
    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)
    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):
    process = subprocess.Popen("pwd", stdout=subprocess.PIPE)
    (stdoutdata, stderrdata) = process.communicate()
    buildDir = stdoutdata.strip() + '/build'
    print "buildDir: " + buildDir
    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)
    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)
    process = subprocess.Popen(signCmd, shell=True)
    (stdoutdata, stderrdata) = process.communicate()

    uploadIpaToPgyer(output)
    cleanBuildDir(buildDir)

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

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

def main():
    
    parser = OptionParser()
    parser.add_option("-w", "--workspace", help="Build the workspace name.xcworkspace.", metavar="name.xcworkspace")
    parser.add_option("-p", "--project", help="Build the project name.xcodeproj.", metavar="name.xcodeproj")
    parser.add_option("-s", "--scheme", help="Build the scheme specified by schemename. Required if building a workspace.", metavar="schemename")
    parser.add_option("-t", "--target", help="Build the target specified by targetname. Required if building a project.", metavar="targetname")
    parser.add_option("-o", "--output", help="specify output filename", metavar="output_filename")

    (options, args) = parser.parse_args()

    print "options: %s, args: %s" % (options, args)

    xcbuild(options)

if __name__ == '__main__':
    main()

总结

其实python文件别人都是已经写好了,在配置的过程中花费时间最长的算是安装python的requests模块了,这个python的实现原理网上有很多,我就不贴了。

你可能感兴趣的:(iOS自动打包生成ipa)