自动化打包直接上代码,逻辑原理自行百度
自己创建个文件将后缀名改为.py,贴代码
脚本执行方法:
python autobuild.py -w ../schemName.xcworkspace -s schemName
importargparse
importsubprocess
importrequests
import os
#configuration for iOS build setting
PRODUCT_NAME ="schemName"
CONFIGURATION ="Release"
EXPORT_OPTIONS_PLIST ="ExportOptions.plist"
EXPORT_MAIN_DIRECTORY = "~/Desktop/iosMoniArchive"
# firToken
Fir_token = "fir.im上的apiToken"
Fir_aPPId = "应用ID
#检验包
defAliasIpaToFirI(ipaPath):
aliasCmd ='fir p %s'%(ipaPath)
process = subprocess.Popen(aliasCmd, shell=True)
process.wait()
aliasReturnCode = process.returncode
ifaliasReturnCode !=0:
print ("alias faild")
else:
print ("alias success")
loginToFirIm(ipaPath)
# login fir account
defloginToFirIm(ipaPath):
loginCmd ='fir login -T %s'%(Fir_token)
process = subprocess.Popen(loginCmd, shell=True)
process.wait()
loginReturnCode = process.returncode
ifloginReturnCode !=0:
print ("login faild")
else:
print ("login success")
publishToFirIm(ipaPath)
# publish
defpublishToFirIm(ipaPath):
publishCmd ='fir publish %s'%(ipaPath)
process = subprocess.Popen(publishCmd, shell=True)
process.wait()
publishReturnCode = process.returncode
ifpublishReturnCode !=0:
print ("publish faild")
else:
print ("publish success")
# des = input("Please input new version description:")
# send_qq_email(PRODUCT_DISPlayName,des)
defcleanArchiveFile(archiveFile):
cleanCmd ="rm -r %s"%(archiveFile)
process = subprocess.Popen(cleanCmd, shell = True)
process.wait()
print "cleaned archiveFile: %s" %(archiveFile)
defparserUploadResult(jsonResult):
resultCode = jsonResult['code']
ifresultCode ==0:
downUrl = DOWNLOAD_BASE_URL +"/"+jsonResult['data']['appShortcutUrl']
print "Upload Success"
print"DownUrl is:"+ downUrl
else:
print "Upload Fail!"
print"Reason:"+jsonResult['message']
defuploadIpaToPgyer(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"成功"
# 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}
defbuildExportDirectory(scheme):
dateCmd ='date "+%Y-%m-%d_%H-%M-%S"'
process = subprocess.Popen(dateCmd, stdout=subprocess.PIPE, shell=True)
(stdoutdata, stderrdata) = process.communicate()
exportDirectory ="%s"%(EXPORT_MAIN_DIRECTORY)
returnexportDirectory
defbuildArchivePath(tempName):
process = subprocess.Popen("pwd", stdout=subprocess.PIPE)
(stdoutdata, stderrdata) = process.communicate()
archiveName ="%s.xcarchive"%(tempName)
archivePath = stdoutdata.strip() +'/'+ archiveName
returnarchivePath
defgetIpaPath(exportPath):
cmd ="ls %s"%(exportPath)
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
(stdoutdata, stderrdata) = process.communicate()
ipaName = stdoutdata.strip()
ipaPath = exportPath +"/"+"schemName.ipa"
print"ipa 地址 %s"%(ipaPath)
returnipaPath
defexportArchive(scheme, archivePath):
exportDirectory = buildExportDirectory(scheme)
exportCmd ="xcodebuild -allowProvisioningUpdates -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
ifsignReturnCode !=0:
print"export %s failed"%(scheme)
return""
else:
returnexportDirectory
defbuildProject(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
ifarchiveReturnCode !=0:
print "archive workspace %s failed" %(workspace)
cleanArchiveFile(archivePath)
else:
exportDirectory = exportArchive(scheme, archivePath)
cleanArchiveFile(archivePath)
ifexportDirectory !="":
ipaPath = getIpaPath(exportDirectory)
defbuildWorkspace(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
ifarchiveReturnCode !=0:
print "archive workspace %s failed" %(workspace)
cleanArchiveFile(archivePath)
else:
exportDirectory = exportArchive(scheme, archivePath)
cleanArchiveFile(archivePath)
ifexportDirectory !="":
ipaPath = getIpaPath(exportDirectory)
AliasIpaToFirI(ipaPath)
defxcbuild(options):
project = options.project
workspace = options.workspace
scheme = options.scheme
ifprojectisNoneandworkspaceisNone:
pass
elifprojectisnotNone:
buildProject(project, scheme)
elifworkspaceisnotNone:
buildWorkspace(workspace, scheme)
defmain():
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)
if__name__ =='__main__':
main()